UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#202062#3520. Fire and Icesnow_trace10011200ms124800kbC++114.4kb2024-02-11 10:51:362024-02-11 13:10:05

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
int a[200005],b[200005];
int posa[400005];
int la[200005],ra[200005],lb[200005],rb[200005];
vector<int> rra[200005],rrb[200005];
int q,op;int n,m;
int s[500005],x[500005];
int nown,nowm;
vector<int>p,pp;int tot;
struct node{
	int l,r;
	int kn,bn;//a
	int km,bm;//b
	int v_mn,v_n,v_m,v_1;			
}tree[2000005];
void push_up(int k){
	int ls = k<<1,rs = k<<1|1;
	tree[k].kn = (tree[ls].kn+tree[rs].kn)%mod;
	tree[k].km = (tree[ls].km+tree[rs].km)%mod;
	tree[k].bn = (tree[ls].bn+tree[rs].bn)%mod;
	tree[k].bm = (tree[ls].bm+tree[rs].bm)%mod;
	tree[k].v_mn = (tree[ls].v_mn+tree[rs].v_mn+tree[ls].km*tree[rs].kn)%mod;
	tree[k].v_n = (tree[ls].v_n+tree[rs].v_n+tree[ls].bm*tree[rs].kn)%mod;
	tree[k].v_m = (tree[ls].v_m+tree[rs].v_m+tree[ls].km*tree[rs].bn)%mod;
	tree[k].v_1 = (tree[ls].v_1+tree[rs].v_1+tree[ls].bm*tree[rs].bn)%mod;
}void build(int l,int r,int k){
	tree[k].l = l,tree[k].r = r;
	if(l+1 == r)return;
	build(l,l+r>>1,k<<1),build(l+r>>1,r,k<<1|1);
}void update(int k,int pos,int op,int k1,int b1){
	int l = tree[k].l,r = tree[k].r;
	if(l+1 == r){
		if(op == 0)tree[k].kn = k1,tree[k].bn = b1;
		else tree[k].km = k1,tree[k].bm = b1;return;
	}int mid = l+r>>1;
	if(pos<mid)update(k<<1,pos,op,k1,b1);
	else update(k<<1|1,pos,op,k1,b1);
	push_up(k);return;
}int query(){
//	cout<<tree[1].kn<<' '<<tree[1].km<<' '<<tree[1].bm<<' '<< tree[1].bn<<endl;
//	cout<<tree[1].v_1<< ' '<<tree[1].v_n <<' '<<tree[1].v_m<<' '<<tree[1].v_mn<<endl;
	int res = (nown*nowm%mod*tree[1].v_mn+nown*tree[1].v_n+nowm*tree[1].v_m+tree[1].v_1)%mod;return (res+mod)%mod;}
signed main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >>q >>op;
	cin >>a[++n]>>b[++m];b[1] = -b[1];
	p.push_back(b[1]),p.push_back(a[1]),pp.push_back(b[1]),pp.push_back(a[1]);
	for(int i = 1;i<=q;i++){
		cin>>s[i]>>x[i];
		if(s[i] == 1)x[i] = -x[i];
		p.push_back(x[i]),pp.push_back(x[i]);
	}//cout<<111<<endl;
	sort(p.begin(),p.end());sort(pp.begin(),pp.end());p.erase(unique(p.begin(),p.end()),p.end());
	for(int i= 0;i<p.size();i++){
	//	cout<<i<<endl;
		int pos = upper_bound(pp.begin(),pp.end(),p[i])-pp.begin();posa[i] = pos;
	}for(int i = 1;i<=q;i++){
		if(s[i] == 0)a[++n] = x[i];
		else b[++m] = x[i];
	}//cout<<111<<endl;
	for(int i = 1;i<=n;i++){
		int ppp = lower_bound(p.begin(),p.end(),a[i])-p.begin();
	//	cout<<ppp<<endl;
		a[i] = posa[ppp]--;
	}for(int i = 1;i<=m;i++){
		int ppp = lower_bound(p.begin(),p.end(),b[i])-p.begin();
	//	cout<<ppp<< endl;
		b[i] = posa[ppp]--;
	}vector<int>p;
	for(int i = 1;i<=n;i++)ra[i] = n+1;
	for(int i = 1;i<=m;i++)rb[i] = m+1;
	for(int i = 1;i<=n;i++){
		while(!p.empty() and a[i]<a[p.back()])p.pop_back();
		if(!p.empty())la[i] = p.back();p.push_back(i);
	}p.clear();
	for(int i = n;i>=1;i--){
		while(!p.empty() and a[i]<a[p.back()])p.pop_back();
		if(!p.empty())ra[i] = p.back(),rra[p.back()].push_back(i);p.push_back(i);
	}p.clear();
	for(int i = 1;i<=m;i++){
		while(!p.empty() and b[i]>b[p.back()])p.pop_back();
		if(!p.empty())lb[i] = p.back();p.push_back(i);
	}p.clear();
	for(int i = m;i>=1;i--){
		while(!p.empty() and b[i]>b[p.back()])p.pop_back();
		if(!p.empty())rb[i] = p.back(),rrb[p.back()].push_back(i);;p.push_back(i);
	}p.clear();
