# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
577662 | lukameladze | Poklon (COCI17_poklon) | C++14 | 397 ms | 46240 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
# include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define pii pair <int, int>
const int N = 5e5 + 5;
int a[N],tree[N],l[N],r[N],ans[N];
vector <int> que[N],v[N];
void update(int idx, int val) {
for (int i = idx; i < N; i += i&(-i)) {
tree[i] += val;
}
}
int getans(int idx) {
int pas = 0;
for (int i = idx; i > 0; i-=i&(-i)) {
pas += tree[i];
}
return pas;
}
signed main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int n,q;
cin>>n>>q;
for (int i = 1; i <= n; i++) {
cin>>a[i];
}
for (int i = 1; i <= q; i++) {
cin>>l[i]>>r[i];
que[r[i]].pb(i);
}
for (int ri = 1; ri <= n; ri++) {
if (v[a[ri]].size()) {
update(v[a[ri]].back(), 1);
}
if (v[a[ri]].size() >= 2) {
int x = v[a[ri]].size();
update(v[a[ri]][x - 2], -2);
}
if (v[a[ri]].size() >= 3) {
int x = v[a[ri]].size();
update(v[a[ri]][x - 3], 1);
}
v[a[ri]].pb(ri);
for (int j = 0; j < que[ri].size(); j++) {
int idx = que[ri][j];
ans[idx] = getans(ri) - getans(l[idx] - 1);
}
}
for (int i = 1; i <= q; i++) {
cout<<ans[i]<<"\n";
}
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |