hdu 3804树链剖分+离线操作

/*
树链刨分+离线操作
题意:给你一棵树,和询问x,y
  从节点x--节点1的小于等于y的最大值.
  解:先建一个空树,将树的边权值从小到大排序,将询问y按从小到大排序
  对于每次询问y将小于等于y的边权值的边加入,在进行询问将结果储存最后输出即可
  易错点:要考虑到节点1到节点1的情况需特判。
*/
#pragma comment(linker, "/STACK:102400000,102400000")
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define N  110000
#define inf 0x3fffffff
int f[N];
int top[N];
int fa[N];
int siz[N];
int nu,yong;
int head[N];
int deep[N];
int son[N];
int w[N];
int Max;
struct node
{
    int u,v,w,next,we;
} bian[N*4],ff[N],fy[N];
void init()
{
    yong=nu=0;
    memset(head,-1,sizeof(head));
    memset(son,-1,sizeof(son));
}
void addedge(int u,int v,int w)
{
    bian[yong].u=u;
    bian[yong].v=v;
    bian[yong].w=w;
    bian[yong].next=head[u];
    head[u]=yong++;
}
void dfs(int u,int father,int d)
{
    deep[u]=d;
    fa[u]=father;
    siz[u]=1;
    int i;
    for(i=head[u]; i!=-1; i=bian[i].next)
    {
        int v=bian[i].v;
        if(v!=father)
        {
            dfs(v,u,d+1);
            siz[u]+=siz[v];
            if(son[u]==-1||siz[son[u]]<siz[v])
                son[u]=v;
        }
    }
    return ;
}
void getnu(int u,int cnt)
{
    f[u]=nu++;
    top[u]=cnt;
    if(son[u]==-1)return ;
    getnu(son[u],cnt);
    int i;
    for(i=head[u]; i!=-1; i=bian[i].next)
    {
        int v=bian[i].v;
        if(v!=son[u]&&v!=fa[u])
            getnu(v,v);
    }
    return ;
}
int cmp(const void *a,const void *b)
{
    return (*(struct node *)a).v-(*(struct node *)b).v;
}
int cmpp(const void *a,const void *b)
{
    return (*(struct node *)a).w-(*(struct node *)b).w;
}
struct nodee
{
    int l,r,maxx;
} tree[N*4];
int Ma(int v,int vv)
{
    return v>vv?v:vv;
}
void pushup(int t)
{
    tree[t].maxx=Ma(tree[t*2].maxx,tree[t*2+1].maxx);
}
void build(int t,int l,int r)
{
    tree[t].l=l;
    tree[t].r=r;
    if(tree[t].l==tree[t].r)
    {
        tree[t].maxx=-1;
        return ;
    }
    int mid=(tree[t].l+tree[t].r)/2;
    build(t*2,l,mid);
    build(t*2+1,mid+1,r);
    pushup(t);
}
void update(int t,int x,int y)
{
    if(tree[t].l==x&&tree[t].r==x)
    {
        tree[t].maxx=y;
        return ;
    }
    int mid=(tree[t].l+tree[t].r)/2;
    if(x<=mid)update(t*2,x,y);
    else
        update(t*2+1,x,y);
    pushup(t);
}
void qury(int t,int l,int r)
{
    if(tree[t].l==l&&tree[t].r==r)
    {
        Max=Ma(Max,tree[t].maxx);
        return ;
    }
    int mid=(tree[t].l+tree[t].r)/2;
    if(r<=mid)qury(t*2,l,r);
    else if(l>mid)qury(t*2+1,l,r);
    else
    {
        qury(t*2,l,mid);
        qury(t*2+1,mid+1,r);
    }
    pushup(t);
}
int findmax(int u,int v)
{
    if(u==v)return -1;//特判因为可能出现节点1到节点1的情况是不存在的
    int f1=top[u];
    int f2=top[v];
    int ans=-inf;
    while(f1!=f2)
    {
        if(deep[f1]<deep[f2])
        {
            swap(f1,f2);
            swap(u,v);
        }
        Max=-inf;
        qury(1,f[f1],f[u]);
        ans=Ma(ans,Max);
        u=fa[f1];
        f1=top[u];
    }
    if(u==v)return ans;
    if(deep[u]>deep[v]) swap(u,v);
    Max=-inf;
    qury(1,f[son[u]],f[v]);
    ans=Ma(ans,Max);
    return ans;
}
int main()
{
    int n,i,j,k,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        init();
        for(i=1; i<n; i++)
        {
            scanf("%d%d%d",&ff[i].u,&ff[i].v,&ff[i].w);
            addedge(ff[i].u,ff[i].v,ff[i].w);
            addedge(ff[i].v,ff[i].u,ff[i].w);
        }
        dfs(1,1,0);//得到deep,fa,siz,son数组值
        getnu(1,1);//得到f,top的值
        for(i=1; i<n; i++)
        {
            if(deep[ff[i].u]<deep[ff[i].v])
                swap(ff[i].u,ff[i].v);
            w[ff[i].u]=ff[i].w;
        }
        build(1,1,nu-1);//建空树
        scanf("%d",&k);
        for(i=1; i<=k; i++)
        {
            scanf("%d%d",&fy[i].u,&fy[i].v);
            fy[i].w=i;//记录下标
        }
        qsort(fy+1,k,sizeof(fy[0]),cmp);//排序
        qsort(ff+1,n-1,sizeof(ff[0]),cmpp);
        for(i=1,j=1; i<=k; i++)
        {
            for(; j<n;)
            {
                if(fy[i].v>=ff[j].w)
                    update(1,f[ff[j].u],ff[j].w);//加边
                else  break;
                j++;
            }
            fy[i].we=findmax(1,fy[i].u);//查找
        }
        qsort(fy+1,k,sizeof(fy[0]),cmpp);//再次按下标排序
        for(i=1; i<=k; i++)//输出
            printf("%d\n",fy[i].we);
    }
    return 0;
}

