前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CodeForces 191C 树链剖分 第4遍「建议收藏」

CodeForces 191C 树链剖分 第4遍「建议收藏」

作者头像
全栈程序员站长
发布2022-07-08 17:05:45
1440
发布2022-07-08 17:05:45
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是全栈君。

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

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

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

代码语言:javascript
复制
#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");
    }
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116097.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档