QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#167854#114. Construction of Highwaypaul20080 3ms11848kbC++143.4kb2023-09-07 17:53:142023-09-07 17:53:14

Judging History

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

  • [2023-09-07 17:53:14]
  • 评测
  • 测评结果:0
  • 用时:3ms
  • 内存:11848kb
  • [2023-09-07 17:53:14]
  • 提交

answer

#include <vector>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int N=1e5+5;

int c[N],fa[N],sz[N],son[N],dfsn[N],point[N],num[N],a[N],top[N],x[N],y[N],n,m,sumroll,cnt;
pair <int,int> roll_back[N];
vector <int> g[N];

struct node
{
	int l,r,min,max,cover;
}res[N*4];

void update(int x,int y)
{
	for(int i=x;i<=m;i+=i&-i)
		c[i] += y;
}

int query(int x)
{
	int ans=0;
	for(int i=x;i;i-=i&-i)
		ans += c[i];
	return ans;
}

void dfs1(int x)
{
	sz[x]=1;
	for(int i=0;i<g[x].size();i++)
	{
		int to=g[x][i];
		if(to==fa[x])
			continue;

		fa[to]=x;
		dfs1(to);
		sz[x] += sz[to];
		if(sz[to]>sz[son[x]])
			son[x]=to;
	}
}

void dfs2(int x,int t)
{
	dfsn[x]=++cnt, top[x]=t, point[cnt]=x;
	if(son[x])
		dfs2(son[x],t);

	for(int i=0;i<g[x].size();i++)
	{
		int to=g[x][i];
		if(to==fa[x] || to==son[x])
			continue;

		dfs2(to,to);
	}
}

void cover(int rt,int val)
{
	res[rt].min=res[rt].max=res[rt].cover=val;
}

void pushup(int rt)
{
	res[rt].min=min(res[rt*2].min,res[rt*2+1].min);
	res[rt].max=max(res[rt*2].max,res[rt*2+1].max);
}

void pushdown(int rt)
{
	if(res[rt].cover)
	{
		cover(rt*2,res[rt].cover);
		cover(rt*2+1,res[rt].cover);
		res[rt].cover=0;
	}
}

void build(int rt,int l,int r)
{
	res[rt].l=l,res[rt].r=r;
	if(l==r)
	{
		res[rt].min=res[rt].max=a[point[l]];
		return;
	}

	int mid=(l+r)/2;
	build(rt*2,l,mid);
	build(rt*2+1,mid+1,r);
	pushup(rt);
}

void update(int rt,int l,int r,int val)
{
	if(l<=res[rt].l && res[rt].r<=r)
	{
		cover(rt,val);
		return;
	}

	pushdown(rt);
	int mid=(res[rt].l+res[rt].r)/2;
	if(l<=mid)
		update(rt*2,l,r,val);
	if(mid+1<=r)
		update(rt*2+1,l,r,val);
	pushup(rt);
}

int query_val(int rt,int pos)
{
	if(res[rt].l==res[rt].r)
		return res[rt].min;

	pushdown(rt);
	int mid=(res[rt].l+res[rt].r)/2;
	if(pos<=mid)
		return query_val(rt*2,pos);
	return query_val(rt*2+1,pos);
}

int query_last(int rt,int r,int val)
{
	if(res[rt].min==val && res[rt].max==val)
		return -1;

	if(res[rt].l==res[rt].r)
		return res[rt].l;

	pushdown(rt);
	if(res[rt*2+1].l>r)
		return query_last(rt*2,r,val);

	int rans=query_last(rt*2+1,r,val);
	if(rans==-1)
		return query_last(rt*2,r,val);
	return rans;
}

void update_path(int x,int y)
{
	while(x)
	{
		update(1,dfsn[top[x]],dfsn[x],y);
		x=fa[top[x]];
	}
}

