HDU 3966 Aragorn's Story(树链剖分+线段树+入点)

题意:

 I,从 a-》b的路线上+c

D,从a->b的路线上+c

Q,   点权和

思路:

树链剖分。第一次学。


 

#pragma comment(linker, "/STACK:102400000,102400000")
#include 
#include 
#include 
#include 

using namespace std;
const int maxn = 50005;
int val_pre[maxn],val[maxn],siz[maxn],son[maxn],id[maxn],fa[maxn],top[maxn],dep[maxn];
int topw;
int M,N,P;
vector vec[maxn];
void dfs1(int u,int f,int d) //第一次搜索建立父子关系,以及节点个数关系
{
    siz[u]=1;
    fa[u]=f;
    dep[u]=d;
    son[u]=0;
    for(int i=0;i>1;
    build(i<<1,left,mid);
    build(i<<1|1,mid+1,right);
}
void push_down(int i)
{
    if(tree[i].lazy)
    {
        tree[i<<1].lazy+=tree[i].lazy;
        tree[i<<1|1].lazy+=tree[i].lazy;
        tree[i<<1].sum+=(tree[i<<1].right-tree[i<<1].left+1)*tree[i].lazy;
        tree[i<<1|1].sum+=(tree[i<<1|1].right-tree[i<<1|1].left+1)*tree[i].lazy;
        tree[i].lazy=0;
    }
    return ;
}
void update(int i,int left,int right,int w)
{
    if(tree[i].left>=left&&tree[i].right<=right)
    {
        tree[i].lazy+=w;
        tree[i].sum+=w*(tree[i].right-tree[i].left+1);
        return ;
    }
    if(tree[i].lazy!=0)
    {
        push_down(i);
    }
    int mid=(tree[i].left+tree[i].right)>>1;
    if(left<=mid)
        update(i<<1,left,right,w);
    if(right>mid)
        update(i<<1|1,left,right,w);
}

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

void change(int u,int v,int addc)
{
    int fu = top[u],fv = top[v];
    //swap语句保证深度小的在左侧,大的在右侧
    while(fu != fv)    //直到同一条重链退出while
    {   
        if(dep[fu] < dep[fv])
        {
            swap(u,v);swap(fu,fv);
        }
        update(1,id[fu],id[u],addc);
        u = fa[fu];
        fu = top[u];
    }

    if(dep[u] > dep[v]) swap(u,v);
        update(1,id[u],id[v],addc);
    return ;
}

int main()
{ 
    while(~scanf("%d%d%d",&N,&M,&P))
    {
        for(int i=0;i

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