QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#563106#4316. pmrmscxipElegiaAC ✓3260ms1320584kbC++234.7kb2024-09-14 02:45:062024-09-14 02:45:06

Judging History

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

  • [2024-09-14 02:45:06]
  • 评测
  • 测评结果:AC
  • 用时:3260ms
  • 内存:1320584kb
  • [2024-09-14 02:45:06]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1000005;

struct Node
{
	int l,r,ch[2];
	int maxs,minl,maxr;
	int sz;
	Node (int L = 0,int R = -1)
	{
		ch[0] = ch[1] = 0;
		l = minl = L,r = maxr = R;
		maxs = r - l + 1;
		sz = 1;
	}
}node[MAXN * 40];

int n,m,id;
int a[MAXN];
int b[MAXN];
int label[MAXN];
int sz[MAXN];
int bel[MAXN];
int st[MAXN];
int rt[MAXN << 3];

set<int> S;

void update(int u)
{
	node[u].sz = node[node[u].ch[0]].sz + 1 + node[node[u].ch[1]].sz;
	node[u].maxs = max(max(node[node[u].ch[0]].maxs,node[node[u].ch[1]].maxs),node[u].r - node[u].l + 1);
	node[u].minl = min(node[u].l,node[node[u].ch[0]].minl);
	node[u].maxr = max(node[u].r,node[node[u].ch[1]].maxr);
}

void link(int u,int v,bool dir)
{
	node[u].ch[dir] = v;
	update(u);
}

pair<int,int> split(int u,int x)
{
	if (!u)
		return make_pair(0,0);
	if (node[u].l <= x)
	{
		pair<int,int> res = split(node[u].ch[1],x);
		link(u,res.first,1);
		return make_pair(u,res.second);
	}
	pair<int,int> res = split(node[u].ch[0],x);
	link(u,res.second,0);
	return make_pair(res.first,u);
}

int merge(int u,int v)
{
	if (!u || !v)
		return u | v;
	if (rand() % (node[u].sz + node[v].sz) + 1 <= node[u].sz)
	{
		link(u,merge(node[u].ch[1],v),1);
		return u;
	}
	link(v,merge(u,node[v].ch[0]),0);
	return v;
}

void modify(int o,int l,int r,int x,int y,int L,int R,int v)
{
	if (l >= x && r <= y)
	{
		pair<int,int> res = split(rt[o],L);
		if (v == 1)
		{
			node[++id] = Node(L,R);
			rt[o] = merge(merge(res.first,id),res.second);
			return;
		}
		pair<int,int> res2 = split(res.first,L - 1);
		rt[o] = merge(res2.first,res.second);
		return;
	}
	int mid = l + r >> 1;
	if (mid >= x)
		modify(o << 1,l,mid,x,y,L,R,v);
	if (mid + 1 <= y)
		modify(o << 1 | 1,mid + 1,r,x,y,L,R,v);
}

int queryt(int u,int l,int r)
{
	if (!u)
		return 0;
	if (node[u].maxr < l || node[u].minl > r)
		return 0;
	if (node[u].maxr <= r && node[u].minl >= l)
		return node[u].maxs;
	int res = (node[u].l >= l && node[u].r <= r ? node[u].r - node[u].l + 1 : 0);
	return max(res,max(queryt(node[u].ch[0],l,r),queryt(node[u].ch[1],l,r)));
}

int query(int o,int l,int r,int p,int x,int y)
{
	int res = queryt(rt[o],x,y);
	if (l == r)
		return res;
	int mid = l + r >> 1;
	if (mid >= p)
		return max(res,query(o << 1,l,mid,p,x,y));
	return max(res,query(o << 1 | 1,mid + 1,r,p,x,y));
}

void add(int l,int r,int v)
{
	if (l > r)
		return;
	int vl = label[a[l]],vr = label[a[l]] + r - l,c = bel[a[l]];
	if (r - l + 1 > sz[c])
		vl = st[c],vr = st[c] + sz[c] - 1;
	modify(1,1,2 * n,vl,vr,l,r,v);
}

