数据结构(线段树):CodeForces 145E Lucky Queries
3 seconds
256 megabytes
standard input
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.
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.
For each query count print an answer on a single line.
2 3
47
count
switch 1 2
count
2
1
3 5
747
count
switch 1 1
count
switch 1 3
count
2
3
2
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):
- 47
- 74
- 74
In the second sample:
- 747
- 447
- 447
- 774
- 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的更多相关文章
- Codeforces 145E Lucky Queries 线段树
Lucky Queries 感觉是很简单的区间合并, 但是好像我写的比较麻烦. #include<bits/stdc++.h> #define LL long long #define f ...
- 算法手记 之 数据结构(线段树详解)(POJ 3468)
依然延续第一篇读书笔记,这一篇是基于<ACM/ICPC 算法训练教程>上关于线段树的讲解的总结和修改(这本书在线段树这里Error非常多),但是总体来说这本书关于具体算法的讲解和案例都是不 ...
- 线段树 Codeforces Round #197 (Div. 2) D. Xenia and Bit Operations
题目传送门 /* 线段树的单点更新:有一个交叉更新,若rank=1,or:rank=0,xor 详细解释:http://www.xuebuyuan.com/1154895.html */ #inclu ...
- set+线段树 Codeforces Round #305 (Div. 2) D. Mike and Feet
题目传送门 /* 题意:对于长度为x的子序列,每个序列存放为最小值,输出长度为x的子序列的最大值 set+线段树:线段树每个结点存放长度为rt的最大值,更新:先升序排序,逐个添加到set中 查找左右相 ...
- [线段树]Codeforces 339D Xenia and Bit Operations
Xenia and Bit Operations time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- ACM/ICPC 之 数据结构-线段树+区间离散化(POJ2528)
这道题用线段树做更方便更新和查询,但是其数据范围很大,因此要将离散化和线段树结合起来,算是一道比较经典的线段树+离散化的例题. 线段树的离散化有很多方法,在这里,我先用一次结点离散化,间接将源左右端点 ...
- ACM/ICPC 之 数据结构-线段树思想(POJ2182,含O(n^2)插入式解法)
这道题在一定程度上体现了线段树的一种用法,解决的问题是:对于总计n个元素的第i个元素,已知其在[1,i]上部分序列的排名,求第i个元素在所有n个元素中的排名. 当然这道题数据比较水,所以用O(n^2) ...
- 模板 - 数据结构 - 线段树/SegmentTree
区间求加法和: 单点修改的,普通线段树. struct SegmentTree { #define ls (o<<1) #define rs (o<<1|1) static c ...
- 第二十九篇 玩转数据结构——线段树(Segment Tree)
1.. 线段树引入 线段树也称为区间树 为什么要使用线段树:对于某些问题,我们只关心区间(线段) 经典的线段树问题:区间染色,有一面长度为n的墙,每次选择一段墙进行染色(染色允许覆盖),问 ...
随机推荐
- (转)HTML表格边框的设置小技巧
对于很多初学HTML的人来说,表格<table>是最常用的标签了,但对于表格边框的控制,很多初学者却不甚其解. 对于很多初学HTML的人来说,表格<table>是最常用的标签了 ...
- 认识k_BackingField【转】
事情从Json的序列化和反序列化说起. 在C#2.0的项目中,以前经常使用Json.Net实现序列化和反序列化.后来从c#3.0中开始使用新增的DataContractJsonSerializer进行 ...
- 如何在安卓/data(而不是/data/data)目录下进行文件的读写操作
分析:Android默认是无法直接操作/data目录的,只能读写程序自己的私有目录,也就是/data/data/package name/下,默认只能操作这个目录下的文件,也就是我们想直接读写/dat ...
- 利用SQLiteOpenHelper创建数据库,进行增删改查操作
Android中提供SQLiteOpenHelper类,在该类的构造器中,调用Context中的方法创建并打开一个指定名称的数据库对象.继承和扩展SQLiteOpenHelper类主要做的工作就是重写 ...
- angularjs-googleMap googleMap api地址解析与反解析
1.js:根据地址得到经纬度var myplace=$scope.place;//获取输入的地址var geocoder = new google.maps.Geocoder();//创建geocod ...
- iOS目录结构
默认情况下,每个沙盒含有3个文件夹:Documents, Library 和 tmp.因为应用的沙盒机制,应用只能在几个目录下读写文件 Documents:苹果建议将程序中建立的或在程序中浏览到的文件 ...
- 12XML(可扩展标记语言)
XML:eXtensible Markup Language 什么是标记语言?什么是标记? 标记(Markup):文档中任何不想被打印输出的部分(不是真正的文档内容,联想读书时做的“读书笔记”,在旁边 ...
- 用连接池提高Servlet访问数据库的效率
Java Servlet作为首选的服务器端数据处理技术,正在迅速取代CGI脚本.Servlet超越CGI的优势之一在于,不仅多个请求可以共享公用资源,而且还可以在不同用户请求之间保留持续数据.本文介绍 ...
- jQuery选择器种类整理
选择器概念 jQuery选择器是通过标签.属性或者内容对HTML内容进行选择,选择器运行对HTML元素组或者单个元素进行操作. jQuery选择器使用$符号,等同于jquery,例如: $(“li”) ...
- html5 实现手机端相册浏览功能
原文地址:http://www.cootm.com/?p=710 在网上找到个浏览图片的jq插件,针对手机做的非常不错,看到乐享的微信微站新开发的功能就是这个,特此分享下! 时间匆忙,没做代码分析,勿 ...