Cod sursa(job #2106674)

Utilizator giotoPopescu Ioan gioto Data 16 ianuarie 2018 00:47:03
Problema Distincte Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.57 kb
#include <bits/stdc++.h>
using namespace std;

int n, k, m;
int a[100005], p[100005], y[100005], x[100005], ans[100005], aib[100005];
vector <pair <int, int> > v[100005];
const int MOD = 666013;
class InParser {
private:
    FILE *fin;
    char *buff;
    int sp;

    char read_ch() {
        ++sp;
        if (sp == 4096) {
            sp = 0;
            fread(buff, 1, 4096, fin);
        }
        return buff[sp];
    }

public:
    InParser(const char* nume) {
        fin = fopen(nume, "r");
        buff = new char[4096]();
        sp = 4095;
    }

    InParser& operator >> (int &n) {
        char c;
        while (!isdigit(c = read_ch()) && c != '-');
        int sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }

    InParser& operator >> (long long &n) {
        char c;
        n = 0;
        while (!isdigit(c = read_ch()) && c != '-');
        long long sgn = 1;
        if (c == '-') {
            n = 0;
            sgn = -1;
        } else {
            n = c - '0';
        }
        while (isdigit(c = read_ch())) {
            n = 10 * n + c - '0';
        }
        n *= sgn;
        return *this;
    }
};
inline bool cmp(int i, int j){
    return y[i] > y[j];
}
inline void U(int p, int val){
    for(int i = p; i <= n ; i = i + (i & (-i))){
        aib[i] += val;
        if(aib[i] >= MOD) aib[i] -= MOD;
    }
}
inline int Q(int p){
    int Sol = 0;
    for(int i = p; i >= 1 ; i = i - (i & (-i))){
        Sol = Sol + aib[i];
        if(Sol >= MOD) Sol -= MOD;
    }
    return Sol;
}
int main()
{
    InParser fin("distincte.in");
    freopen("distincte.out", "w", stdout);
    fin >> n >> k >> m;
    for(int i = 1; i <= n ; ++i)
        fin >> a[i], p[a[i]] = n + 1;
    for(int i = n; i >= 1 ; --i){
        x[i] = i;
        y[i] = p[a[i]];
        p[a[i]] = i;
    }
    sort(x + 1, x + n + 1, cmp);
    int st, dr;
    for(int i = 1; i <= m ; ++i){
        fin >> st >> dr;
        v[dr].push_back(make_pair(i, st));
    }
    int i = 1;
    for(int t = n; t >= 1 ; --t){
        while(y[x[i]] > t && i <= n) U(x[i], a[x[i]]), ++i;
        for(auto it : v[t]){
            int Sol = Q(t) - Q(it.second - 1);
            if(Sol < 0) Sol += MOD;
            ans[it.first] = Sol;
        }
    }
    for(int i = 1; i <= m ; ++i)
        printf("%d\n", ans[i]);
    return 0;
}