Cod sursa(job #1709065)

Utilizator UPT-DaUPT Troncota Teudan Vijdea UPT-Da Data 28 mai 2016 10:45:55
Problema Pq Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 0.84 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
    ifstream in("pq.in");
    ofstream out("pq.out");
    std::vector<int> lastIndex(100100, -1);
    std::vector<int> pos(100100, -1);

    int N, Q;
    in >> N >> Q;
    for (int i = 0; i < N; ++i) {
        int x;
        in >> x;
        if (lastIndex[x] >= 0) {
            pos[i] = i - lastIndex[x];
        }
        lastIndex[x] = i;
    }

    for (int q = 0; q < Q; ++q) {
        int L, R;
        in >> L >> R;
        L -= 1;
        R -= 1;
        int maxCost = -1;
        for (int i = L; i <= R; ++i) {
            if (pos[i] > 0 && i - pos[i] >= L && pos[i] > maxCost) {
                maxCost = pos[i];
            }
        }

        out << maxCost << '\n';
    }

    out.flush();
    return 0;
}