CodeForces 191C 树链剖分 第4遍

非常无奈,模板重新无奈的打错了。。

只是,非常快便找到了。。

题意:给一些边,有一些操作,每次操作,都要在这些边上加上1,求每一个边的边权。。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define lson id << 1
#define rson id << 1|1
const int M = 100008;
int top[M],siz[M],son[M],father[M],ti[M],dep[M];
int e[M][2],idx,tp;
struct {
    int head;
}H[M];
struct{
    int v,next;
}E[M*2];
void init(){
    memset(H,-1,sizeof(H));
    memset(E,-1,sizeof(E));
    tp = 0,idx = 0;
}
void add(int u,int v){
    E[tp].v =v;
    E[tp].next = H[u].head;
    H[u].head = tp++;
}
void dfs_1(int u,int fa){
    son[u] = 0;siz[u] = 1;father[u] = fa;dep[u] = dep[fa] + 1;
    for(int i=H[u].head;i!=-1;i=E[i].next){
        int v = E[i].v;
        if(v == fa)continue;
        dfs_1(v,u);
        siz[u] += siz[v];
        if(siz[v] > siz[son[u]])son[u] = v;
    }
}
void dfs_2(int u,int fa){
    ti[u] = idx++;
    top[u] = fa;
    if(son[u])dfs_2(son[u],fa);
    for(int i=H[u].head;i!=-1;i=E[i].next){
        int v = E[i].v;
        if(v == father[u]|| v == son[u])continue;
        dfs_2(v,v);
    }
}
struct M_tree{
    int mark,l,r;
    int mid(){
        return (l+r) /2;
    }
}node[M*4];
void  build_tree(int id,int l,int r){
    node[id].l = l;node[id].r = r;node[id].mark = 0;

    if(l == r)return;
    int mid = node[id].mid();
    build_tree(lson,l,mid);
    build_tree(rson,mid+1,r);
}
void push_down(int id){
    if(node[id].l == node[id].r)return;
    if(node[id].mark){
        node[lson].mark += node[id].mark ;
        node[rson].mark += node[id].mark ;
        node[id].mark = 0;
    }
}

void nede(int id,int l,int r){

    if(node[id].l == l&&node[id].r == r){

        node[id].mark += 1;
        return;
    }
    push_down(id);
    int mid = node[id].mid();
    if(r <=mid)nede(lson,l,r);
    else if(l > mid)nede(rson,l,r);
    else{
        nede(lson,l,mid);
        nede(rson,mid+1,r);
    }
}
int ans = 0;
int query(int id,int r){

    if( node[id].l == r&&node[id].r == r)return node[id].mark;
    push_down(id);
    int mid= node[id].mid();
    if(r <=mid)return query(lson,r);
    else return query(rson,r);
}
void negat(int u,int v){
    int f1 = top[u];
    int f2 = top[v];

    while(f1 != f2){
        if(dep[f1] < dep[f2]){
            swap(f1,f2);
            swap(u,v);
        }//cout << ti[f1] << "<---->" << ti[u]<<endl;
        nede(1,ti[f1],ti[u]);
        u = father[f1];f1 = top[u];
    }
    if(u == v)return;
    if(dep[u] > dep[v])swap(u,v);
    nede(1,ti[son[u]],ti[v]);
}
int main()
{
    int u,v,n,m;
    while(~scanf("%d",&n)){
            init();
       for(int i=0;i<n-1;i++){
        scanf("%d%d",&e[i][0],&e[i][1]);
        add(e[i][0],e[i][1]);
        add(e[i][1],e[i][0]);

       }
       dfs_1(1,1);
       dfs_2(1,1);
       build_tree(1,0,idx-1);
//cout <<idx<<endl;
       scanf("%d",&m);
       while(m--){
            scanf("%d%d",&u,&v);

            negat(u,v);
       }
       for(int i=0;i<n-1;i++)
       {
           if(dep[e[i][0]] > dep[e[i][1]]){
            swap(e[i][0],e[i][1]);
           }
       }
       for(int i=0;i<n-1;i++){
            printf("%d",query(1,ti[e[i][1]]));
            if(i!=n-2)printf(" ");
       }
       printf("\n");
    }
}
时间: 2024-08-01 10:20:42

