답안 #519393

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
519393 2022-01-26T10:07:48 Z Keshi Diversity (CEOI21_diversity) C++17
64 / 100
7000 ms 46064 KB
//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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1740 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
3 Correct 1 ms 1868 KB Output is correct
4 Correct 1 ms 1888 KB Output is correct
5 Correct 1 ms 1868 KB Output is correct
6 Correct 1 ms 1868 KB Output is correct
7 Correct 1 ms 1888 KB Output is correct
8 Correct 2 ms 1868 KB Output is correct
9 Correct 1 ms 1868 KB Output is correct
10 Correct 2 ms 1868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1868 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
3 Correct 6 ms 1868 KB Output is correct
4 Correct 71 ms 2476 KB Output is correct
5 Correct 98 ms 3020 KB Output is correct
6 Correct 158 ms 3540 KB Output is correct
7 Correct 130 ms 3592 KB Output is correct
8 Correct 177 ms 3584 KB Output is correct
9 Correct 156 ms 3488 KB Output is correct
10 Correct 145 ms 3588 KB Output is correct
11 Correct 158 ms 3588 KB Output is correct
12 Correct 155 ms 3572 KB Output is correct
13 Correct 160 ms 3532 KB Output is correct
14 Correct 152 ms 3600 KB Output is correct
15 Correct 159 ms 3652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1868 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
3 Correct 6 ms 1868 KB Output is correct
4 Correct 71 ms 2476 KB Output is correct
5 Correct 98 ms 3020 KB Output is correct
6 Correct 158 ms 3540 KB Output is correct
7 Correct 130 ms 3592 KB Output is correct
8 Correct 177 ms 3584 KB Output is correct
9 Correct 156 ms 3488 KB Output is correct
10 Correct 145 ms 3588 KB Output is correct
11 Correct 158 ms 3588 KB Output is correct
12 Correct 155 ms 3572 KB Output is correct
13 Correct 160 ms 3532 KB Output is correct
14 Correct 152 ms 3600 KB Output is correct
15 Correct 159 ms 3652 KB Output is correct
16 Correct 1 ms 1868 KB Output is correct
17 Correct 2 ms 1884 KB Output is correct
18 Correct 7 ms 1868 KB Output is correct
19 Correct 54 ms 2500 KB Output is correct
20 Correct 112 ms 3200 KB Output is correct
21 Correct 174 ms 3592 KB Output is correct
22 Correct 168 ms 3656 KB Output is correct
23 Correct 172 ms 3684 KB Output is correct
24 Correct 169 ms 3540 KB Output is correct
25 Correct 172 ms 3560 KB Output is correct
26 Correct 185 ms 3584 KB Output is correct
27 Correct 168 ms 3588 KB Output is correct
28 Correct 159 ms 3588 KB Output is correct
29 Correct 168 ms 3588 KB Output is correct
30 Correct 171 ms 3584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1868 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
3 Correct 6 ms 1868 KB Output is correct
4 Correct 71 ms 2476 KB Output is correct
5 Correct 98 ms 3020 KB Output is correct
6 Correct 158 ms 3540 KB Output is correct
7 Correct 130 ms 3592 KB Output is correct
8 Correct 177 ms 3584 KB Output is correct
9 Correct 156 ms 3488 KB Output is correct
10 Correct 145 ms 3588 KB Output is correct
11 Correct 158 ms 3588 KB Output is correct
12 Correct 155 ms 3572 KB Output is correct
13 Correct 160 ms 3532 KB Output is correct
14 Correct 152 ms 3600 KB Output is correct
15 Correct 159 ms 3652 KB Output is correct
16 Correct 1 ms 1868 KB Output is correct
17 Correct 2 ms 1884 KB Output is correct
18 Correct 7 ms 1868 KB Output is correct
19 Correct 54 ms 2500 KB Output is correct
20 Correct 112 ms 3200 KB Output is correct
21 Correct 174 ms 3592 KB Output is correct
22 Correct 168 ms 3656 KB Output is correct
23 Correct 172 ms 3684 KB Output is correct
24 Correct 169 ms 3540 KB Output is correct
25 Correct 172 ms 3560 KB Output is correct
26 Correct 185 ms 3584 KB Output is correct
27 Correct 168 ms 3588 KB Output is correct
28 Correct 159 ms 3588 KB Output is correct
29 Correct 168 ms 3588 KB Output is correct
30 Correct 171 ms 3584 KB Output is correct
31 Correct 2 ms 1868 KB Output is correct
32 Correct 2 ms 1868 KB Output is correct
33 Correct 2 ms 1868 KB Output is correct
34 Correct 7 ms 1972 KB Output is correct
35 Correct 7 ms 1996 KB Output is correct
36 Correct 8 ms 2124 KB Output is correct
37 Correct 35 ms 2124 KB Output is correct
38 Correct 36 ms 2260 KB Output is correct
39 Correct 71 ms 2456 KB Output is correct
40 Correct 136 ms 2848 KB Output is correct
41 Correct 207 ms 3232 KB Output is correct
42 Correct 209 ms 3200 KB Output is correct
43 Correct 200 ms 3236 KB Output is correct
44 Correct 203 ms 3200 KB Output is correct
45 Correct 214 ms 3240 KB Output is correct
46 Correct 204 ms 3188 KB Output is correct
47 Correct 203 ms 3232 KB Output is correct
48 Correct 208 ms 3276 KB Output is correct
49 Correct 201 ms 3236 KB Output is correct
50 Correct 200 ms 3188 KB Output is correct
51 Correct 224 ms 3236 KB Output is correct
52 Correct 199 ms 3228 KB Output is correct
53 Correct 205 ms 3240 KB Output is correct
54 Correct 230 ms 3304 KB Output is correct
55 Correct 204 ms 3236 KB Output is correct
56 Correct 202 ms 3228 KB Output is correct
57 Correct 215 ms 3236 KB Output is correct
58 Correct 203 ms 3236 KB Output is correct
59 Correct 201 ms 3140 KB Output is correct
60 Correct 207 ms 3268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1740 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
3 Correct 1 ms 1868 KB Output is correct
4 Correct 1 ms 1888 KB Output is correct
5 Correct 1 ms 1868 KB Output is correct
6 Correct 1 ms 1868 KB Output is correct
7 Correct 1 ms 1888 KB Output is correct
8 Correct 2 ms 1868 KB Output is correct
9 Correct 1 ms 1868 KB Output is correct
10 Correct 2 ms 1868 KB Output is correct
11 Correct 1 ms 1868 KB Output is correct
12 Correct 2 ms 1868 KB Output is correct
13 Correct 6 ms 1868 KB Output is correct
14 Correct 71 ms 2476 KB Output is correct
15 Correct 98 ms 3020 KB Output is correct
16 Correct 158 ms 3540 KB Output is correct
17 Correct 130 ms 3592 KB Output is correct
18 Correct 177 ms 3584 KB Output is correct
19 Correct 156 ms 3488 KB Output is correct
20 Correct 145 ms 3588 KB Output is correct
21 Correct 158 ms 3588 KB Output is correct
22 Correct 155 ms 3572 KB Output is correct
23 Correct 160 ms 3532 KB Output is correct
24 Correct 152 ms 3600 KB Output is correct
25 Correct 159 ms 3652 KB Output is correct
26 Correct 1 ms 1868 KB Output is correct
27 Correct 2 ms 1884 KB Output is correct
28 Correct 7 ms 1868 KB Output is correct
29 Correct 54 ms 2500 KB Output is correct
30 Correct 112 ms 3200 KB Output is correct
31 Correct 174 ms 3592 KB Output is correct
32 Correct 168 ms 3656 KB Output is correct
33 Correct 172 ms 3684 KB Output is correct
34 Correct 169 ms 3540 KB Output is correct
35 Correct 172 ms 3560 KB Output is correct
36 Correct 185 ms 3584 KB Output is correct
37 Correct 168 ms 3588 KB Output is correct
38 Correct 159 ms 3588 KB Output is correct
39 Correct 168 ms 3588 KB Output is correct
40 Correct 171 ms 3584 KB Output is correct
41 Correct 2 ms 1868 KB Output is correct
42 Correct 2 ms 1868 KB Output is correct
43 Correct 2 ms 1868 KB Output is correct
44 Correct 7 ms 1972 KB Output is correct
45 Correct 7 ms 1996 KB Output is correct
46 Correct 8 ms 2124 KB Output is correct
47 Correct 35 ms 2124 KB Output is correct
48 Correct 36 ms 2260 KB Output is correct
49 Correct 71 ms 2456 KB Output is correct
50 Correct 136 ms 2848 KB Output is correct
51 Correct 207 ms 3232 KB Output is correct
52 Correct 209 ms 3200 KB Output is correct
53 Correct 200 ms 3236 KB Output is correct
54 Correct 203 ms 3200 KB Output is correct
55 Correct 214 ms 3240 KB Output is correct
56 Correct 204 ms 3188 KB Output is correct
57 Correct 203 ms 3232 KB Output is correct
58 Correct 208 ms 3276 KB Output is correct
59 Correct 201 ms 3236 KB Output is correct
60 Correct 200 ms 3188 KB Output is correct
61 Correct 224 ms 3236 KB Output is correct
62 Correct 199 ms 3228 KB Output is correct
63 Correct 205 ms 3240 KB Output is correct
64 Correct 230 ms 3304 KB Output is correct
65 Correct 204 ms 3236 KB Output is correct
66 Correct 202 ms 3228 KB Output is correct
67 Correct 215 ms 3236 KB Output is correct
68 Correct 203 ms 3236 KB Output is correct
69 Correct 201 ms 3140 KB Output is correct
70 Correct 207 ms 3268 KB Output is correct
71 Correct 36 ms 2380 KB Output is correct
72 Correct 38 ms 2432 KB Output is correct
73 Correct 37 ms 2416 KB Output is correct
74 Correct 38 ms 2440 KB Output is correct
75 Correct 37 ms 2428 KB Output is correct
76 Correct 73 ms 3092 KB Output is correct
77 Correct 77 ms 3148 KB Output is correct
78 Correct 73 ms 3084 KB Output is correct
79 Correct 72 ms 3120 KB Output is correct
80 Correct 72 ms 3116 KB Output is correct
81 Correct 148 ms 4320 KB Output is correct
82 Correct 160 ms 4376 KB Output is correct
83 Correct 150 ms 4292 KB Output is correct
84 Correct 152 ms 4372 KB Output is correct
85 Correct 152 ms 4300 KB Output is correct
86 Correct 158 ms 6076 KB Output is correct
87 Correct 165 ms 6012 KB Output is correct
88 Correct 170 ms 6112 KB Output is correct
89 Correct 164 ms 6084 KB Output is correct
90 Correct 149 ms 6056 KB Output is correct
91 Correct 235 ms 20164 KB Output is correct
92 Correct 234 ms 20220 KB Output is correct
93 Correct 273 ms 20224 KB Output is correct
94 Correct 233 ms 20136 KB Output is correct
95 Correct 276 ms 20212 KB Output is correct
96 Correct 246 ms 37492 KB Output is correct
97 Correct 264 ms 37316 KB Output is correct
98 Correct 262 ms 37288 KB Output is correct
99 Correct 256 ms 37320 KB Output is correct
100 Correct 241 ms 37300 KB Output is correct
101 Correct 244 ms 37308 KB Output is correct
102 Correct 244 ms 37284 KB Output is correct
103 Correct 241 ms 37436 KB Output is correct
104 Correct 238 ms 37420 KB Output is correct
105 Correct 239 ms 37340 KB Output is correct
106 Correct 246 ms 45824 KB Output is correct
107 Correct 248 ms 45892 KB Output is correct
108 Correct 233 ms 45876 KB Output is correct
109 Correct 240 ms 45928 KB Output is correct
110 Correct 248 ms 46064 KB Output is correct
111 Correct 241 ms 45844 KB Output is correct
112 Correct 244 ms 45928 KB Output is correct
113 Correct 241 ms 45888 KB Output is correct
114 Correct 234 ms 45892 KB Output is correct
115 Correct 254 ms 45828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1740 KB Output is correct
2 Correct 2 ms 1868 KB Output is correct
3 Correct 1 ms 1868 KB Output is correct
4 Correct 1 ms 1888 KB Output is correct
5 Correct 1 ms 1868 KB Output is correct
6 Correct 1 ms 1868 KB Output is correct
7 Correct 1 ms 1888 KB Output is correct
8 Correct 2 ms 1868 KB Output is correct
9 Correct 1 ms 1868 KB Output is correct
10 Correct 2 ms 1868 KB Output is correct
11 Correct 1 ms 1868 KB Output is correct
12 Correct 2 ms 1868 KB Output is correct
13 Correct 6 ms 1868 KB Output is correct
14 Correct 71 ms 2476 KB Output is correct
15 Correct 98 ms 3020 KB Output is correct
16 Correct 158 ms 3540 KB Output is correct
17 Correct 130 ms 3592 KB Output is correct
18 Correct 177 ms 3584 KB Output is correct
19 Correct 156 ms 3488 KB Output is correct
20 Correct 145 ms 3588 KB Output is correct
21 Correct 158 ms 3588 KB Output is correct
22 Correct 155 ms 3572 KB Output is correct
23 Correct 160 ms 3532 KB Output is correct
24 Correct 152 ms 3600 KB Output is correct
25 Correct 159 ms 3652 KB Output is correct
26 Correct 1 ms 1868 KB Output is correct
27 Correct 2 ms 1884 KB Output is correct
28 Correct 7 ms 1868 KB Output is correct
29 Correct 54 ms 2500 KB Output is correct
30 Correct 112 ms 3200 KB Output is correct
31 Correct 174 ms 3592 KB Output is correct
32 Correct 168 ms 3656 KB Output is correct
33 Correct 172 ms 3684 KB Output is correct
34 Correct 169 ms 3540 KB Output is correct
35 Correct 172 ms 3560 KB Output is correct
36 Correct 185 ms 3584 KB Output is correct
37 Correct 168 ms 3588 KB Output is correct
38 Correct 159 ms 3588 KB Output is correct
39 Correct 168 ms 3588 KB Output is correct
40 Correct 171 ms 3584 KB Output is correct
41 Correct 2 ms 1868 KB Output is correct
42 Correct 2 ms 1868 KB Output is correct
43 Correct 2 ms 1868 KB Output is correct
44 Correct 7 ms 1972 KB Output is correct
45 Correct 7 ms 1996 KB Output is correct
46 Correct 8 ms 2124 KB Output is correct
47 Correct 35 ms 2124 KB Output is correct
48 Correct 36 ms 2260 KB Output is correct
49 Correct 71 ms 2456 KB Output is correct
50 Correct 136 ms 2848 KB Output is correct
51 Correct 207 ms 3232 KB Output is correct
52 Correct 209 ms 3200 KB Output is correct
53 Correct 200 ms 3236 KB Output is correct
54 Correct 203 ms 3200 KB Output is correct
55 Correct 214 ms 3240 KB Output is correct
56 Correct 204 ms 3188 KB Output is correct
57 Correct 203 ms 3232 KB Output is correct
58 Correct 208 ms 3276 KB Output is correct
59 Correct 201 ms 3236 KB Output is correct
60 Correct 200 ms 3188 KB Output is correct
61 Correct 224 ms 3236 KB Output is correct
62 Correct 199 ms 3228 KB Output is correct
63 Correct 205 ms 3240 KB Output is correct
64 Correct 230 ms 3304 KB Output is correct
65 Correct 204 ms 3236 KB Output is correct
66 Correct 202 ms 3228 KB Output is correct
67 Correct 215 ms 3236 KB Output is correct
68 Correct 203 ms 3236 KB Output is correct
69 Correct 201 ms 3140 KB Output is correct
70 Correct 207 ms 3268 KB Output is correct
71 Correct 36 ms 2380 KB Output is correct
72 Correct 38 ms 2432 KB Output is correct
73 Correct 37 ms 2416 KB Output is correct
74 Correct 38 ms 2440 KB Output is correct
75 Correct 37 ms 2428 KB Output is correct
76 Correct 73 ms 3092 KB Output is correct
77 Correct 77 ms 3148 KB Output is correct
78 Correct 73 ms 3084 KB Output is correct
79 Correct 72 ms 3120 KB Output is correct
80 Correct 72 ms 3116 KB Output is correct
81 Correct 148 ms 4320 KB Output is correct
82 Correct 160 ms 4376 KB Output is correct
83 Correct 150 ms 4292 KB Output is correct
84 Correct 152 ms 4372 KB Output is correct
85 Correct 152 ms 4300 KB Output is correct
86 Correct 158 ms 6076 KB Output is correct
87 Correct 165 ms 6012 KB Output is correct
88 Correct 170 ms 6112 KB Output is correct
89 Correct 164 ms 6084 KB Output is correct
90 Correct 149 ms 6056 KB Output is correct
91 Correct 235 ms 20164 KB Output is correct
92 Correct 234 ms 20220 KB Output is correct
93 Correct 273 ms 20224 KB Output is correct
94 Correct 233 ms 20136 KB Output is correct
95 Correct 276 ms 20212 KB Output is correct
96 Correct 246 ms 37492 KB Output is correct
97 Correct 264 ms 37316 KB Output is correct
98 Correct 262 ms 37288 KB Output is correct
99 Correct 256 ms 37320 KB Output is correct
100 Correct 241 ms 37300 KB Output is correct
101 Correct 244 ms 37308 KB Output is correct
102 Correct 244 ms 37284 KB Output is correct
103 Correct 241 ms 37436 KB Output is correct
104 Correct 238 ms 37420 KB Output is correct
105 Correct 239 ms 37340 KB Output is correct
106 Correct 246 ms 45824 KB Output is correct
107 Correct 248 ms 45892 KB Output is correct
108 Correct 233 ms 45876 KB Output is correct
109 Correct 240 ms 45928 KB Output is correct
110 Correct 248 ms 46064 KB Output is correct
111 Correct 241 ms 45844 KB Output is correct
112 Correct 244 ms 45928 KB Output is correct
113 Correct 241 ms 45888 KB Output is correct
114 Correct 234 ms 45892 KB Output is correct
115 Correct 254 ms 45828 KB Output is correct
116 Execution timed out 7054 ms 2936 KB Time limit exceeded
117 Halted 0 ms 0 KB -