hdu 1540 Tunnel Warfare 线段数区间合并

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<bits/stdc++.h>
using namespace std;
#define ll __int64
#define esp 0.00000000001
const int N=1e5+,M=1e6+,inf=1e9+,mod=;
int max(int x,int y,int z)
{
return max(x,max(y,z));
}
struct is
{
int l,r,len,lans,rans,sans;//这个区间的左端连续最大,右端,区间连续最大
}tree[N*];
void buildtree(int l,int r,int pos)
{
tree[pos].l=l;
tree[pos].r=r;
tree[pos].len=tree[pos].lans=tree[pos].rans=tree[pos].sans=(r-l+);
if(l==r)
return;
int mid=(l+r)>>;
buildtree(l,mid,pos<<);
buildtree(mid+,r,pos<<|);
}
void update(int point,int change,int pos)
{
if(tree[pos].l==point&&tree[pos].r==tree[pos].l)
{
tree[pos].len=tree[pos].lans=tree[pos].rans=tree[pos].sans=change;
return;
}
int mid=(tree[pos].l+tree[pos].r)>>;
if(point<=mid)
update(point,change,pos<<);
else
update(point,change,pos<<|);
tree[pos].lans=tree[pos<<].lans;
tree[pos].rans=tree[pos<<|].rans;
tree[pos].sans=max(tree[pos<<].sans,tree[pos<<|].sans,tree[pos<<].rans+tree[pos<<|].lans);
if(tree[pos<<].lans==tree[pos<<].r-tree[pos<<].l+)
tree[pos].lans+=tree[pos<<|].lans;
if(tree[pos<<|].rans==tree[pos<<|].r-tree[pos<<|].l+)
tree[pos].rans+=tree[pos<<].rans;
}
int query(int x,int pos)
{
if(tree[pos].l==x&&tree[pos].r==x||tree[pos].sans==||tree[pos].sans==tree[pos].r-tree[pos].l+)
{
return tree[pos].sans;
}
int mid=(tree[pos].l+tree[pos].r)>>;
if(x<=mid)
{
if(x>=tree[pos<<].r-tree[pos<<].rans+)
return query(x,pos<<)+query(mid+,pos<<|);
else
return query(x,pos<<);
}
else
{
if(x<=tree[pos<<|].l+tree[pos<<|].lans-)
return query(x,pos<<|)+query(mid,pos<<);
else
return query(x,pos<<|);
}
}
char a[];
int main()
{
int x,y,z,i,t;
int T;
while(~scanf("%d%d",&x,&y))
{
stack<int>s;
buildtree(,x,);
for(i=;i<y;i++)
{
scanf("%s",a);
if(a[]=='D')
{
scanf("%d",&z);
update(z,,);
s.push(z);
}
else if(a[]=='Q')
{
scanf("%d",&z);
printf("%d\n",query(z,));
}
else
{
update(s.top(),,);
s.pop();
}
}
}
return ;
}
上一篇:删除Win10资源管理器中的3D对象/音乐/视频文件夹


下一篇:Spring整合Quartz定时发送邮件