#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 5e5 + 7;
const ll oo = 1e18 + 69;
int n, q, a[maxn], l, r, mp[maxn], near[maxn], res[maxn];
vector<pa> events[maxn];
struct BIT {
int bit[maxn];
void update(int x, int val) {
for(; x <= n; x += x&-x) bit[x] += val;
}
int get(int x) {
int ans = 0;
for(; x; x-= x&-x) ans += bit[x];
return ans;
}
int query(int l, int r) {
return get(r) - get(l - 1);
}
} bit;
void add(int i, int val) {
if(near[i] != 0) {
bit.update(near[i], val);
if(near[near[i]] != 0) {
bit.update(near[near[i]], -val);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen(TASK".inp", "r", stdin);
//freopen(TASK".out", "w", stdout);
cin>>n>>q;
vector<int> val;
for(int i = 1; i <= n; i++) {
cin>>a[i];
val.pb(a[i]);
}
sort(all(val));
val.erase(unique(all(val)), val.end());
for(int i = 1; i <= n; i++) a[i] = lower_bound(all(val), a[i]) - val.begin() + 1;
for(int i = 0; i < q; i++) {
cin>>l>>r;
events[r].pb({l, i});
}
for(int i = 1; i <= r; i++) {
if(mp[a[i]]) add(mp[a[i]], -1);
near[i] = mp[a[i]];
mp[a[i]] = i;
add(i, 1);
for(auto e:events[i]) {
res[e.se] = bit.query(e.fi, i);
}
}
for(int i = 0; i < q; i++) cout<<res[i]<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
11988 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
11988 KB |
Output isn't correct |
3 |
Incorrect |
6 ms |
12116 KB |
Output isn't correct |
4 |
Incorrect |
7 ms |
12244 KB |
Output isn't correct |
5 |
Incorrect |
53 ms |
16024 KB |
Output isn't correct |
6 |
Incorrect |
54 ms |
16072 KB |
Output isn't correct |
7 |
Incorrect |
114 ms |
20060 KB |
Output isn't correct |
8 |
Incorrect |
150 ms |
22404 KB |
Output isn't correct |
9 |
Incorrect |
229 ms |
27672 KB |
Output isn't correct |
10 |
Incorrect |
278 ms |
32024 KB |
Output isn't correct |