QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#511926#8322. 魔法手杖pavement#12 1275ms8004kbC++174.1kb2024-08-10 12:38:192024-08-10 12:38:19

Judging History

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

  • [2024-08-10 12:38:19]
  • 评测
  • 测评结果:12
  • 用时:1275ms
  • 内存:8004kb
  • [2024-08-10 12:38:19]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define pb push_back

using i128 = __int128;

int c, TC, n, m, k, b[100005], b_tmp[100005], idx[100005];
i128 a[100005], a0[100005];

void read(i128 &x){
	// read a __int128 variable
	char c; bool f = 0;
	while(((c = getchar()) < '0' || c > '9') && c != '-');
	if(c == '-'){f = 1; c = getchar();}
	x = c - '0';
	while((c = getchar()) >= '0' && c <= '9')x = x * 10 + c - '0';
	if(f) x = -x;
}

void write(i128 x){
	// print a __int128 variable
	if(x < 0){putchar('-'); x = -x;}
	if(x > 9)write(x / 10);
	putchar(x % 10 + '0');
}

i128 pow2[128];

bool is_on(i128 val, int bit) {
	return val % pow2[bit + 1] >= pow2[bit];
}

i128 f(int l, int r, int bit) {
	if (bit == -1) {
		return 0;
	}
	//~ cout << "AT " << l << ' ' << r << ' ' << bit << endl;
	if (is_on(a[l], bit) == is_on(a[r], bit)) {
		return pow2[bit] + f(l, r, bit - 1);
	} else {
		int pos = -1;
		for (int i = l; i <= r; i++) {
			if (is_on(a[i], bit)) {
				pos = i;
				break;
			}
		}
		assert(pos != -1 && l < pos && pos <= r);
		return max(f(l, pos - 1, bit - 1), f(pos, r, bit - 1));
	}
}

i128 get_max_x(int l, int r, int bit, i128 w) {
	if (bit == -1) {
		return 0;
	}
	if (is_on(a[l], bit) == is_on(a[r], bit)) {
		if (!is_on(a[l], bit)) {
			auto tmp = get_max_x(l, r, bit - 1, w);
			if (tmp == -1) {
				return -1;
			} else {
				return pow2[bit] + tmp;
			}
		} else {
			if (is_on(w, bit)) {
				return get_max_x(l, r, bit - 1, w);
			} else {
				auto tmp = get_max_x(l, r, bit - 1, w);
				if (tmp == -1) {
					return pow2[bit] - 1;
				} else {
					return pow2[bit] + tmp;
				}
			}
		}
	} else {
		if (is_on(w, bit)) {
			return -1;
		}
		int pos = -1;
		for (int i = l; i <= r; i++) {
			if (is_on(a[i], bit)) {
				pos = i;
				break;
			}
		}
		auto l_tmp = get_max_x(l, pos - 1, bit - 1, w);
		auto r_tmp = get_max_x(pos, r, bit - 1, w);
		return max(l_tmp, r_tmp == -1 ? -1 : r_tmp + pow2[bit]);
	}
}

bool can(i128 w) {
	// case 1: don't choose anything into S
	for (int i = 1; i <= n; i++) {
		a[i] = a0[i];
	}
	if (f(1, n, k - 1) >= w) {
		//~ cout << "CAN " << (int)w << " F\n";
		return 1;
	}
	// case 2: choose some things into S
	i128 cur_sum = 0;
	for (int i = n, r = n; i >= 1; i--) {
		cur_sum += b[i];
		while (cur_sum > m) {
			cur_sum -= b[r];
			r--;
		}
		if (i <= r) {
			//~ cout << "@ " << (int)w << " TRY " << i << ' ' << r << '\n';
			assert(!(i == 1 && r == n));
			for (int j = 1; j < i; j++) {
				a[j] = a0[j];
			}
			for (int j = i; j <= n - (r - i + 1); j++) {
				a[j] = a0[j + r - i + 1];
			}
			auto x = get_max_x(1, n - (r - i + 1), k - 1, w);
			if (x != -1 && x >= w - a0[i]) {
				//~ cout << "CAN " << (int)w << ' ' << i << ' ' << r << '\n';
				return 1;
			}
		}
	}
	return 0;
}

