线段树——区间合并——hud1540

来源:互联网 发布:中国数据分析行业协会 编辑:程序博客网 时间:2024/06/05 11:56

线段树的区间合并

用点更新或者区间更新 求满足条件的最大区间
在线段结构体中维护该节点左侧开始的最大区间 右侧开始的最大区间 最大区间

hdu1540原题链接
Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9395 Accepted Submission(s): 3667

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 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4

Sample Output
1
0
2
4

思路 线段树区间更新的模板题 储存被毁坏的村庄可以用栈

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <queue>#include <stack>#include <vector>#define max_ 50010#define inf 0x3f3f3f3f#define ll long longusing namespace std;struct node{    int l,r;    int ls,rs,ms;//分别为左侧最大区间 右侧最大区间 最大区间};struct node tree[max_*4];int n,m;stack<int> s;void build(int i,int l,int r){    tree[i].l=l;    tree[i].r=r;    tree[i].ls=tree[i].rs=tree[i].ms=r-l+1;    if(l==r)        return;    int mid=(l+r)>>1;    build(i<<1,l,mid);    build(i<<1|1,mid+1,r);}void update(int i,int x,int v){    if(tree[i].l==x&&tree[i].r==x)    {        tree[i].ls=tree[i].rs=tree[i].ms=v;//初始化为区间长度        return;    }    int mid=(tree[i].l+tree[i].r)>>1;    if(x<=mid)        update(i<<1,x,v);    else        update(i<<1|1,x,v);    tree[i].ls=tree[i<<1].ls;    tree[i].rs=tree[i<<1|1].rs;    tree[i].ms=max(max(tree[i<<1].ms,tree[i<<1|1].ms),tree[i<<1].rs+tree[i<<1|1].ls);        //最大区间是左子节点的左侧 右子节点的右侧 左子节点的右侧加右子节点的左侧中最大的    if(tree[i<<1].ls==tree[i<<1].r-tree[i<<1].l+1)//如果左子节点满了 就让当前节点的最大值加上右子节点的左侧        tree[i].ls+=tree[i<<1|1].ls;    if(tree[i<<1|1].rs==tree[i<<1|1].r-tree[i<<1|1].l+1)//同上        tree[i].rs+=tree[i<<1].rs;}int query(int i,int x){    if(tree[i].l==tree[i].r||tree[i].ms==0||tree[i].ms==tree[i].r-tree[i].l+1)      //叶节点 最大长度为0 最大长度为总长度 三种情况下直接返回        return tree[i].ms;    int mid=(tree[i].l+tree[i].r)>>1;    if(x<=mid)    {        if(x>=tree[i<<1].r-tree[i<<1].rs+1)//求左子节点右区间的左端点            return query(i<<1,x)+query(i<<1|1,mid+1);//左节点的右区间加右节点的左区间        else            return query(i<<1,x);//只访问左节点的左区间    }    else    {        if(x<=tree[i<<1|1].l+tree[i<<1|1].ls-1)//求右子节点左区间的右端点            return query(i<<1,mid)+query(i<<1|1,x);        else            return query(i<<1|1,x);    }}int main(int argc, char const *argv[]){    while(scanf("%d%d",&n,&m)!=EOF)    {        while(s.size())            s.pop();        memset(tree,0,sizeof(tree));        build(1,1,n);        while(m--)        {            char c;            int x;            scanf(" %c",&c);            if(c=='D')            {                scanf("%d",&x);                update(1,x,0);                s.push(x);//用栈来储存被破坏的村庄            }            else if(c=='Q')            {                scanf("%d",&x);                printf("%d\n",query(1,x));            }            else if(c=='R')            {                update(1,s.top(),1);                s.pop();            }        }           }    return 0;}
原创粉丝点击