[ create a new paste ] login | about

Link: http://codepad.org/0D7V5uha    [ raw code | output | fork ]

C++, pasted on Sep 13:
#include <bits/stdc++.h>
#define N 200050
using namespace std;

int n, A[N], cont, q, freq[N], L, R, ans[N], sqn, tree[4*N], qtd[4*N], resp;

vector<int> v;

map<int, int> comp;

struct S
{
	int i, j, id;
};

bool f(S A, S B)
{
	if(A.i / sqn < B.i / sqn) return true;
	if(A.i / sqn > B.i / sqn) return false;
	return A.j < B.j ;
}

S Q[N];

void compress()
{
	sort(v.begin(), v.end());

	for(int i = 0; i < v.size(); i++)
	{
		int x = v[i];

		if(!comp[x]) comp[x] = ++cont;
	}

	for(int i = 1; i <= n; i++) A[i] = comp[A[i]];
}

void add(int i)
{	
	qtd[freq[A[i]]] --;

	freq[A[i]] ++;

	qtd[freq[A[i]]] ++;

	if(qtd[resp + 1]) resp ++;
}
void remove(int i)
{
	qtd[freq[A[i]]] --;

	freq[A[i]]--;

	qtd[freq[A[i]]] ++;
	
	if(resp > 0 && !qtd[resp]) resp --;
}

int query(int k)
{
	while(L < Q[k].i)
	{
		remove(L);

		L++;
	}

	while(L > Q[k].i)
	{
		L --;

		add(L);
	}

	while(R < Q[k].j)
	{
		R ++;

		add(R);
	}

	while(R > Q[k].j)
	{
		remove(R);

		R --;
	}

	return resp;
}

int main()
{
	ios::sync_with_stdio(false); cin.tie(0);

	cin>>n>>q;

	sqn = sqrt(n);

	for(int i = 1; i <= n; i++) cin>>A[i], v.push_back(A[i]);

	compress();

	for(int i = 1; i <= q; i++)
	{
		cin>>Q[i].i>>Q[i].j;

		Q[i].i ++, Q[i].j ++;

		if(Q[i].j < Q[i].i) swap(Q[i].i, Q[i].j);

	 	Q[i].id = i;
	 }

	sort(Q + 1, Q + q + 1, f);

	for(int i = 1; i <= q; i++) ans[Q[i].id] = query(i);

	for(int i = 1; i <= q; i++) cout<<ans[i]<<"\n";
}


Output:
1
2
3
4
5
6
Line 24: error: bits/stdc++.h: No such file or directory
cc1plus: warnings being treated as errors
In function 'void compress()':
Line 29: warning: comparison between signed and unsigned integer expressions
In function 'int main()':
Line 99: warning: converting to 'int' from 'double'


Create a new paste based on this one


Comments: