ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Tunnel Warfare HDU - 1540 (最长区间合并)

2020-05-04 23:51:43  阅读:329  来源: 互联网

标签:HDU now Warfare 1540 int mid num include lc


Tunnel Warfare HDU - 1540 

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! 

InputThe 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. 
OutputOutput 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

题解:解一:线段树区间合并
      通过数组记录各个节点左or右孩子的最长连续区间,再通过区间的合并将该节点区间的最大值更新到一个数组中,然后进行查询
   解二:记录节点左孩子区间的最右区间边界值L和右孩子区间的最左区间边界值R,查询的时候计算出的 R-L+1 即为最长区间值
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <iomanip>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mem(sum,x) memset(sum,x,sizeof(sum))
#define rep(i,start,end) for(int i=start;i<=end;i++)
#define per(i,end,start) for(int i=end;i>=start;i--)
#define tle ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int mod = 998244353;
const int mxn = 2e5 +7;
int _,n,m,t,k,u,v,ans,cnt,ok,lim;
ll w[mxn] , cost[mxn] ,  far[mxn] , siz[mxn];
char ch;
#define lc now<<1
#define rc now<<1|1
string str ;
struct node {ll l,r,val,lazy,mx,mn;}p[mxn<<4];
int lmx[mxn] , rmx[mxn] , num[mxn] ;
void pushup(int l,int r,int now)
{
    lmx[now] = lmx[lc] ; rmx[now] = rmx[rc];
    int mid = (l+r)>>1;
    if(lmx[now]==mid-l+1) lmx[now]+=lmx[rc];
    if(rmx[now]==r-mid)   rmx[now]+=rmx[lc];
    num[now] = max( rmx[lc]+lmx[rc] , max(num[lc],num[rc]) );
}
void build(int l,int r,int now)
{
    if(l==r){
        lmx[now] = rmx[now] = num[now] = 1 ; return ;
    }
    int mid = (l+r)>>1;
    build(l,mid,lc);build(mid+1,r,rc);pushup(l,r,now);
}
void up(int l,int r,int k,int now,int lim)
{
    if(l==r){
        if(l==k){
            num[now] = lmx[now] = rmx[now] = (lim?0:1);
        }
        return ;
    }
    int mid = (l+r)>>1;
    if(k<=mid) up(l,mid,k,lc,lim);
    if(k>mid)  up(mid+1,r,k,rc,lim);
    pushup(l,r,now);
}
int ask(int l,int r,int k,int now)
{
    if(num[now] == r-l+1 || !num[now]) return num[now] ;
    int mid = (l+r)>>1;
    if(k<=mid)
    {
        if(k>=mid-rmx[lc]+1) return ask(l,mid,k,lc) + ask(mid+1,r,mid+1,rc);
        else return ask(l,mid,k,lc);
    }
    else
    {
        if(k<=mid+1+lmx[rc]-1) return ask(l,mid,mid,lc) + ask(mid+1,r,k,rc);
        else return ask(mid+1,r,k,rc);
    }
}
int main()
{
    while(cin>>n>>m)
    {
        stack<int>s;
        build(1,n,1);
        while(m--)
        {
            cin>>ch;
            if(ch=='D'){
                cin>>k; s.push(k);
                up(1,n,k,1,1);
            }
            else if(ch=='Q'){
                cin>>k;
                cout<<ask(1,n,k,1)<<endl;
            }
            else {
                up(1,n,s.top(),1,0);s.pop();
            }
        }
    }
}

 

标签:HDU,now,Warfare,1540,int,mid,num,include,lc
来源: https://www.cnblogs.com/Shallow-dream/p/12828962.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有