POJ 3237 Tree(树链剖分-线段树点更新-区间更新-区间最值查询-入边)

题意:

给出若干个操作,询问路径最大值。

思路:

裸树链剖分,只不过需要记录最大值和最小值,当翻转的时候可以标记一下,然后互换最大值和最小值。

PS:

WA了5个小时,就因为线段树函数太多。。有个函数忘记释放标记了。真是TM的智障


#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 1000005;

int dep[maxn],siz[maxn],fa[maxn],id[maxn],son[maxn],val[maxn],top[maxn];
int topw,head[maxn],cnt;
///dep 深度 siz个数 fa父 top祖 son重儿子 val值
/// e数组 初始入边  edge数组存边
struct Edge
{
    int to,next;
} edge[maxn*2];
struct treex
{
    int x,y,val;

} e[maxn];
void add(int u,int v )
{
    edge[cnt].to=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void dfs1(int u,int f,int d)
{
    dep[u]=d;
    siz[u]=1;
    son[u]=0;
    fa[u]=f;
    for(int i =head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(v==fa[u])
            continue;
        dfs1(v,u,d+1);
        siz[u]+=siz[v];
        if(siz[son[u]]>1;
    build(i<<1,left,mid);
    build(i<<1|1,mid+1,right);
    push_up(i);
}
void push_down(int i)
{
    if(tree[i].lazy%2)
    {
        tree[i<<1].lazy++;
        tree[i<<1|1].lazy++;
        turn(i<<1);
        turn(i<<1|1);

        tree[i].lazy=0;
    }


}
void update(int i,int aim,int w)
{

    if(tree[i].left==tree[i].right&&tree[i].left==aim)
    {
        tree[i].maxx=w;
        tree[i].minn=w;
        tree[i].lazy=0;
        return ;
    }
    if(tree[i].lazy )
    {
        push_down(i);
    }
    int mid=(tree[i].left+tree[i].right)>>1;
    if(aim<=mid)
        update(i<<1,aim,w);
    else
        update(i<<1|1,aim,w);
    push_up(i);
}
void  rev(int i,int left,int right)
{
    if(tree[i].left==left&&tree[i].right==right)
    {
        tree[i].lazy+=1;
        turn(i);
        return ;
    }
    if(tree[i].lazy)
        push_down(i);
    int mid=(tree[i].left+tree[i].right)>>1;
    if(right<=mid)
        rev(i<<1,left,right);
    else if(left>mid)
        rev(i<<1|1,left,right);
    else
        rev(i<<1,left,mid),rev(i<<1|1,mid+1,right);
    push_up(i);
}
int  query(int i,int left,int right)
{
    if(tree[i].left>=left&&tree[i].right<=right)
    {
        return tree[i].maxx;
    }

    if(tree[i].lazy)
        push_down(i);
    int mid=(tree[i].left+tree[i].right)>>1;
    if(right<=mid)
        return query(i<<1,left,right);
    else if(left>mid)
        return query(i<<1|1,left,right);
    else
        return max(query(i<<1,left,mid),query(i<<1|1,mid+1,right));
}

void  change(int u,int v,int op)
{
    int tpu=top[u],tpv=top[v];
    int ans=-0x3f3f3f3f;
    while(tpu!=tpv)
    {
        if(dep[tpu]dep[v])
            swap(u,v);
        if(op==1)
            ans=max(ans,query(1,id[son[u]],id[v]));
        else
            rev(1,id[son[u]],id[v]);
    }
    if(op==1)
        printf("%d\n",ans);
}
int main()
{
    int n,m;
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(head,-1,sizeof(head));
        for(int i=1;i


你可能感兴趣的:(poj,acm)