CodeForces 191C 树链剖分 第4遍的相关文章

CodeForces - 343D 树链剖分

题目链接:http://codeforces.com/problemset/problem/343/D 题意:给定一棵n个n-1条边的树,起初所有节点权值为0,然后m个操作. 1 x:把x为根的子树的点的权值修改为1: 2 x:把x结点到根路径上的点修改为0: 3 x:查询结点x的值. 思路:树链剖分. 对于操作1,子树操作记录下当前点为根时,dfs的最后一个点的id是多少(endid[]),然后就可以把子树操作用区间来维护了. 对于操作2,树链剖分. 对于3操作,线段树单点查询. #defin

Codeforces Round #425 (Div. 2) Problem D Misha, Grisha and Underground (Codeforces 832D) - 树链剖分 - 树状数组

Misha and Grisha are funny boys, so they like to use new underground. The underground has n stations connected with n - 1 routes so that each route connects two stations, and it is possible to reach every station from any other. The boys decided to h

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 3966 树链剖分第3遍

真心不好意思说话,写的越多,各种问题就暴漏出来了,这次更离谱,什么错误都有...不过还是过了.也明白了代码以后要写规范性,不能想当然... #include<cstdio> #include<cstring> #pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<algorithm> using namespace std;

Codeforces 191C Fools and Roads(树链剖分)

题目链接:Codeforces 191C Fools and Roads 题目大意:给定一个N节点的数,然后有M次操作,每次从u移动到v,问说每条边被移动过的次数. 解题思路:树链剖分维护边,用一个数组标记即可,不需要用线段树. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 1e5 + 5; int N, Q, ne, fir

Codeforces 343D Water Tree & 树链剖分教程

原题链接 题目大意 给定一棵根为1,初始时所有节点值为0的树,进行以下三个操作: 将以某点为根的子树节点值都变为1 将某个节点及其祖先的值都变为0 *询问某个节点的值 解题思路 这是一道裸的树链剖分题.下面详细地介绍一下树链剖分. 树链剖分预备知识: 线段树.DFS序 树链剖分想法|起源 首先,如果一棵树退化成一条链,那么它会有非常好的性质.我们可以用线段树等数据结构来维护相关操作,使得效率更高.那么我们考虑一般的树,它是否能被分成一些链,使它们也能更高效地进行某些操作? 算法流程 以下以点带权

D. Happy Tree Party CodeForces 593D【树链剖分,树边权转点权】

Codeforces Round #329 (Div. 2) D. Happy Tree Party time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Bogdan has a birthday today and mom gave him a tree consisting of n vertecies. For every

Codeforces 191 C Fools and Roads (树链剖分)

题目链接~~> 做题感悟:这题在做了HDU 5044后就感觉很简单了. 解题思路: 先树链剖分一下,把树剖分成链,因为最后全是询问,so~可以线性操作.经过树链剖分后,就会形成许多链,但是每条边都有编号,相当于一个数组进行线性操作,这样,如果在 u  ~ v 去都增加 1 ,那么可以让 sum [ u ] += 1 ; sum [ v + 1 ] -= 1 ; 这里假设 v 的编号大.最后的时候只要从后往前遍历一次就可以了,得到所有的结果.明白这点后再加上树链剖分的思想就可以解决了. 代码: #

CF 191C Fools and Roads lca 或者 树链剖分

They say that Berland has exactly two problems, fools and roads. Besides, Berland has n cities, populated by the fools and connected by the roads. All Berland roads are bidirectional. As there are many fools in Berland, between each pair of cities th