#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return __lg(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T& container, char separator = ' ', char finish = '\n'){
for(auto item: container) cout << item << separator;
cout << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
struct FenwickTree{
int n;
vector<ll> a;
FenwickTree(int _n){
n = _n;
a.resize(n+1);
}
void update(int i, ll v){
while(i <= n){
a[i] += v;
i += LASTBIT(i);
}
}
ll get(int i){
ll ans = 0;
while(i > 0){
ans += a[i];
i -= LASTBIT(i);
}
return ans;
}
ll get(int l, int r){return get(r) - get(l-1);}
};
void solve(){
int n, q; cin >> n >> q;
vector<int> a(n+1);
for(int i = 1; i<=n; ++i) cin >> a[i];
vector<int> b = a; remove_dup(b);
for(int i = 1; i <= n; ++i) a[i] = lower_bound(ALL(b), a[i]) - b.begin();
int m = b.size() - 1;
vector<int> ans(q);
vector<vector<pair<int, int>>> query(n+1);
for(int i= 0; i<q; ++i){
int l, r; cin >> l >> r;
query[r].push_back({l, i});
}
vector<array<int, 2>> last2(m+1, {{n+1, n+1}});
vector<array<int, 3>> last3(m+1, {{n+1, n+1, n+1}});
FenwickTree bit(n);
for(int i = 1; i<=n; ++i){
bit.update(last2[a[i]][0], -1);
bit.update(last3[a[i]][0], 1);
for(int j = 1; j < 2; ++j) swap(last2[a[i]][j-1], last2[a[i]][j]);
for(int j = 1; j < 3; ++j) swap(last3[a[i]][j-1], last3[a[i]][j]);
last2[a[i]][1] = i;
last3[a[i]][2] = i;
bit.update(last2[a[i]][0], 1);
bit.update(last3[a[i]][0], -1);
for(pair<int, int> j: query[i]){
ans[j.second] = bit.get(j.first, i);
}
}
printArr(ans);
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
clock_t start = clock();
solve();
cerr << "Time elapsed: " << clock() - start << "ms!\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
3 ms |
852 KB |
Output is correct |
5 |
Correct |
61 ms |
8244 KB |
Output is correct |
6 |
Correct |
58 ms |
8340 KB |
Output is correct |
7 |
Correct |
179 ms |
16712 KB |
Output is correct |
8 |
Correct |
277 ms |
24788 KB |
Output is correct |
9 |
Correct |
434 ms |
33200 KB |
Output is correct |
10 |
Correct |
424 ms |
41228 KB |
Output is correct |