zoukankan      html  css  js  c++  java
  • 数据结构(线段树):CodeForces 145E 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 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

      比较好写……

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 const int maxn=1000010;
     6 char s[maxn];
     7 int num[maxn],Mark[maxn<<2];
     8 int M1[maxn<<2],M2[maxn<<2];
     9 int M3[maxn<<2],M4[maxn<<2];
    10 int tot[maxn<<2],n,Q;
    11 //M1:00 M2:11 M3:01 M4:10 
    12 
    13 void Swich(int x){
    14     swap(M1[x],M2[x]);
    15     swap(M3[x],M4[x]);
    16     Mark[x]^=1;
    17 }
    18 
    19 void Push_down(int x,int l,int r){
    20     if(!Mark[x]||l==r)return;
    21     Swich(x<<1);Swich(x<<1|1);
    22     Mark[x]=0;
    23 }
    24 
    25 void Push_up(int x){
    26     M1[x]=M1[x<<1]+M1[x<<1|1];
    27     M2[x]=M2[x<<1]+M2[x<<1|1];
    28     M3[x]=max(M1[x<<1]+M2[x<<1|1],max(M1[x<<1]+M3[x<<1|1],M3[x<<1]+M2[x<<1|1]));
    29     M4[x]=max(M2[x<<1]+M1[x<<1|1],max(M4[x<<1]+M1[x<<1|1],M2[x<<1]+M4[x<<1|1]));    
    30 }
    31 
    32 void Build(int x,int l,int r){
    33     if(l==r){
    34         M1[x]=num[l]^1;
    35         M2[x]=num[l];
    36         return;
    37     }
    38     int mid=(l+r)>>1;
    39     Build(x<<1,l,mid);
    40     Build(x<<1|1,mid+1,r);
    41     Push_up(x);
    42 }
    43 
    44 void Update(int x,int l,int r,int a,int b){
    45     Push_down(x,l,r);
    46     if(l>=a&&r<=b){
    47         Swich(x);
    48         return;
    49     }
    50     int mid=(l+r)>>1;
    51     if(mid>=a)Update(x<<1,l,mid,a,b);
    52     if(mid<b)Update(x<<1|1,mid+1,r,a,b);
    53     Push_up(x);
    54 }
    55 
    56 char op[10];
    57 int main(){
    58     scanf("%d%d",&n,&Q);
    59     scanf("%s",s+1);
    60     for(int i=1;i<=n;i++){
    61         num[i]=s[i]=='4'?0:1;
    62     }
    63     Build(1,1,n);
    64     int a,b;
    65     while(Q--){
    66         scanf("%s",op);
    67         if(op[0]=='s'){
    68             scanf("%d%d",&a,&b);
    69             Update(1,1,n,a,b);
    70         }
    71         else
    72             printf("%d
    ",max(max(M1[1],M2[1]),M3[1]));
    73     }
    74     return 0;
    75 }
    尽最大的努力,做最好的自己!
  • 相关阅读:
    Asp.Net Core 2.0 项目实战(11) 基于OnActionExecuting全局过滤器,页面操作权限过滤控制到按钮级 郑州
    NET项目反编译+VS解决方案整理流程 郑州
    iis 目录枚举文件枚举解决方案 郑州
    Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员、后台管理员同时登录 郑州
    SqlServer mssql 按月统计所有部门 郑州
    无法获得数据库 'model' 上的排他锁 解决方法 郑州
    Asp.Net Core 2.0 项目实战(5)Memcached踩坑,基于EnyimMemcachedCore整理MemcachedHelper帮助类。 郑州
    谈谈前端怎样布局切图,程序可以方便快捷添加程序 郑州
    各种UIColor颜色的设置
    iOS
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5578578.html
Copyright © 2011-2022 走看看