E. Lucky Queries
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Petya loves lucky numbers very much. Everybody knows that lucky numbers are positive integers whose decimal record contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

Petya brought home string s with the length of n. The string only consists of lucky digits. The digits are numbered from the left to the right starting with 1. Now Petya should execute m queries of the following form:

  • switch l r — "switch" digits (i.e. replace them with their opposites) at all positions with indexes from l to r, inclusive: each digit 4 is replaced with 7 and each digit 7 is replaced with 4 (1 ≤ l ≤ r ≤ n);
  • count — find and print on the screen the length of the longest non-decreasing subsequence of string s.

Subsequence of a string s is a string that can be obtained from s by removing zero or more of its elements. A string is called non-decreasing if each successive digit is not less than the previous one.

Help Petya process the requests.

Input

The first line contains two integers n and m (1 ≤ n ≤ 106, 1 ≤ m ≤ 3·105) — the length of the string s and the number of queries correspondingly. The second line contains n lucky digits without spaces — Petya's initial string. Next m lines contain queries in the form described in the statement.

Output

For each query count print an answer on a single line.

Examples
Input
2 3
47
count
switch 1 2
count
Output
2
1
Input
3 5
747
count
switch 1 1
count
switch 1 3
count
Output
2
3
2
Note

In the first sample the chronology of string s after some operations are fulfilled is as follows (the sought maximum subsequence is marked with bold):

  1. 47
  2. 74
  3. 74

In the second sample:

  1. 747
  2. 447
  3. 447
  4. 774
  5. 774

  比较好写……

 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
char s[maxn];
int num[maxn],Mark[maxn<<];
int M1[maxn<<],M2[maxn<<];
int M3[maxn<<],M4[maxn<<];
int tot[maxn<<],n,Q;
//M1:00 M2:11 M3:01 M4:10 void Swich(int x){
swap(M1[x],M2[x]);
swap(M3[x],M4[x]);
Mark[x]^=;
} void Push_down(int x,int l,int r){
if(!Mark[x]||l==r)return;
Swich(x<<);Swich(x<<|);
Mark[x]=;
} void Push_up(int x){
M1[x]=M1[x<<]+M1[x<<|];
M2[x]=M2[x<<]+M2[x<<|];
M3[x]=max(M1[x<<]+M2[x<<|],max(M1[x<<]+M3[x<<|],M3[x<<]+M2[x<<|]));
M4[x]=max(M2[x<<]+M1[x<<|],max(M4[x<<]+M1[x<<|],M2[x<<]+M4[x<<|]));
} void Build(int x,int l,int r){
if(l==r){
M1[x]=num[l]^;
M2[x]=num[l];
return;
}
int mid=(l+r)>>;
Build(x<<,l,mid);
Build(x<<|,mid+,r);
Push_up(x);
} void Update(int x,int l,int r,int a,int b){
Push_down(x,l,r);
if(l>=a&&r<=b){
Swich(x);
return;
}
int mid=(l+r)>>;
if(mid>=a)Update(x<<,l,mid,a,b);
if(mid<b)Update(x<<|,mid+,r,a,b);
Push_up(x);
} char op[];
int main(){
scanf("%d%d",&n,&Q);
scanf("%s",s+);
for(int i=;i<=n;i++){
num[i]=s[i]==''?:;
}
Build(,,n);
int a,b;
while(Q--){
scanf("%s",op);
if(op[]=='s'){
scanf("%d%d",&a,&b);
Update(,,n,a,b);
}
else
printf("%d\n",max(max(M1[],M2[]),M3[]));
}
return ;
}

