QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#142639#6404. Shuttle TourSchi2oidML 175ms1041232kbC++142.7kb2023-08-19 16:04:422023-08-19 16:04:46

Judging History

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

  • [2023-08-19 16:04:46]
  • 评测
  • 测评结果:ML
  • 用时:175ms
  • 内存:1041232kb
  • [2023-08-19 16:04:42]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
using namespace std;
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define mid ((l+r)>>1)
#define cle node(INF,0,0)
#define ncle node(loc[l],loc[l],1)
const int INF=1e18;
const int N=200005;
int loc[N],fa[N],val[N],cid[N],top[N],dep[N],len[55],tot=0,ccnt=1;
char s[N];
vector<int>chain[55];
vector<int>cedge[55];
struct edg{
	int v,w;
};
vector<edg>edge[N];
struct node{
	int mn,mx,cnt;
	node(int MN=INF,int MX=0,int CNT=0){
		mn=MN,mx=MX,cnt=CNT;
	}
}t[4*N][55],ret[55];
void merge(node &x,node y){
	x.mn=min(x.mn,y.mn);
	x.mx=max(x.mx,y.mx);
	x.cnt+=y.cnt;
}
void push_up(int p){
	for(int i=1;i<=ccnt;i++){
		t[p][i]=t[ls(p)][i];
		merge(t[p][i],t[rs(p)][i]);
	}
}
void build(int p,int l,int r){
	if(l==r){
		t[p][cid[l]]=(s[l]=='0')?cle:ncle;
		return;
	}
	build(ls(p),l,mid);
	build(rs(p),mid+1,r);
	push_up(p);
}
void modi(int p,int l,int r,int x){
	if(l==r){ 
		t[p][cid[x]]=(t[p][cid[x]].cnt)?cle:ncle;
		return;
	}
	if(x<=mid) modi(ls(p),l,mid,x);
	else modi(rs(p),mid+1,r,x);
	push_up(p);
}
void query(int p,int l,int r,int ql,int qr){
	if(l>=ql&&r<=qr){
		for(int i=1;i<=ccnt;i++) merge(ret[i],t[p][i]);
		return;
	}
	if(ql<=mid) query(ls(p),l,mid,ql,qr);
	if(qr>mid) query(rs(p),mid+1,r,ql,qr);
}
void dfs(int x,int f,int w,int tp){
	fa[x]=f,val[x]=w;
	cid[x]=cid[tp];
	loc[x]=++len[cid[tp]];
	chain[cid[tp]].push_back(x);
	dep[x]=dep[f]+w;
	int ok=0;
	for(int i=0;i<edge[x].size();i++){
		int v=edge[x][i].v,w=edge[x][i].w;
		if(v==f) continue;
		if(!ok) ok=1,dfs(v,x,w,tp);
		else{
			cid[v]=++ccnt;
			top[ccnt]=v;
			dfs(v,x,w,v);
		}
	}
}
int cdfs(int x){
	int l=ret[x].mn,r=ret[x].mx;
	int ans=0;
	for(int i=0;i<cedge[x].size();i++){
		int v=cedge[x][i];
		ans+=cdfs(v);
		int p=loc[chain[v][0]];
		if(ret[v].cnt){
			l=min(l,p);
			r=max(r,p);
		}
		ret[x].cnt+=ret[v].cnt;
	}
	if(ret[x].cnt!=tot) l=0;
	return dep[chain[x][r]]-dep[chain[x][l]]+ans;
}
signed main(){
	int n,q,u,v,w;
	cin>>n>>q;
	for(int i=1;i<=50;i++) chain[i].push_back(0);
	scanf("%s",s+1);
	for(int i=1;i<n;i++){
		scanf("%lld%lld%lld",&u,&v,&w);
		edge[u].push_back((edg){v,w});
		edge[v].push_back((edg){u,w});
	}
	cid[1]=top[1]=1;
	dfs(1,0,0,1);
	for(int i=1;i<=ccnt;i++) chain[i][0]=fa[top[i]];
	for(int i=2;i<=ccnt;i++) cedge[cid[fa[top[i]]]].push_back(i);
	build(1,1,n);
	int op,x,y;
	for(int i=1;i<=q;i++){
		scanf("%lld",&op);
		if(op==1){
			scanf("%lld",&x);
			modi(1,1,n,x);
		}
		else{
			scanf("%lld%lld",&x,&y);
			for(int j=1;j<=ccnt;j++) ret[j]=cle;
			query(1,1,n,x,y);
			tot=0;
			for(int j=1;j<=ccnt;j++) tot+=ret[j].cnt;
			if(!tot) puts("-1");
			else printf("%lld\n",2*cdfs(1));
		}
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 40ms
memory: 1040504kb

input:

5 6
10110
1 2 1
1 3 10
2 4 100
3 5 1000
2 1 5
1 3
2 1 5
2 2 4
2 5 5
2 1 1

output:

222
202
0
-1
0

result:

ok 5 number(s): "222 202 0 -1 0"

Test #2:

score: 0
Accepted
time: 143ms
memory: 1039756kb

input:

50 200000
00100100100001101010110100000111100011101110011010
14 47 940241413
11 43 331483591
37 38 496247070
47 46 832459694
7 15 16479291
1 30 402388666
30 8 513064064
46 50 739311480
5 4 761894947
32 41 631669659
17 24 715786564
35 20 763151642
32 33 492466005
39 11 186023146
9 7 805081251
3 42 25...

output:

17149847378
-1
26540740138
29613692754
21307948558
27003443184
11893407374
18332625722
20412196350
20281573062
29727468956
9593307160
9380710234
23682775470
16688997988
2856655228
14982588156
0
-1
9140136614
26193602866
22558599316
24807163344
19126284190
8533875940
7695830712
18079494744
0
27004673...

result:

ok 100000 numbers

Test #3:

score: 0
Accepted
time: 175ms
memory: 1041232kb

input:

50 200000
10010001110011101101101011010011100101101010111001
1 25 560163135
2 7 696374536
33 39 14485266
39 22 456690113
4 5 267811886
18 6 657680382
3 43 865617406
25 14 885515220
41 34 919525052
42 50 410634144
8 3 487403277
3 30 701549676
2 43 54223104
7 34 691078766
14 23 323352590
26 48 4936558...

output:

17177676326
31373486130
15290175638
8192974494
22734716092
27802380120
6315957348
0
15780401446
15929384564
26423899248
9639532596
21199366790
26413065782
-1
29063665908
29925313598
28651879288
17144176262
16526083688
28206620550
26873163342
14840246174
32414950818
29985336496
0
11937422088
11131990...

result:

ok 100000 numbers

Test #4:

score: 0
Accepted
time: 134ms
memory: 1040424kb

input:

50 200000
01011001001010000100001101100111011001101011101000
34 29 654100339
33 5 947063765
45 25 962067151
11 13 509130797
26 47 988757358
49 22 75267557
7 11 431530851
46 1 531969122
22 43 449891945
34 6 526679193
50 17 499004364
22 23 226725950
26 48 203414490
11 44 900725507
10 29 451714017
35 2...

output:

24080362406
0
0
21418182584
28358635244
28257750030
24520294678
21418182584
0
15335211126
28621468542
18664505530
19335499776
32374868794
23618866752
26801803500
24116134918
27676993638
30222353942
25612316674
20504702130
28400398734
29472795250
24400110084
20586186786
25612316674
0
30067400886
1297...

result:

ok 100000 numbers

Test #5:

score: 0
Accepted
time: 72ms
memory: 1040332kb

input:

2 10
01
1 2 340134039
2 1 2
1 1
2 1 2
1 2
2 1 1
1 2
2 2 2
1 2
2 2 2
1 1

output:

0
680268078
0
0
-1

result:

ok 5 number(s): "0 680268078 0 0 -1"

Test #6:

score: -100
Memory Limit Exceeded

input:

200000 100
1100001001001101010000111111011011101010001011011011011010101111010001111010101100101001010000110100100011111010011100101010010001011010111100101110010000101010000100111100000011001100111101110011000011010011011011100101100111101010111101110111001111111010001010010000110110100000111000111...

output:


result: