QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#359706#5173. 染色zyc0704190 647ms273384kbC++141.6kb2024-03-20 20:07:242024-03-20 20:07:24

Judging History

你现在查看的是最新测评结果

  • [2024-03-20 20:07:24]
  • 评测
  • 测评结果:0
  • 用时:647ms
  • 内存:273384kb
  • [2024-03-20 20:07:24]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 3;

inline int read() {
	char ch = getchar(); int x = 0;
	while (!isdigit(ch)) {ch = getchar();}
	while (isdigit(ch)) {x = x * 10 + ch - 48; ch = getchar();}
	return x;
}

int n, T, a[N], lst[N], nxt[N], fa[N][22], va[N][22], mn[N][22];

inline int get(int l, int r) {
	if (l > r) return 1e9;
	int tmp = __lg(r - l + 1);
	return min(mn[l][tmp], mn[r - (1 << tmp) + 1][tmp]);
}

int main() {
	n = read(); T = read();
	for (int i = 1; i <= n; ++i) a[i] = read();
	for (int i = 1; i <= n; ++i) lst[i] = n + 1;
	for (int i = n; i >= 1; --i) {
		mn[i][0] = nxt[i] = lst[a[i]];
		lst[a[i]] = i;
	}
	for (int j = 1; j < 20; ++j)
		for (int i = 1; i + (1 << j) - 1 <= n; ++i)
			mn[i][j] = min(mn[i][j - 1], mn[i + (1 << (j - 1))][j - 1]);
	fa[n + 1][0] = n + 1; va[n + 1][0] = 0;
	for (int i = n; i >= 1; --i) {
		if (get(i + 1, nxt[i] - 1) > nxt[i]) fa[i][0] = nxt[i], va[i][0] = 1;
		else {
			int l = i + 1, r = nxt[i] - 1, mid;
//			while (l + 1 < r) {
//				mid = (l + r) >> 1;
//				if (get(i + 1, mid) < nxt[i]) r = mid;
//				else l = mid;
//			}
			if (get(i + 1, l) < nxt[i]) fa[i][0] = l;
			else fa[i][0] = r;
			va[i][0] = 0;
		}
	}
	for (int j = 1; j < 20; ++j)
		for (int i = 1; i <= n + 1; ++i)
			fa[i][j] = fa[fa[i][j - 1]][j - 1], va[i][j] = va[i][j - 1] + va[fa[i][j - 1]][j - 1];
	while (T--) {
		int l = read(), r = read(), ans;
		if (l > r) l ^= r ^= l ^= r;
		ans = ((r - l) << 1);
		int x = l;
		for (int i = 19; i >= 0; --i) {
			if (fa[x][i] > r) continue;
			ans -= va[x][i];
			x = fa[x][i];
		}
		printf("%d\n", ans);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 14948kb

input:

10000 100
84 85 52 2 78 53 20 21 23 76 37 44 18 5 37 8 81 65 46 58 69 1 69 37 53 46 37 35 35 89 1 77 35 6 46 59 89 46 25 55 50 38 61 67 44 23 29 24 46 4 42 15 34 77 20 34 83 79 12 50 69 26 38 14 9 66 80 72 22 26 9 68 35 38 19 84 92 30 83 62 100 71 81 60 7 37 64 50 33 60 86 75 45 78 32 53 3 48 87 60 ...

output:

3815
4755
6215
8716
760
7290
192
7613
511
1187
263
2333
13742
6502
474
7796
2242
10897
1864
2774
4988
304
6223
655
6925
6427
11864
2827
3281
8141
18462
530
5700
18526
9943
6395
5190
15148
691
481
16220
13173
9222
8134
11421
6871
6652
5220
9463
8864
2265
10695
2802
13023
5146
2229
9152
1980
6731
2948...

result:

wrong answer 1st words differ - expected: '3668', found: '3815'

Subtask #2:

score: 0
Wrong Answer

Test #7:

score: 0
Wrong Answer
time: 46ms
memory: 39764kb

input:

100000 100000
3 2 3 3 3 3 2 3 2 1 3 1 1 1 3 2 1 3 1 2 2 1 3 1 2 2 1 1 1 3 2 1 3 3 3 3 1 1 1 2 3 3 2 1 1 1 3 1 3 1 3 2 1 3 2 3 3 2 3 3 2 3 3 3 3 3 2 3 2 3 1 3 3 3 3 3 3 3 1 2 3 3 1 3 1 1 2 2 3 1 1 2 3 2 3 1 3 2 1 3 2 3 2 1 1 3 3 1 3 1 2 2 2 3 2 3 2 3 2 1 1 3 1 3 2 2 3 3 3 1 2 2 3 3 2 1 3 1 2 2 2 3 2 ...

output:

115503
135840
56081
6630
9378
79950
95887
29757
39779
113865
67977
7382
67353
43665
45565
50278
11354
109924
73191
10959
4827
88970
47857
77436
27881
11177
12493
60757
19040
56261
11889
8013
5645
39536
68628
59478
17306
71614
71765
54789
52922
558
18865
122331
4129
44165
63749
34670
10075
25487
3391...

result:

wrong answer 1st words differ - expected: '113194', found: '115503'

Subtask #3:

score: 0
Wrong Answer

Test #15:

score: 0
Wrong Answer
time: 3ms
memory: 18560kb

input:

5000 5000
256 63 197 36 75 66 33 72 27 75 66 248 29 166 209 252 141 95 84 226 147 249 116 94 192 256 199 273 182 166 116 274 27 211 154 144 283 23 53 110 215 11 164 284 161 221 251 96 43 47 18 115 12 51 156 61 116 209 93 98 47 165 174 106 83 67 184 75 12 290 183 197 112 240 67 56 215 148 104 5 141 2...

output:

1353
2759
1781
2672
4872
5113
3023
1358
902
5182
7994
4964
955
944
824
7444
7840
5458
3900
3310
4577
1612
2592
40
926
310
4457
3975
3897
5864
4719
1690
444
1450
3704
5372
580
122
2088
7788
9321
2098
3189
3766
2951
432
742
4101
184
3305
1473
1638
7288
3070
6092
8265
1579
3889
370
4576
3371
1783
836
2...

result:

wrong answer 1st words differ - expected: '1322', found: '1353'

Subtask #4:

score: 0
Wrong Answer

Test #23:

score: 0
Wrong Answer
time: 647ms
memory: 273384kb

input:

1000000 1000000
1105 3246 1880 3554 818 2331 2576 4140 149 4562 3498 3536 3400 4788 4363 4742 1216 4218 4032 1701 1489 4889 1761 3022 3145 4945 3067 4304 5016 4624 1612 13 1335 3613 1086 2210 386 3464 1156 3352 4341 5006 3465 3900 622 654 1826 2983 1250 4164 3335 4308 2995 1982 1347 4335 2535 5054 4...

output:

1270922
310349
764367
79906
161250
580119
993772
1725738
1352629
216402
619429
549301
1393687
322515
1100458
52580
277677
228857
2492
148580
145595
670852
25568
225034
185384
1453319
1675675
550721
147242
974749
1112692
238876
821883
113131
85290
1195385
318451
722002
170478
562989
772107
414542
736...

result:

wrong answer 1st words differ - expected: '1263815', found: '1270922'