이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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) {
ans -= sum1(i-1) * f[i] * i;
ans += sum2(i-1) * f[i];
ans -= f[i] * (sum2all - sum2(i) + sum1all - sum1(i));
ans += f[i] * (i-1) * (sum1all - sum1(i));
add1(i, -f[i]);
add2(i, -f[i] * (i-1));
ans -= f[i] * (f[i] + 1) / 2;
f[i] = x;
ans += f[i] * (f[i] + 1) / 2;
add1(i, f[i]);
add2(i, f[i] * (i-1));
ans += sum1(i-1) * f[i] * i;
ans -= sum2(i-1) * f[i];
ans += f[i] * (sum2all - sum2(i) + sum1all - sum1(i));
ans -= f[i] * (i-1) * (sum1all - sum1(i));
}
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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |