zoukankan      html  css  js  c++  java
  • 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
    ",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
  • 相关阅读:
    HTML表格布局例子
    WCF分布式开发必备知识(2):.Net Remoting (转)
    WCF分布式开发必备知识(1):MSMQ消息队列(转)
    WCF数据契约与序列化(转)
    Asp.net中图片存储数据库以及页面读取显示通用方法详解附源码下载
    2010年初的一点随想
    Windows7旗舰版磁盘分区详解—附分区步骤截图
    AjaxControltoolkit(工具包)安装步骤说明
    Windows7安装IIS中关于Windows 系列于谷歌Chrome系统争议一点联想
    Oracle 10G中关于约束在表和列中使用详解.
  • 原文地址:https://www.cnblogs.com/20120125llcai/p/4077313.html
Copyright © 2011-2022 走看看