zoukankan      html  css  js  c++  java
  • (线段树)hdoj 1540-Tunnel Warfare

    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! 

    InputThe 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. 
    OutputOutput 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

    又是一道与hotel很像的题目。做的多了就显得too young too simple了。
      1 #include <iostream>
      2 //#include<bits/stdc++.h>
      3 #include <stack>
      4 #include <queue>
      5 #include <map>
      6 #include <set>
      7 #include <cstdio>
      8 #include <cstring>
      9 #include <algorithm>
     10 #include <math.h>
     11 using namespace std;
     12 int N,M;
     13 const int MAX=5e4+5;
     14 struct node
     15 {
     16     int left,right,mid;
     17     int l,r;
     18     int status;
     19     int len()
     20     {
     21         return r-l+1;
     22     }
     23     void actl()
     24     {
     25         left=right=mid=(status?0:len());
     26     }
     27 }st[10*MAX];
     28 void init(int l,int r,int k)
     29 {
     30     st[k].l=l;
     31     st[k].r=r;
     32     st[k].status=0;
     33     st[k].actl();
     34     if(l==r)
     35         return;
     36     init(l,(l+r)/2,2*k);
     37     init((l+r)/2+1,r,2*k+1);
     38 }
     39 void update(int lo,int l,int r,int k,int val)//单点更新
     40 {
     41     if(l==r&&l==lo)
     42     {
     43         st[k].status=val;
     44         st[k].actl();
     45         return;
     46     }
     47     if(l==r)
     48         return;
     49     int mid=(l+r)/2;
     50     if(lo<=mid)
     51         update(lo,l,mid,2*k,val);
     52     else
     53         update(lo,mid+1,r,2*k+1,val);
     54     st[k].mid=max(st[2*k].mid,st[2*k+1].mid);
     55     st[k].left=st[2*k].left;
     56     st[k].right=st[2*k+1].right;
     57     st[k].mid=max(st[2*k].right+st[2*k+1].left,st[k].mid);
     58     if(st[2*k].left==st[2*k].len())
     59         st[k].left+=st[2*k+1].left;
     60     if(st[2*k+1].right==st[2*k+1].len())
     61         st[k].right+=st[2*k].right;
     62     return;
     63 }
     64 int query(int lo,int k)
     65 {
     66     int an;
     67     if(st[k].l==st[k].r||st[k].mid==0||st[k].mid==st[k].len())
     68         return st[k].mid;
     69     int mid=(st[k].l+st[k].r)/2;
     70     if(lo<=mid)
     71         {
     72             if(lo>=st[2*k].r-st[2*k].right+1)
     73                 return query(lo,2*k)+query(mid+1,2*k+1);
     74             else return
     75                 query(lo,2*k);
     76         }
     77     else
     78         {
     79             if(lo<=st[2*k+1].l+st[2*k+1].left-1)
     80             {
     81                 return query(lo,2*k+1)+query(mid,2*k);
     82             }
     83             return query(lo,2*k+1);
     84         }
     85 }
     86 int lo[MAX];
     87 int cnt;
     88 char opt[10];
     89 int main()
     90 {
     91     while(~scanf("%d %d",&N,&M))
     92     {
     93     init(1,N,1);
     94     memset(lo,0,sizeof(lo));
     95     int i;
     96     int tem;
     97     for(i=1;i<=M;i++)
     98     {
     99         scanf("%s",opt);
    100         if(opt[0]=='D')
    101         {
    102             scanf("%d",&tem);
    103             lo[++cnt]=tem;
    104             update(tem,1,N,1,1);
    105         }
    106         else if(opt[0]=='Q')
    107         {
    108             scanf("%d",&tem);
    109             printf("%d
    ",query(tem,1));
    110         }
    111         else
    112         {
    113             if(cnt>0)
    114             update(lo[cnt--],1,N,1,0);
    115         }
    116     }
    117     }
    118 }
    
    
  • 相关阅读:
    Go 场景
    sourcetree clone 提示url无效
    简单去重比较/复杂数据去重处理
    vue组件之间的通信
    js-执行机制之同步、异步、宏任务、微任务
    react生命周期
    git push被忽略的文件 处理
    请求头的属性
    运算精度问题https://blog.csdn.net/smile_ping/article/details/80284969
    react中状态提升
  • 原文地址:https://www.cnblogs.com/quintessence/p/6481490.html
Copyright © 2011-2022 走看看