Cod sursa(job #2572679)

Utilizator vlad.ulmeanu30Ulmeanu Vlad vlad.ulmeanu30 Data 5 martie 2020 13:48:38
Problema Distincte Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.22 kb
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define fi first
#define se second
#define inf (INT_MAX/2-1)
#define infl (1LL<<60)
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define sz(a) (int)(a).size()
#define all(a) begin(a),end(a)
#define y0 y5656
#define y1 y7878
#define aaa system("pause");
#define dbg(x) cerr<<(#x)<<": "<<(x)<<'\n',aaa
#define dbga(x,n) cerr<<(#x)<<"[]: ";for(int _=0;_<n;_++)cerr<<x[_]<<' ';cerr<<'\n',aaa
#define dbgs(x) cerr<<(#x)<<"[stl]: ";for(int _:x)cerr<<_<<' ';cerr<<'\n',aaa
#define dbgp(x) cerr<<(#x)<<": "<<x.fi<<' '<<x.se<<'\n',aaa
#define maxn 100000

using namespace std;

ifstream fin ("distincte.in");
ofstream fout ("distincte.out");

int n;
const int mod = 666013;

void mad (int &a, int b) {
  a += b;
  while (a >= mod) a -= mod;
}

void msb (int &a, int b) {
  a -= b;
  while (a < 0) a += mod;
}

struct yes {
  int fi, se, ans, ind;
};

struct AIB {
  int yy[maxn+5];
  int lsb (int x) {
    return x&(-x);
  }
  void upd (int x, int amt) {
    for ( ; x <= n; x += lsb(x)) yy[x] += amt;
  }
  int qry (int x) {
    int ans = 0;
    for (; x > 0; x -= lsb(x)) mad(ans, yy[x]);
    return ans;
  }
  int qryrange (int a, int b) {
    int ans = qry(b);
    msb(ans, qry(a-1));
    return ans;
  }
};

AIB aib;
int v[maxn+5], last[maxn+5];
vector<yes> qry;

int main () {
  int k, m; fin >> n >> k >> m;///ai grija sa nu uiti variabile prin main
  int i, j, z;
  for (i = 1; i <= n; i++) fin >> v[i];
  qry.resize(m);
  for (i = 0; i < m; i++) {
    fin >> qry[i].fi >> qry[i].se;
    qry[i].ind = i;
  }
  sort(all(qry), [](yes a, yes b){return a.se < b.se;});
  z = 0;
  fill(all(last), -1);
  for (i = 0; i < m; i++) {
    for (j = z+1; j <= qry[i].se; j++) {
      if (last[v[j]] != -1) aib.upd(last[v[j]], -v[j]);
      last[v[j]] = j;
      aib.upd(last[v[j]], v[j]);
    }
    z = qry[i].se;
    qry[i].ans = aib.qryrange(qry[i].fi, n);
  }
  sort(all(qry), [](yes a, yes b){return a.ind < b.ind;});
  for (i = 0; i < m; i++) fout << qry[i].ans << '\n';
  fin.close(); fout.close();
  return 0;
}