BZOJ-3781 小B的询问

BZOJ2038的弱化版。。。

同样分块莫队。

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <queue>

#define rep(i, l, r) for(int i = l; i <= r; i++)
#define down(i, l, r) for(int i = l; i >= r; i--)
#define N 56789
#define MAX 1<<30
#define ll long long

using namespace std;

struct node{int l, r, id; ll a;} q[N];
int n, m, k, c[N], pos[N];
ll ans, s[N];
ll sqr(ll x) { return x*x; }
bool cmp(node a, node b)
{
	if (pos[a.l] == pos[b.l]) return a.r < b.r; else return a.l < b.l;
}
bool cmp_id(node a, node b) { return a.id < b.id; }

void update(int p, int x)
{
	ans -= sqr(s[c[p]]);
	s[c[p]] += x;
	ans += sqr(s[c[p]]);
}

int main()
{
	scanf("%d%d%d", &n, &m ,&k);
	rep(i, 1, n) scanf("%d", &c[i]);
	int block = int(sqrt(n));
	rep(i, 1, n) pos[i] = (i-1)/block+1;
	rep(i, 1, m) scanf("%d%d", &q[i].l, &q[i].r);
	rep(i, 1, m) q[i].id = i;
	sort(q+1, q+1+m, cmp);
	int l = 1, r = 0;
	rep(i, 1, m)
	{
		while (l < q[i].l) update(l++, -1);
		while (l > q[i].l) update(--l, 1);
		while (r < q[i].r) update(++r, 1);
		while (r > q[i].r) update(r--, -1);
		q[i].a = ans;
	}
	sort(q+1, q+1+m, cmp_id);
	rep(i, 1, m) printf("%lld\n", q[i].a);
	return 0;
}
posted @ 2015-03-09 21:36  NanoApe  阅读(154)  评论(0编辑  收藏  举报
AmazingCounters.com