Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 7367    Accepted Submission(s): 2870

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
 

【题解】

这题为求某个区间从最右往左(或从左往右)的连续序列提供了一个很好的思路。

具体的;

我们这道题的有没有被损坏可以构成一个01串。

然后我们用线段树的域来存储从最左端开始往右的连续的1的个数以及从最右端开始往左的连续的1的个数;

分别表示为llx[rt],rlx[rt];

在建树的时候可以维护一个序列全部为1的llx,rlx的信息;

然后单节点的操作也没问题,操作完修改就好了。

然后就是求一个位置x它所在的连续块的长度。

可以这样。

求1..x-1中从x-1往左连续的1的个数。

求x+1..n中从x+1往右连续的1的个数。

然后加上本身就好。

实现的方法很巧妙。自己看代码吧。

然后说一下。

这题hdu上是多组数据。

然后会重复出现某个村庄x。即x被多次损坏。

但只要修一次就能好。

但是如果你修好了x。栈中又出现了一次x。而你恰好要进行R操作。

那么你不能跳过这个x。虽然它已经修好了。但是还是要消耗一个R操作。

【代码】

#include <cstdio>
#include <algorithm>
#include <cstring>
#define lson begin,m,rt<<1
#define rson m+1,end,rt<<1|1 using namespace std; const int MAXN = 51000; int n, m;
int llx[MAXN * 4],rlx[MAXN*4],stack[MAXN*2],top;
bool lph[MAXN*4],rph[MAXN*4],huihuai[MAXN]; void push_up(int rt,int len) //合并区间要用到节点所代表的区间的长度。
{
llx[rt] = llx[rt << 1];
if (llx[rt] == (len - (len >> 1)))
llx[rt] += llx[rt << 1 | 1];
rlx[rt] = rlx[rt << 1 | 1];
if (rlx[rt] == (len >> 1))
rlx[rt] += rlx[rt << 1];
lph[rt] = lph[rt << 1];
rph[rt] = rph[rt << 1 | 1];
} void build(int begin, int end, int rt)
{
if (begin == end)
{
lph[rt] = rph[rt] = false;
llx[rt] = rlx[rt] = 1;
return;
}
int m = (begin + end) >> 1;
build(lson);
build(rson);
push_up(rt,end-begin+1);
} void up_data(int pos, int num,int begin, int end, int rt) //把某个点修好或毁坏
{
if (begin == end)
{
if (num == 1)
{
lph[rt] = rph[rt] = true;
llx[rt] = rlx[rt] = 0;
}
else
{
lph[rt] = rph[rt] = false;
llx[rt] = rlx[rt] = 1;
}
return;
}
int m = (begin + end) >> 1;
if (pos <= m)
up_data(pos, num, lson);
else
up_data(pos, num, rson);
push_up(rt, end - begin+1);
} int query_left(int l, int r, int begin, int end, int rt)//这是1..x-1中从x-1往左的连续的1的个数
{
if (l <= begin && end <= r)
return rlx[rt];
int m = (begin + end) >> 1;
if (r <= m)
return query_left(l, r, lson);
else
if (m < l)
return query_left(l, r, rson);
else
{
int temp1 = query_left(l, m, lson); //注意所求区间发生了改变。
int temp2 = query_left(m+1, r, rson);
if (r - m == temp2) //如果整个(m+1..r)都是连续的1则可以加上左半部分
return temp1 + temp2;
return temp2;
} } int query_right(int l, int r, int begin, int end, int rt)
{
if (l <= begin && end <= r)
return llx[rt];
int m = (begin + end) >> 1;
if (r <= m)
return query_right(l, r, lson);
else
if (m < l)
return query_right(l, r, rson);
else
{
int temp1 = query_right(l, m, lson);
int temp2 = query_right(m + 1, r, rson);
if (m - l + 1 == temp1)
return temp1 + temp2;
return temp1;
}
} void output_ans()
{
for (int i = 1; i <= m; i++)
{
char op[10];
int x;
scanf("%s", op);
if (op[0] == 'D')
{
scanf("%d", &x);
up_data(x, 1, 1, n, 1);
huihuai[x] = true;
top++;
stack[top] = x;
}
else
if (op[0] == 'R')
{
if (top > 0)
{
if (huihuai[stack[top]])
{
huihuai[stack[top]] = false;
up_data(stack[top], 0, 1, n, 1);
top--;
}
}
}
else
if (op[0] == 'Q')
{
scanf("%d", &x);
if (huihuai[x])
printf("0\n");
else
{
int sum1 = 0, sum2 = 0;
if (x >= 2)
sum1 = query_left(1, x - 1, 1, n, 1);
if (x <= n - 1)
sum2 = query_right(x + 1, n, 1, n, 1);
if (!huihuai[x])
sum2++;
printf("%d\n", sum1 + sum2);
}
}
}
} void init()
{
memset(huihuai, false, sizeof(huihuai));
memset(stack, 0, sizeof(stack));
top = 0;
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
//freopen("F:\\rush_out.txt", "w", stdout);
while (scanf("%d%d", &n, &m) != EOF)
{
init();
build(1, n, 1);
output_ans();
}
return 0; }

