UVA 11402 - Ahoy, Pirates!(段树)
UVA 11402 - Ahoy, Pirates!
题意:总的来说意思就是给一个01串,然后有3种操作
1、把一个区间变成1
2、把一个区间变成0
3、把一个区间翻转(0变1,1变0)
思路:线段树搞,开一个延迟标记当前操作就可以,注意几种状态间的转变方式就可以
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define INF 0x3f3f3f3f
#define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2) const int N = 1100005; int t, s[N], n, sn;
char str[55]; struct Node {
int l, r, b, flag;
Node() {}
Node(int l, int r) {
this->l = l; this->r = r;
b = flag = 0;
}
int len() {
return r - l + 1;
}
} node[4 * N]; void pushup(int x) {
node[x].b = node[lson(x)].b + node[rson(x)].b;
} void build(int l, int r, int x = 0) {
node[x] = Node(l, r);
if (l == r) {
node[x].b = s[l];
return;
}
int mid = (node[x].l + node[x].r) / 2;
build(l, mid, lson(x));
build(mid + 1, r, rson(x));
pushup(x);
} void init() {
scanf("%d", &n);
sn = 0;
while (n--) {
int num;
scanf("%d", &num);
scanf("%s", str);
int len = strlen(str);
while (num--) {
for (int i = 0; i < len; i++)
s[sn++] = str[i] - '0';
}
}
build(0, sn - 1);
} //0不变,1黑。2白。3翻转
int getS(int a, int b) {
if (a == 3 && b == 3) return 0;
if (a == 3 && b == 1) return 2;
if (a == 3 && b == 2) return 1;
if (a == 3 && b == 0) return 3;
if (a == 2) return 2;
if (a == 1) return 1;
return 0;
} void pushdown(int x) {
if (node[x].flag) {
int ls = getS(node[x].flag, node[lson(x)].flag);
node[lson(x)].flag = ls;
if (node[x].flag == 2) node[lson(x)].b = 0;
if (node[x].flag == 1) node[lson(x)].b = node[lson(x)].len();
if (node[x].flag == 3) node[lson(x)].b = node[lson(x)].len() - node[lson(x)].b;
int rs = getS(node[x].flag, node[rson(x)].flag);
node[rson(x)].flag = rs;
if (node[x].flag == 2) node[rson(x)].b = 0;
if (node[x].flag == 1) node[rson(x)].b = node[rson(x)].len();
if (node[x].flag == 3) node[rson(x)].b = node[rson(x)].len() - node[rson(x)].b;
node[x].flag = 0;
}
} void set(int l, int r, int v, int x = 0) {
if (node[x].l >= l && node[x].r <= r) {
int ts = getS(v, node[x].flag);
if (v == 1)
node[x].b = node[x].len();
else if (v == 2)
node[x].b = 0;
else
node[x].b = node[x].len() - node[x].b;
node[x].flag = ts;
return;
}
pushdown(x);
int mid = (node[x].l + node[x].r) / 2;
if (l <= mid) set(l, r, v, lson(x));
if (r > mid) set(l, r, v, rson(x));
pushup(x);
} int S(int l, int r, int x = 0) {
if (node[x].l >= l && node[x].r <= r)
return node[x].b;
int mid = (node[x].l + node[x].r) / 2;
int ans = 0;
pushdown(x);
if (l <= mid) ans += S(l, r, lson(x));
if (r > mid) ans += S(l, r, rson(x));
pushup(x);
return ans;
} int main() {
int cas = 0;
scanf("%d", &t);
while (t--) {
init();
int q;
scanf("%d", &q);
char Q[5]; int a, b;
printf("Case %d:\n", ++cas);
int Qcas = 0;
while (q--) {
scanf("%s%d%d", Q, &a, &b);
if (Q[0] == 'F') set(a, b, 1);
if (Q[0] == 'E') set(a, b, 2);
if (Q[0] == 'I') set(a, b, 3);
if (Q[0] == 'S') printf("Q%d: %d\n", ++Qcas, S(a, b));
}
}
return 0;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
UVA 11402 - Ahoy, Pirates!(段树)的更多相关文章
- UVA11402 - Ahoy, Pirates!(线段树)
UVA11402 - Ahoy, Pirates!(线段树) option=com_onlinejudge&Itemid=8&category=24&page=show_pro ...
- 【UVA】11992 - Fast Matrix Operations(段树模板)
主体段树,要注意,因为有set和add操作,当慵懒的标志下推.递归优先set,后复发add,每次运行set行动add马克清0 WA了好几次是由于计算那一段的时候出问题了,可笑的是我对着模板找了一个多小 ...
- BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...
- ZOJ 1610 间隔染色段树
要长8000仪表板.间染色的范围,问:最后,能看到的颜色,而且颜色一共有段出现 覆盖段 数据对比水 水可太暴力 段树: #include "stdio.h" #include ...
- HDU 1394 Minimum Inversion Number (数据结构-段树)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- PKU A Simple Problem with Integers (段树更新间隔总和)
意甲冠军:一个典型的段树C,Q问题,有n的数量a[i] (1~n),C, a, b,c在[a,b]加c Q a b 求[a,b]的和. #include<cstdio> #include& ...
- BZOJ 2588 Count on a tree (COT) 是持久的段树
标题效果:两棵树之间的首次查询k大点的权利. 思维:树木覆盖树,事实上,它是正常的树木覆盖了持久段树. 由于使用权值段树可以寻求区间k大,然后应用到持久段树思想,间隔可以做减法.详见代码. CODE: ...
- lintcode-439-线段树的构造 II
439-线段树的构造 II 线段树是一棵二叉树,他的每个节点包含了两个额外的属性start和end用于表示该节点所代表的区间.start和end都是整数,并按照如下的方式赋值: 根节点的 start ...
- lintocde-247-线段树的查询 II
247-线段树的查询 II 对于一个数组,我们可以对其建立一棵 线段树, 每个结点存储一个额外的值 count 来代表这个结点所指代的数组区间内的元素个数. (数组中并不一定每个位置上都有元素) 实现 ...
随机推荐
- wpf dll和exe合并成一个新的exe
原文:wpf dll和exe合并成一个新的exe 微软有一个工具叫ILMerge可以合并dll exe等,但是对于wpf的应用程序而言这个工具就不好用了.我的这方法也是从国外一个博客上找来的.仅供大家 ...
- uva :10123 - No Tipping(dfs + 几何力矩 )
option=com_onlinejudge&Itemid=8&page=show_problem&category=109&problem=1064&mosm ...
- 服务器编程入门(5)Linux服务器程序规范
问题聚焦: 除了网络通信外,服务器程序通常还必须考虑许多其他细节问题,这些细节问题涉及面逛且零碎,而且基本上是模板式的,所以称之为服务器程序规范. 工欲善其事,必先利其器,这篇主要来探 ...
- ubuntu终端方向键不能用(主机名不显示)问题的解决
sudo gedit /etc/passwd 在/etc/passwd中改动该用户相应的shell:/bin/sh改为/bin/bash就可以解决该问题
- CMS垃圾回收机制
详解CMS垃圾回收机制 原创不易,未经允许,不得转载~~~ 什么是CMS? Concurrent Mark Sweep. 看名字就知道,CMS是一款并发.使用标记-清除算法的gc. CMS是针对老 ...
- mongoDB 查询附近的人的语句
mongoDB 自带LBS查询附近的人 {"location":{ $nearSphere: { $geometry: { type : "Point", co ...
- shufe前辈名师
前辈名师 姓名 现职/原职 郭秉文 中国现代大学之父.国立东南大学校长.哥伦比亚大学教育学博士,该校第一任校长.为了纪念郭秉文先生,勉励优秀学子,郭夏瑜女士在上海财经大学等校设立了“郭秉文奖学金” 马 ...
- VC中Tab control的用法
1. 新建一个MFC工程, 取名MyTab, 选择Dialog based, 然后Finish. 2. 删除对话框上默认添加的三个控件. 添加Tab Control控件并在Property属性中设置I ...
- [置顶] quartznet任务调度和消息调度(JAVA与C#版对比)
quartznet任务调度和消息调度 1. 作用 自动执行任务. 2. 下载地址 NET版本 JAVA版本 1下载 http://quartznet.sourceforge.net/downloa ...
- VBA怎样统计同一类型的数据的总和
今天是2014-11-01 是周末,忙了一周了,最终能够闲下来了.想起近期工作用到的VBA的一个场景,结合VBA的数组,所以就想试试看.结果还好.出来了.这年头,又玩起了VB了,经过多时才接受了VB的 ...