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 来代表这个结点所指代的数组区间内的元素个数. (数组中并不一定每个位置上都有元素) 实现 ...
随机推荐
- 【Unity 3D】学习笔记三十七:物理引擎——碰撞与休眠
碰撞与休眠 上一篇笔记说过,当给予游戏对象刚体这个组件以后,那么这个组件将存在碰撞的可能性.一旦刚体開始运动,那么系统方法便会监视刚体的碰撞状态.一般刚体的碰撞分为三种:进入碰撞,碰撞中,和碰撞结束. ...
- Shell简易学习练习
1.Linux Shell入门 Quiz 1 一个接受命令行参数的shell脚本 任务 编写一个shell脚本1.sh,这个脚本接受一个命令行参数,并把这个参数打印两次到标准输出. 如果输入没有参数输 ...
- 阿里巴巴2015研究project普通笔试题,与答案
欢迎您对这篇文章的其他建议.我可以留言在以下平台. 个人博客网站:www.anycodex.com/blog/ Csdn博客网站:http://my.csdn.net/?ref=toolbar 微博: ...
- 【Ruby】Ruby的model学习——Active Record Associations
在阅读的过程中有不论什么问题,欢迎一起交流 邮箱:1494713801@qq.com QQ:1494713801 一.怎样定义关联 两个model之间经常会存在关联关系,为了解决这些关联引起的复 ...
- PHP上传文件超过了最大文件大小限制导致无法上传成功
最近的研究<HeadFirst PHP & MySQL>第一本书5章"使用存储在文件中的数据",难道当一个文件上传应用程序,发生了错误.即,文件不能成功上传.这 ...
- SE 2014年4月22日(二)
如图配置: 网络中存在三个公有AS 其中AS200使用了 BGP联盟技术(如图配置) 在AS 100 中R1上起源了四条BGP路由,(1)要求全网BGP设备均能够正常学习 (2)要求:(使用BGP团体 ...
- Service组件 总结 + 绑定理Service三种实现方式 Messager + Binder + AIDL
在Android中进程按优先级可以分为五类,优先级从高到低排列: - 前台进程 该进程包含正在与用户进行交互的界面组件,比如一个Activity - 可视进程 该进程中的组件虽然没有和用户交互,但是仍 ...
- The Building Blocks-Enterprise Applications Part 2- Information Management and Business Analytics
1. Business Analytic Applications Data Analytics Also referred to as 'Business Analytics' or 'Busine ...
- tarjan+缩点
B - Popular Cows Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Su ...
- 状态压缩dp(hdu2167,poj2411)
hdu2167 http://acm.hdu.edu.cn/showproblem.php?pid=2167 给定一个N*N的板子,里面有N*N个数字,选中一些数字,使得和最大 要求任意两个选中的数字 ...