Cod sursa(job #2106659)

Utilizator giotoPopescu Ioan gioto Data 16 ianuarie 2018 00:03:34
Problema Distincte Scor 95
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.66 kb
#include <cstdio>
#include <fstream>
#include <algorithm>
using namespace std;

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;
    }
};
int n, k, m;
int a[100005], p[100005], y[100005], x[100005], ans[100005], aib[100005];
struct query{
    int i, j, p;
    bool operator < (const query &aux)const{
        if(j != aux.j) return j < aux.j;
        return i < aux.i;
    }
};
query q[100005];
const int MOD = 666013;
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);
    for(int i = 1; i <= m ; ++i)
        fin >> q[i].i >> q[i].j, q[i].p = i;
    sort(q + 1, q + m + 1);
    int i = n;
    for(int t = m; t >= 1 ; --t){
        while(y[x[i]] > q[t].j) U(x[i], a[x[i]]), --i;
        int Sol = Q(q[t].j) - Q(q[t].i - 1);
        if(Sol < 0) Sol += MOD;
        ans[q[t].p] = Sol;
    }
    for(int i = 1; i <= m ; ++i)
        printf("%d\n", ans[i]);
    return 0;
}