//	for(int i = 1;i<=n;i++)cout<<a[i]<<' ';cout<<endl;
//	for(int i = 1;i<=m;i++)cout<<b[i]<<' ';cout<<endl;
//	for(int i = 1;i<=m;i++)cout<<lb[i]<<' ';cout<<endl;
//	for(int i = 1;i<=m;i++)cout<<rb[i]<< ' ';cout<<endl;
	tot = n+m;build(1,m+n+1,1);
	update(1,a[1],0,1,0);update(1,b[1],1,1,0);nown = 1,nowm = 1;
	//cout<<query()<<endl;
	for(int i = 1;i<=q;i++){
		if(s[i] == 0){
			nown++;update(1,a[nown],0,nown-la[nown],-(nown-1)*(nown-la[nown]));
			for(int x:rra[nown]){
				update(1,a[x],0,0,(x-la[x])*(ra[x]-x));
			}
		}else{
			nowm++;update(1,b[nowm],1,nowm-lb[nowm],-(nowm-1)*(nowm-lb[nowm]));
			for(int x:rrb[nowm]){
		//		cout << ' '<<x <<' '<<(x-lb[x])*(rb[x]-x)<<endl;
				update(1,b[x],1,0,(x-lb[x])*(rb[x]-x));
			}
		}if(op == 1)cout<<query()<< endl;
	}if(op == 0)cout<<query()<<endl;
	return 0;
}
/*
我们考虑把全黑矩阵转化为 min_{l1,r1}(ai)>=max_{l2,r2}(-bi) 的区间个数
考虑单调栈,维护 ai 在哪里作为最小值,模拟单调栈的过程
发现对于一个序列的答案容易表示成 kn+b
上线段树维护一下
2 1  
3 3
1 1
1 2

3 1 2
-3
*/

Details

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 0ms
memory: 10704kb

input:

98 1
-8893 5507
1 7341
1 -8846
1 -2309
1 -3300
1 2086
0 1931
1 -5825
1 -5710
0 -788
1 1672
1 -7089
0...

output:

0
0
0
0
0
4
4
4
12
15
15
18
18
21
102
115
130
145
181
188
204
220
253
270
283
300
326
344
363
392
41...

result:

ok 98 numbers

Test #2:

score: 0
Accepted
time: 3ms
memory: 10700kb

input:

88 1
-7861 -3277
1 3558
0 -3041
1 -2192
0 -8296
0 -8549
1 4344
1 3331
0 2730
0 8173
1 2115
1 8320
0 ...

output:

0
1
1
1
1
2
4
14
39
55
80
83
88
110
168
200
229
229
233
267
288
375
391
473
523
551
645
736
772
886
...

result:

ok 88 numbers

Test #3:

score: 0
Accepted
time: 0ms
memory: 10704kb

input:

98 1
-29 -9
0 10
1 27
0 16
0 5
1 -21
1 21
0 -13
0 13
1 -25
1 0
0 -3
0 11
1 7
1 -4
0 -20
1 1
1 12
1 1...

output:

1
3
9
12
12
18
26
38
38
45
57
74
94
118
134
166
210
266
334
365
492
492
531
586
631
693
738
827
891
...

result:

ok 98 numbers

Subtask #2:

score: 10
Accepted

Test #4:

score: 10
Accepted
time: 10ms
memory: 10920kb

input:

998 1
215 -163
1 127
0 80
1 281
0 253
0 -28
0 -167
0 244
1 -272
1 71
0 269
0 115
0 195
1 -85
1 -34
0...

output:

3
5
12
24
36
41
52
52
63
82
99
123
147
187
237
237
266
287
374
412
465
539
553
572
766
876
1078
1232...

result:

ok 998 numbers

Test #5:

score: 0
Accepted
time: 6ms
memory: 10928kb

input:

998 1
1909 2306
1 2628
1 1053
1 -2489
0 -1936
1 1451
1 764
1 1162
1 288
1 1962
0 -1068
0 1941
1 -280...

output:

3
6
6
12
13
15
18
22
29
43
78
90
104
120
120
120
130
150
162
184
224
266
278
538
573
611
681
811
855...

result:

ok 998 numbers

Test #6:

score: 0
Accepted
time: 8ms
memory: 10924kb

input:

957 1
-29732 4526
1 -4208
0 14298
0 -27626
0 24336
1 -10280
1 13636
1 -12744
1 4100
0 -29322
1 -1952...

output:

0
3
3
6
12
20
30
42
42
49
77
77
80
98
124
128
154
184
254
295
340
389
434
678
759
1171
1179
1237
143...

result:

ok 957 numbers

Subtask #3:

score: 10
Accepted

Test #7:

score: 10
Accepted
time: 23ms
memory: 14112kb

input:

9998 1
2547 16452
0 4177
1 -387
0 -3242
0 4682
0 22364
1 -28322
1 13928
0 17857
0 -16924
0 25441
0 2...

output:

