QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#483434#6738. Coverucup-team052#WA 386ms61460kbC++142.6kb2024-07-18 17:12:202024-07-18 17:12:21

Judging History

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

  • [2024-07-18 17:12:21]
  • 评测
  • 测评结果:WA
  • 用时:386ms
  • 内存:61460kb
  • [2024-07-18 17:12:20]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
#define D(...) fprintf(stderr,__VA_ARGS__)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
using namespace std;
using LL=long long;
const int N=500005,M=500005,K=20;
int n,m,K0,fa[N][K],dep[N],tin[N],tou[N],idx;
vector<int>e[N];
struct node{
	int a,b,w,c;
	bool operator<(const node&rhs)const{
		return dep[c]>dep[rhs.c];
	}
}A[M];
void dfs1(int k1,int k2){tin[k1]=++idx,dep[k1]=dep[k2]+1;fa[k1][0]=k2;rep(i,1,K-1)fa[k1][i]=fa[fa[k1][i-1]][i-1];for(auto&x:e[k1])if(x!=k2)dfs1(x,k1);tou[k1]=idx;}
int jump(int k1,int k2){
	per(i,K-1,0)if(k2>>i&1)k1=fa[k1][i];
	return k1;
}
int LCA(int k1,int k2){
	if(dep[k1]<dep[k2])swap(k1,k2);
	int dt=dep[k1]-dep[k2];
	per(i,K-1,0)if(dt>>i&1)k1=fa[k1][i];
	if(k1==k2)return k1;
	per(i,K-1,0)if(fa[k1][i]!=fa[k2][i])k1=fa[k1][i],k2=fa[k2][i];
	return fa[k1][0];
}
struct fenwick{
	LL tr[N];
	void add(int x,LL y){
		for(int i=x;i<N;i+=i&-i)tr[i]+=y;
	}
	void add(int l,int r,LL x){
		add(l,x);
		add(r+1,-x);
	}
	LL query(int x){
		LL y=0;
		for(int i=x;i;i&=i-1)y+=tr[i];
		return y;
	}
}fwk;
struct dsu{
	int fa[N];
	dsu(){rep(i,0,N-1)fa[i]=i;}
	int fd(int x){return fa[x]==x?x:fa[x]=fd(fa[x]);}
}o;
LL rem[M];
vector<int>vec[N];
LL ans;
void work(int u,int v,LL &dt){
	while(1){
		u=o.fd(u);
		if(dep[u]<=dep[v])break;
		while(!vec[u].empty()){
			int x=vec[u].back();
			LL t=min(dt,rem[x]);
			dt-=t;
			rem[x]-=t;
			fwk.add(tin[u],tou[u],t);
			if(!dt)return;
			vec[u].pop_back();
		}
		if(fa[u][0]!=v){
			o.fa[u]=fa[u][0];
		}else{
			break;
		}
	}
}
LL qsum(int a,int b,int c){
	return fwk.query(tin[a])+fwk.query(tin[b])-fwk.query(tin[c])*2;
}
int main(){
#ifdef xay5421
	freopen("a.in","r",stdin);
#endif
	cin>>n>>m>>K0;
	rep(i,2,n){int x,y;cin>>x>>y;e[x].pb(y),e[y].pb(x);}
	dfs1(1,0);
	rep(i,1,m){
		cin>>A[i].a>>A[i].b>>A[i].w;
		A[i].c=LCA(A[i].a,A[i].b);
		if(A[i].b==A[i].c)swap(A[i].a,A[i].b);
	}
	sort(A+1,A+m+1);
	rep(i,1,m){
		LL s=qsum(A[i].a,A[i].b,A[i].c);
		if(s>=A[i].w)continue;
		LL dt=A[i].w-s;
		work(A[i].a,A[i].c,dt);
		work(A[i].b,A[i].c,dt);
		ans+=dt;
		if(!dt)continue;
		if(A[i].a==A[i].c){
			int b_=jump(A[i].b,dep[A[i].b]-dep[A[i].c]-1);
			fwk.add(tin[b_],dt);
		}else{
			int a_=jump(A[i].a,dep[A[i].a]-dep[A[i].c]-1);
			int b_=jump(A[i].b,dep[A[i].b]-dep[A[i].c]-1);
			rem[i]=dt;
			vec[a_].eb(i);
			vec[b_].eb(i);
		}
	}
	/*rep(i,2,n)D("i=%d : %lld\n",i,fwk.query(tin[i])-fwk.query(tin[fa[i][0]]));
	D("\n");*/
	printf("%lld\n",ans);
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 8ms
memory: 40864kb

input:

5 7 3
1 2
1 3
2 4
2 5
3 2 8
5 4 10
3 1 2
1 2 7
2 1 2
1 2 1
5 2 3

output:

19

result:

ok 1 number(s): "19"

Test #2:

score: -100
Wrong Answer
time: 386ms
memory: 61460kb

input:

100000 500000 12
2 1
3 2
4 2
5 2
6 5
7 2
8 5
9 3
10 2
11 2
12 5
13 1
14 1
15 1
16 1
17 1
18 1
19 1
20 1
21 1
22 1
23 1
24 12
25 2
26 2
27 2
28 2
29 2
30 15
31 30
32 23
33 26
34 22
35 30
36 26
37 3
38 3
39 3
40 3
41 3
42 3
43 3
44 3
45 3
46 3
47 20
48 21
49 4
50 4
51 4
52 4
53 4
54 4
55 4
56 4
57 4
5...

output:

629781767329

result:

wrong answer 1st numbers differ - expected: '660925834533', found: '629781767329'