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
- 747
- 447
- 447
- 774
- 774
题意: 给出一个4,7序列,操作1,把一段里面的4表7,7变4.操作2询问最长不降子序列
直接线段树搞
Max表示最长不降子序列,Min表示最长不升子序列
num1表示4的个数,num2表示7的个数
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<vector> #include<set> #include<stack> #include<map> #include<ctime> #include<bitset> #define LL long long #define ll long long #define INF 0x3f3f3f3f #define maxn 1000010 #define eps 1e-6 #define mod 1000000007 using namespace std; struct node { int num1,num2,Max,Min,len; bool rev; }tree[maxn*3]; char a[maxn] ; int ql,qr; void up(int o) { tree[o].len=tree[o<<1].len+tree[o<<1|1].len; tree[o].Max=max(tree[o<<1].Max,tree[o<<1|1].Max) ; tree[o].Min=max(tree[o<<1].Min,tree[o<<1|1].Min) ; tree[o].Max=max(tree[o<<1].Max+tree[o<<1|1].num2,tree[o].Max) ; tree[o].Max=max(tree[o<<1].num1+tree[o<<1|1].Max,tree[o].Max) ; tree[o].Min=max(tree[o<<1|1].Min+tree[o<<1].num2,tree[o].Min) ; tree[o].Min=max(tree[o<<1|1].num1+tree[o<<1].Min,tree[o].Min) ; tree[o].num1=tree[o<<1].num1+tree[o<<1|1].num1; tree[o].num2=tree[o<<1].num2+tree[o<<1|1].num2; tree[o].Max=max(tree[o].num1,tree[o].Max) ; tree[o].Max=max(tree[o].num2,tree[o].Max) ; tree[o].Min=max(tree[o].num2,tree[o].Min) ; tree[o].Min=max(tree[o].num1,tree[o].Min) ; } void SWAP(int o) { swap(tree[o].num1,tree[o].num2) ; swap(tree[o].Max,tree[o].Min) ; tree[o].rev ^= 1; } void down(int o ) { if(tree[o].rev) { SWAP(o<<1) ; SWAP(o<<1|1) ; tree[o].rev=0; } } void build(int L,int R,int o) { tree[o].rev=false; if(L==R) { if(a[L]=='4')tree[o].num1=1,tree[o].num2=0 ; else tree[o].num2=1,tree[o].num1=0; tree[o].Max=tree[o].Min=1; return ; } int mid=(L+R)>>1; build(L,mid,o<<1) ; build(mid+1,R,o<<1|1) ; up(o) ; } void update(int L,int R,int o ) { if(ql<=L&&qr>=R) { SWAP(o) ; return ; } int mid=(L+R)>>1 ; down(o) ; if(ql<=mid) update(L,mid,o<<1) ; if(qr>mid) update(mid+1,R,o<<1|1) ; up(o) ; } int main() { int n,m,i,j; char ss[10] ; while(scanf("%d%d",&n,&m) != EOF) { scanf("%s",a+1) ; build(1,n,1) ; while(m--) { scanf("%s",ss) ; if(ss[0]=='c') { printf("%d ",tree[1].Max) ; } else { scanf("%d%d",&ql,&qr) ; update(1,n,1) ; } } } return 0 ; } /* 3 5 747 count switch 1 1 count switch 1 3 count */