QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#371557#8296. Constructing Ranchesucup-team052#WA 153ms3828kbC++232.4kb2024-03-30 13:59:062024-03-30 13:59:06

Judging History

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

  • [2024-03-30 13:59:06]
  • 评测
  • 测评结果:WA
  • 用时:153ms
  • 内存:3828kb
  • [2024-03-30 13:59:06]
  • 提交

answer

#include<bits/stdc++.h>
#ifdef xay5421
#define D(...) fprintf(stderr,__VA_ARGS__)
#else
#define D(...) ((void)0)
#endif
#define pb push_back
#define SZ(x) ((int)(x).size())
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
using LL=long long;
const int N=200005,INF=0X3F3F3F3F;
int T,n,a[N];
int root,full,f[N],sz[N];
vector<int>e[N];
bool ban[N];
void get_root(int k1,int k2){
	sz[k1]=1,f[k1]=0;
	for(auto&x:e[k1])if(x!=k2&&!ban[x])get_root(x,k1),sz[k1]+=sz[x],f[k1]=max(f[k1],sz[x]);
	f[k1]=max(f[k1],full-sz[k1]);
	if(f[k1]<f[root])root=k1;
}
LL sum[N];
int mx[N];
vector<int>V,allV;
vector<vector<int> >VV;
void dfs1(int k1,int k2){
	V.pb(k1);
	allV.pb(k1);
	sum[k1]=sum[k2]+a[k1];
	mx[k1]=max(mx[k2],a[k1]);
	for(auto&x:e[k1])if(x!=k2&&!ban[x]){
		dfs1(x,k1);
	}
}

struct fenwick{
	int n;
	int tr[N];
	void init(int n0){
		n=n0;
		rep(i,1,n)tr[i]=0;
	}
	void add(int x,int y){
		++x;
		for(int i=x;i;i&=i-1)tr[i]+=y;
	}
	LL query(int x){
		++x;
		LL y=0;
		for(int i=x;i<=n;i+=i&-i)y+=tr[i];
		return y;
	}
}fwk;
LL ans;
void solve(int cur){
	// D("solve %d\n",cur);
	ban[cur]=1;
	sum[cur]=0;
	mx[cur]=a[cur];
	
	VV.clear();
	allV.clear();
	for(auto&x:e[cur])if(!ban[x]){
		V.clear();
		dfs1(x,cur);
		VV.pb(V);
	}
	
	auto calc=[&](vector<int>&v){
		sort(v.begin(),v.end(),[&](int lhs,int rhs){return mx[lhs]<mx[rhs];});
		vector<LL>num;
		rep(i,0,SZ(v)-1){
			num.push_back(sum[v[i]]);
		}
		sort(num.begin(),num.end());
		num.erase(unique(num.begin(),num.end()),num.end());
		fwk.init(SZ(num));
		LL ret=0;
		rep(i,0,SZ(v)-1){
			ret+=fwk.query(upper_bound(num.begin(),num.end(),mx[v[i]]*2-sum[v[i]]-a[cur])-num.begin());
			fwk.add(lower_bound(num.begin(),num.end(),sum[v[i]])-num.begin(),1);
		}
		return ret;
	};
	ans+=calc(allV);
	for(auto&x:VV){
		ans-=calc(x);
	}
	
	int old=full;
	for(auto&x:e[cur])if(!ban[x]){
		full=sz[x]<sz[cur]?sz[x]:old-sz[cur];
		root=0;
		get_root(x,cur);
		solve(root);
	}
}
int main(){
#ifdef xay5421
	freopen("a.in","r",stdin);
#endif
	ios::sync_with_stdio(0),cin.tie(0);
	cin>>T;
	rep(tc,1,T){
		cin>>n;
		rep(i,1,n)cin>>a[i],e[i].clear();
		rep(i,2,n){
			int u,v;
			cin>>u>>v;
			e[u].pb(v),e[v].pb(u);
		}
		fill(ban+1,ban+n+1,0);
		full=n;
		root=0;
		f[0]=INF;
		get_root(1,0);
		ans=0;
		solve(root);
		printf("%lld\n",ans);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3828kb

input:

2
3
1 10 100
1 2
3 2
5
1 1 1 1 1
1 2
1 3
1 4
1 5

output:

0
6

result:

ok 2 lines

Test #2:

score: -100
Wrong Answer
time: 153ms
memory: 3804kb

input:

34763
19
98 27 61 17 77 9 97 23 24 5 94 61 100 88 98 43 8 75 96
4 17
12 17
7 3
19 4
12 5
1 18
10 5
7 2
1 4
2 11
19 9
18 16
1 11
17 15
6 3
16 8
15 14
15 13
9
49 4 97 14 1 11 52 84 84
1 3
6 4
9 7
2 6
7 8
1 2
3 8
5 9
19
92 74 62 54 60 78 74 6 76 80 13 94 4 86 85 89 23 46 2
6 17
1 8
8 2
8 14
13 7
12 9
1...

output:

116
18
114
117
126
91
8
3
110
4
1
95
19
4
13
50
46
13
19
128
10
50
105
2
25
62
1
105
2
141
0
4
106
59
113
11
55
98
28
80
125
127
10
74
6
56
26
6
71
8
9
31
14
0
122
0
15
0
20
19
53
140
61
1
4
106
25
95
18
47
16
19
44
76
11
37
12
17
10
1
52
135
10
15
58
72
12
129
71
3
37
140
17
90
27
100
9
23
134
1
4
...

result:

wrong answer 1st lines differ - expected: '135', found: '116'