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 to first
#define next second
#define foreach(it,v) for(__typeof(v.begin()) it = v.begin(); it != v.end(); ++it)
int pos[maxn],head[maxn],fa[maxn],son[maxn];
int a[maxn],siz[maxn],root[maxn];
typedef pair<int,int> Edge;
Edge edges[maxn<<1];
int e,tot;
void AddEdge(int u,int v)
{
    edges[++e] = make_pair(v,head[u]);head[u] = e;
    edges[++e] = make_pair(u,head[v]);head[v] = e;
}
void pre(int u)
{
    siz[u] = 1;son[u] = 0;
    for(int i = head[u]; i ; i = edges[i].next) {
        int v = edges[i].to;
        if(v == fa[u])continue;
        fa[v] = u;
        pre(v);
        siz[u] += siz[v];
        if(siz[son[u]] < siz[v]) son[u] = v;
    }
}
void built(int u,int t)
{
    root[u] = t;
    pos[u] = ++tot;
    if(son[u] < 1) return;
    built(son[u],t);
    for(int i = head[u]; i ; i = edges[i].next) {
        int v = edges[i].to;
        if(v == fa[u] || v == son[u])continue;
        built(v,v);
    }
}
int seg[maxn<<2];
int ql,qr,x,v;
void Modify(int o,int L,int R)
{
    if(L == R) {
        seg[o] = v;
        return ;
    }
    int mid = (L+R)>>1;
    if(x<=mid) Modify(o<<1,L,mid);
    else Modify(o<<1|1,mid+1,R);
    seg[o] = seg[o<<1] ^ seg[o<<1|1];
}
int Queryseg(int o,int L,int R)
{
    if(ql<=L&&qr>=R) return seg[o];
    int mid = (L+R) >> 1;
    int res = 0;
    if(ql <= mid) res = Queryseg(o<<1,L,mid);
    if(qr > mid) res ^= Queryseg(o<<1|1,mid+1,R);
    return res;
}
int solve(int u,int v)
{
    int res = 0;
    while(root[u] != root[v]) {
        if(pos[root[u]] < pos[root[v]]) swap(u,v);
        ql = pos[root[u]],qr = pos[u];
        res ^= Queryseg(1,1,tot);
        u = fa[root[u]];
    }
    ql = pos[u],qr = pos[v];
    if(ql > qr) swap(ql,qr);
    res ^= Queryseg(1,1,tot);
    return res;
}
int main(int argc, char const *argv[])
{
    int T;scanf("%d",&T);
    while(T--) {
        int N,Q;scanf("%d%d",&N,&Q);
        e = tot = 0;
        memset(seg,0,sizeof seg);
        memset(head,0,sizeof(head[0])*(N+1));
        for(int i = 1; i < N; i++) {
            int u,v;scanf("%d%d",&u,&v);
            AddEdge(u,v);
        }
        fa[1] = siz[0] = 0;
        pre(1);
        built(1,1);
        for(int i = 1; i <= N; i++) {
            scanf("%d",&v); ++v; x = pos[i];
            Modify(1,1,tot);
        }
        while(Q--) {
            int op;scanf("%d",&op);
            if(op==0) {
                scanf("%d%d",&x,&v);x = pos[x]; ++v;
                Modify(1,1,tot);
            } else {
                scanf("%d%d",&ql,&qr);
                int ans = solve(ql,qr);
                printf("%d\n", ans - 1);
            }
        }
    }
    return 0;
}
时间: 2024-10-22 21:54:57

HDU 5274(树链剖分)的相关文章

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 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 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 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

hdu 3804树链剖分+离线操作

/* 树链刨分+离线操作 题意:给你一棵树,和询问x,y 从节点x--节点1的小于等于y的最大值. 解:先建一个空树,将树的边权值从小到大排序,将询问y按从小到大排序 对于每次询问y将小于等于y的边权值的边加入,在进行询问将结果储存最后输出即可 易错点:要考虑到节点1到节点1的情况需特判. */ #pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #include<string

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

Problem Sometimes Naive 题目大意 给你一棵n个节点的树,有点权. 要求支持两种操作: 操作1:更改某个节点的权值. 操作2:给定u,v, 求 Σw[i][j]   i , j 为任意两点且i到j的路径与u到v的路径相交. 解题分析 容易发现对于一个询问,答案为总点权和的平方 减去 去掉u--v这条链后各个子树的点权和的平方的和. 开两棵线段树,tag1记录点权和,tag2记录某点的所有轻链子树的点权和的平方的和. 每次沿着重链往上走时,直接加上这条重链的所有点的tag2和