题意:链接
方法:树链剖分
解析:搞得要死要活的一道题-.-
好吧,一棵树,三种操作:
第一种是把某个边的值更改为x;
第二种是将x节点到y节点的路上所有的边的值变为原来的相反数
第三种询问x节点到y节点的路上的边的最大值。
注:定义每个点的值是其父节点到该点的边的权值
先说第一种操作,挺好搞的,因为加的是双向边,所以题中给的编号乘2加1就是找的这条边,再确定一下哪个点是要更改的点就好,然后直接单点更改。
第二种就是一个异或标记,也是挺正常的区间更改
注:线段树维护区间最值的话,就得搞个最大值最小值,然后更新的话就是相当于最大值变为了最小值的相反数,最小值变成了最大值的相反数。相当于把一条线段翻转。
询问操作没啥说的了,不写挂就ok:)
后记:哦天哪写这道题刚开始线段树单点修改忘更新,rp–,然后又是线段树维护的东西跟树节点编号混了一下,rp–,然后是链剖的更新的时候多更新了个父节点!!!!最后交题的时候忘删freopen!!我tm还看了半天,然后发现没删freopen,日了狗了!
代码:(附poj discuss中Ever_ljq的对拍程序)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 100010
using namespace std ;
int t,n,tot;
int siz[N],dep[N],num[N],rnk[N];
int fa[N],son[N],tim[N],top[N],a[N];
int head[N],M[4*N],col[4*N],m[4*N];
char s[10];
struct node
{
int to,next,val,from;
}edge[2*N];
int cnt ;
void init()
{
memset(head,-1,sizeof(head));
memset(son,-1,sizeof(son));
memset(M,0,sizeof(M));
memset(m,0,sizeof(m));
cnt=1,tot=0;
}
void edgeadd(int from,int to,int val)
{
edge[cnt].from=from,edge[cnt].to=to,edge[cnt].next=head[from],edge[cnt].val=val,head[from]=cnt++;
edge[cnt].from=to,edge[cnt].to=from,edge[cnt].next=head[to],edge[cnt].val=val,head[to]=cnt++;
}
void dfs1(int u,int f,int d)
{
siz[u]=1,dep[u]=d,fa[u]=f;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if(to==f)continue;
a[to]=edge[i].val;
dfs1(to,u,d+1);
siz[u]+=siz[to];
if(son[u]==-1||siz[son[u]]<siz[to])son[u]=to;
}
}
void dfs2(int u,int tp)
{
tim[u]=++tot,num[tim[u]]=a[u],top[u]=tp;
if(son[u]==-1)return;
dfs2(son[u],tp);
for(int i=head[u];i!=-1;i=edge[i].next)
{
int to=edge[i].to;
if(to==fa[u]||to==son[u])continue;
dfs2(to,to);
}
}
void pushup(int rt)
{
M[rt]=max(M[rt<<1],M[rt<<1|1]);
m[rt]=min(m[rt<<1],m[rt<<1|1]);
}
void pushdown(int rt)
{
col[rt]=0;
col[rt<<1]^=1,col[rt<<1|1]^=1;
M[rt<<1]*=-1,M[rt<<1|1]*=-1;
m[rt<<1]*=-1,m[rt<<1|1]*=-1;
swap(M[rt<<1],m[rt<<1]);
swap(M[rt<<1|1],m[rt<<1|1]);
}
void build(int l,int r,int rt)
{
col[rt]=0;
if(l==r)
{
M[rt]=m[rt]=num[l];
return;
}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
pushup(rt);
}
void update(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
col[rt]^=1;
M[rt]*=-1,m[rt]*=-1;
swap(M[rt],m[rt]);
return ;
}
int mid=(l+r)>>1;
if(col[rt])pushdown(rt);
if(L<=mid)update(L,R,l,mid,rt<<1);
if(R>mid)update(L,R,mid+1,r,rt<<1|1);
pushup(rt);
}
void update2(int x,int v,int l,int r,int rt)
{
if(l==r&&l==x)
{
M[rt]=m[rt]=v;
col[rt]=0;
return ;
}
int mid=(l+r)>>1;
if(col[rt])pushdown(rt);
if(x<=mid)update2(x,v,l,mid,rt<<1);
if(x>mid)update2(x,v,mid+1,r,rt<<1|1);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return M[rt];
}
int mid=(l+r)>>1;
if(col[rt])pushdown(rt);
int ret=-0x3f3f3f3f;
if(L<=mid)ret=max(ret,query(L,R,l,mid,rt<<1));
if(R>mid)ret=max(ret,query(L,R,mid+1,r,rt<<1|1));
pushup(rt);
return ret ;
}
void change(int x,int y)
{
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])swap(x,y);
update(tim[top[x]],tim[x],1,n,1);
x=fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
if(x!=y)
update(tim[x]+1,tim[y],1,n,1);
}
int ask(int x,int y)
{
int ans=-0x3f3f3f3f;
while(top[x]!=top[y])
{
if(dep[top[x]]<dep[top[y]])swap(x,y);
ans=max(ans,query(tim[top[x]],tim[x],1,n,1));
x=fa[top[x]];
}
if(dep[x]>dep[y])swap(x,y);
if(x!=y)ans=max(ans,query(tim[x]+1,tim[y],1,n,1));
return ans;
}
int main()
{
scanf("%d",&t);
while(t--)
{
init();
scanf("%d",&n);
int x,y,val;
for(int i=1;i<=n-1;i++){scanf("%d%d%d",&x,&y,&val);edgeadd(x,y,val);}
dfs1(1,0,1);
dfs2(1,0);
build(1,n,1);
while(scanf("%s",s)&&s[0]!='D')
{
int x,y;
scanf("%d%d",&x,&y);
if(s[0]=='Q')
{
printf("%d\n",ask(x,y));
}else if(s[0]=='N')
{
change(x,y);
}else
{
x=x*2-1;
if(dep[edge[x].from]>dep[edge[x].to])swap(edge[x].from,edge[x].to);
update2(tim[edge[x].to],y,1,n,1);
}
}
}
}
对拍程序:(嫌点大自己该小就好)
using namespace std;
FILE *fin, *fout;
const int n = 1000, m = 1000;
int adj[n + 5][n + 5];
int ex[n + 5], ey[n + 5];
int fat[n + 5], num[n + 5], last[n + 5], ans;
int vis[n + 5];
int find(int x)
{
if (fat[x] != x) fat[x] = find(fat[x]); return fat[x];
}
void dfs(int t, int u)
{
last[t] = u;
for (int i = 1; i <= n; i++)
if (adj[t][i] != 0 && i != u) dfs(i, t);
}
int lca(int x, int y)
{
for (int i = 1; i <= n; i++) vis[i] = 0;
while (x){
vis[x] = 1; x = last[x];
}
while (!vis[y]) y = last[y];
return y;
}
void tree_change(int t, int u)
{
while (t != u){
adj[t][last[t]] = adj[last[t]][t] = -adj[t][last[t]]; t = last[t];
}
}
void tree_calc(int t, int u)
{
while (t != u){
if (adj[last[t]][t] > ans) ans = adj[last[t]][t];
t = last[t];
}
}
int main()
{
fin = fopen("tt.in", "w");
fout = fopen("force.out", "w");
int i, u, v, c, j, k;
fprintf(fin, "10\n");
for (int w = 1; w <=10 ; w++){
memset(adj, 0, sizeof(adj));
memset(last, 0, sizeof(last));
memset(fat, 0, sizeof(fat));
memset(ex, 0, sizeof(ex));
memset(ey, 0, sizeof(ey));
memset(num, 0, sizeof(num));
memset(vis, 0, sizeof(vis));
fprintf(fin, "%d\n", n);
for (i = 1; i <= n; i++) fat[i] = i;
srand(time(0));
for (i = 1; i < n; i++)
while (1){
u = rand() % n + 1; v = rand() % n + 1;
if (find(u) == find(v)) continue; c = rand() - rand(); if (c == 0) c++;
fat[find(v)] = fat[u]; adj[u][v] = adj[v][u] = c; ex[i] = u; ey[i] = v;
fprintf(fin, "%d %d %d\n", u, v, c); break;
}
dfs(1, 0);
for (i = 1; i <= m; i++){
k = rand() % 4;
if (k == 0){
u = rand() % (n - 1) + 1; c = rand() - rand(); if (c == 0) c++;
fprintf(fin, "CHANGE %d %d\n", u, c); adj[ex[u]][ey[u]] = adj[ey[u]][ex[u]] = c;
} else if (k == 1){
u = rand() % n + 1; v = rand() % n + 1;
while (u == v) v = rand() % n + 1; j = lca(u, v);
fprintf(fin, "NEGATE %d %d\n", u, v);
tree_change(u, j); tree_change(v, j);
} else {
u = rand() % n + 1; v = rand() % n + 1;
while (u == v) v = rand() % n + 1; j = lca(u, v);
fprintf(fin, "QUERY %d %d\n", u, v); ans = 1 << 30; ans = -ans;
tree_calc(u, j); tree_calc(v, j);
fprintf(fout, "%d\n", ans);
}
}
fprintf(fin, "DONE\n");
}
fclose(fin); fclose(fout);
return 0;
}