Submission #477551

#TimeUsernameProblemLanguageResultExecution timeMemory
4775512548631Poklon (COCI17_poklon)C++17
140 / 140
429 ms32888 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pii> vii; typedef vector<vi> vvi; typedef vector<vll> vvll; typedef vector<vii> vvii; #define fastIO ios::sync_with_stdio(false), cin.tie(NULL) #define forw(i, l, r) for( int i = (l) ; i < (r) ; i++ ) #define forb(i, r, l) for( int i = (r) ; i >= (l) ; i-- ) #define log2i(x) (64 - __builtin_clzll(1ll * (x)) - 1) #define numBit(x) (__builtin_popcountll(1ll * (x))) #define getBit(x, i) ((x) >> (i) & 1) #define Pi acos(-1.0l) #define sz(x) int(x.size()) #define mt make_tuple #define mp make_pair #define fi first #define se second #define pb push_back #define pf push_front #define pob pop_back #define pof pop_front #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() #define debug(x) cerr << #x << " = " << x << '\n'; const int N = 5e5 + 7; int n, q; int a[N], prv[N], T[N], ans[N], pos[N]; vi tmp; vii event[N]; void upd(int x, int v) { if(x == 0) return; for(; x <= n; x += -x & x) T[x] += v; } int get(int x) { int res = 0; for(; x > 0; x -= -x & x) res += T[x]; return res; } int main() { fastIO; #ifndef ONLINE_JUDGE //freopen("test.inp", "r", stdin); //freopen("test.out", "w", stdout); #endif cin >> n >> q; forw(i, 1, n + 1) cin >> a[i], tmp.pb(a[i]); sort(all(tmp)); tmp.resize(unique(all(tmp)) - tmp.begin()); forw(i, 1, n + 1) a[i] = lower_bound(all(tmp), a[i]) - tmp.begin(); forw(i, 0, q) { int l, r; cin >> l >> r; event[r].pb(mp(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[i]] != prv[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 x:event[i]) { ans[x.se] = get(n) - get(x.fi - 1); } } forw(i, 0, q) cout << ans[i] << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...