#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vb = vector<bool>;
using vvb = vector<vb>;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<string>;
const ll mod = 1e9 + 7,inf = 1e18;
#define pb push_back
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
void setIO()
{
fast;
// #ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// #endif
}
const int rn = 700;
vi freq(5e5 + 1);
int ans1;
bool cmp(vi&a,vi&b)
{
if ((a[0]/rn) != (b[0]/rn))
return ((a[0]/rn) < (b[0]/rn));
return a[1] < b[1];
}
void add(int i)
{
if (freq[i] == 1)
ans1++;
if (freq[i] == 2)
ans1--;
freq[i]++;
}
void sub(int i)
{
if (freq[i] == 3)
ans1++;
if (freq[i] == 2)
ans1--;
freq[i]--;
}
int main()
{
setIO();
int n,q;
cin>>n>>q;
vi a(n + 1),ans(q);
vvi queries(q,vi(3));
for (int i = 1;i<=n;i++)
cin>>a[i];
vector<int> b = a;
sort(b.begin(), b.end());
map<int, int> m;
for (int i = 0; i < n; i++)
m[b[i]] = i;
for (int i = 0; i < n; i++)
a[i] = m[a[i]];
for (int i = 0;i<q;i++){
int l,r;
cin>>l>>r;
queries[i][0] = l,queries[i][1] = r;
queries[i][2] = i;
}
sort(queries.begin(),queries.end(),cmp);
int curL = 1,curR = 0;
for (int i = 0;i<q;i++){
while (curL > queries[i][0]){
curL--;
add(a[curL]);
}
while (curR < queries[i][1]){
curR++;
add(a[curR]);
}
while (curL < queries[i][0]){
sub(a[curL]);
curL++;
}
while (curR > queries[i][1]){
sub(a[curR]);
curR--;
}
ans[queries[i][2]] = ans1;
}
for (int i = 0;i<q;i++)cout<<ans[i]<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2288 KB |
Output is correct |
2 |
Incorrect |
4 ms |
2296 KB |
Output isn't correct |
3 |
Correct |
3 ms |
2296 KB |
Output is correct |
4 |
Correct |
13 ms |
2668 KB |
Output is correct |
5 |
Correct |
278 ms |
10624 KB |
Output is correct |
6 |
Correct |
297 ms |
10500 KB |
Output is correct |
7 |
Correct |
805 ms |
19252 KB |
Output is correct |
8 |
Correct |
1485 ms |
27816 KB |
Output is correct |
9 |
Correct |
2293 ms |
36432 KB |
Output is correct |
10 |
Correct |
3273 ms |
44948 KB |
Output is correct |