Cod sursa(job #1710546)

Utilizator daniel.grosuDaniel Grosu daniel.grosu Data 29 mai 2016 11:39:01
Problema Pq Scor 0
Compilator cpp Status done
Runda Arhiva ICPC Marime 2.52 kb
// Template v2
#define pb push_back
#define mp make_pair
#define det(a,b,c,d)  (a*d-b*c)
#define first x
#define second y
#define lsb(x) x & -x
#define l(x) (x<<1)
#define r(x) ((x<<1)|1)
#define PI   3.14159265358979323846
#include<fstream>
#include<vector>
#include<iomanip>
#include<map>
#include<set>
#include<algorithm>
#include<string.h>
#include<stack>
#include<unordered_map>
#include <bitset>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<double, double> PKK;
// primes less than 100
const int PRIM[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; const int CMAX = 100069;
const int MOD = 1000000007;
const int POW1 = 31;
const int MOD2 = 10429;
const int POW2 = 13;
const int P = 73;
const int NMAX = 105069;
const double EPS = 1e-12;
const int INF16 = 320000;
const int INF = 0x3F3F3F3F;
const LL INF64 = LL(1e18);
const int dx[]={-1,1,0,0};
const int dy[]={0,0,1,-1};

ifstream cin("pq.in");
ofstream cout("pq.out");

struct interval{
	int x, y, id;
};

interval q[NMAX];
int A[NMAX], used[NMAX], last[NMAX],rsint[NMAX];
bool cmp(interval l, interval r)
{
	return (l.x<r.x || (l.x==r.x && l.y<r.y));
}
int rs;
int T[4*NMAX];

void update(int n, int l, int r, int poz, int val)
{
	if(l==r)
	{
		T[n]=val;
		return;
	}
	int pivot=(l+r)>>1;
	if(poz<=pivot)
		update(l(n), l, pivot, poz, val);
	else
		update(r(n), pivot+1, r, poz, val);
	T[n]=max(T[l(n)], T[r(n)]);
}

void query(int n, int l, int r, int ql, int qr)
{
	if(ql<=l && r<=qr)
	{
		rs=max(rs, T[n]);
		return;
	}
	int pivot=(l+r)>>1;
	if(ql<=pivot)
		query(l(n), l, pivot, ql, qr);
	if(qr>pivot)
		query(r(n), pivot+1, r, ql, qr);
}
int n,m;
void read()
{
	cin>>n>>m;
	for(int i=1; i<=n; ++i)
		cin>>A[i];

	for(int i=1; i<=m; ++i)
		cin>>q[i].x>>q[i].y, q[i].id=i;

	sort(q+1, q+1+m, cmp);

	memset(T, -1, sizeof(T));

	int l=0, r=0;
	
	for(int i=1; i<=m; ++i)
	{
		int currl=q[i].x, currr=q[i].y;
		for(int j=r+1; j<=currr; ++j)
		{
			if(!last[A[j]]){
				last[A[j]]=j;
			}
			else
			{
				update(1, 1, n, j, j-last[A[j]]);
				used[last[A[j]]]=j;
				last[A[j]]=j;
			}
		}
		for(int j=l; j<currl; ++j)
		{
			if(used[j]){
				update(1, 1, n, used[j], -1);
				used[A[j]]=0;
			}
		}
		l=currl;
		r=currr;
		rs=-1;
		query(1, 1, n, currl, currr);
		rsint[q[i].id]=rs;
	}
	for(int i=1; i<=m; ++i)
		cout<<rsint[i]<<"\n";
}
 
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout<<setprecision(12)<<fixed;
    read();
    return 0;
}