# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1223426 | duyanhloveav | Poklon (COCI17_poklon) | C++20 | 213 ms | 28944 KiB |
#include <bits/stdc++.h>
using namespace std;
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1LL)
using ll = long long;
const int mxn = 9 + 1e6;
const int inf = 0x3f3f3f3f;
const ll lnf = 0x3f3f3f3f3f3f3f3f;
template<class T>
struct Fenwick {
vector<T> fenw;
int n;
Fenwick(int n): n(n) {
fenw.resize(n);
}
void upd(int x, T v) {
for (; x < n; x |= x + 1) {
fenw[x] += v;
}
}
T get(int x) {
T v{};
for (; x >= 0; x &= x + 1, --x) {
v += fenw[x];
}
return v;
}
};
void _27augain(void) {
int n, q;
cin >> n >> q;
map<int, int> mp;
vector<int> p(n + 1);
for (int i = 1; i <= n; ++i) {
int r;
cin >> r;
p[i] = mp[r];
mp[r] = i;
}
vector<vector<array<int, 2>>> query(n + 1);
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
query[r].push_back({l, i});
}
Fenwick<int> fenw(n + 1);
vector<int> res(q);
for (int r = 1; r <= n; ++r) {
fenw.upd(p[r], 1), fenw.upd(p[p[r]], -2), fenw.upd(p[p[p[r]]], 1);
for (auto [l, i] : query[r]) {
res[i] = fenw.get(r) - fenw.get(l - 1);
}
}
for (auto &x : res) {
cout << x << "\n";
}
}
int32_t main(void) {
#define task "27augain"
cin.tie(0)->sync_with_stdio(0);
for (string iext : {"in", "inp"}) {
if (fopen((task "." + iext).c_str(), "r")) {
freopen((task "." + iext).c_str(), "r", stdin);
freopen(task ".out", "w", stdout);
}
}
int testcase = 1;
// cin >> testcase;
while (testcase--) {
_27augain();
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |