QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#142637#6404. Shuttle TourSchi2oidWA 143ms525184kbC++142.7kb2023-08-19 16:02:402023-08-19 16:02:44

Judging History

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

  • [2023-08-19 16:02:44]
  • 评测
  • 测评结果:WA
  • 用时:143ms
  • 内存:525184kb
  • [2023-08-19 16:02:40]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
#define mid ((l+r)>>1)
#define cle node(1000000000,0,0)
#define ncle node(loc[l],loc[l],1)
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=1000000000,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;
}
int 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("%d%d%d",&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("%d",&op);
		if(op==1){
			scanf("%d",&x);
			modi(1,1,n,x);
		}
		else{
			scanf("%d%d",&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("%d\n",2*cdfs(1));
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 24ms
memory: 525184kb

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: -100
Wrong Answer
time: 143ms
memory: 525096kb

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:

-30021806
-1
770936362
-451078318
-166887922
1233639408
-991494514
1152756538
-1062640130
-1193263418
-337302116
1003372568
790775642
-2087028306
-490871196
-1438312068
2097686268
0
-1
550202022
423799090
1083762836
-962640432
1946415006
-56058652
-894103880
899625560
0
1234869268
0
864051766
494524...

result:

wrong answer 1st numbers differ - expected: '17149847378', found: '-30021806'