Cod sursa(job #2632829)

Utilizator AlexMariMarinescu Alexandru AlexMari Data 5 iulie 2020 10:55:18
Problema Distincte Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.23 kb
#include<cstdio>
#include<fstream>
#include<algorithm>
#define MOD 666013
using namespace std;
ifstream f("distincte.in");
ofstream g("distincte.out");
struct elem
{
    long long st,dr,index;
};
elem w[100002];

inline bool cmp(const elem &a,const elem &b)
{
    if(a.dr==b.dr)
        return a.st<b.st;
    return a.dr<b.dr;
}

long long v[100002],prv[100002],ans[100002],n;
long long aib[100002];

long long querySum(long long poz) {
  long long answer = 0;
  for (int i = poz; i > 0; i -= i & -i)
    answer += aib[i];
  return answer;
}

void update(long long poz, long long val) {
  for (int i = poz; i <= n; i += i & -i)
    aib[i] += val;
}


int main()
{
    long long m,k,i,j;
    f>>n>>k>>m;
    for(i=1;i<=n;i++)
    {
        f>>v[i];
        update(i,v[i]);
    }
    for(i=1;i<=m;i++)
        f>>w[i].st>>w[i].dr,w[i].index=i;
    sort(w+1,w+m+1,cmp);
    i=1;
    for(j=1;j<=m;j++)
    {
        while(i<=w[j].dr)
        {
            if(prv[v[i]]!=0)
                update(prv[v[i]],-v[i]);
            prv[v[i]]=i;
            i++;
        }
        ans[w[j].index]=querySum(w[j].dr)-querySum(w[j].st-1);
    }
    for(i=1;i<=m;i++)
        g<<ans[i]%MOD<<'\n';
    return 0;
}