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 }
  • 相关阅读:
    Evolution of Image Classifiers,进化算法在神经网络结构搜索的首次尝试 | ICML 2017
    YOLOv1/v2/v3简述 | 目标检测
    MetaQNN : 与Google同场竞技,MIT提出基于Q-Learning的神经网络搜索 | ICLR 2017
    FCOS : 找到诀窍了,anchor-free的one-stage目标检测算法也可以很准 | ICCV 2019
    canvas图表(3)
    WebGL学习(2)
    canvas图表(1)
    WebGL学习(1)
    前端特效列表
    canvas绘制太阳系
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5561403.html
Copyright © 2011-2022 走看看