QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#713824#892. Minimal CutliulianllRE 4ms19072kbC++145.6kb2024-11-05 20:38:112024-11-05 20:38:20

Judging History

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

  • [2024-11-05 20:38:20]
  • 评测
  • 测评结果:RE
  • 用时:4ms
  • 内存:19072kb
  • [2024-11-05 20:38:11]
  • 提交

answer

#include<bits/stdc++.h>
#pragma GCC optimize(2)
#define int long long
#define inf 1000000000000000
using namespace std;
int n,m,u,v,w,tot,x,y,cnt,xx;
long long c;
const int mod=998244353;
int rt[30010],vv[30010],id[30010],id2[30010],fa[30010],siz[30010];
long long ans;
struct Line{
	int u,v,w;
}l[60010]; 
struct Line2{
	int u,v;
	long long w;
}line[60010]; 
struct Val{
	int mn,hmn,nowid,hid;
};
struct Tag{
	int ad,had,id2;
};
struct Segtree{
	int lson,rson;
	Val val;
	Tag tag;
}tree[8000010];
vector<Line> a[30010],b[30010];
inline long long Qread(){
	char st;long long ans=0;st=getchar();
	while(!(st<='9'&&st>='0')) st=getchar();
	while(st<='9'&&st>='0') ans=(ans<<3)+(ans<<1)+(st^48),st=getchar();
	return ans;
}
int fnd(int x){
	if(fa[x]!=x) fa[x]=fnd(fa[x]);
	return fa[x];
}
Val DD(Val x,Val y){
	if(x.mn==-1) return y;
	if(y.mn==-1) return x;
	Val ans;
	if(x.hmn<y.hmn) ans.hmn=x.hmn,ans.hid=x.hid;
	else ans.hmn=y.hmn,ans.hid=y.hid;
	if(x.mn<y.mn) ans.mn=x.mn,ans.nowid=x.nowid;
	else ans.mn=y.mn,ans.nowid=y.nowid;
	return ans;
}
Val DM(Val x,Tag y){
	if(x.hmn<=y.had+x.mn||y.id2==0) return (Val){x.mn+y.ad,x.hmn,x.nowid,x.hid};
	return (Val){x.mn+y.ad,x.mn+y.had,x.nowid,x.nowid};
}
Tag MM(Tag x,Tag y){
	if(x.had<=y.had+x.ad) return (Tag){x.ad+y.ad,x.had,x.id2};
	return (Tag){x.ad+y.ad,y.had+x.ad,y.id2};
}
int new_node(int p){
	++tot;
	tree[tot]=tree[p];
	return tot;
}
void Pushdown(const int p){
	tree[p].lson=new_node(tree[p].lson);
	tree[p].rson=new_node(tree[p].rson);
	tree[tree[p].lson].val=DM(tree[tree[p].lson].val,tree[p].tag);
	tree[tree[p].lson].tag=MM(tree[tree[p].lson].tag,tree[p].tag);
	tree[tree[p].rson].val=DM(tree[tree[p].rson].val,tree[p].tag);
	tree[tree[p].rson].tag=MM(tree[tree[p].rson].tag,tree[p].tag);
	tree[p].tag=(Tag){0,0,0};
	return;
} 
void Build(int &p,const int l,const int r){
	p=++tot;
	if(l==r){
		tree[p].val=(Val){vv[l],vv[l],l,l};return;
	}
	const int mid=(l+r)>>1;
	Build(tree[p].lson,l,mid);
	Build(tree[p].rson,mid+1,r);
	tree[p].val=DD(tree[tree[p].lson].val,tree[tree[p].rson].val);
	return;
}
void Modify(int &p,const int L,const int R,const int l,const int r,const Tag tag){
	p=new_node(p);
	if(L<=l&&r<=R){
		tree[p].val=DM(tree[p].val,tag);
		tree[p].tag=MM(tree[p].tag,tag);
		return;
	}
	if(tree[p].tag.id2||tree[p].tag.ad||tree[p].tag.had) Pushdown(p);
	const int mid=(l+r)>>1;
	if(L<=mid) Modify(tree[p].lson,L,R,l,mid,tag);
	if(R>mid) Modify(tree[p].rson,L,R,mid+1,r,tag);
	tree[p].val=DD(tree[tree[p].lson].val,tree[tree[p].rson].val);
	return;
}
void Del(int &p,const int L,const int R,const int l,const int r){
	p=new_node(p);
	if(L<=l&&r<=R){
		tree[p].val=(Val){inf,inf,0,0};
		return;
	}
	if(tree[p].tag.id2||tree[p].tag.ad||tree[p].tag.had) Pushdown(p);
	const int mid=(l+r)>>1;
	if(L<=mid) Del(tree[p].lson,L,R,l,mid);
	if(R>mid) Del(tree[p].rson,L,R,mid+1,r);
	tree[p].val=DD(tree[tree[p].lson].val,tree[tree[p].rson].val);
	return;
}
Val Query(int p,const int L,const int R,const int l,const int r,Tag tag){
	if(L<=l&&r<=R){
		return DM(tree[p].val,tag);
	}
	if(tree[p].tag.id2||tree[p].tag.ad||tree[p].tag.had) tag=MM(tree[p].tag,tag);
	const int mid=(l+r)>>1;Val ans=(Val){-1,-1,-1,-1};
	if(L<=mid) ans=DD(ans,Query(tree[p].lson,L,R,l,mid,tag));
	if(R>mid) ans=DD(ans,Query(tree[p].rson,L,R,mid+1,r,tag));
	return ans;
}
void Addline(int u,int v,long long w){
	line[++cnt].u=u,line[cnt].v=v,line[cnt].w=w;
	return;
}
int xxx;
void solve(int l,int r){
	if(l==r) return;
	int x=id[l],y=id[r];
	if(x>y) swap(x,y);
	Val w=(Val){-1,-1,-1,-1};
	if(x>1) w=DD(w,Query(rt[x-1],x,y-1,1,n,(Tag){0,0,0}));
	w=DD(w,Query(rt[y+n],x,y-1,1,n,(Tag){0,0,0}));
	Addline(l,r,2*c+(long long)w.hmn);
	solve(l,w.hid);
	solve(w.hid+1,r); 
	return;
}
bool cmp(Line2 x,Line2 y){
	return x.w>y.w;
}
bool cmpw(Line x,Line y){
	return x.w>y.w;
}
signed main()
{
	n=Qread(),m=Qread();c=1e9;
	for(int i=1;i<=m;i++){
		u=Qread(),v=Qread(),w=Qread();
		if(u>v) swap(u,v);
		a[u].push_back((Line){u,v-1,-w});
		a[u].push_back((Line){v,n,w});
		a[v].push_back((Line){v,n,-w});
		
		b[v-1].push_back((Line){u,v-1,-w});
		if(u-1) b[v-1].push_back((Line){1,u-1,w});
		if(u-1) b[u-1].push_back((Line){1,u-1,-w});
		l[i].u=u,l[i].v=v,l[i].w=w;
	}
	for(int i=1;i<=n;i++){
		sort(a[i].begin(),a[i].end(),cmpw);
		sort(b[i].begin(),b[i].end(),cmpw);
	}
	for(int i=1;i<=m;i++){
		vv[l[i].u]+=l[i].w;
		vv[l[i].v]-=l[i].w;
	}
	for(int i=0;i<a[1].size();++i){
		vv[a[1][i].u]+=a[1][i].w;
		vv[a[1][i].v+1]-=a[1][i].w;
	}
	for(int i=1;i<=n;i++){
		vv[i]+=vv[i-1];
	}
	vv[1]=inf;
	xx=1,Build(rt[1],1,n);
	for(int i=2;i<=n;i++){
		Del(rt[i-1],i-1,i-1,1,n);
		rt[i]=rt[i-1];
		for(int j=0;j<a[i].size();++j){
			Modify(rt[i],a[i][j].u,a[i][j].v,1,n,(Tag){a[i][j].w,a[i][j].w,i});
		}
	}
	for(int i=1;i<=n;i++) vv[i]=0;
	for(int i=1;i<=m;i++){
		vv[l[i].u]+=l[i].w;
		vv[l[i].v]-=l[i].w;
	}
	for(int i=0;i<b[n].size();++i){
		vv[b[n][i].u]+=b[n][i].w;
		vv[b[n][i].v+1]-=b[n][i].w;
	}
	for(int i=1;i<=n;i++) vv[i]+=vv[i-1];
	vv[n]=inf;
	xx=n,Build(rt[n<<1],1,n);
	for(int i=n-1;i>=1;i--){
		Del(rt[n+i+1],i+1,i+1,1,n);
		rt[n+i]=rt[n+i+1];
		for(int j=0;j<b[i].size();++j){
			Modify(rt[n+i],b[i][j].u,b[i][j].v,1,n,(Tag){b[i][j].w,b[i][j].w,i});
		}
	}
	Del(rt[n],n,n,1,n);
	Del(rt[n+1],1,1,1,n);
	for(int i=1;i<=n;i++) id[i]=i,fa[i]=i,siz[i]=1;
	solve(1,n);
	sort(line+1,line+cnt+1,cmp);
	for(int i=1;i<=cnt;i++){
		x=fnd(line[i].u),y=fnd(line[i].v);
		ans=(ans+siz[x]%mod*siz[y]%mod*(line[i].w%mod))%mod;
		siz[x]+=siz[y];
		fa[y]=x;
	}
	printf("%lld",ans);
	return 0;
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 11352kb

input:

4 2
1 3 2
2 4 2

output:

21067776

result:

ok "21067776"

Test #2:

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

input:

79 190
24 30 8372
16 17 3623
47 43 577
47 45 10000
50 49 9644
35 25 882
23 24 1994
54 55 7778
32 33 230
52 53 6078
5 10 8214
25 26 6829
21 22 340
67 68 6066
10 1 8104
9 10 3085
44 43 5659
33 34 5505
7 10 337
12 13 3804
5 1 4735
25 28 6650
61 60 9290
14 6 9857
38 35 6228
48 49 3076
60 59 4972
54 52 4...

output:

844859152

result:

ok "844859152"

Test #3:

score: 0
Accepted
time: 1ms
memory: 10520kb

input:

71 199
20 19 869
46 61 9375
62 52 877
32 31 267
41 63 678
25 23 1224
61 62 103
28 29 4640
51 52 2086
47 49 5163
27 28 3198
9 8 5530
68 69 5035
26 28 7214
36 40 8779
11 12 2949
10 11 5381
1 71 1450
23 24 4741
33 32 6857
6 9 8673
34 39 6459
32 33 1827
14 13 8042
5 6 51
70 6 146
21 22 276
20 17 5504
18...

output:

746051489

result:

ok "746051489"

Test #4:

score: 0
Accepted
time: 2ms
memory: 11304kb

input:

78 190
54 66 589
64 65 9958
71 72 2115
77 73 5248
54 58 5868
68 57 72
56 60 1476
1 77 7739
16 17 9093
60 59 5315
20 14 313
6 59 277
60 64 3132
75 72 455
63 64 6752
30 38 4810
71 76 2424
30 31 8090
71 73 6643
26 27 3489
4 60 315
62 63 8501
38 39 7761
61 59 2258
46 47 5454
17 18 972
59 55 333
69 56 73...

output:

582293834

result:

ok "582293834"

Test #5:

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

input:

74 198
10 11 767
25 26 4090
46 42 1653
2 5 3333
43 38 7106
53 52 6205
4 2 529
20 27 3335
49 52 2604
21 15 5265
28 29 949
8 7 4439
55 56 99
7 8 238
66 69 7812
17 29 1364
39 47 1270
9 8 5229
5 4 3465
43 42 7064
30 31 7767
25 32 5244
30 31 7196
7 9 3118
56 57 5779
43 39 9187
51 50 5735
16 12 7606
40 41...

output:

508442899

result:

ok "508442899"

Test #6:

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

input:

78 192
21 22 4125
3 4 3230
71 72 4489
76 77 8868
55 56 7672
17 18 5081
30 29 1254
18 19 3896
39 32 8008
7 8 7118
70 71 3546
47 39 994
12 13 4972
77 78 123
12 13 1701
8 7 1029
63 1 478
31 28 4211
5 6 8249
77 74 4092
73 74 795
51 52 6818
5 6 8430
9 10 7953
14 15 8539
45 46 3682
11 9 9308
15 17 2643
12...

output:

568925767

result:

ok "568925767"

Test #7:

score: 0
Accepted
time: 1ms
memory: 10604kb

input:

71 192
44 28 4637
45 27 8128
61 11 4055
3 69 777
71 1 7158
9 63 5849
55 17 1415
42 30 1583
17 55 7200
12 60 5049
42 30 2190
14 58 9752
40 32 2155
22 50 2608
55 17 422
64 8 6165
27 45 4354
48 24 2506
67 5 6327
45 27 4340
56 16 6535
6 66 5083
47 25 7297
33 39 1273
23 49 8104
12 60 8773
62 10 472
70 2 ...

output:

740575153

result:

ok "740575153"

Test #8:

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

input:

74 196
19 56 2208
20 55 5639
58 17 5675
68 7 3559
10 65 873
25 50 193
50 25 6346
54 21 931
56 19 6211
59 16 6563
7 68 7976
35 40 7101
67 8 2437
53 22 7139
7 68 8435
9 66 464
57 18 9881
18 57 1338
57 18 3184
2 73 7381
34 41 5615
36 39 3187
58 17 2430
66 9 7393
55 20 1222
41 34 1139
68 7 5408
39 36 16...

output:

500821698

result:

ok "500821698"

Test #9:

score: 0
Accepted
time: 2ms
memory: 11700kb

input:

77 192
15 55 74
46 5 1414
67 68 4865
8 25 3974
41 31 2480
42 63 3038
22 77 1372
37 17 1208
50 17 2197
58 68 669
1 22 1878
11 49 1465
74 2 3145
11 20 4555
42 25 979
50 49 7595
71 16 2859
13 77 4927
31 21 2599
21 23 1947
28 25 7614
28 58 1171
72 49 1826
70 58 731
48 74 2202
60 72 2635
52 27 1781
34 2 ...

output:

319693148

result:

ok "319693148"

Test #10:

score: 0
Accepted
time: 2ms
memory: 11648kb

input:

76 190
50 34 4806
62 10 2548
45 34 2023
21 28 189
17 49 2411
50 39 3689
28 11 2519
59 32 2598
72 64 8754
31 56 1325
57 58 3376
47 46 6921
28 61 752
75 35 1452
9 33 1213
63 19 208
42 73 1115
22 40 929
59 53 1063
46 51 5736
35 15 994
22 23 5109
49 21 428
12 58 494
47 74 2423
1 5 8528
29 75 2183
43 6 1...

output:

50667227

result:

ok "50667227"

Test #11:

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

input:

71 196
12 37 1395
29 70 684
3 38 855
37 36 2708
69 37 1550
47 52 9602
19 45 1957
14 9 7724
63 52 1541
18 14 8279
64 50 2595
33 69 1303
33 57 989
12 17 4600
50 64 3908
60 29 468
15 24 1803
64 39 1795
34 42 2170
28 23 1308
33 45 428
16 20 131
28 8 1027
42 49 1658
48 53 772
69 23 509
69 50 3253
9 42 16...

output:

765565056

result:

ok "765565056"

Test #12:

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

input:

293 993
140 142 9682
232 243 313
67 68 5028
130 129 5855
36 35 8091
96 95 2322
68 67 2355
176 175 4817
242 238 5923
213 211 2343
278 277 6300
281 283 596
269 265 642
160 165 1585
274 275 2148
224 225 9745
99 98 680
130 127 4692
224 222 7351
270 271 5653
140 142 5254
219 217 7041
204 203 1154
263 266...

output:

487446357

result:

ok "487446357"

Test #13:

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

input:

297 993
240 241 6989
266 264 4892
280 281 6358
266 267 5237
88 84 6062
281 286 5497
1 296 3082
49 48 9502
148 149 9732
294 1 4162
176 174 5358
216 217 2924
279 283 61
9 10 681
42 43 2886
43 38 1310
22 24 690
128 129 9666
10 9 7299
26 28 7230
19 21 2030
173 171 8453
34 31 4980
148 149 6886
166 167 33...

output:

635119625

result:

ok "635119625"

Test #14:

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

input:

291 991
205 204 7961
153 148 7043
56 54 3207
130 129 235
47 49 982
7 6 7918
70 74 66
259 260 5461
199 200 4584
271 273 8554
130 128 8487
23 22 7830
269 274 811
94 95 7381
98 99 4011
170 171 8357
83 86 2266
10 9 4076
152 148 2084
143 142 4403
70 71 3178
8 9 6140
110 111 9432
156 157 5593
147 146 2216...

output:

435016731

result:

ok "435016731"

Test #15:

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

input:

294 997
31 27 1334
48 43 9810
40 41 6209
260 270 7098
246 245 3813
246 247 2591
94 87 6194
220 221 1845
94 95 747
33 34 6336
73 67 8120
212 213 5883
127 144 7212
231 239 7259
35 38 767
35 33 4346
92 91 4569
126 127 1683
74 84 7833
89 90 4039
141 106 9006
183 190 9225
100 101 7471
31 34 148
280 282 6...

output:

585062748

result:

ok "585062748"

Test #16:

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

input:

298 992
260 261 8940
229 228 7784
37 38 3250
59 60 3266
43 42 8674
194 195 4921
202 200 941
110 114 5448
225 226 3661
204 205 3635
238 240 896
98 105 867
33 34 5019
13 14 1558
163 159 9152
36 40 8423
226 225 8471
250 251 9567
207 209 6328
77 78 8566
282 283 9587
32 33 2271
62 63 7413
122 123 303
201...

output:

676742353

result:

ok "676742353"

Test #17:

score: 0
Accepted
time: 4ms
memory: 17236kb

input:

291 997
140 152 2769
165 127 7362
191 101 3702
17 275 6461
162 130 7143
52 240 7388
240 52 6220
285 7 5148
286 6 1998
161 131 542
136 156 8621
36 256 6382
287 5 4876
118 174 1539
182 110 5065
149 143 7397
130 162 1150
186 106 8769
22 270 9991
48 244 8800
161 131 3201
46 246 9799
4 288 9053
53 239 74...

output:

423941983

result:

ok "423941983"

Test #18:

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

input:

297 998
80 218 6306
187 111 4494
158 140 5704
122 176 7475
31 267 969
293 5 5388
147 151 1299
289 9 7048
48 250 757
293 5 5914
5 293 223
63 235 6179
188 110 6762
154 144 9141
14 284 355
244 54 8132
46 252 6727
231 67 1171
244 54 6727
32 266 1816
198 100 1956
107 191 7170
112 186 8344
41 257 1620
269...

output:

617769996

result:

ok "617769996"

Test #19:

score: 0
Accepted
time: 4ms
memory: 19072kb

input:

299 994
44 136 381
161 120 1914
267 44 1155
204 232 2751
235 13 163
36 25 4549
195 285 717
188 3 571
72 250 459
106 201 989
92 281 191
37 250 618
183 84 891
137 60 1146
46 135 122
106 205 545
45 22 182
122 238 257
283 98 363
137 215 1056
4 294 3498
73 116 198
96 111 4957
138 29 407
138 7 114
21 154 ...

output:

895803524

result:

ok "895803524"

Test #20:

score: 0
Accepted
time: 4ms
memory: 18744kb

input:

294 991
258 164 932
35 44 3568
284 293 683
160 111 222
274 49 744
165 108 1098
54 106 576
293 168 428
186 219 2100
237 76 135
132 174 175
56 274 1168
190 138 1286
20 218 367
42 74 1586
105 279 181
146 208 391
98 292 140
268 119 387
124 21 198
37 281 1038
112 178 1060
246 168 124
27 229 250
117 280 2...

output:

704435821

result:

ok "704435821"

Test #21:

score: 0
Accepted
time: 2ms
memory: 18712kb

input:

295 997
38 250 335
95 66 3294
23 26 6828
248 116 63
271 120 28
260 160 238
102 118 2914
244 137 765
253 247 5979
40 99 371
249 112 136
193 70 738
287 40 1432
96 227 220
61 20 399
5 42 2190
63 199 235
148 247 532
27 150 680
36 33 163
236 109 613
204 281 1151
286 130 643
225 293 38
153 79 372
11 181 5...

output:

724135682

result:

ok "724135682"

Test #22:

score: -100
Runtime Error

input:

18294 19992
7993 7990 4961
3549 3550 4737
15480 15481 5224
11212 11213 7993
15209 15210 661
13866 13867 5556
15788 15789 9331
7279 7280 260
4084 4085 3153
16816 16817 4944
16503 16504 6105
3542 3543 6081
555 556 5133
7461 7462 446
3027 3028 2409
16493 16494 1055
1511 1512 5530
10259 10260 7165
16503...

output:


result: