• poj2892 Tunnel Warfare


    Tunnel Warfare

    Time Limit: 1000MS   Memory Limit: 131072K
    Total Submissions: 8800   Accepted: 3641

     

    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 (nm ≤ 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:

    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


    分析

    平衡树,练了下treap,寻找前驱与后继,

    set,vector,数组,线段树,spaly...都支持

    code

      1 #include<cstdio>
      2 #include<algorithm>
      3 #include<cstring>
      4 #include<cmath>
      5 #include<iostream>
      6 
      7 using namespace std;
      8 
      9 #define lson t[k].l
     10 #define rson t[k].r
     11 const int N = 200100;
     12 
     13 struct Data{
     14     int val,key,l,r;
     15 }t[N];
     16 int st[N],top,tn,Root,ans;
     17 
     18 inline int read() {
     19     int x = 0,f = 1;char ch = getchar();
     20     for (; ch<'0'||ch>'9'; ch = getchar())
     21         if (ch=='-') f = -1;
     22     for (; ch>='0'&&ch<='9'; ch = getchar()) 
     23         x = x * 10 + ch - '0';
     24     return x * f;
     25 }
     26 
     27 inline void leftturn(int &k) {
     28     int a = rson;
     29     rson = t[a].l;
     30     t[a].l = k;
     31     k = a;
     32 }
     33 inline void rightturn(int &k) {
     34     int a = lson;
     35     lson = t[a].r;
     36     t[a].r = k;
     37     k = a;
     38 }
     39 void Insert(int &k,int x) {
     40     if (k==0) {
     41         ++tn;k = tn;
     42         t[k].val = x;t[k].key = rand();
     43         return ;
     44     }
     45     if (x > t[k].val) {
     46         Insert(rson,x);
     47         if (t[rson].key < t[k].key) leftturn(k);
     48     }
     49     else {
     50         Insert(lson,x);
     51         if (t[lson].key < t[k].key) rightturn(k);
     52     }
     53 }
     54 void Delete(int &k,int x) {
     55     if (k==0) return ;
     56     if (t[k].val==x) {
     57         if (t[lson].key * t[rson].key == 0) k = lson + rson;
     58         else if (t[lson].key < t[rson].key) {
     59             rightturn(k);Delete(k,x);
     60         }
     61         else {
     62             leftturn(k);Delete(k,x);
     63         }
     64     }
     65     else if (x > t[k].val) Delete(rson,x);
     66     else Delete(lson,x);
     67 }
     68 void getpre(int k,int x) {
     69     if (k==0) return ;
     70     //if (x==t[k].val) {ans = k;return ;} // 加这一句可以等于它,也可以下面多打一个等号
     71     if (x >= t[k].val) ans = k,getpre(rson,x);
     72     else getpre(lson,x);
     73 }
     74 void getsuc(int k,int x) {
     75     if (k==0) return ;
     76     //if (x==t[k].val) {ans = k;return ;}
     77     if (x <= t[k].val) ans = k,getsuc(lson,x);
     78     else getsuc(rson,x);
     79 }
     80 int main() {
     81     int n = read(),m = read();
     82     char s[10];
     83     Insert(Root,0);Insert(Root,n+1);
     84     for (int a,b,c,i=1; i<=m; ++i) {
     85         scanf("%s",s);
     86         if (s[0]=='D') {
     87             a = read();
     88             Insert(Root,a);
     89             st[++top] = a;
     90         }
     91         else if (s[0]=='R') Delete(Root,st[top--]);
     92         else {
     93             a = read();
     94             ans = 0;getpre(Root,a);b = ans;
     95             ans = 0;getsuc(Root,a);c = ans;
     96             if (b==c) puts("0");
     97             else printf("%d
    ",t[c].val-t[b].val-1);
     98         }
     99     }
    100     return 0;
    101 }
  • 相关阅读:
    利用python将表格中的汉字转化为拼音
    vi中批量加注释
    Xtrabackup
    mydumper下载安装
    Adobe Acrobat Pro DC破解
    InnoDB关键特性之double write
    聚集索引与非聚集索引
    has the wrong structure
    初学者如何理解网络协议
    电脑重装系统之后,删除之前的系统
  • 原文地址:https://www.cnblogs.com/mjtcn/p/8012877.html
Copyright © 2020-2023  润新知