제출 #818067

#제출 시각아이디문제언어결과실행 시간메모리
818067vjudge1Poklon (COCI17_poklon)C++17
140 / 140
1020 ms23000 KiB
#include <bits/stdc++.h> #define ll long long #define ull unsigned ll #define pb push_back #define pa pair<int, int> #define pall pair<ll, int> #define fi first #define se second #define TASK "test" #define Size(x) (int) x.size() #define all(x) x.begin(), x.end() using namespace std; template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;} template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;} const int MOD = 1e9 + 7; const int LOG = 20; const int maxn = 5e5 + 7; const ll oo = 1e18 + 69; const int sz = 800; int n, q, a[maxn], res[maxn], l, r, cnt, mp[maxn]; struct query { int l, r, id; friend bool operator < (query &a, query &b) { if(a.l / sz == b.l / sz) return ((a.l / sz) & 1) == 0 ? a.r < b.r:a.r > b.r; return a.l / sz < b.l / sz; } }; vector<query> que; void add(int i) { mp[a[i]]++; if(mp[a[i]] == 3) cnt--; if(mp[a[i]] == 2) cnt++; } void sub(int i) { mp[a[i]]--; if(mp[a[i]] == 2) cnt++; if(mp[a[i]] == 1) cnt--; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); //freopen(TASK".inp", "r", stdin); //freopen(TASK".out", "w", stdout); cin>>n>>q; vector<int> val; for(int i = 1; i <= n; i++) { cin>>a[i]; val.pb(a[i]); } sort(all(val)); val.erase(unique(all(val)), val.end()); for(int i = 1; i <= n; i++) { a[i] = lower_bound(all(val), a[i]) - val.begin(); } for(int i = 0; i < q; i++) { cin>>l>>r; que.pb({l, r, i}); } sort(all(que)); l = 1, r = 0, cnt = 0; for(auto e:que) { while(r < e.r) add(++r); while(l > e.l) add(--l); while(r > e.r) sub(r--); while(l < e.l) sub(l++); res[e.id] = cnt; } for(int i = 0; i < q; i++) cout<<res[i]<<"\n"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...