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 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4
 
Sample Output
1
0
2
4
 
Source
 
题解:区间合并基础题
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<bitset>
#include<set>
#include<vector>
#include<stack>
///#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define memfy(a) memset(a,-1,sizeof(a))
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a,b) scanf("%d%d",&a,&b)
#define mod 530600414
#define eps 0.0000000001
#define inf 1000000000.0
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//**************************************** #define maxn 500000+5
struct ss
{
int l,r,L,R,v;
}tr[maxn];
int n,m,lflag,rflag;
void pushup(int k)
{
if(tr[k<<].L+tr[k<<|].R==(tr[k].r-tr[k].l+))
{
tr[k].L=tr[k].R=(tr[k].r-tr[k].l+);
}
else {
if(tr[k<<].L>=(tr[k<<].r-tr[k<<].l+))
tr[k].L=tr[k<<].L+tr[k<<|].L;
else tr[k].L=tr[k<<].L;
if(tr[k<<|].R>=tr[k<<|].r-tr[k<<|].l+)
tr[k].R=tr[k<<|].R+tr[k<<].R;
else tr[k].R=tr[k<<|].R;
}
}
void build(int k,int s,int t)
{
tr[k].l=s;
tr[k].r=t;
tr[k].L=tr[k].R=t-s+;
if(s==t)
{
return ;
}
int mid=(s+t)>>;
build(k<<,s,mid);
build(k<<|,mid+,t);
}
void update(int k,int x,int c)
{
if(tr[k].l==x&&tr[k].r==x)
{
tr[k].L=tr[k].R=c;return ;
}
int mid=(tr[k].l+tr[k].r)>>;
if(x<=mid)update(k<<,x,c);
else update(k<<|,x,c);
pushup(k);
}
int ask(int k,int x)
{
if(tr[k].l==tr[k].r&&tr[k].l==x)
{
if(tr[k].L)rflag=lflag=;
return tr[k].L;
}
int mid=(tr[k].l+tr[k].r)>>;
int A=,B=;
if(x<=mid)
{
A=ask(k<<,x);
}
else {
B=ask(k<<|,x);
}
int ans=A+B;
if(A && rflag)
{
ans+=tr[k<<|].L;
if(tr[k<<|].L<tr[k<<|].r-tr[k<<|].l+)
rflag=;
}
if(B && lflag)
{
ans+=tr[k<<].R;
if(tr[k<<].R<tr[k<<].r-tr[k<<].l+)
lflag=;
}
return ans;
}
int main()
{
char ch[];
int x; while(scanf("%d%d",&n,&m)!=EOF)
{
stack<int >q;
int last=;
build(,,n);
FOR(i,,m)
{
scanf("%s",ch);
if(ch[]=='D')
{cin>>x;
q.push(x);
update(,x,);
}
else if(ch[]=='Q')
{
cin>>x;
rflag=lflag=;
cout<<ask(,x)<<endl;
}
else {
last=q.top();
q.pop();
update(,last,);
}
}
}
return ;
}

代码

补个 treap写法

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<map>
using namespace std;
struct data{
int l,r,v,size,rnd,w;
}tr[];
int n,size,root,ans;
int last[];
void update(int k)//更新结点信息
{
tr[k].size=tr[tr[k].l].size+tr[tr[k].r].size+tr[k].w;
}
void rturn(int &k)
{
int t=tr[k].l;tr[k].l=tr[t].r;tr[t].r=k;
tr[t].size=tr[k].size;update(k);k=t;
}
void lturn(int &k)
{
int t=tr[k].r;tr[k].r=tr[t].l;tr[t].l=k;
tr[t].size=tr[k].size;update(k);k=t;
}
void insert(int &k,int x)
{
if(k==)
{
size++;k=size;
tr[k].size=tr[k].w=;tr[k].v=x;tr[k].rnd=rand();
return;
}
tr[k].size++;
if(tr[k].v==x)tr[k].w++;
else if(x>tr[k].v)
{
insert(tr[k].r,x);
if(tr[tr[k].r].rnd<tr[k].rnd)lturn(k);
}
else
{
insert(tr[k].l,x);
if(tr[tr[k].l].rnd<tr[k].rnd)rturn(k);
}
}
void del(int &k,int x)
{
if(k==)return;
if(tr[k].v==x)
{
if(tr[k].w>)
{
tr[k].w--;tr[k].size--;return;
}
if(tr[k].l*tr[k].r==)k=tr[k].l+tr[k].r;
else if(tr[tr[k].l].rnd<tr[tr[k].r].rnd)
rturn(k),del(k,x);
else lturn(k),del(k,x);
}
else if(x>tr[k].v)
tr[k].size--,del(tr[k].r,x);
else tr[k].size--,del(tr[k].l,x);
} int query_rank(int k,int x)
{
if(k==)return ;
if(tr[k].v==x)return tr[tr[k].l].size+;
else if(x>tr[k].v)
return tr[tr[k].l].size+tr[k].w+query_rank(tr[k].r,x);
else return query_rank(tr[k].l,x);
} int query_num(int k,int x)
{
if(k==)return ;
if(x<=tr[tr[k].l].size)
return query_num(tr[k].l,x);
else if(x>tr[tr[k].l].size+tr[k].w)
return query_num(tr[k].r,x-tr[tr[k].l].size-tr[k].w);
else return tr[k].v;
} void query_pre(int k,int x)
{
if(k==)return;
if(tr[k].v == x) {
ans = k;
return ;
}
if(tr[k].v<x)
{
ans=k;query_pre(tr[k].r,x);
}
else query_pre(tr[k].l,x);
}
void query_sub(int k,int x)
{
if(k==)return;
if(tr[k].v == x) {
ans = k;
return ;
}
if(tr[k].v>x)
{
ans=k;query_sub(tr[k].l,x);
}
else query_sub(tr[k].r,x);
}
int m;
map<int,int > mp;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF) {
int cnt = ,t1, t2 , x;
root = ;
mp.clear();
tr[root].size = ;
char ch[];
insert(root,);
insert(root,n+);
for(int i=;i<=m;i++) {
scanf("%s",ch);
if(ch[]=='D') {
scanf("%d",&x) ;
if(mp[x]==) {
insert(root,x);
mp[x] = ;
}last[++cnt] = x; }
if(ch[]=='R') {
if(!cnt) continue;
if(mp[last[cnt]] == ) {
del(root,last[cnt]);
mp[last[cnt]] = ;
}
cnt--;
}
if(ch[]=='Q') {
scanf("%d",&x);
ans = ;
query_sub(root,x);
t1= tr[ans].v ;
ans = ;
query_pre(root,x) ;
t2 = tr[ans].v;
printf("%d\n", max(t1 - t2 - ,));
}
}
}
}

