Submission #518773

#TimeUsernameProblemLanguageResultExecution timeMemory
518773aloo123Poklon (COCI17_poklon)C++14
140 / 140
2300 ms20852 KiB
#include <algorithm> #include <bitset> #include <cassert> #include <chrono> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <iterator> #include <limits> #include <list> #include <map> #include <numeric> #include <queue> #include <random> #include <ratio> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <unordered_set> #include <utility> #include <vector> using namespace std; using ll = long long; using pl = pair<ll, ll>; using vl = vector<ll>; #define mp make_pair #define f first #define s second // vectors #define sz(x) (int)(x).size() #define all(x) begin(x), end(x) #define ins insert #define pf push_front #define pb push_back // loops #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define F0R(i, a) FOR(i, 0, a) #define ROF(i, a, b) for (int i = (b)-1; i >= (a); --i) #define R0F(i, a) ROF(i, 0, a) #define trav(a, x) for (auto &a : x) const int MOD = 1e9 + 7; // 998244353; const ll INF = 1e18; // not too close to LLONG_MAX #define int ll const int xd[8] = {1, 0, -1, 0, -1,-1,1,1}, yd[8] = {0, 1, 0, -1,-1,1,-1,1}; // for every grid problem!! const int N = 5e5+5; const int LOGN = 25; const int BLOCKSZ = 350; using namespace std; int a[N]; struct query{ int l,r,idx; }; query Q[N]; bool cmp(query x, query y){ return mp(x.l/BLOCKSZ, x.r) < mp(y.l/BLOCKSZ, y.r); } int freq[N]; int ans = 0; int fans[N]; void add(int x){ freq[a[x]]++; if(freq[a[x]] == 2) ans++; if(freq[a[x]] == 3) ans--; } void rem(int x){ freq[a[x]]--; if(freq[a[x]] == 2) ans++; if(freq[a[x]] == 1) ans--; } void solve(){ int n, q; cin >>n >> q; for(int i =0;i<n;i++)cin>>a[i]; //// set<int> s; for(int i =0;i<n;i++)s.ins(a[i]); vl v; for(int i:s) v.pb(i); map<int,int>m; for(int i = 0;i<sz(v);i++){ m[v[i]] = i + 1; } for(int i =0;i<n;i++){ a[i] = m[a[i]]; } /////// for(int i =1;i<=q;i++){ cin >> Q[i].l>> Q[i].r;Q[i].idx=i; Q[i].l--; Q[i].r--; } sort(Q+1,Q+q+1,cmp); int cur_l= 0, cur_r = -1; for(int i = 1;i <=q;i++){ while(cur_l>Q[i].l){ cur_l--; add(cur_l); } while(cur_r < Q[i].r){ cur_r++; add(cur_r); } while(cur_l<Q[i].l){ rem(cur_l); cur_l++; } while(cur_r>Q[i].r){ rem(cur_r); cur_r--; } fans[Q[i].idx] = ans; } for(int i= 1;i<=q;i++){ cout<<fans[i]<<"\n"; } } signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; // cin>>t; while (t--){ solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...