QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#141586#5249. BanditsPhantomThreshold#WA 83ms73464kbC++203.6kb2023-08-17 17:00:462023-08-17 17:00:49

Judging History

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

  • [2023-08-17 17:00:49]
  • 评测
  • 测评结果:WA
  • 用时:83ms
  • 内存:73464kb
  • [2023-08-17 17:00:46]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxn = 200050;
const int maxd = 18;
const int maxp = maxn*60;
const ll  mxR  = 1e14;

int n,Q;
int e[maxn][3];
vector< pair<int,int> >E[maxn];

struct segment
{
	int cnt;
	int seg[maxp],lc[maxp],rc[maxp];
	void clear()
	{
		cnt=0;
	}
	int newnode()
	{
		++cnt;
		seg[cnt]=0;
		lc[cnt]=rc[cnt]=0;
		return cnt;
	}
	ll loc;
	void ins(int &x,const ll l,const ll r)
	{
		if(!x) x=newnode();
		seg[x]++;
		if(l==r) return;
		ll mid=(l+r)>>1;
		if(loc<=mid) ins(lc[x],l,mid);
		else ins(rc[x],mid+1,r);
	}
	ll lx,rx;
	int query(int x,const ll l,const ll r)
	{
		if(!seg[x]) return 0;
		if(lx<=l&&r<=rx) return seg[x];
		ll mid=(l+r)>>1;
		return query(lc[x],l,mid)+query(rc[x],mid+1,r);
	}
}seg1,seg2;
int rt1[maxn],rt2[maxn];

int siz[maxn],son[maxn],dep[maxn],Fa[maxn];
int fai[maxd][maxn],fan,mxd;
ll dis[maxd][maxn];
void dfs(const int x,const int ff,const int nowd,const int tfai)
{
	fai[nowd][x]=tfai;
	siz[x]=1; son[x]=0;
	for(auto [y,c]:E[x]) if(y!=ff && !dep[y])
	{
		dis[nowd][y]=dis[nowd][x]+c;
		dfs(y,x,nowd,tfai);
		siz[x]+=siz[y];
		if(siz[y]>siz[son[x]]) son[x]=y;
	}
}
int findroot(int x)
{
	int Siz= siz[x];
	while( son[x] && !( siz[son[x]]*2<=Siz && (Siz-siz[x])*2<=Siz ) )
	{
		x=son[x];
	}
	return x;
}
void divide(const int x,const int ff,const int nowd)
{
	mxd=max(mxd,nowd);
	dfs(x,ff,nowd,0);
	int rt=findroot(x);
	dep[rt]=nowd; Fa[rt]=ff;
	dis[nowd][rt]=0;
	for(auto [y,c]:E[x]) if(!dep[y])
	{
		++fan;
		dis[nowd][y]=c;
		dfs(y,x,nowd,fan);
	}
	
	for(auto [y,c]:E[x]) if(!dep[y])
		divide(y,x,nowd+1);
}

int qi[maxn][3],ans[maxn];

signed main()
{
	ios_base::sync_with_stdio(false); //////////////////////////////////////////////////////
	
	cin>>n;
	for(int i=1;i<n;i++)
	{
		int x,y,c; cin>>x>>y>>c;
		e[i][0]=x,e[i][1]=y,e[i][2]=c;
		E[x].emplace_back(y,c);
		E[y].emplace_back(x,c);
	}
	mxd=0; divide(1,0,1);
	
	cin>>Q;
	for(int i=1;i<=Q;i++)
	{
		string str; cin>>str;
		if(str[0]=='+')
		{
			qi[i][0]=0;
			cin>>qi[i][1]>>qi[i][2];
		}
		else 
		{
			qi[i][0]=1;
			cin>>qi[i][1];
		}
	}
	
	for(int d=1;d<=mxd;d++)
	{
		seg1.clear(); seg2.clear();
		for(int i=1;i<=n;i++) rt1[i]=0;
		for(int i=1;i<=fan;i++) rt2[i]=0;
		
		for(int i=1;i<=Q;i++)
		{
			if(qi[i][0]==0) //+
			{
				const int x=qi[i][1];
				if( dep[x]<d ) continue;
				
				int top=x;
				for(int j=dep[x];j>d;j--)
					top=Fa[top];
				
				if( dis[d][x]<=qi[i][2] )
				{
					seg1.loc=qi[i][2]-dis[d][x];
					seg1.ins(rt1[top],1ll,mxR);
				}
				if( x!=top && dis[d][x]<=qi[i][2] )
				{
					seg2.loc=qi[i][2]-dis[d][x];
					seg2.ins(rt2[fai[d][x]],1ll,mxR);
				}
			}
			else //?
			{
				int x= e[ qi[i][1] ][0], y =e[ qi[i][1] ][1];
				if( dep[x]>dep[y] ) swap(x,y);
				
				if(d<=dep[x])
				{
					if( dis[d][x] > dis[d][y] ) swap(x,y);
				
					int top=x;
					for(int j=dep[x];j>d;j--)
						top=Fa[top];
					
					seg1.lx=dis[d][y],seg1.rx=mxR;
					ans[i]+= seg1.query( rt1[top],1ll,mxR );
					
					seg2.lx=dis[d][y],seg2.rx=mxR;
					ans[i]-= seg2.query( rt2[fai[d][y]],1ll,mxR );
				}
				else if(d<=dep[y])
				{
					int top=y;
					for(int j=dep[y];j>d;j--)
						top=Fa[top];
					
					seg1.lx=dis[d][y]+e[ qi[i][1] ][2],seg1.rx=mxR;
					ans[i]+= seg1.query( rt1[top],1ll,mxR );
					
					if(y!=top)
					{
						seg2.lx=dis[d][y]+e[ qi[i][1] ][2],seg2.rx=mxR;
						ans[i]-= seg2.query( rt1[top],1ll,mxR );
					}
				}
			}
		}
	}
	
	for(int i=1;i<=Q;i++) if(qi[i][0]==1) 
		cout<<ans[i]<<'\n';
	
	return 0;
}

詳細信息

Test #1:

score: 0
Wrong Answer
time: 83ms
memory: 73464kb

input:

100000
2670 75097 4080
87477 75802 1712
51835 36626 2883
19412 25923 5852
23976 19312 2520
82536 19514 2492
27160 66601 4483
99087 15088 3504
47050 58820 2964
37063 5696 9901
7717 1496 4891
79136 5448 4340
22575 81285 9289
96280 3803 9877
41980 32139 2855
44236 64938 3298
5983 99947 9666
95856 62545...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

wrong answer 4th lines differ - expected: '2', found: '0'