QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#188926#5249. Bandits275307894aTL 0ms0kbC++142.6kb2023-09-26 16:54:332023-09-26 16:54:33

Judging History

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

  • [2023-09-26 16:54:33]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2023-09-26 16:54:33]
  • 提交

answer

#include<bits/stdc++.h>
#define Gc() getchar() 
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;using LL=__int128;
const int N=2e5+5,M=N*20+5,K=600+5,mod=998244353,Mod=mod-1;const db eps=1e-9;const int INF=1e9+7;mt19937 rnd(263082);
int n,m,k,x,y,Z[N],ans[N];vector<pii> S[N],q2[N];vector<int> q1[N];
int vis[N],Si[N],dp[N],Ct,Rt;
void GI(int x,int La){Si[x]=1;Ct++;for(auto i:S[x]) if(i.fi^La&&!vis[i.fi]) GI(i.fi,x),Si[x]+=Si[i.fi];}
void DP(int x,int La){dp[x]=Ct-Si[x];for(auto i:S[x]) if(i.fi^La&&!vis[i.fi]) DP(i.fi,x),dp[x]=max(dp[x],Si[i.fi]);if(dp[x]<dp[Rt]) Rt=x;}
namespace BIT{
	int f[N];void add(int x,int y=1){while(x<=m) f[x]+=y,x+=x&-x;}int qry(int x){int ans=0;while(x) ans+=f[x],x-=x&-x;return ans;}
}
vector<pair<ll,int> > A,B;
ll Sum[N];
void Make(int x,int La){for(auto i:S[x]) if(i.fi^La&&!vis[i.fi]) Sum[i.fi]=Sum[x]+Z[i.se],Make(i.fi,x);}
void init(int x,int La){
	for(auto i:q2[x]) A.emplace_back(i.fi-Sum[x],i.se);
	for(auto i:S[x]) if(i.fi^La&&!vis[i.fi]){
		for(auto j:q1[i.se]) B.emplace_back(Sum[i.fi],j);
		init(i.fi,x);
	}
}
void calc(int w){
	sort(A.begin(),A.end());sort(B.begin(),B.end());
	reverse(A.begin(),A.end());reverse(B.begin(),B.end());
	int R=0;for(auto i:B){
		while(R<A.size()&&A[R].fi>=i.fi) BIT::add(A[R].se),R++;
		ans[i.se]+=BIT::qry(i.se)*w;
	}
	for(int i=0;i<R;i++) BIT::add(A[i].se,-1);
	// cerr<<"w "<<w<<'\n';
}
void dfs(int x){
	GI(x,0);dp[Rt=0]=INF;DP(x,0);vis[x=Rt]=1;//cerr<<x<<'\n';
	Sum[x]=0;Make(x,0);A.clear();B.clear();init(x,0);calc(1);cerr<<ans[2]<<'\n';
	for(auto i:S[x]) if(!vis[i.fi]){
		A.clear(),B.clear(),init(i.fi,x);for(auto j:q1[i.se]) B.emplace_back(Sum[i.fi],j);calc(-1);cerr<<i.fi<<' '<<ans[2]<<'\n';
		B.clear();for(auto j:q1[i.se]) B.emplace_back(0ll,j);calc(1);cerr<<i.fi<<' '<<ans[2]<<'\n';
	} 
	for(auto i:S[x]) if(!vis[i.fi]) dfs(i.fi);
}
void Solve(){
	int i,j;scanf("%d",&n);
	for(i=1;i<n;i++) scanf("%d%d%d",&x,&y,&Z[i]),S[x].emplace_back(y,i),S[y].emplace_back(x,i);
	scanf("%d",&m);for(i=1;i<=m;i++){
		char c;int x,y;cin>>c;scanf("%d",&x);
		if(c=='?') q1[x].emplace_back(i);
		else scanf("%d",&y),q2[x].emplace_back(y,i),ans[i]=-1;
	}
	dfs(1);
	for(i=1;i<=m;i++) if(~ans[i]) printf("%d\n",ans[i]);
}
int main(){
	int t=1;
	// scanf("%d",&t);
	while(t--) Solve();
	cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

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:


result: