QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#141595#5249. BanditsPhantomThreshold#WA 83ms71628kbC++203.7kb2023-08-17 17:16:042023-08-17 17:16:05

Judging History

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

  • [2023-08-17 17:16:05]
  • 评测
  • 测评结果:WA
  • 用时:83ms
  • 内存:71628kb
  • [2023-08-17 17:16:04]
  • 提交

answer

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

const int maxn = 200050;
const int maxd = 18;
const int maxp = maxn*31;
const int  mxR  = 1e9;

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;
	}
	int loc;
	void ins(int &x,const int l,const int r)
	{
		if(!x) x=newnode();
		seg[x]++;
		if(l==r) return;
		int mid=(l+r)>>1;
		if(loc<=mid) ins(lc[x],l,mid);
		else ins(rc[x],mid+1,r);
	}
	int lx,rx;
	int query(int x,const int l,const int r)
	{
		if(!seg[x] || rx<l || r<lx) return 0;
		if(lx<=l&&r<=rx) return seg[x];
		int 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],0,mxR);
				}
				if( x!=top && dis[d][x]<=qi[i][2] )
				{
					seg2.loc=qi[i][2]-dis[d][x];
					seg2.ins(rt2[fai[d][x]],0,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];
					if( y==top ) swap(x,y);
					
					seg1.lx=dis[d][y],seg1.rx=mxR;
					ans[i]+= seg1.query( rt1[top],0,mxR );
					
					seg2.lx=dis[d][y],seg2.rx=mxR;
					ans[i]-= seg2.query( rt2[fai[d][y]],0,mxR );
				}
				else if(d<=dep[y])
				{
					int top=y;
					for(int j=dep[y];j>d;j--)
						top=Fa[top];
					
					if( dis[d][y]+e[ qi[i][1] ][2] <= 1LL*mxR )
					{
						seg1.lx=dis[d][y]+e[ qi[i][1] ][2],seg1.rx=mxR;
						ans[i]+= seg1.query( rt1[top],0,mxR );
						
						if(y!=top)
						{
							seg2.lx=dis[d][y]+e[ qi[i][1] ][2],seg2.rx=mxR;
							ans[i]-= seg2.query( rt1[top],0,mxR );
						}
					}
				}
			}
		}
	}
	
	for(int i=1;i<=Q;i++) if(qi[i][0]==1) 
		cout<<ans[i]<<'\n';
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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'