【38.96%】【hdu 1540】Tunnel Warfare的更多相关文章

  1. 【改革春风吹满地 HDU - 2036 】【计算几何-----利用叉积计算多边形的面积】

    利用叉积计算多边形的面积 我们都知道计算三角形的面积时可以用两个邻边对应向量积(叉积)的绝对值的一半表示,那么同样,对于多边形,我们可以以多边形上的一个点为源点,作过该点并且过多边形其他点中的某一个的 ...

  2. hdu 1540/POJ 2892 Tunnel Warfare 【线段树区间合并】

    Tunnel Warfare                                                             Time Limit: 4000/2000 MS ...

  3. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  4. 【二分】【最长上升子序列】HDU 5489 Removed Interval (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5489 题目大意: 一个N(N<=100000)个数的序列,要从中去掉相邻的L个数(去掉整个区间 ...

  5. 【贪心】【模拟】HDU 5491 The Next (2015 ACM/ICPC Asia Regional Hefei Online)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5491 题目大意: 一个数D(0<=D<231),求比D大的第一个满足:二进制下1个个数在 ...

  6. 【动态规划】【二分】【最长上升子序列】HDU 5773 The All-purpose Zero

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5773 题目大意: T组数据,n个数(n<=100000),求最长上升子序列长度(0可以替代任何 ...

  7. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

  8. 【归并排序】【逆序数】HDU 5775 Bubble Sort

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 题目大意: 冒泡排序的规则如下,一开始给定1~n的一个排列,求每个数字在排序过程中出现的最远端 ...

  9. 【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 题目大意: T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai  的数 ...

随机推荐

  1. python中Sting字符串

    字符串连接 方法1: 用字符串的join方法 a = ['a','b','c','d'] content = '' content = ''.join(a) print content 方法2: 用字 ...

  2. Android实战:手把手实现“捧腹网”APP(一)-----捧腹网网页分析、数据获取

    Android实战:手把手实现"捧腹网"APP(一)-–捧腹网网页分析.数据获取 Android实战:手把手实现"捧腹网"APP(二)-–捧腹APP原型设计.实 ...

  3. 【JZOJ4841】【NOIP2016提高A组集训第4场11.1】平衡的子集

    题目描述 夏令营有N个人,每个人的力气为M(i).请大家从这N个人中选出若干人,如果这些人可以分成两组且两组力气之和完全相等,则称为一个合法的选法,问有多少种合法的选法? 数据范围 40%的数据满足: ...

  4. python 顺序传入

  5. Java练习 SDUT-2670_3-1 Point类的构造函数

    3-1 Point类的构造函数 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 通过本题目的练习可以掌握类的构造函数的定 ...

  6. 学习Vue.js

    Vue.js官网 Vue.js官方教程 Vue.js教程 哔哩哔哩Vue.js教程 Vue.js百度百科

  7. 三角形数且是完全平方数 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 E.Half-consecutive Numbers

    三角形数:an=n*(n+1)/2; 完全平方数:bn=c^2; 既是三角形数又是完全平方数:An=6*A(n-1)-A(n-2)+2; A[23]={ 0, 1, 8, 49, 288, 1681, ...

  8. python 类与类之间的关系. 特殊成员

    一.类与类之间的关系 1.依赖关系 在方法的参数位置把另一个类的对象作为参数进行传递 class Person: def play(self, tools): # 通过参数的传递把另一个类的对象传递进 ...

  9. LightOJ 1370 Bi-shoe and Phi-shoe【欧拉函数 && 质数】

    题目链接: http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1370 题意: 给定值,求满足欧拉值大于等于这个 ...

  10. mysql原来是按自然日统计。怎么可以用今天10点到次日10点这样统计???

    关于网友提出的" mysql原来是按自然日统计.怎么可以用今天10点到次日10点这样统计???"问题疑问,本网通过在网上对" mysql原来是按自然日统计.怎么可以用今天 ...