#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<ll, ll> ii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vii> vvii;
#define task "test"
#define fastIO ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define forw(i, l, r) for( ll i = (l) ; i < (r) ; i++ )
#define forb(i, r, l) for( ll i = (r) ; i >= (l) ; i-- )
#define sz(x) (int)x.size()
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define numBit(x) __builtin_popcount(x)
#define lb lower_bound
#define ub upper_bound
#define ar array
#define endl '\n'
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
const int N = 5e5 + 7;
const ll inf = 0x3f3f3f3f;
int n, q, a[N], prv[N], pos[N], bit[N], ans[N];
vi tmp;
vii event[N];
void upd(int p, int v) {
if (p == 0) return;
for (; p < N; p += p & -p)
bit[p] += v;
}
int get(int p) {
int ans = 0;
for (; p > 0; p -= p & -p)
ans += bit[p];
return ans;
}
int main() {
// fastIO;
scanf("%d %d", &n, &q);
forw(i, 1, n + 1) scanf("%d", &a[i]), tmp.push_back(a[i]);
sort(all(tmp)); tmp.resize(unique(all(tmp)) - tmp.begin());
forw(i, 1, n + 1) a[i] = lb(all(tmp), a[i]) - tmp.begin();
for (int i = 1, l, r; i <= q; i++) {
scanf("%d %d", &l, &r);
event[r].push_back(ii(l, i));
}
forw(i, 1, n + 1) {
prv[i] = pos[a[i]];
pos[a[i]] = i;
if (prv[i] != prv[prv[i]]) {
if (prv[prv[prv[i]]] != prv[prv[i]]) {
upd(prv[prv[prv[i]]], 1);
upd(prv[prv[i]], -1);
}
upd(prv[prv[i]], -1);
upd(prv[i], 1);
}
for (auto it: event[i])
ans[it.se] = get(i) - get(it.fi - 1);
}
forw(i, 1, q + 1) printf("%d\n", ans[i]);
}
/*
3 5 5 4 3 1 2 3 0 0 3
*/
Compilation message
poklon.cpp: In function 'int main()':
poklon.cpp:56:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
56 | scanf("%d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~
poklon.cpp:57:26: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
57 | forw(i, 1, n + 1) scanf("%d", &a[i]), tmp.push_back(a[i]);
| ~~~~~^~~~~~~~~~~~~
poklon.cpp:62:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d %d", &l, &r);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12112 KB |
Output is correct |
2 |
Correct |
9 ms |
12048 KB |
Output is correct |
3 |
Correct |
8 ms |
12108 KB |
Output is correct |
4 |
Correct |
11 ms |
12416 KB |
Output is correct |
5 |
Correct |
84 ms |
17076 KB |
Output is correct |
6 |
Correct |
112 ms |
17044 KB |
Output is correct |
7 |
Correct |
185 ms |
21940 KB |
Output is correct |
8 |
Correct |
288 ms |
26624 KB |
Output is correct |
9 |
Correct |
380 ms |
31532 KB |
Output is correct |
10 |
Correct |
526 ms |
36284 KB |
Output is correct |