void solve() {
	scanf("%d %d %d", &n, &m, &k);
	for (int i = 1; i <= n; i++) {
		read(a0[i]);
	}
	i128 sum_b = 0;
	for (int i = 1; i <= n; i++) {
		scanf("%d", &b[i]);
		sum_b += b[i];
	}
	if (sum_b <= m) {
		// choose x = 2^k - 1
		i128 x = pow2[k] - 1;
		i128 ans = numeric_limits<i128>::max();
		for (int i = 1; i <= n; i++) {
			ans = min(ans, a0[i] + x);
		}
		write(ans);
		puts("");
		return;
	}
	iota(idx + 1, idx + 1 + n, 1);
	sort(idx + 1, idx + 1 + n, [&](const auto &lhs, const auto &rhs) {
		return a0[lhs] < a0[rhs];
	});
	//~ for (int i = 1; i <= n; i++) {
		//~ cout << idx[i] << ' ';
	//~ }
	//~ cout << '\n';
	for (int i = 1; i <= n; i++) {
		b_tmp[i] = b[idx[i]];
	}
	sort(a0 + 1, a0 + 1 + n);
	//~ for (int i = 1; i <= n; i++) {
		//~ write(a0[i]);
		//~ cout << ' ';
	//~ }
	//~ cout << '\n';
	//~ copy(b_tmp + 1, b_tmp + 1 + n, b + 1);
	//~ for (int i = 1; i <= n; i++) {
		//~ cout << b[i] << ' ';
	//~ }
	//~ cout << '\n';
	i128 lo = 0, hi = pow2[k] - 1, ans = 0;
	while (lo <= hi) {
		i128 mid = (lo + hi) / 2;
		if (can(mid)) {
			ans = mid;
			lo = mid + 1;
		} else {
			hi = mid - 1;
		}
	}
	write(ans);
	puts("");
}

int main() {
	pow2[0] = 1;
	for (int i = 1; i <= 123; i++) {
		pow2[i] = pow2[i - 1] * 2;
	}
	scanf("%d %d", &c, &TC);
	while (TC--) {
		solve();
	}
}

Details

Tip: Click on the bar to expand more detailed information

Pretests


Final Tests

Test #1:

score: 0
Wrong Answer
time: 1ms
memory: 5760kb

input:

1 2
1 1 10
69
0
9 1000000000 10
244 710 380 144 439 863 870 166 346
495676227 842003627 148079269 750582321 584950601 767126829 909307499 254106473 942938842

output:

1092
351

result:

wrong answer 2nd lines differ - expected: '479', found: '351'

Test #2:

score: 0
Wrong Answer
time: 1ms
memory: 5700kb

input:

2 2
1 1 10
523
0
9 1000000000 10
848 862 206 186 563 318 692 557 937
922116005 577545690 363781833 81032507 443868714 352716275 50542823 305806582 28805127

output:

1546
511

result:

wrong answer 2nd lines differ - expected: '681', found: '511'

Test #3:

score: 0
Wrong Answer
time: 1ms
memory: 5728kb

input:

3 2
1 1 10
98
0
9 1000000000 10
329 357 633 469 110 721 457 238 51
40948203 144541423 719902898 403414385 625735025 473335146 107749900 238792543 449945390

output:

1121
639

result:

wrong answer 2nd lines differ - expected: '511', found: '639'

Test #4:

score: 4
Accepted
time: 1274ms
memory: 7996kb

input:

4 5
100000 0 30
76630934 108936482 130420626 131855744 105346843 128458246 108243259 68059982 126654362 129080907 113416035 106976866 67840827 131702105 31856035 114313339 128664146 83795983 89307173 119627353 87072570 90008429 101830816 86771207 109365348 78483596 96660700 94183829 84341841 6114378...

output:

939540479
1073610765
1073641824
939540479
1073610766

result:

ok 5 lines

Test #5:

score: 4
Accepted
time: 1275ms
memory: 8000kb

input:

5 5
100000 0 30
76630412 108936989 130419821 131855908 105346921 128458123 108243250 68060024 126653456 129080620 113415939 106976648 67840659 131702589 31855988 114313916 128664271 83796359 89307559 119627595 87072119 90007889 101830798 86770759 109365415 78484075 96661493 94184047 84342493 6114315...

output:

939540479
1073610765
1073641824
939540479
1073610766

result:

ok 5 lines

Test #6:

score: 4
Accepted
time: 1272ms
memory: 7988kb

input:

6 5
100000 0 30
76630333 108936734 130420608 131855493 105346592 128458438 108243159 68059640 126653892 129080993 113415838 106976338 67840970 131701923 31855999 114313236 128664302 83796315 89307977 119627172 87072644 90007888 101831307 86771011 109365285 78483904 96661399 94183858 84341903 6114386...

output:

939540479
1073610765
1073641824
939540479
1073610766

result:

ok 5 lines

Test #7:

score: 0
Time Limit Exceeded

input:

7 250000
2 1 16
40654 60936
1 2
2 1 5
29 18
1 2
2 1 9
367 278
1 2
2 1 6
32 40
1 2
2 1 28
211414711 120442951
1 2
2 1 9
440 62
1 2
2 1 2
2 2
1 2
2 1 6
60 1
1 2
2 1 1
0 0
1 2
2 1 16
39195 9530
1 2
2 1 19
260304 52003
1 2
2 1 12
1492 2878
1 2
2 1 22
2112709 2056045
1 2
2 1 30
840827142 778131799
1 2
2 ...

