Cod sursa(job #1709444)

Utilizator ALL10iUPB ALL10i ALL10i Data 28 mai 2016 12:18:49
Problema Pq Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 0.62 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int A[100001];
int poz[100001];
int last[100001];

int main()
{
	int N, Q;
	ifstream f("pq.in");
	ofstream g("pq.out");

	f >> N >> Q;

	for (int i = 1; i <= N; ++i) {
		f >> A[i];
		if (poz[A[i]] == 0) {
			last[i] = -1;
			poz[A[i]] = i;
			continue;
		}
		last[i] = poz[A[i]];
		poz[A[i]] = i;
	}
	
	for (; Q > 0; --Q) {
		int x, y;
		f >> x >> y;
		int max = -1;
		for (int i = x + 1; i <= y; ++i) {
			if (last[i] >= x) {
				int max_p = i - last[i];
				max = max ^ ((max ^ max_p) & -(max < max_p));
			}
		}
		g << max << '\n';
	}

	return 0;
}