long long query_path(int x)
{
	if(x==0)
		return 0;

	int val=query_val(1,dfsn[x]), pos=query_last(1,dfsn[x],query_val(1,val));
	pos=max(pos,dfsn[top[x]]);
	long long ans=1ll*query(val-1)*(dfsn[x]-pos+1);
	update(val,dfsn[x]-pos+1);
	roll_back[++sumroll]=make_pair(val,dfsn[x]-pos+1);
	if(pos==dfsn[top[x]])
		return ans+query_path(fa[top[x]]);
	return ans+query_path(point[pos-1]);
}

void clear()
{
	for(int i=1;i<=sumroll;i++)
		update(roll_back[i].first,-roll_back[i].second);
	sumroll=0;
}

int main()
{
	cin >> n;
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		num[++m]=a[i];
	}

	sort(num+1,num+m+1);
	m=unique(num+1,num+m+1)-num-1;
	for(int i=1;i<=n;i++)
		a[i]=lower_bound(num+1,num+m+1,a[i])-num;

	for(int i=1;i<n;i++)
	{
		scanf("%d %d",&x[i],&y[i]);
		g[x[i]].push_back(y[i]);
		g[y[i]].push_back(x[i]);
	}

	dfs1(1);
	dfs2(1,1);
	build(1,1,n);
	for(int i=1;i<n;i++)
	{
		printf("%lld\n",query_path(x[i]));
		update_path(x[i],a[y[i]]);
		clear();
	}
	return 0;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 7
Accepted
time: 0ms
memory: 9964kb

input:

2
804289384 846930887
1 2

output:

0

result:

ok single line: '0'

Test #2:

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

input:

10
505335291 738766720 190686789 260874576 747983062 906156499 502820865 142559278 261608746 380759628
1 3
1 5
5 7
3 8
1 4
3 10
7 6
5 9
5 2

output:

0
0
0
1
0
1
0
0
0

result:

ok 9 lines

Test #3:

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

input:

100
205554747 483147986 844158169 953350441 612121426 310914941 210224073 856883377 922860802 495649265 8614859 989089925 378651394 344681740 29100603 816952842 21468265 552076976 87517202 953369896 374612516 787097143 126313439 207815259 287632274 886964648 220723886 119448938 444268469 865680799 6...

output:

0
0
0
0
0
0
0
0
0
1
0
0
2
0
0
0
0
0
2
0
3
4
3
0
0
1
2
1
0
2
0
2
0
5
6
1
1
2
0
0
1
1
0
1
0
2
4
0
4
1
2
2
3
0
2
0
0
0
8
0
2
2
0
1
3
2
8
2
0
0
2
0
0
1
0
4
2
0
3
0
2
6
3
0
1
1
0
0
4
6
1
1
0
0
1
6
1
2
0

result:

ok 99 lines

Test #4:

score: -7
Wrong Answer
time: 0ms
memory: 11844kb

input:

300
968078302 287724084 410622275 558519327 460165364 773440538 901520026 404622364 417397029 665131386 88500545 246243955 225558715 439197965 991031404 638538415 465622903 21944942 554535402 204144150 501551718 340552605 608463969 970964280 749109574 736758719 557300323 501093883 605082721 41831082...

output:

0
0
0
0
0
0
0
4
0
0
2
0
0
0
0
0
0
1
0
0
0
5
0
0
1
0
0
1
5
4
2
7
5
0
0
1
0
0
3
2
2
2
5
0
0
0
0
0
3
6
4
3
0
4
3
4
2
4
0
6
0
0
0
0
6
3
0
3
4
4
7
1
6
3
0
7
3
0
2
8
3
0
0
0
2
4
0
11
2
6
4
7
9
4
0
8
3
3
12
2
6
6
4
4
0
5
3
5
3
8
4
0
0
3
0
7
0
0
0
12
0
0
8
0
0
0
4
5
16
4
0
2
12
0
11
4
0
3
5
4
3
6
19
4
3
2
0...

result:

wrong answer 51st lines differ - expected: '5', found: '4'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #1:

0%