QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#90497#5249. BanditsRandom_CodeWA 1553ms100852kbC++114.7kb2023-03-23 13:20:372023-03-23 13:20:41

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-23 13:20:41]
  • 评测
  • 测评结果:WA
  • 用时:1553ms
  • 内存:100852kb
  • [2023-03-23 13:20:37]
  • 提交

answer

//ANMHLIJKTJIY!
#pragma GCC optimize(2)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline","fast-math","unroll-loops","no-stack-protector")
#pragma GCC diagnostic error "-fwhole-program"
#pragma GCC diagnostic error "-fcse-skip-blocks"
#pragma GCC diagnostic error "-funsafe-loop-optimizations"
#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 998244353
#define F first
#define S second
#define ll long long
#define N 100010
#define M (N*30)
using namespace std;
struct BIT{
	ll val[M];
	void upd(ll x,ll v)
	{
		++x;
		while(x<M)
		{
			val[x]+=v;
			x+=x&(-x);
		}
		return;
	}
	ll qry(ll x)
	{
		++x;
		ll ret=0;
		while(x)
		{
			ret+=val[x];
			x-=x&(-x);
		}
		return ret;
	}
}bit,bitfa;
ll n,q,sz[N],mxsz[N],fa[N],f[17][N],dep[N],dist[N],fdis[22][N],fadep[N];
bool iscent[N];
vector<ll> pth;
vector<pair<ll,ll> > vt[N];
void predfs(ll x,ll lst)
{
	ll i;
	for(i=1;i<17;i++)
	{
		f[i][x]=f[i-1][f[i-1][x]];
	}
	for(i=0;i<vt[x].size();i++)
	{
		if(vt[x][i].F!=lst)
		{
			f[0][vt[x][i].F]=x;
			dep[vt[x][i].F]=dep[x]+1;
			dist[vt[x][i].F]=dist[x]+vt[x][i].S;
			predfs(vt[x][i].F,x);
		}
	}
	return;
}
ll glca(ll x,ll y)
{
	if(dep[x]<dep[y])
	{
		swap(x,y);
	}
	ll i;
	for(i=16;i>=0;i--)
	{
		if(dep[f[i][x]]>=dep[y])
		{
			x=f[i][x];
		}
	}
	if(x==y)
	{
		return x;
	}
	for(i=16;i>=0;i--)
	{
		if(f[i][x]!=f[i][y])
		{
			x=f[i][x],y=f[i][y];
		}
	}
	return f[0][x];
}
void dfs(ll x,ll lst)
{
	ll i;
	pth.push_back(x);
	sz[x]=1,mxsz[x]=0;
	for(i=0;i<vt[x].size();i++)
	{
		if(vt[x][i].F!=lst&&(!iscent[vt[x][i].F]))
		{
			dfs(vt[x][i].F,x);
			sz[x]+=sz[vt[x][i].F];
			mxsz[x]=max(mxsz[x],sz[vt[x][i].F]);
		}
	}
	return;
}
ll getcent(ll x)
{
	ll i;
	pth.clear();
	dfs(x,-1);
	for(i=0;i<pth.size();i++)
	{
		if(max(mxsz[pth[i]],(ll)pth.size()-sz[pth[i]])*2<=pth.size())
		{
			return pth[i];
		}
	}
	return -1;
}
void solve(ll x,ll lst=-1,ll d=0)
{
	ll i;
	x=getcent(x);
	fa[x]=lst;
	fadep[x]=d;
	iscent[x]=true;
	for(i=0;i<vt[x].size();i++)
	{
		if(!iscent[vt[x][i].F])
		{
			solve(vt[x][i].F,x,d+1);
		}
	}
	return;
}
char buf[3];
pair<ll,ll> qry[N];
vector<pair<ll,ll> > upd[N],updfa[N];
vector<pair<pair<ll,ll>,ll> > eds;
ll st[N],ed[N],stfa[N],edfa[N];
int main(){
	ll i,j,k,x,y,w;
	scanf("%lld",&n);
	for(i=1;i<n;i++)
	{
		scanf("%lld%lld%lld",&x,&y,&w);
		x--,y--;
		vt[x].push_back(make_pair(y,w));
		vt[y].push_back(make_pair(x,w));
		eds.push_back(make_pair(make_pair(x,y),w));
	}
	predfs(0,-1);
	solve(0);
	for(i=0;i<n;i++)
	{
		for(j=0,x=i;x>=0;j++,x=fa[x])
		{
			fdis[j][i]=dist[i]+dist[x]-dist[glca(i,x)]*2;
		}
	}
	scanf("%lld",&q);
	for(i=0;i<q;i++)
	{
		scanf("%s",&buf);
		if(buf[0]=='+')
		{
			scanf("%lld%lld",&x,&w);
			x--;
			qry[i]=make_pair(x,w);
			for(y=x,j=0;y>=0;j++,y=fa[y])
			{
				if(w>=fdis[j][x])
				{
					upd[y].push_back(make_pair(w-fdis[j][x],y));
				}
				if(fa[y]>=0&&w>=fdis[j+1][x])
				{
					updfa[y].push_back(make_pair(w-fdis[j+1][x],y));
				}
			}
		}
		else
		{
			scanf("%lld",&x);
			x--;
			qry[i]=make_pair(x,-1);
		}
	}
	for(i=0;i<n;i++)
	{
		st[i]=(i==0?0:ed[i-1]+1);
		ed[i]=st[i]+upd[i].size()-1;
		sort(upd[i].begin(),upd[i].end());
		stfa[i]=(i==0?0:edfa[i-1]+1);
		edfa[i]=stfa[i]+updfa[i].size()-1;
		sort(updfa[i].begin(),updfa[i].end());
	}
	for(i=0;i<q;i++)
	{
		if(qry[i].S==-1)
		{
			x=eds[qry[i].F].F.F,y=eds[qry[i].F].F.S,w=eds[qry[i].F].S;
			ll ans=0;
			if(fadep[x]<fadep[y])
			{
				swap(x,y);
			}
			for(k=x,j=0;fa[k]!=y;j++,k=fa[k]);
			{
				pair<ll,ll> fd=make_pair(w,0);
				ans+=bitfa.qry(stfa[k]-1);
				ans-=bitfa.qry(lower_bound(updfa[k].begin(),updfa[k].end(),fd)-updfa[k].begin()+stfa[k]-1);
			}
			for(k=y,j=0;k>=0;j++,k=fa[k])
			{
				{
					pair<ll,ll> fd=make_pair(fdis[j][y]+w,0);
					ans+=bit.qry(ed[k]);
					ans-=bit.qry(lower_bound(upd[k].begin(),upd[k].end(),fd)-upd[k].begin()+st[k]-1);
				}
				if(fa[k]>=0)
				{
					pair<ll,ll> fd=make_pair(fdis[j+1][y]+w,0);
					ans+=bitfa.qry(edfa[k]);
					ans-=bitfa.qry(lower_bound(updfa[k].begin(),updfa[k].end(),fd)-updfa[k].begin()+stfa[k]-1);
				}
			}
			printf("%lld\n",ans);
		}
		else
		{
			x=qry[i].F,w=qry[i].S;
			for(y=x,j=0;y>=0;j++,y=fa[y])
			{
				if(w>=fdis[j][x])
				{
					pair<ll,ll> fd=make_pair(w-fdis[j][x],y);
					bit.upd(lower_bound(upd[y].begin(),upd[y].end(),fd)-upd[y].begin()+st[y],1);
				}
				if(fa[y]>=0&&w>=fdis[j+1][x])
				{
					pair<ll,ll> fd=make_pair(w-fdis[j+1][x],y);
					bitfa.upd(lower_bound(updfa[y].begin(),updfa[y].end(),fd)-updfa[y].begin()+stfa[y],-1);
				}
			}
		}
	}
	return 0;
}

详细

Test #1:

score: 0
Wrong Answer
time: 1553ms
memory: 100852kb

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
2
2
5
2
2
3
4
4
7
8
9
11
10
14
12
12
10
11
10
10
9
10
11
11
9
15
11
14
13
14
16
11
17
15
13
15
14
14
20
15
20
22
22
20
17
23
23
24
29
24
26
30
31
36
28
37
39
35
34
45
39
46
45
43
46
42
49
44
50
43
47
52
50
49
57
51
56
61
58
68
66
69
69
61
63
67
63
72
74
78
72
73
78
77
73
85
76
86
82
85
76
82
8...

result:

wrong answer 487th lines differ - expected: '434', found: '433'