zoukankan      html  css  js  c++  java
  • POJ2892 Tunnel Warfare

    Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %I64d & %I64u

    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:

    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树存储所有被摧毁的村子,查询村子x的时候,只要在树里查离x最近的被摧毁的村子,就知道联系范围了

      1 /*by SilverN*/
      2 #include<iostream>
      3 #include<cstdio>
      4 #include<cmath>
      5 #include<cstring>
      6 #include<algorithm>
      7 using namespace std;
      8 struct node{
      9     int l,r;
     10     int num;
     11     int rand;
     12 }t[120000];
     13 int st[120000],top=0;//用栈存储已经摧毁的村子 
     14 int cnt=0;
     15 int root=0;
     16 int n,m;
     17 int s,e;//查询左右边界 
     18 void lt(int &k){
     19     int now=t[k].r;
     20     t[k].r=t[now].l;
     21     t[now].l=k;
     22     k=now;
     23     return;
     24 }
     25 void rt(int &k){
     26     int now=t[k].l;
     27     t[k].l=t[now].r;
     28     t[now].r=k;
     29     k=now;
     30     return;
     31 }
     32 void insert(int &k,int x){
     33     if(!k){
     34         cnt++;
     35         k=cnt;
     36         t[k].num=x;
     37         t[k].rand=rand();
     38         return;
     39     }
     40     if(x==t[k].num)return;//不会重复破坏
     41     if(x<t[k].num){
     42         insert(t[k].l,x);//建成左子树 
     43         if(t[t[k].l].rand<t[k].rand)rt(k);
     44     }else if(x>t[k].num){
     45         insert(t[k].r,x);//建成右子树 
     46         if(t[t[k].r].rand<t[k].rand)lt(k);
     47     }
     48     return;
     49 }
     50 void del(int &k,int x){
     51     if(t[k].num==x){
     52         if(t[k].l*t[k].r==0)k=t[k].l+t[k].r;
     53         else if(t[t[k].l].rand<t[t[k].r].rand){
     54             rt(k);
     55             del(k,x);
     56         }
     57         else if(t[t[k].l].rand>t[t[k].r].rand){
     58             lt(k);
     59             del(k,x);
     60         }
     61         return;    
     62     }
     63     if(x<t[k].num) del(t[k].l,x);
     64     else del(t[k].r,x);
     65     return;
     66 }
     67 void find(int k,int x){//查询 
     68     if(!k)return;
     69     if(t[k].num>=x && t[k].num<e)e=t[k].num;
     70     if(t[k].num<=x && t[k].num>s)s=t[k].num;
     71     if(t[k].num<x)find(t[k].r,x);
     72     else find(t[k].l,x);
     73     return;
     74 }
     75 int main(){
     76     scanf("%d%d",&n,&m);
     77     char ch;
     78     int x;
     79     for(int i=1;i<=m;i++){
     80 //        scanf("%s",ch);
     81 //        ch=getchar();
     82         cin>>ch;
     83         if(ch=='D'){//摧毁 
     84             scanf("%d",&x);
     85             st[++top]=x;
     86             insert(root,x);
     87         }
     88         if(ch=='R'){//恢复 
     89             del(root,st[top--]);
     90         }
     91         if(ch=='Q'){//查询 
     92             scanf("%d",&x);
     93             s=0;e=n+1;//设置边界 
     94             find(root,x);
     95             if(s==x && e==x)printf("0
    ");
     96             else printf("%d
    ",e-s-1);
     97         }
     98     }
     99     return 0;
    100 }
  • 相关阅读:
    使用vscode 用git 拉取代码,提示:在签出前,请清理存储库工作树
    区分手机端和pc端加载不同的样式
    关于vue的代码规范
    各种名词汇总整理
    ZB埋点汇总
    项目实战 OLAP数据提取
    大数据intern_1总结:数据埋点以及SQL复习
    leetcode 343+279+91+64+70 动态规划
    leetcode 241 加优先级括号
    leetcode 17+79+93+46+47+77+39+40+78+90+131+37 回溯法
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5561403.html
Copyright © 2011-2022 走看看