Submission #610893

#TimeUsernameProblemLanguageResultExecution timeMemory
610893racsosabePoklon (COCI17_poklon)C++14
140 / 140
750 ms32372 KiB
#include<bits/stdc++.h> using namespace::std; const int N = 500000 + 5; int n; int q; int a[N]; int L[N]; int st[N << 2]; int ans[N]; int last1[N]; int last2[N]; int last3[N]; vector<int> Q[N]; void compress(){ set<int> S; for(int i = 1; i <= n; i++) S.emplace(a[i]); int len = 0; map<int, int> id; for(int x : S){ id[x] = len++; } for(int i = 1; i <= n; i++) a[i] = id[a[i]]; } void update(int x, int y, int pos = 1, int l = 1, int r = n + 1){ if(l == r){ st[pos] += y; return; } int mi = (l + r) / 2; if(x <= mi) update(x, y, 2 * pos, l, mi); else update(x, y, 2 * pos + 1, mi + 1, r); st[pos] = st[2 * pos] + st[2 * pos + 1]; } int query(int x, int y, int pos = 1, int l = 1, int r = n + 1){ if(y < l or r < x) return 0; if(x <= l and r <= y) return st[pos]; int mi = (l + r) / 2; return query(x, y, 2 * pos, l, mi) + query(x, y, 2 * pos + 1, mi + 1, r); } int get_sum(int pos){ return query(1, pos); } void fix(int i){ int x = a[i]; int l1 = last1[x], l2 = last2[x], l3 = last3[x]; if(l2){ update(l3 + 1, -1); update(l2 + 1, 1); } if(l1){ update(l2 + 1, 1); update(l1 + 1, -1); } last3[x] = l2; last2[x] = l1; last1[x] = i; } void download(int i){ for(int at : Q[i]){ ans[at] = get_sum(L[at]); } } void solve(){ for(int i = 1; i <= n; i++){ fix(i); download(i); } } int main(){ scanf("%d %d", &n, &q); for(int i = 1; i <= n; i++) scanf("%d", a + i); compress(); for(int i = 1; i <= q; i++){ int r; scanf("%d %d", L + i, &r); Q[r].emplace_back(i); } solve(); for(int i = 1; i <= q; i++) printf("%d\n", ans[i]); return 0; }

Compilation message (stderr)

poklon.cpp: In function 'int main()':
poklon.cpp:80:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |  scanf("%d %d", &n, &q);
      |  ~~~~~^~~~~~~~~~~~~~~~~
poklon.cpp:81:35: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |  for(int i = 1; i <= n; i++) scanf("%d", a + i);
      |                              ~~~~~^~~~~~~~~~~~~
poklon.cpp:85:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |   scanf("%d %d", L + i, &r);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...