Cod sursa(job #2572723)

Utilizator vlad.ulmeanu30Ulmeanu Vlad vlad.ulmeanu30 Data 5 martie 2020 13:58:12
Problema Distincte Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.76 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;

FILE *fin, *fout;
namespace prs {
  const int DIM = (1<<17);
  char BUFF[DIM+5];
  int kbu = 0;
  void reset () { if (prs::kbu == prs::DIM) fread (prs::BUFF, 1, prs::DIM, fin), prs::kbu = 0; }
  void read (int &nr) {
    nr = 0;
    while (prs::BUFF[prs::kbu] < '0' || prs::BUFF[prs::kbu] > '9') { prs::kbu++; prs::reset (); }
    while (prs::BUFF[prs::kbu] >= '0' && prs::BUFF[prs::kbu] <= '9') { nr = nr * 10 + prs::BUFF[prs::kbu++] - '0'; prs::reset (); }
  }
}

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)) {
      if (amt >= 0) mad(yy[x], amt);
      else msb(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 () {
  fin = fopen ("distincte.in", "r");
  fout = fopen ("distincte.out", "w");
  int k, m; prs::read(n); prs::read(k); prs::read(m);
  int i, j, z;
  for (i = 1; i <= n; i++) prs::read(v[i]);
  qry.resize(m);
  for (i = 0; i < m; i++) {
    prs::read(qry[i].fi);
    prs::read(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++) fprintf(fout, "%d\n", qry[i].ans);
  fclose(fin); fclose(fout);
  return 0;
}