3
9
12
18
27
27
42
60
60
64
72
72
84
87
95
95
95
95
175
193
224
224
227
247
335
346
589
628
647
685
...

result:

ok 9998 numbers

Test #8:

score: 0
Accepted
time: 14ms
memory: 14080kb

input:

9998 1
229 1912
0 2514
0 2276
1 2831
1 2651
0 1553
1 676
1 1634
0 2451
1 103
1 2988
0 1842
1 1348
1 ...

output:

3
6
18
36
60
100
150
225
315
420
588
756
945
1155
1386
1848
2376
2970
3510
4095
5005
6006
7098
8190
...

result:

ok 9998 numbers

Test #9:

score: 0
Accepted
time: 30ms
memory: 14112kb

input:

9993 1
-25069 16168
0 22566
0 -1054
1 21976
1 28294
1 -28558
1 20894
1 2074
0 -20167
0 -26068
0 4443...

output:

1
3
9
21
21
24
30
43
48
62
65
70
89
94
134
156
184
229
278
300
377
460
594
634
634
779
907
907
1027
...

result:

ok 9993 numbers

Subtask #4:

score: 20
Accepted

Test #10:

score: 20
Accepted
time: 765ms
memory: 122156kb

input:

399998 0
25881 -13672
1 -14025
0 -16182
1 -23140
1 21862
0 8936
1 18168
1 -20997
1 5796
0 1291
0 -16...

output:

615140815

result:

ok 1 number(s): "615140815"

Test #11:

score: 0
Accepted
time: 751ms
memory: 124004kb

input:

399998 0
86386 -257505
0 270235
0 -253563
1 39880
0 157332
0 -10713
0 -279088
0 78889
1 239382
1 -11...

output:

483874022

result:

ok 1 number(s): "483874022"

Test #12:

score: 0
Accepted
time: 805ms
memory: 124796kb

input:

399993 0
-946592 -2209872
0 -2448922
0 -2074552
1 -647455
0 2491470
0 -65776
1 -1236977
0 2683006
1 ...

output:

475504591

result:

ok 1 number(s): "475504591"

Subtask #5:

score: 20
Accepted

Test #13:

score: 20
Accepted
time: 1282ms
memory: 124796kb

input:

399998 1
170847520 -733230204
0 437049364
1 307907810
1 192475487
0 208194163
0 439190852
1 -3961554...

output:

0
3
9
18
30
36
36
39
56
64
64
64
92
112
121
141
143
162
186
186
264
312
364
442
500
575
659
691
696
...

result:

ok 399998 numbers

Test #14:

score: 0
Accepted
time: 1200ms
memory: 124796kb

input:

399993 1
644188428 971215643
0 972912015
0 -245037205
1 721267121
0 162702949
0 846528337
1 22818373...

output:

3
6
18
30
45
63
71
89
110
134
144
171
201
212
238
309
338
363
399
422
449
488
506
557
644
675
877
90...

result:

ok 399993 numbers

Subtask #6:

score: 30
Accepted

Test #15:

score: 30
Accepted
time: 1304ms
memory: 123744kb

input:

399993 1
-196331 -13389
1 184084
1 141712
0 127607
1 -125103
1 -83579
1 178000
1 118306
1 138807
1 -...

output:

0
0
6
10
15
21
28
36
45
55
73
101
118
141
154
173
276
306
542
621
887
904
922
1036
1222
1276
1543
15...

result:

ok 399993 numbers

Test #16:

score: 0
Accepted
time: 1187ms
memory: 124800kb

input:

399998 1
-61894 -495271
1 -693277
0 -238496
1 -728272
1 -514560
1 660191
0 -1144705
1 278476
0 -5875...

output:

0
0
0
0
3
3
9
10
26
33
33
33
39
81
98
108
129
151
173
201
217
246
366
577
591
893
908
939
948
1039
1...

result:

ok 399998 numbers

Test #17:

score: 0
Accepted
time: 1308ms
memory: 124800kb

input:

399998 1
69460 1564551
1 1065080
1 1263405
0 1548702
1 1237874
1 1752136
0 61349
0 1027888
0 299794
...

output:

3
6
18
30
45
90
150
225
315
420
588
756
1008
1296
1620
2025
2475
3025
3630
4290
5005
5775
6600
7480
...

result:

ok 399998 numbers

Test #18:

score: 0
Accepted
time: 1211ms
memory: 124800kb

input:

399998 1
-674207 -1021614
0 -790812
0 -747261
1 -1309571
1 -1622552
1 -707684
1 -1044094
0 -680452
0...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
3
3
3
3
3
6
6
6
6
6
6
6
6
...

result:

ok 399998 numbers

Test #19:

score: 0
Accepted
time: 1293ms
memory: 123740kb

input:

399993 1
-125772 56415
0 -107863
1 22954
1 -77695
0 118391
0 34524
1 166725
1 41418
1 1449
0 56146
1...

output:

0
0
0
6
12
25
34
46
75
86
112
124
147
154
185
221
232
298
313
383
416
474
649
902
1058
1073
1278
160...

result:

ok 399993 numbers

Extra Test:

score: 0
Extra Test Passed