lightoj 1135 - Count the Multiples of 3(线段树)

You have an array with n elements which is indexed from 0 to n - 1. Initially all elements are zero. Now you have to deal with two types of operations

1.      Increase the numbers between indices i and j (inclusive) by 1. This is represented by the command '0 i j'.

2.      Answer how many numbers between indices i and j (inclusive) are divisible by 3. This is represented by the command '1 i j'.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case starts with a line containing two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000) denoting the number of queries. Each query will be either in the form '0 i j' or '1 i j' where ij are integers and 0 ≤ i ≤ j < n.

Output

For each case, print the case number first. Then for each query in the form '1 i j', print the desired result.

Sample Input

Output for Sample Input

1

10 9

0 0 9

0 3 7

0 1 4

1 1 7

0 2 2

1 2 4

1 8 8

0 5 8

1 6 9

Case 1:

2

3

0

2



给你一个n个点的序列,q个操作,操作有两种,一种是把区间内的点都加上一,一种是询问区间内有多个数能被3整除。

明显的是用线段树,开三个数组存下被3整除余0、1、2的数有多少个,初始的时候值是0,所以所有元素整除3都是余0的。然后如果某一个区间内的数都加1,就相当于三个数组的值互换了一下,这样查询的时候查询被3乘除余0的个数就行了。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int maxn = 100010;
int sum0[maxn*4];
int sum1[maxn*4];
int sum2[maxn*4];
int add[maxn*4];

void change(int x,int m)
{
    m %= 3;
    if(m == 0)
        return;
    int t0 = sum0[x];
    int t1 = sum1[x];
    int t2 = sum2[x];
    if(m == 1)
    {
        sum0[x] = t2;
        sum1[x] = t0;
        sum2[x] = t1;
    }
    if(m == 2)
    {
        sum0[x] = t1;
        sum1[x] = t2;
        sum2[x] = t0;
    }
}
void PushUp(int rt)
{
    sum0[rt] = sum0[rt*2] + sum0[rt*2+1];
    sum1[rt] = sum1[rt*2] + sum1[rt*2+1];
    sum2[rt] = sum2[rt*2] + sum2[rt*2+1];
}
void PushDown(int l,int r,int rt)
{
    if(add[rt])
    {
        int m = (l+r)/2;
        add[rt*2] += add[rt];
        add[rt*2+1] += add[rt];
        change(rt*2,add[rt]);
        change(rt*2+1,add[rt]);
        add[rt] = 0;
    }
}
void build(int l, int r, int rt)//建立线段树
{
    if (l == r)
    {
        sum0[rt] = 1;
        return;
    }
    int m = (l + r) / 2;
    build(l, m, rt*2);
    build(m + 1, r, rt*2+1);
    PushUp(rt);
}
void update(int x,int y,int l, int r, int rt)
{
    if (x<=l && y>=r)
    {
        change(rt,1);
        add[rt] += 1;
        return;
    }
    PushDown(l,r,rt);
    int m = (l + r) / 2;
    if (x <= m)
        update(x, y, l, m, rt*2);
    if(y > m)
        update(x, y, m + 1, r, rt*2+1);

    PushUp(rt);
}

int query(int ll, int rr, int l, int r, int rt)//查询线段树
{
    if (ll <= l && rr >= r) return sum0[rt];
    PushDown(l,r,rt);
    int m = (l + r) / 2;
    int ret = 0;
    if (ll <= m)
        ret += query(ll, rr, l, m, rt*2);
    if (rr > m)
        ret += query(ll, rr, m + 1, r, rt*2+1);
    return ret;
}

int main()
{
    int T,n,m,i,j;
    scanf("%d",&T);
    int cas = 1;
    while(T--)
    {
        memset(add,0,sizeof(add));
        memset(sum0,0,sizeof(sum0));
        memset(sum1,0,sizeof(sum1));
        memset(sum2,0,sizeof(sum2));
        scanf("%d%d",&n,&m);
        build(1,n,1);
        printf("Case %d:\n",cas++);
        while(m--)
        {
            int op,x,y;
            scanf("%d%d%d",&op,&x,&y);
            x++,y++;
            if(op == 0)
                update(x,y,1,n,1);
            else
            {
                int ans = query(x,y,1,n,1);
                printf("%d\n",ans);
            }
        }
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值