Light oj 1080 - Binary Simulation(树状数组区间更新点查询)

简介: 有一字符串只包含0和1,然后又m组操作,I L R是将从L到R的字符进行翻转操作0变为1、1变为0,Q x表示询问第x的字符。

题意:


   有一字符串只包含0和1,然后又m组操作,I L R是将从L到R的字符进行翻转操作0变为1、1变为0,Q x表示询问第x的字符。


思路:


   我们只需要计算对询问的字符进行了多少次翻转,如果是偶数次,该字符变,否则翻转。对于区间的更新,我们可以使用线段树,不过对于这个题,因为只是对点的查询,而且每个节点的初始值都相同,为0,因此我们可以直接使用树状数组。下面是一个很巧妙的做法,而且很容易理解。


用了树状数组的区间更新 单点查找(一般为单点更新 区间查找)


例如 区间(2,4)加1


则Updata(2,1)   Updata(4+1,-1)


实现了更新(2,4)的值而不改变其他值


求Sum时即可得到某一点的值

//LA 1080 - Binary Simulation
//2013-04-12-18.52
#include <stdio.h>
#include <string.h>
const int maxn = 100010;
char str[maxn];
int n;
int sum[maxn];
int lowbit(int x)
{
    return x&(-x);
}
void change(int x, int v)
{
    while (x <= n)
    {
        sum[x] += v;
        x += lowbit(x);
    }
}
int get(int x)
{
    int s = 0;
    while (x)
    {
        s += sum[x];
        x -= lowbit(x);
    }
    return s;
}
void update(int l, int r)
{
    change(l, 1);
    change(r+1, -1);
}
int main()
{
    int t, m;
    scanf("%d",&t);
    for(int i = 1; i <= t; i++)
    {
        memset(sum, 0, sizeof(sum));
        scanf("%s",&str[1]);
        scanf("%d",&m);
        n =strlen(&str[1]);
        char op;
        int l, r;
        printf("Case %d:\n",i);
        while (m--)
        {
            getchar();
            scanf("%c",&op);
            if (op == 'I')
            {
                scanf("%d %d",&l, &r);
                update(l, r);
            }
            else
            {
                scanf("%d",&l);
                if (get(l)%2 == 1)
                {
                    if (str[l] == '1')
                        puts("0");
                    else
                        puts("1");
                }
                else
                    printf("%c\n",str[l]);
            }
        }
    }
    return 0;
}
目录
相关文章
|
9月前
GEE错误——Layer error: Image.connectedPixelCount: Segment size calculation on floating point bands is n
GEE错误——Layer error: Image.connectedPixelCount: Segment size calculation on floating point bands is n
95 0
UVa1584 - Circular Sequence
UVa1584 - Circular Sequence
46 0
UVa 374 Big Mod
UVa 374 Big Mod
45 0
|
搜索推荐 索引
Term Suggester 中 suggest_mode 的三种模式missing、popular、always 的区别
Term Suggester 中 suggest_mode 的三种模式missing、popular、always 的区别
|
数据挖掘
Re19:读论文 Paragraph-level Rationale Extraction through Regularization: A case study on European Court
Re19:读论文 Paragraph-level Rationale Extraction through Regularization: A case study on European Court
Re19:读论文 Paragraph-level Rationale Extraction through Regularization: A case study on European Court
|
人工智能 BI
CF1169C. Increasing by Modulo(二分)
CF1169C. Increasing by Modulo(二分)
142 0
|
人工智能 BI
CodeForces - 1485D Multiples and Power Differences (构造+lcm)
CodeForces - 1485D Multiples and Power Differences (构造+lcm)
92 0
|
算法
【欧拉计划第 12 题】 高度可除的三角数 Highly divisible triangular number
【欧拉计划第 12 题】 高度可除的三角数 Highly divisible triangular number
138 0
【欧拉计划第 12 题】 高度可除的三角数 Highly divisible triangular number
|
人工智能
Kuroni and Impossible Calculation——容斥原理-鸽笼原理-抽屉原理
题目描述 已知一个数组a[n],请计算式子:∏_{1≤i<j≤n}|ai−aj| 的值,其中1<=i,j<=n;我们可以认为,这一式子等价于 |a1−a2|⋅|a1−a3|⋅ … ⋅|a1−an|⋅|a2−a3|⋅|a2−a4|⋅ … ⋅|a2−an|⋅ … ⋅|an−1−an|
133 0
Kuroni and Impossible Calculation——容斥原理-鸽笼原理-抽屉原理