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!(段树)的更多相关文章

  1. UVA11402 - Ahoy, Pirates!(线段树)

    UVA11402 - Ahoy, Pirates!(线段树) option=com_onlinejudge&Itemid=8&category=24&page=show_pro ...

  2. 【UVA】11992 - Fast Matrix Operations(段树模板)

    主体段树,要注意,因为有set和add操作,当慵懒的标志下推.递归优先set,后复发add,每次运行set行动add马克清0 WA了好几次是由于计算那一段的时候出问题了,可笑的是我对着模板找了一个多小 ...

  3. 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 ...

  4. ZOJ 1610 间隔染色段树

    要长8000仪表板.间染色的范围,问:最后,能看到的颜色,而且颜色一共有段出现 覆盖段 数据对比水   水可太暴力 段树: #include "stdio.h" #include ...

  5. HDU 1394 Minimum Inversion Number (数据结构-段树)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  6. 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& ...

  7. BZOJ 2588 Count on a tree (COT) 是持久的段树

    标题效果:两棵树之间的首次查询k大点的权利. 思维:树木覆盖树,事实上,它是正常的树木覆盖了持久段树. 由于使用权值段树可以寻求区间k大,然后应用到持久段树思想,间隔可以做减法.详见代码. CODE: ...

  8. lintcode-439-线段树的构造 II

    439-线段树的构造 II 线段树是一棵二叉树,他的每个节点包含了两个额外的属性start和end用于表示该节点所代表的区间.start和end都是整数,并按照如下的方式赋值: 根节点的 start ...

  9. lintocde-247-线段树的查询 II

    247-线段树的查询 II 对于一个数组,我们可以对其建立一棵 线段树, 每个结点存储一个额外的值 count 来代表这个结点所指代的数组区间内的元素个数. (数组中并不一定每个位置上都有元素) 实现 ...

随机推荐

  1. 《windows核心编程系列》十八谈谈windows钩子

    windows应用程序是基于消息驱动的.各种应用程序对各种消息作出响应从而实现各种功能. windows钩子是windows消息处理机制的一个监视点,通过安装钩子能够达到监视指定窗体某种类型的消息的功 ...

  2. hdu1881(贪心+dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1881 分析:按照结束时间从小到大排序,然后以每个结束点为容量进行01背包,选入的必定符合条件的. 因为 ...

  3. SE 2014年4月16日

    一. 描述BGP路由协议中  BGP路由携带 AS-PATH/ next-hop  / ORIGIN /  local-preference 属性的特点! BGP协议中的AS-PATH是AS列表,用来 ...

  4. Date和String类型的相互转换

    String转Date: SimpleDateFormat bartDateFormat = new SimpleDateFormat("MM-dd-yyyy"); String ...

  5. HTML中的div标签

    在网页制作过程过中,能够把一些独立的逻辑部分划分出来.放在一个<div>标签中,这个<div>标签的作用就相当于一个容器. 为了使逻辑更加清晰,我们能够为这一个独立的逻辑部分设 ...

  6. embedded dylibs/frameworks are only supported on iOS 8.0 and later 错误解决

    ld: warning: embedded dylibs/frameworks only run on iOS 8 or later ld: embedded dylibs/frameworks ar ...

  7. Unreal Engine 4 RenderTarget制作Live Camera效果

    Unreal Engine 4 RenderTarget制作Live Camera效果 先上效果: Live Camera我不知道怎么翻译.反正意思就是将一个摄影机的Image渲染到一个2D平面上. ...

  8. Android应用Activity、Dialog、PopWindow、Toast窗体加入机制及源代码分析

    [工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处.尊重劳动成果] 1 背景 之所以写这一篇博客的原因是由于之前有写过一篇<Android应用setCont ...

  9. 《JavaScript设计模式与开发实践》读书笔记之代理模式

    1.代理模式 代理模式是为一个对象提供一个代用品或占位符,以便控制对它的访问 1.1 一般的图片加载 var myImage=(function () { var imgNode=document.c ...

  10. 《JavaScript设计模式与开发实践》读书笔记之策略模式

    1.策略模式 定义一系列算法,把它们一个个封装起来,并且使它们可以相互替换 1.1 传统实现 根据工资基数和年底绩效来发送年终奖 var calculateBonus= function (perfo ...