zoukankan      html  css  js  c++  java
  • Tunnel Warfare(hdu1540 线段树)

    Tunnel Warfare

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3412    Accepted Submission(s): 1323

    Problem 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 next m lines describes an event.
    There are three different events described in different format shown below:
    D x: The x-th village was destroyed.
    Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
    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
     
     
    好久木有写博客了,,,,最近在搞线段树,,,o(︶︿︶)o 唉,表示还是很菜啊!
    就本题而言,我就没想到线段树,结果自己的代码就超时了,,,悲剧啊!
    附上我的拙作:
                 求改进!
    #include<stdio.h>
    __int64 i,j,m,n;
    int main()
    {
        int b[70000],t;
        while(scanf("%d",&n)!=EOF)
        {
            m=0;
            for(i=0;i<n;i++)
            {
                scanf("%d",&b[i]);
                m+=b[i];
            }
            for(i=1;i<m;i++)
            {
                if(m==(1+i)*i/2.0)
                {
                    t=1;
                    break;
                }
                else if(m<(1+i)*i/2.0)
                {
                    t=i;
                    break;
                }
                
                else
                    continue;
            }
            if(m==2)
                printf("yes
    2
    ");
            else
                printf("yes
    %d
    ",t);
        }
        return 0;
    }

    然后下面是AC代码,线段树

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #include <stdlib.h>
    using namespace std;
    
    const int maxn = 50000+10;
    
    int n,m;
    int s[maxn],top;
    
    struct node
    {
        int l,r;
        int ls,rs,ms;
    }a[maxn<<2];
    
    int max(int a,int b)
    {
        return a>b?a:b;
    }
    
    void init(int l,int r,int i)
    {
        a[i].l = l;
        a[i].r = r;
        a[i].ls = a[i].rs = a[i].ms = r-l+1;
        if(l!=r)
        {
            int mid = (l+r)>>1;
            init(l,mid,i*2);
            init(mid+1,r,2*i+1);
        }
    }
    
    void insert(int i,int t,int x)
    {
        if(a[i].l == a[i].r)
        {
            if(x==1)
                a[i].ls = a[i].rs = a[i].ms = 1;
            else
                a[i].ls = a[i].rs = a[i].ms = 0;
            return ;
        }
        int mid = (a[i].l+a[i].r)>>1;
        if(t<=mid)
            insert(2*i,t,x);
        else
            insert(2*i+1,t,x);
        a[i].ls = a[2*i].ls;
        a[i].rs = a[2*i+1].rs;
        a[i].ms = max(max(a[2*i].ms,a[2*i+1].ms),a[2*i].rs+a[2*i+1].ls);
        if(a[2*i].ls == a[2*i].r-a[2*i].l+1)
            a[i].ls += a[2*i+1].ls;
        if(a[2*i+1].rs == a[2*i+1].r-a[2*i+1].l+1)
            a[i].rs += a[2*i].rs;
    }
    
    int query(int i,int t)
    {
        if(a[i].l == a[i].r || a[i].ms == 0 || a[i].ms == a[i].r-a[i].l+1)
            return a[i].ms;
        int mid = (a[i].l+a[i].r)>>1;
        if(t<=mid)
        {
            if(t>=a[2*i].r-a[2*i].rs+1)
                return query(2*i,t)+query(2*i+1,mid+1);
            else
                return query(2*i,t);
        }
        else
        {
            if(t<=a[2*i+1].l+a[2*i+1].ls-1)
                return query(2*i+1,t)+query(2*i,mid);
            else
                return query(2*i+1,t);
        }
    }
    
    int main()
    {
        int i,j,x;
        char ch[2];
        while(~scanf("%d%d",&n,&m))
        {
            top = 0;
            init(1,n,1);
            while(m--)
            {
                scanf("%s",ch);
                if(ch[0] == 'D')
                {
                    scanf("%d",&x);
                    s[top++] = x;
                    insert(1,x,0);
                }
                else if(ch[0] == 'Q')
                {
                    scanf("%d",&x);
                    printf("%d
    ",query(1,x));
                }
                else
                {
                    if(x>0)
                    {
                        x = s[--top];
                        insert(1,x,1);
                    }
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    【杭电】[2071]Max Num
    【POJ】[1679]The Unique MST
    【POJ】[1679]The Unique MST
    Mac 系统引导过程概述 & BootCamp 的秘密
    Mac 系统引导过程概述 & BootCamp 的秘密
    在Eclipse中参看项目中的所有文件
    在Eclipse中参看项目中的所有文件
    让Eclipse不仅仅只是通过点号来激活代码提示功能
    让Eclipse不仅仅只是通过点号来激活代码提示功能
    Eclipse汉化包的下载和安装
  • 原文地址:https://www.cnblogs.com/yuyixingkong/p/3407311.html
Copyright © 2011-2022 走看看