Submission #696606

#TimeUsernameProblemLanguageResultExecution timeMemory
696606cig32Diversity (CEOI21_diversity)C++14
64 / 100
7023 ms12512 KiB
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; #define int long long const int MAXN = 3e5 + 10; const int MOD = 1e9 + 7; mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count()); int rnd(int x, int y) { int u = uniform_int_distribution<int>(x, y)(rng); return u; } int bm(int b, int p) { if(p==0) return 1 % MOD; int r = bm(b, p >> 1); if(p&1) return (((r*r) % MOD) * b) % MOD; return (r*r) % MOD; } int inv(int b) { return bm(b, MOD-2); } int fastlog(int x) { return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1); } void printcase(int i) { cout << "Case #" << i << ": "; } int f[MAXN], ans, n, sum1all, sum2all; int bit1[MAXN]; void add1(int x, int v) { sum1all += v; for(;x<MAXN;x+=x&-x) bit1[x] += v; } int sum1(int x) { int s=0; for(;x;x-=x&-x) s += bit1[x]; return s; } int bit2[MAXN]; void add2(int x, int v) { sum2all += v; for(;x<MAXN;x+=x&-x) bit2[x] += v; } int sum2(int x) { int s=0; for(;x;x-=x&-x) s += bit2[x]; return s; } int bit3[MAXN]; void add3(int x, int v) { x++; for(;x<MAXN;x+=x&-x) bit3[x] += v; } int sum3(int x) { x++; int s=0; for(;x;x-=x&-x) s += bit3[x]; return s; } void edit(int i, int x) { int cal1 = sum1(i), cal2 = sum2(i); ans -= (cal1 - f[i]) * f[i] * i; ans += (cal2 - f[i] * (i-1)) * f[i]; ans -= f[i] * (sum2all - cal2 + sum1all - cal1); ans += f[i] * (i-1) * (sum1all - cal1); add1(i, x-f[i]); add2(i, (x-f[i]) * (i-1)); ans -= f[i] * (f[i] + 1) / 2; f[i] = x; ans += f[i] * (f[i] + 1) / 2; cal1 += x-f[i]; cal2 += (x-f[i]) * (i-1); ans += (cal1 - f[i]) * f[i] * i; ans -= (cal2 - f[i] * (i-1)) * f[i]; ans += f[i] * (sum2all - cal2 + sum1all - cal1); ans -= f[i] * (i-1) * (sum1all - cal1); } int block; bool modui(pair<pair<int,int>,int> a, pair<pair<int,int>,int> b) { if(a.first.first / block != b.first.first / block) return a.first.first / block < b.first.first / block; return a.first.second < b.first.second; } void solve(int tc) { int q; cin >> n >> q; int c = 300000; block = ceil(sqrt(c)); int arr[n+1]; for(int i=1; i<=n; i++) cin >> arr[i]; pair<pair<int,int>,int> qry[q+1]; for(int i=1; i<=q; i++) { cin >> qry[i].first.first >> qry[i].first.second; qry[i].second = i; } sort(qry+1, qry+q+1, modui); int res[q+1]; int l = qry[1].first.first, r = qry[1].first.first - 1; int freq[c+1]; for(int i=1; i<=c; i++) freq[i] = 0; add3(0, n); for(int i=1; i<=q; i++) { while(l > qry[i].first.first) { l--; add3(freq[arr[l]], -1); freq[arr[l]]++; add3(freq[arr[l]], 1); int rank = sum3(freq[arr[l]] - 1) + 1; int pos = (rank & 1 ? (rank + 1) / 2 : n + 1 - rank / 2); edit(pos, freq[arr[l]]); } while(r > qry[i].first.second) { add3(freq[arr[r]], -1); freq[arr[r]]--; add3(freq[arr[r]], 1); int rank = sum3(freq[arr[r]]); int pos = (rank & 1 ? (rank + 1) / 2 : n + 1 - rank / 2); edit(pos, freq[arr[r]]); r--; } while(r < qry[i].first.second) { r++; add3(freq[arr[r]], -1); freq[arr[r]]++; add3(freq[arr[r]], 1); int rank = sum3(freq[arr[r]] - 1) + 1; int pos = (rank & 1 ? (rank + 1) / 2 : n + 1 - rank / 2); edit(pos, freq[arr[r]]); } while(l < qry[i].first.first) { add3(freq[arr[l]], -1); freq[arr[l]]--; add3(freq[arr[l]], 1); int rank = sum3(freq[arr[l]]); int pos = (rank & 1 ? (rank + 1) / 2 : n + 1 - rank / 2); edit(pos, freq[arr[l]]); l++; } res[qry[i].second] = ans; } for(int i=1; i<=q; i++) cout << res[i] << "\n"; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; //cin >> t; for(int i=1; i<=t; i++) solve(i); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...