Submission #736164

#TimeUsernameProblemLanguageResultExecution timeMemory
736164marvinthangPoklon (COCI17_poklon)C++17
140 / 140
1546 ms21460 KiB
/****************************** * author : @marvinthang * * date : 02 / 03 / 2021 * ******************************/ #include <bits/stdc++.h> using namespace std; #define superspeed ios_base::sync_with_stdio(false);\ cin.tie(NULL);\ //cout.tie(NULL); #define file(name) freopen(name".inp", "r", stdin);\ freopen(name".out", "w", stdout); const double PI = 3.1415926535897932384626433832795; // acos((db)-1); atan(-1.0); const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; const long long oo = 1e9; const int MAX = 5e5 + 5; const int BLOCK_SIZE = 700; #define getBlock(i) ((i) - 1) / BLOCK_SIZE + 1 #define getLeft(id) ((id) - 1) * BLOCK_SIZE + 1 #define getRight(id) (id) * BLOCK_SIZE int N, Q, A[MAX], pos[MAX], cnt[MAX]; struct Query { int l, r, id; Query(int _l = 0, int _r = 0, int _id = 0): l(_l), r(_r), id(_id) {}; bool operator < (const Query &other) const { return make_pair(getBlock(l), r) < make_pair(getBlock(other.l), other.r); } }; int ANS[MAX], res; void add(int id) { ++cnt[A[id]]; if (cnt[A[id]] == 3) --res; if (cnt[A[id]] == 2) ++res; } void del(int id) { --cnt[A[id]]; if (cnt[A[id]] == 1) --res; if (cnt[A[id]] == 2) ++res; } Query q[MAX]; int main() { superspeed; cin >> N >> Q; for (int i = 1; i <= N; ++i) cin >> A[i], pos[i] = i; sort(pos + 1, pos + 1 + N, [&](int a, int b) { return A[a] < A[b]; }); int last = -oo, cnt = 0; for (int i = 1; i <= N; ++i) { if (A[pos[i]] > last) ++cnt; last = A[pos[i]]; A[pos[i]] = cnt; } for (int i = 1; i <= Q; ++i) { int l, r; cin >> l >> r; q[i] = Query(l, r, i); } sort(q + 1, q + 1 + Q); int curL = 1, curR = 0; for (int i = 1; i <= Q; ++i) { int l = q[i].l, r = q[i].r; while (curL > l) add(--curL); while (curR < r) add(++curR); while (curL < l) del(curL++); while (curR > r) del(curR--); ANS[q[i].id] = res; } for (int i = 1; i <= Q; ++i) cout << ANS[i] << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...