#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define pf push_front
#define N 100100
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9
using namespace std;
//using namespace __gnu_pbds;
typedef long double ld;
typedef long long ll;
typedef short int si;
typedef array <int, 3> a3;
//typedef tree <int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
vector <a3> qr;
int kol[500005], a[500005], koler = 0;
bool cmp(a3 a, a3 b)
{
int lx = a[0] / 700;
int rx = b[0] / 700;
if (lx != rx) return lx < rx;
if (lx % 2) return a[1] > b[1];
return a[1] < b[1];
}
void del(int x)
{
kol[a[x]]--;
if (kol[a[x]] == 1) koler--;
if (kol[a[x]] == 2) koler++;
}
void add(int x)
{
kol[a[x]]++;
if (kol[a[x]] == 3) koler--;
if (kol[a[x]] == 2) koler++;
}
int main()
{
////freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, k;
cin >> n >> k;
vector <int> pr(n);
for (int i = 0; i < n; i++) {cin >> a[i]; pr[i] = a[i];}
sort(pr.begin(), pr.end());
pr.resize(unique(pr.begin(), pr.end()) - pr.begin());
for (int i = 0; i < n; i++) a[i] = lower_bound(pr.begin(), pr.end(), a[i]) - pr.begin();
for (int i = 0; i < k; i++) {int x, y; cin >> x >> y; qr.pb({x, y, i});}
sort(qr.begin(), qr.end(), cmp);
int ans[n], L = 0, R = -1;
for (int i = 0; i < k; i++)
{
int nm = qr[i][2], l = qr[i][0], r = qr[i][1];
l--; r--;
while (R < r) {R++; add(R);}
while (L < l) {del(L); L++;}
while (R > r) {del(R); R--;}
while (L > l) {L--; add(L);}
ans[nm] = koler;
}
for (int i = 0; i < k; i++) cout << ans[i] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
11 ms |
512 KB |
Output is correct |
5 |
Correct |
173 ms |
3008 KB |
Output is correct |
6 |
Correct |
171 ms |
3072 KB |
Output is correct |
7 |
Correct |
403 ms |
5732 KB |
Output is correct |
8 |
Correct |
699 ms |
8936 KB |
Output is correct |
9 |
Correct |
1024 ms |
10768 KB |
Output is correct |
10 |
Correct |
1398 ms |
13352 KB |
Output is correct |