题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少。

析:首先可以用set水过,set用来记录每个被破坏的村庄,然后查找时,只要查找左右两个端点好。

用线段树的话,就维护三个值分别是左端点连续右端点连续,全连续的最长的区别,然后用线段树维护就好。

代码如下:

set过:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e4 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} set<int> destroy;
stack<int> st; int main(){
while(scanf("%d %d", &n, &m) == 2){
destroy.clear();
while(!st.empty()) st.pop();
destroy.insert(0);
destroy.insert(n+1);
while(m--){
char s[5];
int x;
scanf("%s", s);
if(s[0] == 'D'){
scanf("%d", &x);
st.push(x);
destroy.insert(x);
}
else if(s[0] == 'R'){
if(st.empty()) continue;
destroy.erase(st.top());
st.pop();
}
else{
scanf("%d", &x);
if(destroy.count(x)){
printf("0\n"); continue;
}
set<int> :: iterator it1 = destroy.lower_bound(x);
set<int> :: iterator it2 = it1--;
printf("%d\n", *it2-*it1-1);
}
}
}
return 0;
}

  

线段树:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e4 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} //0 - left 1 - right 2 - all
int sum[maxn<<2][3]; void push_up(int rt, int len){
int l = rt<<1, r = rt<<1|1;
sum[rt][0] = sum[l][0];
sum[rt][1] = sum[r][1];
sum[rt][2] = max(max(sum[l][2], sum[r][2]), sum[l][1]+sum[r][0]);
if(sum[l][0] == len-len/2) sum[rt][0] += sum[r][0];
if(sum[r][1] == len/2) sum[rt][1] += sum[l][1];
} void build(int l, int r, int rt){
sum[rt][0] = sum[rt][1] = sum[rt][2] = r - l + 1;
if(l == r) return ;
int m = l + r >> 1;
build(lson);
build(rson);
} void update(int M, int val, int l, int r, int rt){
if(l == r){
sum[rt][0] = sum[rt][1] = sum[rt][2] = val;
return ;
}
int m = l + r >> 1;
if(M <= m) update(M, val, lson);
else update(M, val, rson);
push_up(rt, r-l+1);
} int query(int M, int l, int r, int rt){
if(l == r || sum[rt][2] == 0 || sum[rt][2] == r-l+1) return sum[rt][2];
int m = l + r >> 1;
if(M <= m){
if(M > m - sum[rt<<1][1]) return query(M, lson) + sum[rt<<1|1][0];
return query(M, lson);
}
if(M < m+1 + sum[rt<<1|1][0]) return query(M, rson) + sum[rt<<1][1];
return query(M, rson);
} stack<int> st; int main(){
while(scanf("%d %d", &n, &m) == 2){
build(1, n, 1);
char s[10];
while(!st.empty()) st.pop();
while(m--){
scanf("%s", s);
int x;
if(s[0] == 'D'){
scanf("%d", &x);
st.push(x);
update(x, 0, 1, n, 1);
}
else if(s[0] == 'R'){
if(st.empty()) continue;
update(st.top(), 1, 1, n, 1);
st.pop();
}
else{
scanf("%d", &x);
printf("%d\n", query(x, 1, n, 1));
}
}
}
return 0;
}

  

HDU 1540 Tunnel Warfare (线段树或set水过)的更多相关文章

  1. hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并

    Tunnel Warfare Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  2. HDU 1540 Tunnel Warfare 线段树区间合并

    Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...

  3. hdu 1540 Tunnel Warfare(线段树区间统计)

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

  4. HDU 1540 Tunnel Warfare (线段树)

    Tunnel Warfare Problem Description During the War of Resistance Against Japan, tunnel warfare was ca ...

  5. HDU 1540 Tunnel Warfare (线段树)

    题目大意: n 个村庄排列在一条直线上,相邻的村庄有地道连接,除首尾两个村庄外,其余村庄都有两个相邻的村庄.其中有 3 中操作 D x :表示摧毁编号为 x 的村庄,Q x:表示求出包含村庄 x 的最 ...

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

    题意: 三个操作符 D x:摧毁第x个隧道 R x:修复上一个被摧毁的隧道,将摧毁的隧道入栈,修复就出栈 Q x:查询x所在的最长未摧毁隧道的区间长度. 1.如果当前区间全是未摧毁隧道,返回长度 2. ...

  7. hdu 1540 Tunnel Warfare 线段数区间合并

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

  8. HDU 1540 Tunnel Warfare(最长连续区间 基础)

    校赛,还有什么途径可以申请加入ACM校队?  Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/ ...

  9. hdu 1540 Tunnel Warfare (区间线段树(模板))

    http://acm.hdu.edu.cn/showproblem.php?pid=1540 Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) ...

随机推荐

  1. jQuery的ajax跨域实现

    今天有人问我跨域ajax请求是否可以发送,之前没接触过此类问题,没答上,后来查了下,以下备忘. 我在本地建了三个站点,并设置了host文件模拟跨子域和跨全域 coolkissbh.com blog.c ...

  2. Redis 字符串(String)

    Redis 字符串(String) Redis 字符串数据类型的相关命令用于管理 redis 字符串值,基本语法如下: 语法 redis 127.0.0.1:6379> COMMAND KEY_ ...

  3. photoshop画矩形款

    1.屏幕截图 2.在photoshop新建图形 3.用矩形选框 4.右键打开描边,宽度设成5个像素

  4. $GLOBALS超级全局变量(PHP学习)

    1.$GLOBALS是一个数组,里面有所有的全局变量 2.$GLOBALS是超级全局变量,函数内部可以通过它直接操作全局变量.(严重不推荐,因为违反了封装原则) 3.通过$GLOBALS操作全局变量, ...

  5. 73个word使用终极技巧

    1.问:Word里边怎样设置每页不同的页眉?如何使不同的章节显示的页眉不同? 答:分节,每节可以设置不同的页眉.文件——页面设置——版式——页眉和页脚——首页不同 2.问:请问Word中怎样让每一章用 ...

  6. 【转】Jenkins+Ant+Jmeter搭建持续集成的接口测试平台

    一.什么是接口测试? 接口测试是测试系统组件间接口的一种测试.接口测试主要用于检测外部系统与系统之间以及内部各个子系统之间的交互点.测试的重点是要检查数据的交换,传递和控制管理过程,以及系统间的相互逻 ...

  7. Py修行路 python基础 (十七)property 静态方法 类方法

    一.property 属性 特性 (装饰器是可调用对象,被装饰对象也是可调用对象)   1.在类内函数属性上添加一个@property,就会对函数属性进行伪装. import math class C ...

  8. get与post两种方式的优缺点

    get: get是从服务器上获取数据,post是向服务器传送数据: get传送的数据量较小,不能大于2KB.post传送的数据量较大,一般被默认为不受限制.但理论上,IIS4中最大量为80KB,IIS ...

  9. WEB扫描器Atscan的安装和使用

    项目地址:https://github.com/AlisamTechnology/ATSCAN root@sch01ar:/sch01ar# git clone https://github.com/ ...

  10. Python类(一)-实例化一个类

    #-*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" class Person(): n = 123 #类变量 def __init_ ...