output:

55295
24
458
55
201326591
287
3
32
1
38143
419839
2815
4141055
1040187391
1000
150994943
603979775
19
11619391
8811383
194846
23
94966
223743
4424
30357224
1
8716287
0
482344959
1015807
159
39958296
87
7
636
5124719
528875519
3
244235
0
0
12630
1535
8061695
51
16252927
53
3955199
60415
24903679
302
...

result:


Test #8:

score: 0
Time Limit Exceeded

input:

8 250000
2 1 24
7245747 7751903
1 2
2 1 18
7301 21614
1 2
2 1 2
1 3
1 2
2 1 3
0 4
1 2
2 1 22
1946046 3459042
1 2
2 1 1
0 1
1 2
2 1 27
110608129 62075905
1 2
2 1 7
79 78
1 2
2 1 1
0 0
1 2
2 1 8
190 156
1 2
2 1 29
213608577 61390223
1 2
2 1 21
194242 1613306
1 2
2 1 8
46 195
1 2
2 1 12
2674 3172
1 2
2...

output:

16515071
249855
2
3
3145727
0
100663295
126
1
237
452354047
1212415
170
3647
1
92
457727
111615
115711
2054207
58195967
3370495
1777
41
3026943
1475
14
7531007
7
52340
12799
319487
1
30
1753
1926
10043
216
217219967
5
1955
33534847
32214143
1
12
34
59391
1081087
1791
12499296
97
7864319
61
1
383
233...

result:


Test #9:

score: 0
Wrong Answer
time: 1159ms
memory: 8004kb

input:

9 40003
100000 1 30
76630623 108936745 130419844 131856358 105346889 128458736 108243812 68059447 126654184 129080977 113416077 106976936 67840619 131702296 31855896 114313546 128664506 83796193 89307481 119627719 87072176 90007912 101831608 86771305 109366220 78484119 96661356 94183598 84341931 611...

output:

939540479
1073610765
1073641824
3005
470286335
3195
992
3938975
402653183
31195135
22429
2047
1791
10
28671
103
772927
5505023
0
3
4694015
36863
39247871
939524095
13762559
71
7701
849298282
210351743
293601279
7
3670015
98303
491519
3735551
131071
27
1151
8389241
148759809
100663295
125311
178412
1...

result:

wrong answer 5th lines differ - expected: '536870911', found: '470286335'

Test #10:

score: 0
Wrong Answer
time: 1156ms
memory: 7964kb

input:

10 40003
100000 1 30
76630457 108937083 130420232 131855828 105346260 128458262 108242996 68059167 126653670 129080772 113416051 106976793 67840568 131702468 31855850 114313412 128664110 83796459 89307450 119627418 87071762 90007680 101831024 86771509 109365868 78483555 96661066 94183460 84342625 61...

output:

939540479
1073610765
1073641824
119200937
786431
62914559
6300883
41943039
383
4095
47
109051903
1406
75124094
33554431
6399
98303
3327
20334
2830
30
24575
506
106495
786431
29
134217727
411903
49375
57202
8236863
95
524287
1187963
77823
2
536870911
39
207771
7
364
261881855
5
369663
63
1136
255
0
2...

result:

wrong answer 5th lines differ - expected: '1048575', found: '786431'

Test #11:

score: 0
Time Limit Exceeded

input:

11 40003
100000 1 30
76630674 108936653 130420433 131856265 105346983 128458653 108243409 68059656 126653505 129080757 113415755 106976914 67840002 131701979 31855773 114313774 128664187 83796634 89307850 119627507 87071827 90007813 101831495 86771399 109365557 78483822 96660811 94184023 84342680 61...

output:


result:


Test #12:

score: 0
Time Limit Exceeded

input:

12 40003
100000 1 30
76630775 108937143 130420127 131855472 105346114 128458369 108243185 68060064 126653745 129080952 113415931 106976535 67841016 131701837 31855974 114313669 128664055 83796306 89307860 119626895 87072131 90008292 101831649 86771064 109366112 78484048 96660693 94184079 84342338 61...

output:


result:


Test #13:

score: 0
Time Limit Exceeded

input:

13 40003
100000 1 30
76630774 108936584 130420001 131856305 105347060 128457982 108243169 68059620 126654022 129081029 113415335 106976790 67840471 131702040 31855824 114314232 128663903 83796420 89307827 119626984 87072029 90008558 101830939 86770995 109365419 78483800 96660514 94183753 84342755 61...

output:


result:


Test #14:

score: 0
Wrong Answer
time: 2ms
memory: 5684kb

input:

