Subway Lines Gym - 101908L

 https://cn.vjudge.net/problem/Gym-101908L

#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <set>
#include <map>
#include <tr1/unordered_map>
#include <cmath>
//#include<bits/stdc++.h>
using namespace std;

#define sfi(i) scanf("%d",&i)
#define sfl(i) scanf("%I64d",&i)
#define sfs(i) scanf("%s",(i))
#define pri(i) printf("%d\n",i)
#define prl(i) printf("%I64d\n",i)
#define sff(i) scanf("%lf",&i)
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define lowbit(x) ((x)&(-x))
#define fl() printf("flag\n")
#define MOD(x) ((x%mod)+mod)%mod
#define endl '\n'
#define pb push_back
#define lson (rt<<1)
#define rson (rt<<1|1)
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)

template<typename T>inline void read(T &x)
{
    x=0;
    static int p;p=1;
    static char c;c=getchar();
    while(!isdigit(c)){if(c=='-')p=-1;c=getchar();}
    while(isdigit(c)) {x=(x<<1)+(x<<3)+(c-48);c=getchar();}
    x*=p;
}

const int maxn=1e5+9;
const int mod=1e9+7;

struct E
{
    int to,nex;
}edge[maxn*4];

struct node
{
    int sum,lazy;
}tree[maxn<<2];

int q;
int n;
int head[maxn];
int tot;
int rk[maxn],fa[maxn],d[maxn],siz[maxn],son[maxn],top[maxn],id[maxn];
int cnt;

void addedge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].nex=head[u];
    head[u]=tot++;
}
void dfs1(int u)
{
    //cout<<u<<endl;
    d[u]=d[fa[u]]+1;
    siz[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].nex)
    {
        int v=edge[i].to;
        if(v==fa[u]) continue;
        fa[v]=u;
        dfs1(v);
        siz[u]+=siz[v];
        if(siz[v]>siz[son[u]])
            son[u]=v;
    }
}
void dfs2(int u,int tp)
{
    top[u]=tp;
    id[u]=++cnt;
    rk[cnt]=u;
    if(!son[u]) return ;
    dfs2(son[u],tp);
    for(int i=head[u];i!=-1;i=edge[i].nex)
    {
        int v=edge[i].to;
        if(v!=son[u]&&v!=fa[u])
            dfs2(v,v);
    }
}

void pushup(int rt)
{
    tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
}
void pushdown(int rt,int l,int r)
{
    int mid=(l+r)>>1;
    if(tree[rt].lazy&&l!=r)
    {
        tree[rt<<1].sum+=(mid-l+1)*tree[rt].lazy;
        tree[rt<<1|1].sum+=(r-mid)*tree[rt].lazy;

        tree[rt<<1].lazy+=tree[rt].lazy;
        tree[rt<<1|1].lazy+=tree[rt].lazy;

        tree[rt].lazy=0;
    }
}
void build(int rt,int l,int r)
{
    tree[rt].sum=0;
    tree[rt].lazy=0;
    if(l==r)
    {
        return ;
    }
    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
    //pushup(rt);
}
void update(int rt,int l,int r,int ql,int qr,int val)
{
    if(ql<=l&&qr>=r)
    {
        tree[rt].lazy+=val;
        tree[rt].sum+=(r-l+1)*val;
        return ;
    }
    pushdown(rt,l,r);
    int mid=(l+r)>>1;
    if(ql<=mid) update(rt<<1,l,mid,ql,qr,val);
    if(qr>mid) update(rt<<1|1,mid+1,r,ql,qr,val);
    pushup(rt);
}
void update_line(int x,int y,int z)
{
    int tx=top[x];
    int ty=top[y];
    while(tx!=ty)
    {
        if(d[tx]>d[ty]) swap(x,y),swap(tx,ty);
        update(1,1,n,id[ty],id[y],z);
        y=fa[ty];
        ty=top[y];
    }
    if(d[x]>d[y]) swap(x,y);
    update(1,1,n,id[x],id[y],z);
}
int query(int rt,int l,int r,int ql,int qr)
{
    if(ql<=l&&qr>=r)
    {
        return tree[rt].sum;
    }
    pushdown(rt,l,r);
    int ans=0;
    int mid=(l+r)>>1;
    if(ql<=mid) ans+=query(rt<<1,l,mid,ql,qr);
    if(qr>mid) ans+=query(rt<<1|1,mid+1,r,ql,qr);
    return ans;
}
int query_line(int x,int y)
{
    int ans=0;
    int tx=top[x];
    int ty=top[y];
    while(tx!=ty)
    {
        if(d[tx]>d[ty])swap(x,y),swap(tx,ty);
        ans=ans+query(1,1,n,id[ty],id[y]);
        y=fa[ty];
        ty=top[y];
    }
    if(d[x]>d[y])swap(x,y);
    ans+=query(1,1,n,id[x],id[y]);
    return ans;
}

void init()
{
    tot=0;
    mem(head,-1);
    for(int i=0;i<=n;i++)
    {
        rk[i]=son[i]=d[i]=top[i]=fa[i]=siz[i]=id[i]=0;
    }
    cnt=0;
}

int main()
{
    FAST_IO;
    //freopen("input.txt","r",stdin);

    init();

    cin>>n>>q;

    for(int i=0;i<n-1;i++)
    {
        int u,v;
        cin>>u>>v;
        addedge(u,v);
        addedge(v,u);
    }

    build(1,1,n);

    dfs1(1);
    dfs2(1,1);

    while(q--)
    {
        int a,b,c,d;
        cin>>a>>b>>c>>d;

        update_line(a,b,1);

        cout<<query_line(c,d)<<endl;

        update_line(a,b,-1);
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值