• Luogu P3459 [POI2007]MEG-Megalopolis(线段树)


    P3459 [POI2007]MEG-Megalopolis

    题意

    题目描述

    Byteotia has been eventually touched by globalisation, and so has Byteasar the Postman, who once roamedthe country lanes amidst sleepy hamlets and who now dashes down the motorways. But it is those strolls inthe days of yore that he reminisces about with a touch of tenderness.

    In the olden days nn Byteotian villages (numbered from 11 to nn) were connected by bidirectional dirt roadsin such a way, that one could reach the village number 11 (called Bitburg) from any other village in exactlyone way. This unique route passed only through villages with number less or equal to that of the startingvillage. Furthermore, each road connected exactly two distinct villages without passing through any othervillage. The roads did not intersect outside the villages, but tunnels and viaducts were not unheard of.

    Time passing by, successive roads were being transformed into motorways. Byteasar remembers distinctly, when each of the country roads so disappeared. Nowadays, there is not a single country lane left in Byteotia - all of them have been replaced with motorways, which connect the villages into Byteotian Megalopolis.

    Byteasar recalls his trips with post to those villages. Each time he was beginning his journey with letters to some distinct village in Bitburg. He asks you to calculate, for each such journey (which took place in a specific moment of time and led from Bitburg to a specified village), how many country roads it led through.

    TaskWrite a programme which:

    reads from the standard input:

    descriptions of roads that once connected Byteotian villages, sequence of events: Byteasar's trips and the moments when respective roads were transformed into motorways, for each trip, calculates how many country roads Byteasar has had to walk, writes the outcome to the standard output.

    在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员(Blue Mary)也开始骑着摩托车传递邮件了。不过,她经常回忆起以前在乡间漫步的情景。昔日,乡下有依次编号为(1 cdots n)(n)个小村庄,某些村庄之间有一些双向的土路。从每个村庄都恰好有一条路径到达村庄(1)(即比特堡)。并且,对于每个村庄,它到比特堡的路径恰好只经过编号比它的编号小的村庄。另外,对于所有道路而言,它们都不在除村庄以外的其他地点相遇。在这个未开化的地方,从来没有过高架桥和地下铁道。随着时间的推移,越来越多的土路被改造成了公路。至今,(Blue Mary)还清晰地记得最后一条土路被改造为公路的情景。现在,这里已经没有土路了——所有的路都成为了公路,而昔日的村庄已经变成了一个大都市。(Blue Mary)想起了在改造期间她送信的经历。她从比特堡出发,需要去某个村庄,并且在两次送信经历的间隔期间,有某些土路被改造成了公路。现在(Blue Mary)需要你的帮助:计算出每次送信她需要走过的土路数目。(对于公路,她可以骑摩托车;而对于土路,她就只好推车了。)

    输入输出格式

    输入格式:

    In the first line of the standard input there is a single integer (n) ((1 leq n leq 250 000)),denoting the number of villages in Byteotia. The following (n-1) lines contain descriptions of the roads, in the form of two integers (a,b) ((1 leq a<b leq n))separated by a single space, denoting the numbers of villages connected with a road. Inthe next line there is a single integer (m)((1 leq m leq 250 000)),denoting the number of trips Byteasar has made.

    The following (n+m-1) lines contain descriptions of the events, in chronological order:

    A description of the form "A (a) (b)"(for (a<b)) denotes a country road between villages (a) and (b) beingtransformed into a motorway in that particular moment.

    A description of the from "W (a)" denotes Byteasar's trip from Bitburg to village (a).

    输出格式:

    Your programme should write out exactly (m) integers to the standard output, one a line, denoting the numberof country roads Byteasar has travelled during his successive trips.

    输入输出样例

    输入样例:

    5
    1 2
    1 3
    1 4
    4 5
    4
    W 5
    A 1 4
    W 5
    A 4 5
    W 5
    W 2
    A 1 2
    A 1 3
    

    输出样例:

    2
    1
    0
    1
    

    思路

    数据过水。 --logeadd

    简单地说,给你一棵树,树上的边权都是(1),接下来有两种操作,一种把一条边的边权改为(0),另一种询问某个结点到根节点(1)的边权之和。这不就是一道树剖板子题吗?于是这样想的我在考场上被卡掉了一个点(不过听(logeadd)巨佬说洛谷上交树剖也能过)。

    树剖的时间复杂度为(O(n log ^2n)),显然会挂,能不能优化到(O(n log n))呢?对于每次修改操作,被修改边所指向的子树中的所有结点的答案都会减少一,我们不如在线段树中直接记录答案,每次只进行一次区间修改,也就是修改子树,询问时单点询问,这样时间复杂度就可以变为(O(n log n))了。

    从另一个角度来想,我们处理出树的(dfs)序,然后要用一种支持区间修改和单点查询的时间复杂度都不大于(O(log n))的数据结构,同样,我们也可以大力一发树状数组过了这道题。这里提供的是线段树做法。

    AC代码

    #include<bits/stdc++.h>
    using namespace std;
    const int MAXN=250005;
    int n,m,tot,sz[MAXN],dfn[MAXN],dep[MAXN],a[MAXN];
    int cnt,top[MAXN],to[MAXN<<1],nex[MAXN<<1];
    struct SegmentTree
    {
        int l,r,data,tag;
        #define l(a) tree[a].l
        #define r(a) tree[a].r
        #define d(a) tree[a].data
        #define t(a) tree[a].tag
    }tree[MAXN<<2];
    int read()
    {
        int re=0;char ch=getchar();
        while(!isdigit(ch)) ch=getchar();
        while(isdigit(ch)) re=(re<<3)+(re<<1)+ch-'0',ch=getchar();
        return re;
    }
    char readc()
    {
        char ch=getchar();
        while(!isalpha(ch)) ch=getchar();
        return ch;
    }
    void dfs(int now)
    {
        dfn[now]=++tot,a[tot]=dep[now],sz[now]=1;
        for(int i=top[now];i;i=nex[i])
        {
            if(dfn[to[i]]) continue;
            dep[to[i]]=dep[now]+1;
            dfs(to[i]);
            sz[now]+=sz[to[i]];
        }
    }
    void build(int p,int ll,int rr)
    {
        l(p)=ll,r(p)=rr;
        if(ll==rr)
        {
            d(p)=a[ll];
            return ;
        }
        int mid=(ll+rr)>>1;
        build(p<<1,ll,mid);
        build(p<<1|1,mid+1,rr);
        d(p)=d(p<<1)+d(p<<1|1);
    }
    void pushdown(int p)
    {
        if(t(p))
        {
            d(p<<1)-=t(p)*(r(p<<1)-l(p<<1)+1),d(p<<1|1)-=t(p)*(r(p<<1|1)-l(p<<1|1)+1);
            t(p<<1)+=t(p),t(p<<1|1)+=t(p);
            t(p)=0;
        }
    }
    void change(int p,int ll,int rr)
    {
        if(ll<=l(p)&&r(p)<=rr)
        {
            d(p)-=r(p)-l(p)+1;
            t(p)++;
            return ;
        }
        pushdown(p);
        int mid=(l(p)+r(p))>>1;
        if(ll<=mid) change(p<<1,ll,rr);
        if(rr>mid) change(p<<1|1,ll,rr);
        d(p)=d(p<<1)+d(p<<1|1);
    }
    int ask(int p,int des)
    {
        if(l(p)==r(p)) return d(p);
        pushdown(p);
        int mid=(l(p)+r(p))>>1;
        if(des<=mid) return ask(p<<1,des);
        else return ask(p<<1|1,des);
    }
    int main()
    {
        n=read();
        for(int i=0;i<n-1;i++)
        {
            int x=read(),y=read();
            to[++cnt]=y,nex[cnt]=top[x],top[x]=cnt;
            to[++cnt]=x,nex[cnt]=top[y],top[y]=cnt;
        }
        dfs(1);
        build(1,1,n);
        m=read()+n-1;
        while(m--)
        {
            char opt=readc();
            if(opt=='A')
            {
                int x=read(),y=read();
                if(dfn[x]<dfn[y]) swap(x,y);
                change(1,dfn[x],dfn[x]+sz[x]-1);
            }
            else if(opt=='W') printf("%d
    ",ask(1,dfn[read()]));
        }
        return 0;
    }
    
  • 相关阅读:
    父亲的心
    英美主要报刊杂志网站整合【更新订正版】
    【更新】免费下载百度文库文档
    About Pull Strings 英语走后门议论文
    实验室安全事故读后感
    WorldWind上风羽线的显示
    传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确
    DevExpress的Web控件汉化方法
    安装rails卡住很慢 出现302 Moved Temporarily
    MSCRM4 在过滤后的LOOKUP框中实现查找
  • 原文地址:https://www.cnblogs.com/coder-Uranus/p/9805031.html
Copyright © 2020-2023  润新知