HDU 1540 Tunnel Warfare 平衡树 / 线段树:单点更新,区间合并的更多相关文章

  1. POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 D ...

  2. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  3. HDU 1540 Tunnel Warfare(线段树+区间合并)

    http://acm.hdu.edu.cn/showproblem.php?pid=1540 题目大意:抗日战争期间进行地道战,存在n个村庄用地道连接,输入D表示破坏某个村庄(摧毁与其相连的地道, 包 ...

  4. hdu 1540 Tunnel Warfare(线段树)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1540 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. ...

  5. HDU - 1540 Tunnel Warfare(线段树区间合并)

    https://cn.vjudge.net/problem/HDU-1540 题意 D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. 分析 线段树的区间内,我 ...

  6. hdu 1540 Tunnel Warfare (线段树 区间合并)

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  7. hdu 1540 Tunnel Warfare (线段树,维护当前最大连续区间)

    Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively i ...

  8. HDU 3308 LCIS(线段树单点更新区间合并)

    LCIS Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting ...

  9. hdu 1754 I Hate It 线段树 单点更新 区间最值

    线段树功能:update:单点更新 query:区间最值 #include <bits/stdc++.h> #define lson l, m, rt<<1 #define r ...

随机推荐

  1. The Imitation Game

    <The Imitation Game>是一部非常好的电影,讲述了人工智能之父——阿兰图灵的传奇一生. 重点讲述了他通过破译德国的通讯密码缩短了二战的持续时间,因而拯救了无数生命的伟大事迹 ...

  2. ffmpeg-20160525-git-bin

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...

  3. 数据库SQL语句

    增删改查 --增加 create INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....) --删除 delete DELETE FROM 表名 ...

  4. Windows 下的 MarkdownPad 2 工具使用

    MarkdownPad 2 工具(windows) 一. 软件下载和安装 下载登陆官网: http://markdownpad.com/ 点击Download,会自动下载.或者直接点击http://m ...

  5. MFC Initinstance中DoModal()返回-1

    新建一个基于对话框的MFC应用程序,在App的Initinstance中调用对话框DoModal()来显示对话框,这是框架的内容,应用程序框架生成的全部是正常的. 当把我对话框的资源文件提取到一个资源 ...

  6. VC++ 判断当前系统为32位还是64位

    尝试了在VC++环境下判断系统为32位还是64位的方法,亲测有效!提供的函数如下 BOOL IsWow64() { typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) ...

  7. 【leetcode】Text Justification(hard) ☆

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  8. 在HTML网页中设置弹出窗口的办法

    [1.最基本的弹出窗口代码] 其实代码非常简单: <SCRIPT LANGUAGE="javascript"> <!-- window.open ('page.h ...

  9. JS判断IE版本并在页面显示内容

    <script type="text/javascript"> var isIE = function (ver) { var b = document.createE ...

  10. 51nod1057(python2计算n!)

    题目链接:www.51nod.com/onlineJudge/questionCode.html#!problemId=1057 思路:直接for循环呗- 代码: n = int( raw_input ...