This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//In the name of God
#include <bits/stdc++.h>
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll maxn = 3e5 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
const int sq = 512;
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r+", stdin);freopen("output.txt", "w+", stdout);
#define pb push_back
#define Mp make_pair
#define F first
#define S second
#define Sz(x) ll((x).size())
#define all(x) (x).begin(), (x).end()
#define lc (id << 1)
#define rc (lc | 1)
ll pw(ll a, ll b){
ll c = 1;
while(b){
if(b & 1) c = c * a % mod;
a = a * a % mod;
b >>= 1;
}
return c;
}
int n, q, a[maxn], c[maxn], b[maxn], N, ind[maxn], cnt[maxn], cn[maxn];
namespace A{
ll seg[maxn << 2], sg[maxn << 2], lz[maxn << 2];
inline void Do(int id, int s, int e, ll x){
seg[id] += x * ((e - s) * (x + 1) + 2 * sg[id]) / 2;
sg[id] += x * (e - s);
lz[id] += x;
return;
}
void upd(int id, int s, int e, int l, int r, ll x){
if(r <= s || e <= l) return;
if(l <= s && e <= r){
Do(id, s, e, x);
return;
}
int mid = (s + e) >> 1;
Do(lc, s, mid, lz[id]);
Do(rc, mid, e, lz[id]);
lz[id] = 0;
upd(lc, s, mid, l, r, x);
upd(rc, mid, e, l, r, x);
seg[id] = seg[lc] + seg[rc];
sg[id] = sg[lc] + sg[rc];
return;
}
};
namespace B{
ll seg[maxn << 2], sg[maxn << 2], lz[maxn << 2];
inline void Do(int id, int s, int e, ll x){
seg[id] += x * ((e - s) * (x + 1) + 2 * sg[id]) / 2;
sg[id] += x * (e - s);
lz[id] += x;
return;
}
void upd(int id, int s, int e, int l, int r, ll x){
if(r <= s || e <= l) return;
if(l <= s && e <= r){
Do(id, s, e, x);
return;
}
int mid = (s + e) >> 1;
Do(lc, s, mid, lz[id]);
Do(rc, mid, e, lz[id]);
lz[id] = 0;
upd(lc, s, mid, l, r, x);
upd(rc, mid, e, l, r, x);
seg[id] = seg[lc] + seg[rc];
sg[id] = sg[lc] + sg[rc];
return;
}
};
void upd(int i, int x){
int j = ind[i];
cnt[i] += x;
A::upd(1, 0, N, j, N, x);
B::upd(1, 0, N, N - 1 - j, N, x);
return;
}
int wtf;
void add(int x){
wtf++;
x = a[x];
upd(upper_bound(cnt, cnt + N, cn[x]) - cnt - 1, 1);
cn[x]++;
}
void rem(int x){
wtf++;
x = a[x];
upd(lower_bound(cnt, cnt + N, cn[x]) - cnt, -1);
cn[x]--;
}
pll qq[maxn];
int p[maxn];
ll ans[maxn];
bool cmp(int i, int j){
return Mp(qq[i].F / sq, qq[i].S) < Mp(qq[j].F / sq, qq[j].S);
}
int main(){
fast_io;
{
N = maxn - 10;
int l = 0, r = N - 1;
for(int i = 0; i < N; i++){
if(i & 1) ind[i] = r--;
else ind[i] = l++;
}
}
cin >> n >> q;
for(int i = 0; i < n; i++){
cin >> a[i];
}
for(int i = 0; i < q; i++){
int l, r;
cin >> l >> r;
qq[i] = Mp(l, r);
p[i] = i;
}
//sort(p, p + q, cmp);
int L = 0, R = 0;
for(int i = 0; i < q; i++){
int l, r;
l = qq[p[i]].F;
r = qq[p[i]].S;
l--;
while(R < r) add(R++);
while(L > l) add(--L);
while(R > r) rem(--R);
while(L < l) rem(L++);
assert(wtf < n * 2 * (i + 1) + 1000);
ll m = r - l;
ans[p[i]] = m * (m + 1) / 2 * (N + 2) - A::seg[1] - B::seg[1];
}
for(int i = 0; i < q; i++){
cout << ans[i] << "\n";
}
return 0;
}
# | 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... |