14 5
100 1000000000 30
13132 1189 129473 100435 99142 27988 125913 84108 88618 104500 18510 127909 33713 41539 73168 52981 93666 111173 97215 18318 116125 48796 66468 68341 66995 8141 6453 78522 5137 102161 79767 83305 55735 35923 57672 40415 26124 3517 89580 103805 63333 70578 15006 115257 25327 11...

output:

1073638399
1073741720
1073658879
1073639423
1073741731

result:

wrong answer 1st lines differ - expected: '1073641471', found: '1073638399'

Test #15:

score: 0
Wrong Answer
time: 5ms
memory: 5956kb

input:

15 5
100 1000000000 30
13147 1100 129952 101117 98742 27859 125802 84826 88433 104546 18804 127522 33108 41885 72848 52261 94114 110852 97024 17835 116004 48702 65612 68427 67541 7813 6415 78436 5289 102296 78964 83467 56256 36720 58141 39956 25783 3977 89405 104251 63057 69881 14546 115008 24936 10...

output:

1073639423
1073741728
1073655807
1073641471
1073741727

result:

wrong answer 1st lines differ - expected: '1073641471', found: '1073639423'

Test #16:

score: 0
Time Limit Exceeded

input:

16 4003
10000 1000000000 60
6301301488220013 13036910415217886 3587708952967093 9679001900905121 15370074903700843 10699351669343223 3693263165367499 11069887286230748 16712579672850495 7401912839403587 2739985286259605 4571772340856038 7048969239142713 1894459710006182 17112800883496323 12320029364...

output:


result:


Test #17:

score: 0
Time Limit Exceeded

input:

17 4003
10000 1000000000 60
6301301819534654 13036910822653667 3587709968963537 9679000983663529 15370074961371554 10699347928378657 3693263473368908 11069883997526525 16712580951412763 7401916234416503 2739986911540418 4571770484223398 7048969495053322 1894461412350279 17112799936179837 12320030978...

output:


result:


Test #18:

score: 0
Time Limit Exceeded

input:

18 4003
10000 1000000000 60
6301305150188370 13036910274580735 3587707298181505 9679004703165232 15370075141428877 10699349494581236 3693260766091278 11069885717856260 16712580184551432 7401916423007610 2739984412836669 4571773004118082 7048970479824295 1894458866748282 17112803260092779 12320029000...

output:


result:


Test #19:

score: 0
Time Limit Exceeded

input:

19 62501
100000 1000000000 120
1154838249421518343773531773357597778 1154838249421518343773531773357629327 1154838249421518343773531773357650307 1154838249421518343773531773357651709 1154838249421518343773531773357625821 1154838249421518343773531773357648391 1154838249421518343773531773357628650 115...

output:


result:


Test #20:

score: 0
Time Limit Exceeded

input:

20 62501
100000 1000000000 120
323632031276481459143358069001495634 323632031276481459143358069001527183 323632031276481459143358069001548163 323632031276481459143358069001549565 323632031276481459143358069001523677 323632031276481459143358069001546247 323632031276481459143358069001526506 3236320312...

output:


result:


Test #21:

score: 0
Time Limit Exceeded

input:

21 62501
100000 1000000000 120
94863733210630102424008628084424216 134857169996406520360367913319523910 161452665267901932764497978184577026 163229916633468866695669655916793917 130413130894076501442669761804067887 159024080878679252370015542440688101 133998758779238309221865949392709341 84253191505...

output:


result:


Test #22:

score: 0
Time Limit Exceeded

input:

22 120001
100000 1000000000 120
47471234275189825886337730002101330 47471234275189825886337730002132879 47471234275189825886337730002153859 47471234275189825886337730002155261 47471234275189825886337730002129373 47471234275189825886337730002151943 47471234275189825886337730002132202 4747123427518982...

output:


result:


Test #23:

score: 0
Time Limit Exceeded

input:

23 120001
100000 1000000000 120
10384594543745281933157089286344285 10384595087221325583432181750603761 10384596344313448100419024142756824 10384597896452402824418716086599406 10384599662161038685053136018241571 10384600136159512396111755729028709 10384601958291386084902861505361244 1038460344973912...

output:


result:


Test #24:

score: 0
Time Limit Exceeded

input:

24 120001
100000 1000000000 120
10384594585679242355840829886289379 10384595083750076569134324633632941 10384596423425537641166720528386677 10384598270636404608178558554968102 10384598723408000881156593449465505 10384600392016296777984789088889743 10384602014485619490959986853506329 1038460298717050...

output:


result:


Test #25:

score: 0
Time Limit Exceeded

input:

25 120001
100000 1000000000 120
10384594059032377855972206804990515 10384595922446112233978399250497752 10384596677646074836565734489994716 10384597585175584397302672981127521 10384599121981302637859909713499899 10384600188980133483790411300324684 10384601157708131158985167495674683 1038460244379676...

output:


result: