Cod sursa(job #2571409)

Utilizator NicolaalexandraNicola Alexandra Mihaela Nicolaalexandra Data 4 martie 2020 22:57:48
Problema Distincte Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.21 kb
#include <bits/stdc++.h>
#define DIM 100010
#define MOD 666013
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;
	}
};

struct query{
    int st,dr,poz;
};
query qry[DIM];
int v[DIM],aib[DIM],last[DIM],sol[DIM];
int n,m,k,i,j;
inline int cmp (query a, query b){
    return a.dr < b.dr;
}

void update (int p, int val){
    for (;p<=n;p+=(p&-p))
        aib[p] += val;
}
int query (int p){
    int sol = 0;
    for (;p;p-=(p&-p))
        sol = (sol + aib[p])%MOD;

    return sol;
}
int main (){
    InParser fin ("distincte.in");
    ofstream fout ("distincte.out");

    fin>>n>>k>>m;
    for (i=1;i<=n;++i)
        fin>>v[i];

    for (i=1;i<=m;i++){
        fin>>qry[i].st>>qry[i].dr;
        qry[i].poz = i;
    }
    sort (qry+1,qry+m+1,cmp);

    int pos = 1;
    for (i=1;i<=n;i++){

        if (last[v[i]]){
            /// il demarchez si il marchez la loc
            update (last[v[i]],-v[i]);
            update (i,v[i]);
        } else {
            update (i,v[i]);
        }
        last[v[i]] = i;

        while (pos <= m && qry[pos].dr == i){
            sol[qry[pos].poz] = (query (i) - query (qry[pos].st-1)) % MOD;
            if (sol[qry[pos].poz] < 0)
                sol[qry[pos].poz] += MOD;
            pos++;
        }

    }

    for (i=1;i<=m;i++)
        fout<<sol[i]<<"\n";


    return 0;
}