时间: 2024-10-13 07:17:05

hdu 3804树链剖分+离线操作的相关文章

Hdu 3804 树链剖分 第5遍

不能原谅自己的错误..还怀疑人家的数据错了,瞬间感觉自己弱爆了 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstring> #include<algorithm> #define lson id << 1 #define rson id << 1|1 #include<cstdio> usi

hdu 5893 (树链剖分+合并)

List wants to travel Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 429    Accepted Submission(s): 92 Problem Description A boy named List who is perfect in English. Now he wants to travel an

hdu 5052 树链剖分

Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 982    Accepted Submission(s): 274 Problem Description Yaoge likes to eat chicken chops late at night. Yaoge has eaten too

hdu 5242 树链剖分找权值最大的前k条链

http://acm.hdu.edu.cn/showproblem.php?pid=5242 Problem Description It is well known that Keima Katsuragi is The Capturing God because of his exceptional skills and experience in ''capturing'' virtual girls in gal games. He is able to play k games sim

hdu 5274 树链剖分

Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1484    Accepted Submission(s): 347 Problem Description Dylans is given a tree with N nodes. All nodes have a value A[i].Nodes

hdu 4897 树链剖分(重轻链)

Little Devil I Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 894    Accepted Submission(s): 296 Problem Description There is an old country and the king fell in love with a devil. The devil

HDU 3966 (树链剖分+线段树)

Problem Aragorn's Story (HDU 3966) 题目大意 给定一颗树,有点权. 要求支持两种操作,将一条路径上的所有点权值增加或减少ai,询问某点的权值. 解题分析 树链剖分模板题. 实质上树链剖分进行了点对点的一次映射,保证了重链上的点在线段树上的位置是连续的. 树链剖分的两个性质(转): 性质1:如果(v,u)为轻边,则siz[u] * 2 < siz[v]: 性质2:从根到某一点的路径上轻链.重链的个数都不大于logn. 保证了一个区间的时间复杂度是log2(n).

HDU 5274(树链剖分)

树链剖分第一题QAQ,纪念下 #pragma comment(linker, "/STACK:102400000,102400000") #include <iostream> #include <cstdio> #include <cstring> using namespace std; typedef long long ll; const ll mod = 1e9 + 7; const int maxn = 1e5 + 10; #define

HDU 5029 树链剖分

Relief grain Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 861    Accepted Submission(s): 219 Problem Description The soil is cracking up because of the drought and the rabbit kingdom is f