#include <bits/stdc++.h>
#define inf 2e9
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <int, int> pii;
const int N = 500000 + 3, sz = 707;
int n, nq, a[N], cnt[N], k2, ans[N], b[N];
pair <pii, int> q[N];
unordered_map <int, int> mp;
bool cmp(pair <pii, int> a, pair <pii, int> b){
return (a.first.first / sz) < (b.first.first / sz) ||
((a.first.first / sz) == (b.first.first / sz) && a.first.second < b.first.second);
}
void add(int x){
if (cnt[x] == 2) k2--;
cnt[x]++;
if (cnt[x] == 2) k2++;
}
void err(int x){
if (cnt[x] == 2) k2--;
cnt[x]--;
if (cnt[x] == 2) k2++;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
cin >> n >> nq;
for (int i = 0; i < n; i++) {
cin >> a[i];
b[i] = a[i];
}
for (int i = 0; i < nq; i++) {
cin >> q[i].first.first >> q[i].first.second;
q[i].second = i;
}
sort(q, q + nq, cmp);
sort(b, b + n);
int b_sz = unique(b, b + n) - b;
for (int i = 0; i < b_sz; i++)
mp[b[i]] = i;
for (int i = 0; i < n; i++) a[i] = mp[a[i]];
int l = 0, r = -1;
for (int i = 0; i < nq; i++){
while (r < q[i].first.second) add(a[++r]);
while (r > q[i].first.second) err(a[r--]);
while (l < q[i].first.first) err(a[l++]);
while (l > q[i].first.first) add(a[--l]);
ans[q[i].second] = k2;
}
for (int i = 0; i < nq; i++) cout << ans[i] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
3 |
Incorrect |
6 ms |
384 KB |
Output isn't correct |
4 |
Incorrect |
12 ms |
512 KB |
Output isn't correct |
5 |
Incorrect |
193 ms |
3040 KB |
Output isn't correct |
6 |
Incorrect |
196 ms |
2924 KB |
Output isn't correct |
7 |
Incorrect |
561 ms |
5600 KB |
Output isn't correct |
8 |
Incorrect |
899 ms |
8184 KB |
Output isn't correct |
9 |
Incorrect |
1398 ms |
11256 KB |
Output isn't correct |
10 |
Incorrect |
1999 ms |
13944 KB |
Output isn't correct |