HDU 1540 Tunnel Warfare 线段树区间合并

来源:互联网 发布:sqlserver恢复表数据 编辑:程序博客网 时间:2024/06/08 04:26

http://acm.hdu.edu.cn/showproblem.php?pid=1540

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)


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 9D 3D 6D 5Q 4Q 5RQ 4RQ 4
 
Sample Output
1024
题目大意:有n个村庄,m个操作。操作有三种。1.'D x' 摧毁村庄x,2.'R' 把最后摧毁的村庄重建,3.'Q x'问与x相连的村庄有多少个

思路:线段树区间合并(学了一上午才会)

AC代码:

#include<cstdio>#include<iostream>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<vector>#include<stack>#include<map>#include<string>#define LL long long#define eps 1e-8using namespace std;const int mod2 = 1e9+7;const int INF = 1e8;const int inf = 0x3f3f3f3f;const int maxx = 50010;struct node{    int l,r;//l表示在当前区间从左往右数有多少个好村庄相连  直到数到坏村庄停止,r同理。} t[maxx*4];void build(int k,int l,int r){    t[k].r=t[k].l=r-l+1;    if(l==r) return ;    int m=(l+r)>>1;    build(k<<1,l,m);    build(k<<1|1,m+1,r);}void up(int k,int p){    int k1=k<<1,k2=k<<1|1;    t[k].l=t[k1].l;    t[k].r=t[k2].r;    if(t[k].l==p-(p>>1)) t[k].l+=t[k2].l;    if(t[k].r==p>>1) t[k].r+=t[k1].r;}void Insert(int k,int l,int r,int v,int id){    if(l==r)    {        t[k].l=t[k].r=v;        return ;    }    int m=(l+r)>>1;    if(id<=m) Insert(k<<1,l,m,v,id);    else Insert(k<<1|1,m+1,r,v,id);    up(k,r-l+1);}int ans;void Query(int k,int l,int r,int id){    if(l==r)    {        ans=t[k].l;        return ;    }    int m=(l+r)>>1;    int k1=k<<1,k2=k<<1|1;    if(id<=m)    {        Query(k1,l,m,id);        if(m+1-t[k1].r<=id)  ans+=t[k2].l;    }    else    {        Query(k2,m+1,r,id);        if(m+t[k2].l>=id)  ans+=t[k1].r;    }}stack<int> s;int main(){    int n,m;    while(~scanf("%d%d",&n,&m))    {        while(!s.empty()) s.pop();        build(1,1,n);        char c[10];        int a;        while(m--)        {            scanf("%s",c);            if(c[0]=='D')            {                scanf("%d",&a);                s.push(a);                Insert(1,1,n,0,a);            }            else if(c[0]=='Q')            {                ans=0;                scanf("%d",&a);                Query(1,1,n,a);                printf("%d\n",ans);            }            else            {                if(!s.empty())                {                    a=s.top();                    s.pop();                    Insert(1,1,n,1,a);                }            }        }    }}








阅读全文
0 0
原创粉丝点击