Cod sursa(job #1709680)

Utilizator ACTeam2016TUIASI NeculaiDimitrie ACTeam2016 Data 28 mai 2016 13:22:26
Problema Pq Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 0.94 kb
#include <fstream>
#include <iostream>
#include <list>
#include <algorithm> 

using namespace std;

ifstream in("pq.in");
ofstream out("pq.out");
int n, q;
list<int> v[100001];
int a[100002];
int main()
{
	in >> n >> q;
	for (int i = 1; i <= n; ++i)
	{
		in >> a[i];
		v[a[i]].push_back(i);
	}
	for (int i = 1; i <= q; ++i)
	{
		int x, y;
		in >> x >> y;
		int max = -1;

		for (int j = x; j <= y; ++j) {
			int n1 = 0, n2 = 0;
			int prim = 0, ultim = 0;
			for (list<int>::iterator it = v[a[j]].begin(); it != v[a[j]].end(); ++it) {
				if (prim == 0) {
					if (*it >= x) {
						prim = 1;
						n1 = *it;
					}
				}
				else {
					if (ultim == 0) {
						if (*it <= y) {
							ultim = 1;
							n2 = *it;
							max = max > (n2 - n1) ? max : (n2 - n1);
						}
						else {
							break;
						}
					}
					else {
						break;
					}
				}
			}


		}
		out << max << endl;
	}

	return 0;

}