Codeforces Round #104 (Div. 1) E. Lucky Queries

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

Sample test(s)
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

题意: 给出一个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\n",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
*/
View Code

 

转载于:https://www.cnblogs.com/20120125llcai/p/4077313.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值