Cod sursa(job #1710054)

Utilizator UPT-DaUPT Troncota Teudan Vijdea UPT-Da Data 28 mai 2016 14:54:54
Problema Pq Scor 0
Compilator cpp Status done
Runda ONIS 2016 - Runda - 2 - ACM ICPC Romanian Programming Contest Marime 1.28 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);
    std::vector<int> maxBefore(100100, -1);

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

    for (int i = 0; i < N; ++i) {
        cout << maxBefore[i] << " ";
    }

    for (int q = 0; q < Q; ++q) {
        int L, R;
        in >> L >> R;
        L -= 1;
        R -= 1;
        int maxCost = -1;
        if (maxBefore[R] - pos[maxBefore[R]] >= L) {
            out << pos[maxBefore[R]] << '\n';
            continue;
        }
        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;
}