zoukankan      html  css  js  c++  java
  • POI2007 MEG-Megalopolis [树状数组]

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

    输入输出格式

    输入格式:

    In the first line of the standard input there is a single integer nn ( 1≤n≤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≤a<b≤n )separated by a single space, denoting the numbers of villages connected with a road. Inthe next line there is a single integer mm ( 1≤m≤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 mm integers to the standard output, one a line, denoting the numberof country roads Byteasar has travelled during his successive trips.

    输入输出样例

    输入样例#1:

    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

    输出样例#1:

    2
    1
    0
    1

    题解

    题目保证输入是一棵树,有修改操作和查询操作

    暴力方法很容易想到从单个节点向根节点跑最短路

    正解:其实也很容易想到,我们可以利用它的dfs序,我们发现按dfs序维护树状数组就是一个点到1点经过的土路的条数,利用差分思想就可以了

    在这个基础上统计区间值,用树状数组就可以了

    Code

    #include<bits/stdc++.h>
    using namespace std;
    void in(int &ans) {
        ans=0;int f=1; char i=getchar();
        while(i<'0' || i>'9') {if(i=='-') f=-1; i=getchar();}
        while(i>='0' && i<='9') {ans=(ans<<1)+(ans<<3)+i-'0'; i=getchar();}
        ans*f;
    }
    int n,m,len,dfc;
    struct egde {
        int to,next;
    }e[500010];
    int head[250100],dfn[250010],f[250010],dep[250010],size[250010];
    void add(int a,int b) {
        e[++len].to=b;
        e[len].next=head[a];
        head[a]=len;
    }
    int lowbit(int x) {
        return x&-x;
    }
    int check(int x) {
        int ans=0;
        while(x) {
            ans+=f[x];
            x-=lowbit(x);
        }
        return ans;
    }
    void change(int x,int a) {
        while(x<=n) {
            f[x]+=a;
            x+=lowbit(x);
        }
    }
    void dfs(int u,int fa) {
        dfn[u]=++dfc,size[u]=1;
        for(int i=head[u];i;i=e[i].next) {
            int to=e[i].to;
            if(to!=fa) {
                dep[to]=dep[u]+1;
                dfs(to,u);
                size[u]+=size[to];
            }
        }
    }
    int main()
    {
        int a,b;
        in(n);
        for(int i=1;i<n;i++) {
            in(a); in(b);
            add(a,b); add(b,a);
        }
        dep[1]=1; dfs(1,0);
        for(int i=2;i<=n;i++) {
            change(dfn[i],1); change(dfn[i]+size[i],-1);
        }
        in(m);
        for(int i=1;i<=m+n-1;i++) {
            char ch;int a,b; cin>>ch;
            if(ch=='A') {
                in(a);in(b);
                if(dep[a]>dep[b]) swap(a,b);
                change(dfn[b],-1); change(dfn[b]+size[b],1);
            }
            else {
                in(a); cout<<check(dfn[a])<<endl;
            }
        }
        return 0;
    }
    

    博主蒟蒻,随意转载.但必须附上原文链接

    http://www.cnblogs.com/real-l/

  • 相关阅读:
    ICONS-图标库
    图形资源
    vue项目中,如果修改了组件名称,vscode编辑器会在引入修改组件的名字处提示红色波浪线 The file is in the program because:Imported via xxx Root file specified for compilation .
    接口在dev环境报跨域问题(has been blocked by CORS policy:Response to preflight request doesn't pass access control check:No 'Access-Control-Allow-Origin' header ispresent on the requested resource.),qa环境正常
    阿里云occ的图片文件URL用浏览器直接打开无法访问,提示This XML file does noe appear to have any style information associated with it. The document tree is shown below.
    vue 项目使用element ui 中tree组件 check-strictly 用法(父子不互相关联的反显情况)
    高德地图进行线路规划绘制标记点操作(vue)
    vue中实现拖拽调整顺序功能
    2021-01-22 浏览器相关知识
    2021-01-22 js 相关知识点
  • 原文地址:https://www.cnblogs.com/real-l/p/9314513.html
Copyright © 2011-2022 走看看