# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
552331 | Tien_Noob | Poklon (COCI17_poklon) | C++17 | 639 ms | 37448 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |