제출 #519388

#제출 시각아이디문제언어결과실행 시간메모리
519388KeshiDiversity (CEOI21_diversity)C++17
64 / 100
7006 ms47940 KiB
//In the name of God #include <bits/stdc++.h> 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; #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; } void add(int x){ x = a[x]; upd(upper_bound(cnt, cnt + N, cn[x]) - cnt - 1, 1); cn[x]++; } void rem(int x){ x = a[x]; upd(lower_bound(cnt, cnt + N, cn[x]) - cnt, -1); cn[x]--; } 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]; } int L = 0, R = 0; while(q--){ int l, r; cin >> l >> r; l--; while(R < r) add(R++); while(L > l) add(--L); while(R > r) rem(--R); while(L < l) rem(L++); ll m = r - l; ll ans = m * (m + 1) / 2 * (N + 2) - A::seg[1] - B::seg[1]; cout << ans << "\n"; } return 0; }
#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...