社区讨论
样例过0pts求调(全WA)
P1505[国家集训队] 旅游参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @mk7syyg1
- 此快照首次捕获于
- 2026/01/10 12:26 上个月
- 此快照最后确认于
- 2026/01/10 14:20 上个月
CPP
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5;
int n,m,w[N],a[N],top[N],dep[N],father[N],sz[N],son[N],dfn[N],id[N],cnt;
vector <int> g[N];
struct node{
int l,r,sum,lazy,maxx,minn;
}tree[4*N];
pair <int,int> bian[N];
void build(int p,int l,int r){
tree[p]={l,r,0,0,LLONG_MIN,LLONG_MAX};
if(l==r){
tree[p].sum=tree[p].maxx=tree[p].minn=a[id[l]];
return ;
}
int mid=l+r>>1;
build(p<<1,l,mid);build(p<<1|1,mid+1,r);
tree[p].sum=tree[p<<1].sum+tree[p<<1|1].sum;
tree[p].maxx=max(tree[p<<1].maxx,tree[p<<1|1].maxx);
tree[p].minn=min(tree[p<<1].minn,tree[p<<1|1].minn);
}
void pushdown(int p){
if(tree[p].lazy==0) return ;
tree[p<<1].lazy^=1;
tree[p<<1|1].lazy^=1;
tree[p<<1].sum*=-1;tree[p<<1|1].sum*=-1;
tree[p<<1].maxx*=-1;tree[p<<1|1].maxx*=-1;
tree[p<<1].minn*=-1;tree[p<<1|1].minn*=-1;
swap(tree[p<<1].maxx,tree[p<<1].minn);
swap(tree[p<<1|1].maxx,tree[p<<1|1].minn);
tree[p].lazy=0;
}
void update1(int p,int x,int y,int k){
int l=tree[p].l;
int r=tree[p].r;
if(l>=x&&r<=y){
tree[p].sum=tree[p].maxx=tree[p].minn=k;
return ;
}
pushdown(p);
int mid=l+r>>1;
if(x<=mid) update1(p<<1,x,y,k);
if(y>mid) update1(p<<1|1,x,y,k);
tree[p].sum=tree[p<<1].sum+tree[p<<1|1].sum;
tree[p].maxx=max(tree[p<<1].maxx,tree[p<<1|1].maxx);
tree[p].minn=min(tree[p<<1].minn,tree[p<<1|1].minn);
}
void update2(int p,int x,int y){
int l=tree[p].l;
int r=tree[p].r;
if(l>=x&&r<=y){
tree[p].lazy^=1;
tree[p].sum*=-1;
tree[p].maxx*=-1;
tree[p].minn*=-1;
swap(tree[p].maxx,tree[p].minn);
return ;
}
pushdown(p);
int mid=l+r>>1;
if(x<=mid) update2(p<<1,x,y);
if(y>mid) update2(p<<1|1,x,y);
tree[p].sum=tree[p<<1].sum+tree[p<<1|1].sum;
tree[p].maxx=max(tree[p<<1].maxx,tree[p<<1|1].maxx);
tree[p].minn=min(tree[p<<1].minn,tree[p<<1|1].minn);
}
int query_sum(int p,int x,int y){
if(x<=tree[p].l&&tree[p].r<=y) return tree[p].sum;
pushdown(p);
int ans=0;
int mid=tree[p].l+tree[p].r>>1;
if(x<=mid) ans+=query_sum(p<<1,x,y);
if(y>mid) ans+=query_sum(p<<1|1,x,y);
tree[p].sum=tree[p<<1].sum+tree[p<<1|1].sum;
tree[p].maxx=max(tree[p<<1].maxx,tree[p<<1|1].maxx);
tree[p].minn=min(tree[p<<1].minn,tree[p<<1|1].minn);
return ans;
}
int query_max(int p,int x,int y){
if(x<=tree[p].l&&tree[p].r<=y) return tree[p].maxx;
pushdown(p);
int ans=LLONG_MIN;
int mid=tree[p].l+tree[p].r>>1;
if(x<=mid) ans=max(ans,query_max(p<<1,x,y));
if(y>mid) ans=max(ans,query_max(p<<1|1,x,y));
tree[p].sum=tree[p<<1].sum+tree[p<<1|1].sum;
tree[p].maxx=max(tree[p<<1].maxx,tree[p<<1|1].maxx);
tree[p].minn=min(tree[p<<1].minn,tree[p<<1|1].minn);
return ans;
}
int query_min(int p,int x,int y){
if(x<=tree[p].l&&tree[p].r<=y) return tree[p].minn;
pushdown(p);
int ans=LLONG_MAX;
int mid=tree[p].l+tree[p].r>>1;
if(x<=mid) ans=min(ans,query_min(p<<1,x,y));
if(y>mid) ans=min(ans,query_min(p<<1|1,x,y));
tree[p].sum=tree[p<<1].sum+tree[p<<1|1].sum;
tree[p].maxx=max(tree[p<<1].maxx,tree[p<<1|1].maxx);
tree[p].minn=min(tree[p<<1].minn,tree[p<<1|1].minn);
return ans;
}
void dfs1(int u,int fa){
dep[u]=dep[fa]+1,father[u]=fa,sz[u]=1;
for(auto v:g[u]){
if(v==fa) continue;
dfs1(v,u);sz[u]+=sz[v];
if(sz[v]>sz[son[u]]) son[u]=v;
}
}
void dfs2(int u,int ftop){
top[u]=ftop,dfn[++cnt]=u,id[u]=cnt,a[cnt]=w[cnt-1];
if(son[u]) dfs2(son[u],ftop);
for(auto v:g[u])
if(v!=son[u]&&v!=father[u]) dfs2(v,v);
}
void fan(int u,int v){
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]]) swap(u,v);
update2(1,id[top[u]],id[u]);
u=father[top[u]];
}
if(dep[u]>dep[v]) swap(u,v);
update2(1,id[u],id[v]);
}
void work_sum(int u,int v){
int ans=0;
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]]) swap(u,v);
ans+=query_sum(1,id[top[u]],id[u]);
u=father[top[u]];
}
if(dep[u]>dep[v]) swap(u,v);
if(u!=v) ans+=query_sum(1,id[u]+1,id[v]);
printf("%lld\n",ans);
}
void work_max(int u,int v){
int ans=LLONG_MIN;
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]]) swap(u,v);
ans=max(ans,query_max(1,id[top[u]],id[u]));
u=father[top[u]];
}
if(dep[u]>dep[v]) swap(u,v);
if(u!=v) ans=max(ans,query_max(1,id[u]+1,id[v]));
printf("%lld\n",ans);
}
void work_min(int u,int v){
int ans=LLONG_MAX;
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]]) swap(u,v);
ans=min(ans,query_min(1,id[top[u]],id[u]));
u=father[top[u]];
}
if(dep[u]>dep[v]) swap(u,v);
if(u!=v) ans=min(ans,query_min(1,id[u]+1,id[v]));
printf("%lld\n",ans);
}
signed main(){
scanf("%lld",&n);
for(int i=1,u,v,ww;i<n;i++){
scanf("%lld%lld%lld",&u,&v,&ww);
u++,v++;
g[u].push_back(v);
g[v].push_back(u);
w[i]=ww;
bian[i]={u,v};
}
scanf("%lld",&m);
dfs1(1,0);dfs2(1,1);build(1,1,n);
while(m--){
string op;int x,y;
cin>>op;scanf("%lld%lld",&x,&y);x++,y++;
if(op=="C"){
x--,y--;
int u=bian[x].first,v=bian[x].second;
if(dep[u]<dep[v]) swap(u,v);
update1(1,id[u],id[u],y);
}
else if(op=="N") fan(x,y);
else if(op=="MAX") work_max(x,y);
else if(op=="MIN") work_min(x,y);
else work_sum(x,y);
}
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...