poj2892 Tunnel Warface
2015-12-03 22:57
211 查看
Tunnel Warfare
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
Sample Output
Hint
An illustration of the sample input:
Source
POJ Monthly--2006.07.30, updog
平衡树的应用
平衡树中保存所有被炸毁的节点。
对于炸毁和修复操作,直接在平衡树中插入或删除。
对于查询操作,先判断该节点是否被炸毁,如果被炸毁答案为0,否则在平衡树中求出前驱x和后继y,答案为y-x-1。
还有一种方法是二分+树状数组,感觉速度比这个慢就没有写…
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<stack>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define LL long long
#define MAXN 50005
#define pa pair<int,int>
#define INF 1000000000
using namespace std;
int n,m,x,tot=0,rt=0,l[MAXN],r[MAXN],rnd[MAXN],v[MAXN];
char op;
bool f[MAXN];
stack<int> st;
inline int read()
{
int ret=0,flag=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') flag=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();}
return ret*flag;
}
inline void rturn(int &k)
{
int tmp=l[k];
l[k]=r[tmp];r[tmp]=k;
k=tmp;
}
inline void lturn(int &k)
{
int tmp=r[k];
r[k]=l[tmp];l[tmp]=k;
k=tmp;
}
inline void ins(int &k,int x)
{
if (!k){k=++tot;v[k]=x;l[k]=r[k]=0;rnd[k]=rand();return;}
if (x<v[k]) {ins(l[k],x);if (rnd[l[k]]<rnd[k]) rturn(k);}
else {ins(r[k],x);if (rnd[r[k]]<rnd[k]) lturn(k);}
}
inline void del(int &k,int x)
{
if (v[k]==x)
{
if (!l[k]||!r[k]) k=l[k]+r[k];
else if (rnd[l[k]]<rnd[r[k]]) {rturn(k);del(k,x);}
else {lturn(k);del(k,x);}
return;
}
if (x<v[k]) del(l[k],x);
else del(r[k],x);
}
inline int pre(int k,int x)
{
if (!k) return 0;
else if (x<=v[k]) return pre(l[k],x);
else {int tmp=pre(r[k],x);return tmp==0?v[k]:tmp;}
}
inline int suc(int k,int x)
{
if (!k) return n+1;
if (x>=v[k]) return suc(r[k],x);
else {int tmp=suc(l[k],x);return tmp==n+1?v[k]:tmp;}
}
inline int getans(int x)
{
if (f[x]) return 0;
return suc(rt,x)-pre(rt,x)-1;
}
int main()
{
memset(f,false,sizeof(f));
n=read();m=read();
while (m--)
{
op=getchar();while (op<'A'||op>'Z') op=getchar();
if (op=='D') {x=read();f[x]=true;st.push(x);ins(rt,x);}
else if (op=='Q') {x=read();printf("%d\n",getans(x));}
else {del(rt,st.top());f[st.top()]=false;st.pop();}
}
}
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 7434 | Accepted: 3070 |
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
Hint
An illustration of the sample input:
OOOOOOO D 3 OOXOOOO D 6 OOXOOXO D 5 OOXOXXO R OOXOOXO R OOXOOOO
Source
POJ Monthly--2006.07.30, updog
平衡树的应用
平衡树中保存所有被炸毁的节点。
对于炸毁和修复操作,直接在平衡树中插入或删除。
对于查询操作,先判断该节点是否被炸毁,如果被炸毁答案为0,否则在平衡树中求出前驱x和后继y,答案为y-x-1。
还有一种方法是二分+树状数组,感觉速度比这个慢就没有写…
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<stack>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define LL long long
#define MAXN 50005
#define pa pair<int,int>
#define INF 1000000000
using namespace std;
int n,m,x,tot=0,rt=0,l[MAXN],r[MAXN],rnd[MAXN],v[MAXN];
char op;
bool f[MAXN];
stack<int> st;
inline int read()
{
int ret=0,flag=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') flag=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();}
return ret*flag;
}
inline void rturn(int &k)
{
int tmp=l[k];
l[k]=r[tmp];r[tmp]=k;
k=tmp;
}
inline void lturn(int &k)
{
int tmp=r[k];
r[k]=l[tmp];l[tmp]=k;
k=tmp;
}
inline void ins(int &k,int x)
{
if (!k){k=++tot;v[k]=x;l[k]=r[k]=0;rnd[k]=rand();return;}
if (x<v[k]) {ins(l[k],x);if (rnd[l[k]]<rnd[k]) rturn(k);}
else {ins(r[k],x);if (rnd[r[k]]<rnd[k]) lturn(k);}
}
inline void del(int &k,int x)
{
if (v[k]==x)
{
if (!l[k]||!r[k]) k=l[k]+r[k];
else if (rnd[l[k]]<rnd[r[k]]) {rturn(k);del(k,x);}
else {lturn(k);del(k,x);}
return;
}
if (x<v[k]) del(l[k],x);
else del(r[k],x);
}
inline int pre(int k,int x)
{
if (!k) return 0;
else if (x<=v[k]) return pre(l[k],x);
else {int tmp=pre(r[k],x);return tmp==0?v[k]:tmp;}
}
inline int suc(int k,int x)
{
if (!k) return n+1;
if (x>=v[k]) return suc(r[k],x);
else {int tmp=suc(l[k],x);return tmp==n+1?v[k]:tmp;}
}
inline int getans(int x)
{
if (f[x]) return 0;
return suc(rt,x)-pre(rt,x)-1;
}
int main()
{
memset(f,false,sizeof(f));
n=read();m=read();
while (m--)
{
op=getchar();while (op<'A'||op>'Z') op=getchar();
if (op=='D') {x=read();f[x]=true;st.push(x);ins(rt,x);}
else if (op=='Q') {x=read();printf("%d\n",getans(x));}
else {del(rt,st.top());f[st.top()]=false;st.pop();}
}
}
相关文章推荐
- Linux内核与根文件系统的关系
- Android Studio 快捷键整理分享
- uImage、zImage、bzImage、vlinzx区别
- WCF 入门(29)
- 三角形 JAVA 代码
- Flex布局之box-flex
- Shell学习笔记 - 正则表达式
- 黑马程序员—javaSE—异常
- 我的思维方式和我的性格
- Android进程和线程 --消息队列模型--ThreadLocal (3)(2015-12-02 19:41)
- 冰山
- 使用Python开发windows GUI程序入门实例
- 《JavaScript权威指南》学习笔记 第六天 开始学习DOM了。
- C语言实现封装
- 解决itunes启动后一直显示 正在访问iTunes Store 的问题
- 项目跟进经验总结
- 黑马程序员—javaSE—Mep集合
- 美学和音乐
- 按钮设置文字图片排版
- 基于MLlib的机器学习--协同过滤与推荐