P1494 小Z的袜子

由于过于懒惰,就不写详细题解了,反正是给自己看的。

#include <cstdio>
#include <iostream>
#include <cmath> 
#include <algorithm>
#define orz cout<<"AK IOI"
#define int long long

using namespace std;
const int maxx = 50010;

inline int read()
{
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
	while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
	return x * f;
}
int n, m, a[maxx];//a为袜子的颜色 
int qn, l = 0, r = -1, ans, cs[maxx];//初始保证l r之间为空 
int dafz[maxx], dafm[maxx];
struct node{
	int l, r, id, k;
}q[maxx];
bool cmp(node a, node b)
{
	if(a.k < b.k || (a.k == b.k && a.r < b.r)) return 1;
	return 0;
}
int c(int x)
{
	if(x < 2) return 0;
	return x * (x - 1) / 2;  
}
void delet(int x)
{
    int tp = --cs[a[x]];
    ans -= c(tp + 1);
    ans += c(tp);
}
void add(int x)
{
    int tp = ++cs[a[x]];
    ans -= c(tp - 1);
    ans += c(tp);
}
int gcd(int a,int b)
{
	if(b % a == 0) return a;
    return gcd(b, a%b);
}
signed main()
{
	//freopen("P1494_7.in","r",stdin);
	//freopen("P1494.out","w",stdout); 
	n = read(); m = read();
	for(int i = 1; i <= n; i++)
	a[i] = read();
	qn = sqrt(n + 0.5);//每块的大小 和 一共有多少块
	for(int i = 1; i <= m; i++)
	{
		q[i].l = read(); q[i].r = read();
		q[i].id = i;
		q[i].k = q[i].l / qn;//在第几块 
	}
	sort(q + 1, q + m + 1, cmp);//对查询进行排序 
	for(int i = 1; i <= m; i++)
	{
		while(l < q[i].l)
		{
			delet(l);//因为当前的l值不包含在要查询的区间内,先删再减 
			l++;
		}
		while(l > q[i].l)
		{
		    l--;//因为当前的l值包含在要查询的区间内,先加再加上当前 
			add(l);
		}
		while(r < q[i].r)
		{
		    r++;
			add(r);
		}
		while(r > q[i].r)
		{
		    delet(r);
			r--;
		}
		dafz[q[i].id] = ans;                 
		dafm[q[i].id] = c(r - l + 1);
	} 
	for(int i = 1; i <= m; i++)                 
	{
		if(dafz[i] == 0)
		{
			printf("0/1\n");
			continue;
		}
		int tp = gcd(dafz[i], dafm[i]);
		printf("%lld/%lld\n",dafz[i]/tp, dafm[i]/tp);
	}
	return 0;
}
上一篇:luogu P1494 [国家集训队]小Z的袜子


下一篇:P1494 [国家集训队]小Z的袜子