#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <numeric>
#include <array>
using namespace std;
using ll = long long;
#define int ll
#define vi vector<int>
#define fore(i, a, b) for(int i=a; i<b; ++i)
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define pb push_back
#define endl '\n'
const int N = 5e5+5;
struct fenwick {
int n;
vi t;
fenwick(int nn){
n = nn;
t.assign(n+1, 0);
}
void add(int i, int val=1){
for(; i<=n; i+=i&-i){
t[i] += val;
}
}
int query(int i){
int r = 0;
while(i > 0){
r += t[i];
i -= i & -i;
}
return r;
}
};
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q;
cin >> n >> q;
vi a(n+1);
fore(i, 1, n+1){
cin >> a[i];
}
// compression
vi A = a;
sort(all(A));
A.erase(unique(all(A)), A.end());
fore(i, 1, n+1){
a[i] = lower_bound(all(A), a[i]) - A.begin() + 1;
}
fenwick dos(n+1), tres(n+1);
vector<array<int,3>> last(n+1, {-1, -1, -1});
vi ans(q);
vector<vector<pair<int,int>>> queries(n+1);
fore(i, 0, q){
int l, r;
cin >> l >> r;
queries[r].pb({l, i});
}
fore(r, 1, n+1){
int x = a[r];
auto &[o, p, q] = last[x];
if(q == -1){
q = r;
}
else if(p == -1){
p = q;
q = r;
dos.add(p);
}
else if(o == -1){
dos.add(p, -1);
o = p;
p = q;
dos.add(p);
tres.add(o);
q = r;
}
else {
dos.add(p, -1);
tres.add(o, -1);
o = p;
p = q;
dos.add(p);
tres.add(o);
q = r;
}
for(auto [l, j] : queries[r]){
int res = dos.query(r) - dos.query(l-1);
res -= tres.query(r) - tres.query(l-1);
ans[j] = res;
}
}
fore(i, 0, q){
cout << ans[i] << endl;
}
}
Compilation message
poklon.cpp: In function 'int main()':
poklon.cpp:80:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
80 | auto &[o, p, q] = last[x];
| ^
poklon.cpp:108:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
108 | for(auto [l, j] : queries[r]){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
860 KB |
Output is correct |
5 |
Correct |
52 ms |
13144 KB |
Output is correct |
6 |
Correct |
57 ms |
13124 KB |
Output is correct |
7 |
Correct |
137 ms |
26428 KB |
Output is correct |
8 |
Correct |
219 ms |
39912 KB |
Output is correct |
9 |
Correct |
318 ms |
52904 KB |
Output is correct |
10 |
Correct |
432 ms |
65892 KB |
Output is correct |