数据结构(线段树):CodeForces 145E Lucky Queries的更多相关文章

  1. Codeforces 145E Lucky Queries 线段树

    Lucky Queries 感觉是很简单的区间合并, 但是好像我写的比较麻烦. #include<bits/stdc++.h> #define LL long long #define f ...

  2. 算法手记 之 数据结构(线段树详解)(POJ 3468)

    依然延续第一篇读书笔记,这一篇是基于<ACM/ICPC 算法训练教程>上关于线段树的讲解的总结和修改(这本书在线段树这里Error非常多),但是总体来说这本书关于具体算法的讲解和案例都是不 ...

  3. 线段树 Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations

    题目传送门 /* 线段树的单点更新:有一个交叉更新,若rank=1,or:rank=0,xor 详细解释:http://www.xuebuyuan.com/1154895.html */ #inclu ...

  4. set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet

    题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...

  5. [线段树]Codeforces 339D Xenia and Bit Operations

    Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  6. ACM/ICPC 之 数据结构-线段树+区间离散化(POJ2528)

    这道题用线段树做更方便更新和查询,但是其数据范围很大,因此要将离散化和线段树结合起来,算是一道比较经典的线段树+离散化的例题. 线段树的离散化有很多方法,在这里,我先用一次结点离散化,间接将源左右端点 ...

  7. ACM/ICPC 之 数据结构-线段树思想(POJ2182,含O(n^2)插入式解法)

    这道题在一定程度上体现了线段树的一种用法,解决的问题是:对于总计n个元素的第i个元素,已知其在[1,i]上部分序列的排名,求第i个元素在所有n个元素中的排名. 当然这道题数据比较水,所以用O(n^2) ...

  8. 模板 - 数据结构 - 线段树/SegmentTree

    区间求加法和: 单点修改的,普通线段树. struct SegmentTree { #define ls (o<<1) #define rs (o<<1|1) static c ...

  9. 第二十九篇 玩转数据结构——线段树(Segment Tree)

          1.. 线段树引入 线段树也称为区间树 为什么要使用线段树:对于某些问题,我们只关心区间(线段) 经典的线段树问题:区间染色,有一面长度为n的墙,每次选择一段墙进行染色(染色允许覆盖),问 ...

随机推荐

  1. 移动web设计稿尺寸,关于移动web尺寸的那点事

    我自己的做稿子的时候,一开始就有一个习惯,先放上这段代码<meta name="viewport" content="width=device-width, ini ...

  2. 【SSMS增强工具】SQL Sharper 2014介绍

    产品介绍 SQL Sharper是一款SQL Server Management Studio插件,用于数据库对象快速查询.表结构查询.优化查询结果导出.代码生成等方面. 适用人群:T-SQL开发者. ...

  3. webServices 执行流程,(我是菜鸟,我怕谁,仅代表个人理解,欢迎各位大神们指导,不和您的胃口,请默默离开!!)

    二.上图仅仅代表个人理解,下面以代码方式解释一下. (1) strtus.xml <?xml version="1.0" encoding="UTF-8" ...

  4. 玩转CSLA.NET小技巧系列二:使用WCF无法上传附件,提示413 Entity Too Large

    背景:由于系统需要展示图片,客户上传图片到本地客户端目录,然后在数据库中存储本地图片地址,和图片二进制数据 错误原因:我是使用CSLA的WCF服务,使用了数据门户,WCF协议使用的是wsHttpBin ...

  5. 使用Xcode插件,让iOS开发更加便捷

    在iOS开发过程中,写注释是一项必不可少的工作.这不仅有助于自己对代码整理回顾,而且提高了代码的可读性,让代码维护变得容易.但是,写注释又是一项枯燥的工作.我们浪费了大量的时间在输入/*,*,*/这样 ...

  6. Python:运算符

    #!/usr/bin/python3 #运算符 #算术运算符 print("算术运算符:","+ - * / % **(幂) //(取整)") #比较运算符 p ...

  7. Excel等外部程序点击链接会带上IE信息的bug

    今天碰到一个问题,在Excel内点击链接到默认浏览器Chrome打开,奇怪的是服务端收到的Session一直对不上. 查了很久发现这个Excel到Chrome的跳转竟然带上了IE的Cookie 和 U ...

  8. php之上传小案例,根据时间:月日分创建目录并随机生成文件名

    <?php /* 接收文件,并分目录存储,生成随机文件名 1.根据时间戳,并按一定规则创建目录 2.获取文件名的后缀名 3.判断大小 */ //根据月日分计算并创建目录 function mk_ ...

  9. PHP 用户注册

    注册页面 reg.html 负责收集用户填写的注册信息.教程里只列出关键的代码片段,完整的代码附在本节最后. 注册表单 <fieldset> <legend>用户注册</ ...

  10. PHP常用代码段:

    1.PHP加密解密   function encryptDecrypt($key, $string, $decrypt){      if($decrypt){          $decrypted ...