#include <bits/stdc++.h>
#define pb push_back
#define ll long long
#define fi first
#define se second
using namespace std;
const int N = 1e6+7;
const int oo = 1e9+7;
int n, q;
ll s[N], a[N], p[N], res[N];
vector<pair<int, int>> v[N];
map<ll, int> mp;
void update(int id, int l, int r, int x, int val)
{
if (l > x || r < x)
return;
if (l == r)
{
s[id] += val;
return;
}
int mid = (l + r) /2;
update(id * 2, l, mid, x, val);
update(id * 2 + 1, mid + 1, r, x, val);
s[id] = s[id * 2] + s[id * 2 + 1];
}
ll get(int id, int l, int r, int x, int y)
{
if (l > y || r < x)
return 0;
if (x <= l && r <= y)
return s[id];
int mid = (l + r) / 2;
return get(id * 2, l, mid, x, y) + get(id * 2 + 1, mid + 1, r, x, y);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen(".INP", "r", stdin);
// freopen(".OUT", "w", stdout);
cin>> n>> q;
for (int i = 1; i <= n; i++)
{
cin>> a[i];
p[i] = mp[a[i]];
mp[a[i]] = i;
}
for (int i = 1; i <= q; i++)
{
int l, r;
cin>> l>> r;
v[r].pb({l, i});
}
for (int i = 1; i <= n; i++)
{
int x = p[i];
int y = p[x];
if (x > 0)
update(1, 1, n, x, 1);
if (y > 0)
update(1, 1, n, y, -2);
if (v[i].size())
for (auto l:v[i])
res[l.se] = get(1, 1, n, l.fi, i);
update(1, 1, n, y, 1);
}
for (int i = 1; i <= q; i++)
cout<< res[i]<< '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
12 ms |
23836 KB |
Output isn't correct |
2 |
Incorrect |
13 ms |
23824 KB |
Output isn't correct |
3 |
Incorrect |
13 ms |
23892 KB |
Output isn't correct |
4 |
Incorrect |
16 ms |
24076 KB |
Output isn't correct |
5 |
Incorrect |
111 ms |
30480 KB |
Output isn't correct |
6 |
Incorrect |
119 ms |
30428 KB |
Output isn't correct |
7 |
Incorrect |
280 ms |
37380 KB |
Output isn't correct |
8 |
Runtime error |
386 ms |
89732 KB |
Execution killed with signal 11 |
9 |
Runtime error |
481 ms |
98392 KB |
Execution killed with signal 11 |
10 |
Runtime error |
681 ms |
107160 KB |
Execution killed with signal 11 |