• HRBUST 1867 差分+BIT


    我在群上看到的某道题,貌似用的是线段树,因为前几天遇到差分,再用BIT动态维护一下前缀和,感觉可做就A了. 加了个读优就Rank1啦! 某个不常见的题库,还是把题目拿下来把..

     1 Description
     2 给你一些二进制串,我们有对这些数有两种操作。
     3 'I i j'    将[i,j]内的所有数取反(0变1,1变0)
     4 'Q i'    输出第i个数是多少
     5 下标从1开始,输入串允许有前导0
     6 Input
     7 输入包含一个整数T(T<=10000),表示测试样例个数。
     8 每组样例的第一行有一个长度为n的01串(n<=20000)
     9 第二行有一个整数Q(Q<=10000)代表将会有多少个操作
    10 Output
    11 输出每一个Q i的结果
    12 Sample Input
    13 2
    14 0011001100
    15 6
    16 I 1 10
    17 I 2 7
    18 Q 2
    19 Q 1
    20 Q 7
    21 Q 5
    22 1011110111
    23 6
    24 I 1 10
    25 I 2 7
    26 Q 2
    27 Q 1
    28 Q 7
    29 Q 5
    30 Sample Output
    31 Case 1:
    32 0
    33 1
    34 1
    35 0
    36 Case 2:
    37 0
    38 0
    39 0
    40 1
    Problem
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 #include <bitset>
     6 using namespace std;
     7 const int Maxn=20010;
     8 int Kase,B[Maxn],n,pos,l,r,C[Maxn<<1],Q;
     9 char str[Maxn];
    10 inline void Get_Int(int &x)
    11 {
    12     x=0;  char ch=getchar(); int f=1;
    13     while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
    14     while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} x*=f;
    15 }
    16 inline void Put_Int(int x)
    17 {
    18     char ch[20];  int top=0;
    19     if (x==0) ch[++top]='0';
    20     while (x) ch[++top]=x%10+'0',x/=10;
    21     while (top) putchar(ch[top--]); putchar('
    ');
    22 }
    23 inline int lowbit(int x) {return x&(-x);}
    24 inline int Query(int x)
    25 {
    26     int ret=0;
    27     for (int i=x;i;i-=lowbit(i)) ret=(ret+C[i])%2;
    28     return ret;
    29 }
    30 inline int Add(int x,int v)
    31 {
    32     for (int i=x;i<=n;i+=lowbit(i)) C[i]=(C[i]+v)%2;
    33 }
    34 int main()
    35 {
    36     Get_Int(Kase);
    37     for (int kase=1;kase<=Kase;kase++)
    38     {
    39         scanf("%s",str+1);
    40         printf("Case %d:
    ",kase);
    41         n=strlen(str+1);
    42         memset(C,0,sizeof(C));
    43         for (int i=1;i<=n;i++) B[i]=str[i]-'0';
    44         Get_Int(Q);
    45         for (int i=1;i<=Q;i++)
    46         {
    47             char ch=getchar();
    48             while (ch!='Q' && ch!='I') ch=getchar();
    49             if (ch=='Q')
    50             {
    51                 Get_Int(pos);
    52                 int tmp=(B[pos]+Query(pos))%2;
    53                 if (tmp==1) putchar('1'),putchar('
    ');
    54                 else putchar('0'),putchar('
    ');
    55             }
    56             if (ch=='I')
    57             {
    58                 Get_Int(l),Get_Int(r);
    59                 Add(l,1),Add(r+1,1);
    60             }
    61         }
    62     }
    63     return 0;
    64 }
    C++
  • 相关阅读:
    document.querySelector和querySelectorAll方法
    Hello World!
    tomcat8总结默认端口默认项目,以及图片上传和展示的路径问题.
    理解java移位运算符
    tomcat8的apr模式配置SLL证书
    SpringData
    JPQL
    JPA多对多
    JPA一对一双向
    JPA一对多单向
  • 原文地址:https://www.cnblogs.com/yyjxx2010xyu/p/5515862.html
Copyright © 2020-2023  润新知