poj3237 树链剖分 暴力

时间:2022-06-16 02:07:19

    NEGATE a,b 将a b间的线段取反,这题应该用线段树+成段更新。我成段更新写的挫了,试了暴力修改过了(数据水)。

也是简单的题目。不过要注意点和边的区别。

#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 99999999
#define ll __int64
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN = ;
struct node{
int to;
int v;
int next;
}edge[MAXN*];
int ind,pre[MAXN],top[MAXN],fa[MAXN],siz[MAXN],son[MAXN],w[MAXN],deq[MAXN],fn;
int n,tree[MAXN<<],val[MAXN][],mark[MAXN<<];
void add(int x,int y,int z)
{
edge[ind].to = y;
edge[ind].v = z;
edge[ind].next = pre[x];
pre[x] = ind++;
}
void dfs1(int rt,int pa,int d)
{
deq[rt] = d;
siz[rt] = ;
fa[rt] = pa;
son[rt] = ;
for(int i=pre[rt]; i!=-; i=edge[i].next){
int t = edge[i].to;
if(t != fa[rt]){
dfs1(t,rt,d+);
siz[rt] += siz[t];
if(siz[son[rt]] < siz[t]){
son[rt] = t;
}
}
}
}
void dfs2(int rt,int tp)
{
top[rt] = tp;
w[rt] = ++fn;
if(son[rt] != ){
dfs2(son[rt],tp);
} for(int i=pre[rt]; i!=-; i=edge[i].next){
int t = edge[i].to;
if(t != fa[rt] && t != son[rt]){
dfs2(t,t);
}
}
}
void pushup(int rt)
{
tree[rt] = max(tree[rt<<],tree[rt<<|]);
}
/*void pushdown(int rt)
{
if(mark[rt] != 0){
if(mark[rt]%2){
tree[rt<<1] *= -1;
tree[rt<<1|1] *= -1;
}
mark[rt<<1] += mark[rt];
mark[rt<<1|1] += mark[rt];
mark[rt] = 0;
}
}*/
void updata(int p,int v,int l,int r,int rt)
{
if(l == r){
tree[rt] = v;
return ;
} int m = (l+r)/;
if(m >= p){
updata(p,v,lson);
}
else {
updata(p,v,rson);
}
pushup(rt);
} void seg_updata(int L,int R,int l,int r,int rt)
{
if(l == r){
tree[rt] *= -;
return ;
}
int m = (l+r)/;
if(m >= L){
seg_updata(L,R,lson);
}
if(m < R){
seg_updata(L,R,rson);
}
pushup(rt);
}
void updata_all(int x,int y)
{
while(top[x] != top[y])
{
if(deq[top[x]] < deq[top[y]]){
swap(x,y);
}
seg_updata(w[top[x]],w[x],,fn,);
x = fa[top[x]];
}
if(x == y){
return ;
}
if(deq[x] < deq[y]){
swap(x,y);
}
seg_updata(w[son[y]],w[x],,fn,);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l && r<=R){
return tree[rt];
}
int m = (l+r)/;
int ans = -INF;
if(m >= L){
ans = max(ans,query(L,R,lson));
}
if(m < R){
ans = max(ans,query(L,R,rson));
}
return ans;
}
int lca(int x,int y)
{
int ans = -INF;
while(top[x] != top[y])
{
if(deq[top[x]] < deq[top[y]]){
swap(x,y);
}
ans = max(ans,query(w[top[x]],w[x],,fn,));
x = fa[top[x]];
}
if(x == y)
return ans;
if(deq[x] < deq[y]){
swap(x,y);
}
ans = max(ans,query(w[son[y]],w[x],,fn,));
return ans;
}
int main()
{
int t,i,j;
scanf("%d",&t);
while(t--)
{
ind = ;
memset(pre,-,sizeof(pre));
scanf("%d",&n);
for(i=; i<n; i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
val[i][] = x;
val[i][] = y;
val[i][] = z;
}
dfs1(,,);
fn = ;
dfs2(,);
for(i=; i<n; i++){
if(deq[val[i][]] < deq[val[i][]]){
swap(val[i][],val[i][]);
}
updata(w[val[i][]],val[i][],,fn,);
}
char s[];
memset(mark,,sizeof(mark));
while()
{
scanf("%s",s);
if(s[] == 'D')
break;
if(s[] == 'C'){
int x,y;
scanf("%d%d",&x,&y);
updata(w[val[x][]],y,,fn,);
}
else if(s[] == 'N'){
int x,y;
scanf("%d%d",&x,&y);
updata_all(x,y);
}
else {
int x,y;
scanf("%d%d",&x,&y);
printf("%d\n",lca(x,y));
}
}
}
}