zoukankan      html  css  js  c++  java
  • poj 2892

    Tunnel Warfare
    Time Limit: 1000MS   Memory Limit: 131072K
    Total Submissions: 7725   Accepted: 3188

    Description

    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.

    Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!

    Input

    The first line of the input contains two positive integers n and m (n, m 50,000) indicating the number of villages and events. Each of the nextm lines describes an event.

    There are three different events described in different format shown below:

    1. D x: The x-th village was destroyed.
    2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
    3. R: The village destroyed last was rebuilt.

    Output

    Output the answer to each of the Army commanders request in order on a separate line.

    Sample Input

    7 9
    D 3
    D 6
    D 5
    Q 4
    Q 5
    R
    Q 4
    R
    Q 4

    Sample Output

    1
    0
    2
    4

    Hint

    An illustration of the sample input:

          OOOOOOO
    
    D 3   OOXOOOO
    
    D 6   OOXOOXO
    
    D 5   OOXOXXO
    
    R     OOXOOXO
    
    R     OOXOOOO

    Source

    这题被用作学平衡树的模板题,用的是treap。将被删除的村庄存入treap,若修复则删除,求连续序列用find函数。
    #include<ctime>
    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    using namespace std;
    int n,m,root=0,d[50100],st,ed,sz;
    struct Treap{int rnd,v,w,l,r;}tr[80100];
    inline void lturn(int &k)
    {
        int t=tr[k].r;
        tr[k].r=tr[t].l;
        tr[t].l=k;
        k=t;
    }
    inline void rturn(int &k)
    {
        int t=tr[k].l;
        tr[k].l=tr[t].r;
        tr[t].r=k;
        k=t;
    }
    inline void insert(int &w,int x)
    {
        if(w==0){
            sz++;
            w=sz;
            tr[w].v=x;
            tr[w].w=1;
            tr[w].rnd=rand();
            return;
        }
        if(tr[w].v==x){
            tr[w].w++;
            return;
        }
        if(tr[w].v>x){
            insert(tr[w].l,x);
            if(tr[tr[w].l].rnd<tr[w].rnd)rturn(w);
        }
        else{
            insert(tr[w].r,x);
            if(tr[tr[w].r].rnd<tr[w].rnd)lturn(w);
        }
    }
    inline void del(int &w,int x)
    {
        if(tr[w].v==x){
            if(tr[w].w>1){tr[w].w--;return;}
            if(tr[w].l*tr[w].r==0)w=tr[w].l+tr[w].r;
            else{
                if(tr[tr[w].l].rnd<tr[tr[w].r].rnd){
                    rturn(w);
                    del(w,x);}
                else{
                    lturn(w);
                    del(w,x);}
            }
            return;
        }
        else if(tr[w].v<x)del(tr[w].r,x);
        else del(tr[w].l,x);
    }
    inline void find(int &w,int x)
    {
        if(w==0)return;
        if(tr[w].v>=x&&tr[w].v<ed)ed=tr[w].v;
        if(tr[w].v<=x&&tr[w].v>st)st=tr[w].v;
        if(tr[w].v<x)find(tr[w].r,x);
        else find(tr[w].l,x);
    }
    int main()
    {
        srand(time(0));
        scanf("%d%d",&n,&m);
        char od;
        int a;
        for(int i=1,j=0;i<=m;i++){
            cin>>od;
            if(od=='D'){
                scanf("%d",&a);
                insert(root,a);
                j++;
                d[j]=a;
            }
            if(od=='R'){
                del(root,d[j]);
                j--;
            }
            if(od=='Q'){
                scanf("%d",&a);
                st=0,ed=n+1;
                find(root,a);
                if(st==a&&ed==a)puts("0");
                else printf("%d
    ",ed-st-1);
            }
        }
        return 0;
    }
    

  • 相关阅读:
    基本数据类型(int, bool, str)
    万恶之源之运算符
    python基础初识
    leetcode 653. Two Sum IV
    leetcode 16 3Sum Closest
    leetcode15 3Sum
    leetcode 1 Two Sum
    【站立会议】第四天
    【站立会议】第三天
    【站立会议】第二天
  • 原文地址:https://www.cnblogs.com/keshuqi/p/5957778.html
Copyright © 2011-2022 走看看