Problem 1007 幸运数 线段树成段更新
题目链接:
题目
Problem 1007 幸运数
Time Limit: 2000 mSec
Memory Limit : 131072 KB
问题描述
皮特的幸运数是2和5。只由幸运数字2和5组成的数列,称为幸运数列。对于幸运数列,有两种操作。
1、switch i j
表示数列从第i个数到第j个数中,所有的2改成5,所有的5改成2.例如幸运数列25525,执行switch 2 4操作,则数列变成22255
2、count
表示要求输出当前幸运数列的最长不下降子序列(即子序列中后面的数都不小于前面的数)的长度。例如幸运数列252255,其中222,555,2555是它的不下降子序列,而2525不是,可以看出,最长不下降子序列是22255,长度为5。
现在给出一个长度为n的幸运数列,再依次给出m个操作,对于每个count操作,输出当前幸运数列的最长不下降子序列的长度。
输入
输入包含多组数据。(数据不超过10组)
每组数据的第一行有两个整数n,m(n,m<=100000),分别表示数列的长度和操作个数。
接下来一行,有一个长度为n的只由2和5构成的数列。
接下来m行,每行一个操作,是count或者switch i j,含义如题所述
输出
对于每组数据的每个count操作,都输出一行,包含一个整数,表示当前幸运数列的最长不下降子序列的长度。
样例
input
2 3
25
count
switch 1 2
count
3 5
525
count
switch 1 1
count
switch 1 3
count
output
2
1
2
3
2
题解
可以用线段树跑成段更新。
每个节点维护四个变量,分别表示子序列"22","25","52","55"的长度
在2,5对换的时候,只要交换"22"和"55";"25"和"52"。
需要懒惰标记。(感觉懒惰标记写搓了。orz,后面学一下)
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define lson (o<<1)
#define rson ((o<<1)|1)
#define M L+(R-L)/2
using namespace std;
const int maxn=101010;
int sumv[maxn<<2][4],setv[maxn<<2];
int n,m;
char str[maxn];
void pushdown(int o,int L,int R){
if(setv[o]>0){
swap(sumv[o][1],sumv[o][2]);
swap(sumv[o][0],sumv[o][3]);
if(L<R){
setv[lson]^=1; setv[rson]^=1;
}
setv[o]=0;
}
}
void maintain(int o,int L,int R){
if(L==R){
pushdown(o,L,R);
}else{
pushdown(lson,L,M);
pushdown(rson,M+1,R);
sumv[o][0]=sumv[lson][0]+sumv[rson][0];
sumv[o][3]=sumv[lson][3]+sumv[rson][3];
sumv[o][1]=max(sumv[lson][0]+sumv[rson][3],sumv[lson][1]+sumv[rson][3]);
sumv[o][1]=max(sumv[o][1],sumv[lson][0]+sumv[rson][1]);
sumv[o][2]=max(sumv[lson][3]+sumv[rson][0],sumv[lson][2]+sumv[rson][0]);
sumv[o][2]=max(sumv[o][2],sumv[lson][3]+sumv[rson][2]);
}
}
int ql,qr;
void update(int o,int L,int R){
if(ql<=L&&R<=qr){
setv[o]^=1;
pushdown(o,L,R);
}else{
pushdown(o,L,R);
if(ql<=M) update(lson,L,M);
if(qr>M) update(rson,M+1,R);
}
maintain(o,L,R);
}
void build(int o,int L,int R){
if(L==R){
memset(sumv[o],0,sizeof(sumv[o]));
if(str[L]=='2') sumv[o][0]=1;
else sumv[o][3]=1;
}else{
build(lson,L,M);
build(rson,M+1,R);
}
maintain(o,L,R);
}
void init(){
memset(setv,0,sizeof(setv));
}
int main(){
while(scanf("%d%d",&n,&m)==2){
init();
scanf("%s",str+1);
build(1,1,n);
char cmd[22];
while(m--){
scanf("%s",cmd);
if(cmd[0]=='c'){
printf("%d\n",max(sumv[1][0],max(sumv[1][1],sumv[1][3])));
}else{
scanf("%d%d",&ql,&qr);
update(1,1,n);
}
}
}
return 0;
}
改进版本:(看起来爽多了)
打标记的位置其实已经更新完毕了,打标记知识告诉后代要更新啦。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define lson (o<<1)
#define rson ((o<<1)|1)
#define M L+(R-L)/2
using namespace std;
const int maxn = 101010;
int sumv[maxn << 2][4], setv[maxn << 2];
int n, m;
char str[maxn];
void pushdown(int o, int L, int R) {
if (setv[o]>0&&L<R) {
swap(sumv[lson][1], sumv[lson][2]); swap(sumv[lson][0], sumv[lson][3]);
swap(sumv[rson][1], sumv[rson][2]); swap(sumv[rson][0], sumv[rson][3]);
setv[lson] ^= 1; setv[rson] ^= 1;
setv[o] = 0;
}
}
void maintain(int o, int L, int R) {
sumv[o][0] = sumv[lson][0] + sumv[rson][0];
sumv[o][3] = sumv[lson][3] + sumv[rson][3];
sumv[o][1] = max(sumv[lson][0] + sumv[rson][3], sumv[lson][1] + sumv[rson][3]);
sumv[o][1] = max(sumv[o][1], sumv[lson][0] + sumv[rson][1]);
sumv[o][2] = max(sumv[lson][3] + sumv[rson][0], sumv[lson][2] + sumv[rson][0]);
sumv[o][2] = max(sumv[o][2], sumv[lson][3] + sumv[rson][2]);
}
int ql, qr;
void update(int o, int L, int R) {
if (ql <= L&&R <= qr) {
setv[o] ^= 1;
swap(sumv[o][1], sumv[o][2]);
swap(sumv[o][0], sumv[o][3]);
}
else {
pushdown(o, L, R);
if (ql <= M) update(lson, L, M);
if (qr>M) update(rson, M + 1, R);
maintain(o, L, R);
}
}
void build(int o, int L, int R) {
if (L == R) {
memset(sumv[o], 0, sizeof(sumv[o]));
if (str[L] == '2') sumv[o][0] = 1;
else sumv[o][3] = 1;
}
else {
build(lson, L, M);
build(rson, M + 1, R);
maintain(o, L, R);
}
}
void init() {
memset(setv, 0, sizeof(setv));
}
int main() {
while (scanf("%d%d", &n, &m) == 2) {
init();
scanf("%s", str + 1);
build(1, 1, n);
char cmd[22];
while (m--) {
scanf("%s", cmd);
if (cmd[0] == 'c') {
printf("%d\n", max(sumv[1][0], max(sumv[1][1], sumv[1][3])));
}
else {
scanf("%d%d", &ql, &qr);
update(1, 1, n);
}
}
}
return 0;
}
Problem 1007 幸运数 线段树成段更新的更多相关文章
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- POJ 3468:A Simple Problem with Integers(线段树[成段更新])
题意:N个数Q次操作.一共两种操作:Q l r :询问[l,r]这个区间里的数字和,C l r c: [l,r]区间里的每个数都加上c.1 ≤ N,Q ≤ 100000. 方法:线段树的成段更新.注意 ...
- poj 3468 A Simple Problem with Integers (线段树 成段更新 加值 求和)
题目链接 题意: 只有这两种操作 C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.&quo ...
- POJ3468_A Simple Problem with Integers(线段树/成段更新)
解题报告 题意: 略 思路: 线段树成段更新,区间求和. #include <iostream> #include <cstring> #include <cstdio& ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- ACM: Copying Data 线段树-成段更新-解题报告
Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- HDU1698_Just a Hook(线段树/成段更新)
解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...
随机推荐
- Part 7Handling events in AngularJS
Let us understand with an example. Here is what we want to do. 1. Display the list of technologies i ...
- 微软SQLHelper.cs类 中文版
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Co ...
- javascript将浮点数转换成整数的三个方法
浮点数转换成整数方法有很多,本例为大家介绍常用的三个方法,如果读者想到其他好用方法,也可以交流一下 Summary 暂时我就想到3个方法而已.如果读者想到其他好用方法,也可以交流一下 parseI ...
- C#调用存储过程简单完整例子
CREATE PROC P_TEST@Name VARCHAR(20),@Rowcount INT OUTPUTASBEGIN SELECT * FROM T_Customer WHERE NAME= ...
- iOS数据持久化(三)
#pragma mark - Core Data stack /** * @synthesize 关联成员变量和属性 */ @synthesize managedObjectContext = _ma ...
- 20141016--for 菱形
Console.Write("请输入一个数:"); int n = int.Parse(Console.ReadLine()); ; i <= n; i++) { ; b & ...
- BoneCP主要配置参数
二.BoneCP主要配置参数 1.jdbcUrl 设置数据库URL 2.username 设置数据库用户名 3.password 设置数据库密码 4.partitionCount 设置分区个数.这个参 ...
- java利用反射绕过私有检查机制实行对private、protected成员变量或方法的访问
在java中,如果类里面的变量是声明了private的,那么只能在被类中访问,外界不能调用,如果是protected类型的,只能在子类或本包中调用,俗话说没有不透风的墙.但是可以利用java中的反射从 ...
- (转载)浅谈我对DDD领域驱动设计的理解
原文地址:http://www.cnblogs.com/netfocus/p/5548025.html 从遇到问题开始 当人们要做一个软件系统时,一般总是因为遇到了什么问题,然后希望通过一个软件系统来 ...
- ArcGIS 坐标系统文件
坐标是GIS数据的骨骼框架,能够将我们的数据定位到相应的位置,为地图中的每一点提供准确的坐标. ArcGIS自带了多种坐标系统,在${ArcGISHome}Coordinate Systems目录下可 ...