int main()
{
	node[0].minl = 1e9;
	node[0].sz = 0;
	scanf("%d%d",&n,&m);
	for (int i = 1;i <= n;i++)
		scanf("%d",&a[i]);
	for (int i = 1;i <= n;i++)
		scanf("%d",&b[i]);
	int cnt = 0;
	for (int i = 1;i <= n;i++)
		if (!label[i])
		{
			int cur = i;
			id++;
			st[id] = cnt + 1;
			while (!label[cur])
			{
				label[cur] = ++cnt;
				sz[id]++;
				bel[cur] = id;
				cur = b[cur];
			}
			cnt += sz[id];
		}
	id = 0;
	S.insert(0);
	S.insert(n);
	for (int i = 1;i < n;i++)
		if (b[a[i]] != a[i + 1])
			S.insert(i);
	for (set<int>::iterator it = S.begin();it != S.end();it++)
	{
		set<int>::iterator nxt = it;
		nxt++;
		if (nxt == S.end())
			break;
		add((*it) + 1,(*nxt),1);
	}
	while (m--)
	{
		int tp,l,r,x;
		scanf("%d%d%d",&tp,&l,&r);
		if (tp == 1)
		{
			set<int>::iterator R = S.upper_bound(l);
			if (R == S.end())
				R--;
			set<int>::iterator L = R;
			while ((*L) && (*L) >= l - 1)
				L--;
			int tmpl = *L,tmpr = *R;
			int g = tmpl + 1;
			if (b[a[l - 1]] != a[l])
			{
				add(g,l - 1,-1),g = l;
				S.erase(l - 1);
			}
			if (b[a[l]] != a[l + 1])
			{
				add(g,l,-1),g = l + 1;
				S.erase(l);
			}
			add(g,tmpr,-1);
			a[l] = r;
			g = tmpl + 1;
			if (b[a[l - 1]] != a[l])
			{
				add(g,l - 1,1),g = l;
				S.insert(l - 1);
			}
			if (b[a[l]] != a[l + 1])
			{
				add(g,l,1),g = l + 1;
				S.insert(l);
			}
			add(g,tmpr,1);
		}
		else
		{
			scanf("%d",&x);
			int ans = max(query(1,1,2 * n,label[x],l,r),query(1,1,2 * n,label[x] + sz[bel[x]],l,r));
			if (bel[a[l]] == bel[x])
			{
				int R = min(r,*S.lower_bound(l));
				if ((label[a[l]] <= label[x] && label[a[l]] + R - l >= label[x]) || (label[a[l]] <= label[x] + sz[bel[x]] && label[a[l]] + R - l >= label[x] + sz[bel[x]]))
					ans = max(ans,R - l + 1);
			}
			if (bel[a[r]] == bel[x])
			{
				int L = max(l,*(--S.lower_bound(r)) + 1);
				if ((label[a[L]] <= label[x] && label[a[L]] + r - L >= label[x]) || (label[a[L]] <= label[x] + sz[bel[x]] && label[a[L]] + r - L >= label[x] + sz[bel[x]]))
					ans = max(ans,r - L + 1);
			}
			printf("%d\n",ans);
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2793ms
memory: 1301492kb

input:

1000000 1000000
380 679 777 226 644 42 593 557 637 18 249 696 391 107 169 834 866 651 679 856 57 398 112 555 787 176 546 127 487 720 726 376 603 404 545 356 418 756 785 355 684 904 212 809 855 783 736 17 386 389 487 773 159 579 607 6 480 378 78 103 360 351 700 427 377 99 741 572 189 202 222 570 611 ...

output:

5543
1
8009
2140
3788
1
1
0
3799
3948
0
0
4527
2706
2060
1
1
4895
3569
0
1
1
1906
0
1
1
7513
0
0
824
2620
0
2699
1
1595
1
2826
2329
0
0
496
196
1
1
3761
5543
7513
1119
4498
2826
2180
265
1
1228
2822
8994
7513
3761
3761
427
4428
8994
0
0
1
4318
0
0
0
0
3569
0
5032
3193
0
1
2818
0
8994
1
3447
0
1
3574...

result:

ok 600294 lines

Test #2:

score: 0
Accepted
time: 3007ms
memory: 1305176kb

input:

1000000 1000000
366 426 472 183 425 269 129 22 369 63 397 24 338 257 517 346 345 321 383 388 440 415 153 160 284 154 515 299 97 17 562 515 215 499 442 481 456 451 498 99 381 114 312 265 458 242 55 48 276 458 142 201 58 413 424 25 144 416 545 170 363 280 359 202 533 268 483 121 155 150 3 307 454 204 ...

output:

1
0
3809
1
0
3755
2517
0
0
1794
0
4007
0
0
1
2517
3025
3809
1
1
0
122
204
1852
1
1
3809
3809
0
0
0
1
1472
1558
1097
773
3809
491
3755
228
750
229
0
1
1
1
1396
1
4007
3809
3809
627
1
3809
1
857
0
0
857
3809
1073
2665
3809
0
3809
3243
2340
1
0
3809
3025
0
108
1536
2186
3809
3025
1
1
0
2432
1795
1
2725...

result:

ok 340568 lines

Test #3:

score: 0
Accepted
time: 1880ms
memory: 1292200kb

input:

1000000 1000000
541159 630205 363352 227192 99958 296471 975613 252233 80475 167864 836828 184560 804755 920944 576350 202914 992777 227911 768340 897922 808870 227585 852752 389725 725117 606119 512884 901002 908921 621574 51105 341960 133213 287651 147511 904531 156337 772636 265996 148408 590582 ...

output:

94831
364157
1
21511
30275
312226
80166
70143
288395
2626
1
0
599886
98565
224972
10519
386182
6597
194321
277685
199886
417920
0
417920
124034
202410
417920
386374
0
417920
0
122597
396259
0
162795
0
417920
417920
1
170320
322059
37486
101891
99293
127561
325074
0
357374
0
417920
18114
37486
64241
...

result:

ok 400161 lines

Test #4:

score: 0
Accepted
time: 1515ms
memory: 1291392kb

input:

1000000 1000000
478682 402589 343446 886390 519427 530835 920809 530746 264424 76914 220245 285386 423650 908771 30964 47856 299202 621549 380015 286899 519653 350008 13272 741730 464354 510706 527460 165532 332635 384850 269550 729279 812716 521816 984488 741626 495345 909549 828641 158485 163138 5...

output:

158037
1
1
38955
0
0
265180
35487
199902
0
0
0
240826
319830
1
0
3383
199012
31817
0
2339
632803
463676
43536
592975
654058
376089
124854
179136
97267
37883
37660
117981
280364
32583
337865
405391
0
189814
0
670814
631725
102503
523348
269032
0
0
512018
284712
0
0
95509
0
477618
504901
201653
239652...

result:

ok 930311 lines

Test #5:

score: 0
Accepted
time: 2232ms
memory: 1303184kb

input:

999999 999997
305 420 87 845 139 246 742 802 492 255 570 292 405 90 341 148 324 875 840 393 476 604 109 484 598 517 273 22 604 459 524 489 611 497 8 731 43 124 841 807 334 846 116 410 429 67 230 808 540 175 296 435 75 848 827 469 896 663 592 636 207 542 617 567 589 562 362 108 689 727 304 675 429 58...

output:

6847
3498
0
1250
0
0
1
96
5287
758
1
0
1
5420
1995
3022
658
0
3159
0
0
6772
1682
470
4667
5411
1
2713
1
4303
152
2107
0
6772
6847
0
1
6452
3231
2522
6772
0
7045
333
1
6452
1745
6772
1
6772
1
0
1
1995
3498
1358
1
1988
0
4303
1
1
4303
0
231
2863
1
5287
0
1
1
2366
1
6452
4115
1
0
4824
2432
3310
1
4303
...

result:

ok 660468 lines

Test #6:

score: 0
Accepted
time: 3260ms
memory: 1320584kb

input:

1000000 1000000
966844 792708 79445 826065 58907 359120 455041 821080 480002 482408 649383 987817 73459 866255 621855 769479 739703 370633 378741 305445 516311 759166 595734 851705 334917 203032 382423 67773 129354 768453 979877 167641 162497 421117 260710 501475 957715 991724 117045 477798 452959 8...

output:

0
1
1
1
431
1
1
0
1
1
1
39076
32171
1
0
1
0
1
1
1
1
1
0
0
1
0
1
0
1
0
21571
1
1
1
1
1
1
1
1
1
1
1
0
0
1
1
1
1
1
0
1
0
18306
1
1
0
15066
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
9072
1
1
1
1
1
1
0
1
0
0
1
0
1
1
1
1
1
1
1
1
0
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
0
1
1
1
0
4862
1
1
1
1
1
1
1
1
1
...

result:

ok 80404 lines

Test #7:

score: 0
Accepted
time: 3038ms
memory: 1312588kb

input:

1000000 1000000
988810 735815 608532 420222 386157 890306 768406 657225 646210 315600 139962 932151 768827 984450 770731 453487 685598 543002 581870 552313 503972 177764 493840 25621 452770 179448 941470 104226 30789 573414 597085 789824 463108 197854 530800 848974 111409 970712 903459 212253 260725...

output:

1
1
1
1
1
54929
1
1
1
1
235784
32344
0
235784
1
1
1
1
1
141539
1
0
1
1
1
1
55919
1
1
1
179864
0
1
1
1
1
1
1
0
1
1
1
1
1
0
89912
1
1
55919
1
55919
1
1
0
1
179864
40867
1
1
1
1
1
1
1
1
1
0
1
1
0
1
1
115075
1
1
0
0
1
1
1
1
0
1
179864
115783
85395
0
1
1
1
0
1
0
0
1
1
0
1
1
1
0
1
1
0
1
1
1
1
1
1
179864
1...

result:

ok 569671 lines

Test #8:

score: 0
Accepted
time: 2728ms
memory: 1317056kb

input:

1000000 999997
754737 382545 22625 954994 176479 904745 522195 954780 129554 460864 188166 177823 578535 615385 54905 733463 394448 693133 747012 65334 43823 106344 194190 129336 123404 33389 166630 520947 358644 905734 338275 13073 934015 6553 755842 760436 319861 87505 675549 627517 750211 667991 ...

output:

1
1
1
0
1
0
1
1
1
0
1
1
1
1
1
102801
143146
133046
1
0
1
0
1
143146
1
1
89418
1
1
1
1
1
1
1
1
1
0
1
0
1
0
1
1
1
85963
1
1
143146
143146
0
1
130758
1
1
0
0
0
110274
1
1
1
1
0
15621
1
116756
1
143146
0
0
0
0
1
1
1
1
143146
0
1
1
126754
1
1
0
143146
1
1
1
0
1
1
1
1
0
1
1
1
0
1
143146
1
1
83346
1
1
0
1
...

result:

ok 800084 lines

Test #9:

score: 0
Accepted
time: 3232ms
memory: 1305484kb

input:

999999 999998
80 75 103 38 6 21 16 18 38 70 71 110 19 46 24 37 81 48 78 28 103 72 79 41 76 109 36 83 79 69 80 60 97 71 42 80 81 23 33 79 109 88 51 83 40 14 65 57 54 89 2 90 35 42 80 81 23 33 79 109 88 51 83 40 14 65 57 54 89 2 90 35 42 80 81 23 33 79 109 88 51 83 40 14 65 57 54 89 2 90 35 42 80 81 2...

output:

0
543
1
971
0
570
971
0
0
909
298
0
675
332
848
58
139
0
911
1
451
455
0
535
911
669
0
144
1
137
971
0
1139
1
1
224
257
269
1022
909
0
419
1
118
1
0
363
1
908
1
1
1
1
753
909
971
1
0
219
0
0
1
675
348
0
1
971
1143
0
909
30
811
269
1
1
0
0
282
114
1
0
1
911
0
152
1
1139
1
373
543
1
608
909
1
290
0
97...

result:

ok 220349 lines

Test #10:

score: 0
Accepted
time: 2149ms
memory: 1303860kb

input:

1000000 1000000
9 10 16 28 11 33 10 22 13 15 20 21 7 33 13 31 25 20 14 22 22 19 1 2 23 35 18 22 19 16 23 23 24 248837 1 12 11 14 8 695844 12 34 32 26 32 35 10 28 12 24 18 1 17 29 1 28 5 32 6 5 32 21 33 5 17 3 7 24 12 21 25 15 11 9 9 35 27 19 7 33 26 14 37 36 15 34 13 7 2 32 23 4 16 17 135768 21 7 21...

output:

0
0
93
401
258
129
19
93
504
10
1
504
0
0
375
504
425
76
237
0
94
31
201
1
1
375
45
1
0
378
134
40
504
1
338
64
97
132
47
0
1
1
102
0
61
504
14
0
1
102
176
0
0
71
1
1
0
1
317
74
3
341
71
0
0
0
0
1
425
94
71
0
1
0
36
254
1
60
1
0
1
27
504
504
12
1
265
1
94
25
1
0
504
425
68
0
1
385
24
1
37
1
15
13
50...

result:

ok 979973 lines