//Make CSP great again
//You're as beautiful as the day I lost you
#include <bits/stdc++.h>
#define TASK "TESTCODE"
#define Log2(x) 31 - __builtin_clz(x)
using namespace std;
const int N = 5e5;
int n, q, a[N + 1], res[N + 1];
struct FenwickTree
{
int Tree[N + 1];
void add(int pos, int val)
{
if (pos == 0)
{
return ;
}
for (; pos <= n; pos += pos & (-pos))
{
Tree[pos] += val;
}
}
int sum(int pos)
{
int ret = 0;
for (; pos > 0; pos -= pos & (-pos))
{
ret += Tree[pos];
}
return ret;
}
int sum(int l, int r)
{
return sum(r) - sum(l - 1);
}
} Tree;
vector<pair<int, int> > Query[N + 1];
map<int, int> Map[3];
void read()
{
cin >> n >> q;
for (int i = 1; i <= n; ++ i)
{
cin >> a[i];
}
for (int i = 1; i <= q; ++ i)
{
int l, r;
cin >> l >> r;
Query[r].push_back(make_pair(i, l));
}
}
void solve()
{
for (int i = 1; i <= n; ++ i)
{
Tree.add(Map[2][a[i]], 1);
Map[2][a[i]] = Map[1][a[i]];
Tree.add(Map[2][a[i]], -2);
Map[1][a[i]] = Map[0][a[i]];
Tree.add(Map[1][a[i]], 1);
Map[0][a[i]] = i;
for (auto x : Query[i])
{
res[x.first] = Tree.sum(x.second, i);
}
}
for (int i = 1; i <= q; ++ i)
{
cout << res[i] << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(TASK".INP", "r"))
{
freopen(TASK".INP", "r", stdin);
//freopen(TASK".OUT", "w", stdout);
}
int t = 1;
bool typetest = false;
if (typetest)
{
cin >> t;
}
for (int __ = 1; __ <= t; ++ __)
{
//cout << "Case " << __ << ": ";
read();
solve();
}
}
Compilation message
poklon.cpp: In function 'int main()':
poklon.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12080 KB |
Output is correct |
2 |
Correct |
8 ms |
12084 KB |
Output is correct |
3 |
Correct |
8 ms |
12116 KB |
Output is correct |
4 |
Correct |
11 ms |
12244 KB |
Output is correct |
5 |
Correct |
80 ms |
16884 KB |
Output is correct |
6 |
Correct |
76 ms |
16808 KB |
Output is correct |
7 |
Correct |
198 ms |
22064 KB |
Output is correct |
8 |
Correct |
349 ms |
27388 KB |
Output is correct |
9 |
Correct |
426 ms |
32416 KB |
Output is correct |
10 |
Correct |
639 ms |
37448 KB |
Output is correct |