0
点赞
收藏
分享

微信扫一扫

POJ-2892/HDU-1540 Tunnel Warfare (树状数组+二分查找)


Tunnel Warfare

Time Limit: 1000MS

 

Memory Limit: 131072K

Total Submissions: 9766

 

Accepted: 4048

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 (nm ≤ 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:

  1. D x: The x-th village was destroyed.
  2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
  3. 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

 

题意:

三个操作

D pos  将pos位置摧毁,让它和周围不相连。

Q pos 问和pos 相连的有多少个村庄。

R 修复最近摧毁的村庄。

分析:

思路稍微转换一下,询问操作Q x,查找当前x所在的最长的连续1,我想的思路是二分查找x左右第一个0的位置,树状数组去维护,但就在二分的时候卡住了,我找不到它的单调性,冥思苦想实在想不到,看了别人的题解,才恍然大悟,位置越靠近x,成功的机会就越大,什么情况才算成功,

以左边为例子sum(x)-sum(mid-1)==x-mid+1  (mid-1 画一图就知道了),这时候mid就为正确的,如果成功我们希望的更更往左,r=mid,否则我们往右找符合的,l=mid+1,具体实现步骤看代码吧。

HDU数据有毒

他可能会有重复的毁灭

这题还有很多方法,回来再补。

poj通过代码

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
using namespace std;
typedef long long ll;
const int MAXN=50000+5;//最大元素个数

int n;//元素个数
int c[MAXN];//c[i]==A[i]+A[i-1]+...+A[i-lowbit(i)+1]

//返回i的二进制最右边1的值
int lowbit(int i)
{
return i&(-i);
}



//返回A[1]+...A[i]的和

ll sum(int x){
ll sum = 0;
while(x){
sum += c[x];
x -= lowbit(x);
}
return sum;
}

//令A[i] += val

void add(int x, ll val){
while(x <= n){ //注意这里的n,是树状数组维护的长度
c[x]+=val;
x += lowbit(x);
}
}
int find_1(int l,int r,int x)
{
int ans=x;
while(l<r)
{
int mid=(l+r)>>1;//cout<<" "<<mid<<" "<<sum(mid)<<" "<<sum(x)<<endl;
if(sum(x)-sum(mid-1)==x-mid+1)
{
r=mid;
}
else
l=mid+1;
}
return l;
}
int find_2(int l,int r,int x)
{
int ans=x;
while(l<r)
{

int mid=(l+r+1)>>1;
//
if(sum(mid)-sum(x-1)==mid-x+1)
{
l=mid;
}
else
{
r=mid-1;

}
}
return l;
}
int main()
{
int m;
while(scanf("%d%d",&n,&m)!=-1)
{
stack<int> s;

for(int i=1;i<=n;i++)
{
add(i,1);
}
char ch[10];
while(m--)
{
//cout<<m<<endl;
scanf("%s",ch);
int x;
if(ch[0]=='D')
{
scanf("%d",&x);
add(x,-1);
s.push(x);
}
else if(ch[0]=='R')
{

x=s.top();
//cout<<" "<<x<<" "<<sum(5)<<endl;
add(x,1);
s.pop();
}
else
{
scanf("%d",&x);
if(sum(x)-sum(x-1)==0)
{
printf("0\n");
continue;
}
int pos1=find_1(1,x+1,x);
int pos2=find_2(x-1,n,x);
//cout<<pos1<<" "<<pos2<<endl;
printf("%d\n",pos2-pos1+1);
}
}
}
}

 

HDU通过代码

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
using namespace std;
typedef long long ll;
const int MAXN=50000+5;//最大元素个数

int n;//元素个数
int c[MAXN],vis[MAXN];//c[i]==A[i]+A[i-1]+...+A[i-lowbit(i)+1]

int lowbit(int x){return x&(-x);}
void add(int x,int y)
{
while(x<=n+1)
{
c[x]+=y;
x+=lowbit(x);
}
}
int sum(int x)
{
int sum=0;
while(x>0)
{
sum+=c[x];
x-=lowbit(x);
}
return sum;
}
int find_1(int l,int r,int x)
{
int ans=x;
while(l<r)
{
int mid=(l+r)>>1;//cout<<" "<<mid<<" "<<sum(mid)<<" "<<sum(x)<<endl;
if(sum(x)-sum(mid-1)==x-mid+1)
{
r=mid;
}
else
l=mid+1;
}
return l;
}
int find_2(int l,int r,int x)
{
int ans=x;
while(l<r)
{

int mid=(l+r+1)>>1;
//
if(sum(mid)-sum(x-1)==mid-x+1)
{
l=mid;
}
else
{
r=mid-1;

}
}
return l;
}
int main()
{
int m;
while(scanf("%d%d",&n,&m)!=-1)
{
stack<int> s;
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
{
add(i,1);
}
memset(vis,0,sizeof(vis));
char ch[10];
while(m--)
{
//cout<<m<<endl;
scanf("%s",ch);
int x;
if(ch[0]=='D')
{
scanf("%d",&x);
s.push(x);
if(vis[x]==1) continue;
add(x,-1);
vis[x]=1;

}
else if(ch[0]=='R')
{
while(vis[s.top()]==0) s.pop();
x=s.top();
vis[x]=0;
add(x,1);
s.pop();
}
else
{
scanf("%d",&x);
if(vis[x]==1)
{
printf("0\n");
continue;
}
int pos1=find_1(1,x+1,x);
int pos2=find_2(x-1,n,x);
//cout<<pos1<<" "<<pos2<<endl;
printf("%d\n",pos2-pos1+1);
}
}
}
}

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;
using namespace std;
typedef long long ll;
const int MAXN=50000+5;//最大元素个数

int n;//元素个数
int c[MAXN],vis[MAXN];//c[i]==A[i]+A[i-1]+...+A[i-lowbit(i)+1]

int lowbit(int x){return x&(-x);}
void add(int x,int y)
{
while(x<=n+1)
{
c[x]+=y;
x+=lowbit(x);
}
}
int sum(int x)
{
int sum=0;
while(x>0)
{
sum+=c[x];
x-=lowbit(x);
}
return sum;
}
int find_1(int l,int r,int x)
{
int ans=x;
while(l<r)
{
int mid=(l+r)>>1;//cout<<" "<<mid<<" "<<sum(mid)<<" "<<sum(x)<<endl;
if(sum(x)-sum(mid-1)>=x-mid+1)
{
r=mid;
}
else
l=mid+1;
}
return l;
}
int find_2(int l,int r,int x)
{
int ans=x;
while(l<r)
{

int mid=(l+r+1)>>1;
//
if(sum(mid)-sum(x-1)>=mid-x+1)
{
l=mid;
}
else
{
r=mid-1;

}
}
return l;
}
int main()
{
int m;
while(scanf("%d%d",&n,&m)!=-1)
{
stack<int> s;
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
{
add(i,1);
}
memset(vis,0,sizeof(vis));
char ch[10];
while(m--)
{
//cout<<m<<endl;
scanf("%s",ch);
int x;
if(ch[0]=='D')
{
scanf("%d",&x);
s.push(x);
if(vis[x]==1) continue;
add(x,-1);
vis[x]=1;

}
else if(ch[0]=='R')
{

x=s.top();
vis[x]=0;
add(x,1);
s.pop();
}
else
{
scanf("%d",&x);
if(vis[x]==1)
{
printf("0\n");
continue;
}
int pos1=find_1(1,x+1,x);
int pos2=find_2(x-1,n,x);
//cout<<pos1<<" "<<pos2<<endl;
printf("%d\n",pos2-pos1+1);
}
}
}
}

 

举报

相关推荐

0 条评论