LightOJ 1135 Count the Multiples of 3 (Segmengt + 懒惰标记)

Description

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

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

Sample Output

Case 1:

2

3

0

2


这不是一道裸线段树的题。。它涉及到区间更新,这里就需要用到懒惰标记了。。而且还要自己在考虑一下结构体中的数据元素都需要什么,这里需要左右区间指示标志,懒惰标记,记录区间中对3取余分别余1、余2、余0的元素个数,还有就是需要增加一个MOVE();它的功能是当访问到该区间时,,转换m0、m1、m2之间的值的操作。分析出来这些这道题就只剩下“ma”代码的工作了。

#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <map>
#include <queue>
#include <stack>
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a));
#define For(a,b) for(int i = a;i<b;i++)
#define LL long long
#define MAX_N 100010
using namespace std;
int x[MAX_N];
struct SegmentTree
{
    int l;
    int r;
    int m0;
    int m1;
    int m2;
    int flag;
}p[MAX_N<<2];
void BuildTree(int root,int l,int r)
{
    p[root].l = l;
    p[root].r = r;
    p[root].m1 = 0;
    p[root].m2 = 0;
    p[root].flag = 0;
    if(l == r)
    {
        p[root].m0 = 1;
        return ;
    }
    int mid = (l + r)/2;
    BuildTree(root<<1,l,mid);
    BuildTree(root<<1|1,mid+1,r);
    p[root].m0 = p[root<<1].m0 + p[root<<1|1].m0;
//    p[root].m1 = p[root<<1].m1 + p[root<<1|1].m1;
//    p[root].m2 = p[root<<1].m2 + p[root<<1|1].m2;
}
void mov_e(SegmentTree &Tree)
{
    int x = Tree.m0;
    Tree.m0 = Tree.m2;
    Tree.m2 = Tree.m1;
    Tree.m1 = x;
    return ;
}
void pushup(int now)
{
    p[now].m0 = p[now<<1].m0 + p[now<<1|1].m0;
    p[now].m1 = p[now<<1].m1 + p[now<<1|1].m1;
    p[now].m2 = p[now<<1].m2 + p[now<<1|1].m2;
    return ;
}
void pushdown(int now)
{
    int tag = p[now].flag;
    if(tag)
    {
        tag %= 3;
        p[now<<1|1].flag += tag;
        p[now<<1].flag += tag;
        for(int i = 0; i<tag; i++)
        {
            mov_e(p[now<<1]);
            mov_e(p[now<<1|1]);
        }
        p[now].flag = 0;
    }
    return ;
}
void Update(int root,int l,int r)
{
    if(l<=p[root].l && p[root].r <=r)
    {
        p[root].flag += 1;
        mov_e(p[root]);
        return ;
    }
    pushdown(root);
    int mid = (p[root].l+p[root].r)/2;
    int ans = 0;
    if(l>mid)
        Update(root<<1|1,l,r);
    else if(r<=mid)
        Update(root<<1,l,r);
    else
    {
        Update(root<<1|1,l,r);
        Update(root<<1,l,r);
    }
    pushup(root);
}
int Query(int root,int l,int r)
{
    if(l <= p[root].l && p[root].r <= r)
    {
        return p[root].m0;
    }
    pushdown(root);
    int mid = (p[root].l + p[root].r)/2;
    if(l>mid)
        return Query(root<<1|1,l,r);
    else if(r<=mid)
        return Query(root<<1,l,r);
    else
    {
        int ans = 0,ans1 = 0;
        ans = Query(root<<1|1,l,r);
        ans1 = Query(root<<1,l,r);
        return (ans + ans1);
    }
}
int main()
{
    int T;
    while(~scanf("%d",&T))
    {
        for(int w = 1; w<=T; w++)
        {
            int n,q,x,y,ope;
            scanf("%d%d",&n,&q);
            BuildTree(1,1,n);
            printf("Case %d:\n",w);
            for(int i = 1; i<=q; i++)
            {
                scanf("%d",&ope);
                if(ope == 0)
                {
                    scanf("%d %d",&x,&y);
                    Update(1,x+1,y+1);
                }
                else if(ope == 1)
                {
                    int ans = 0;
                    scanf("%d %d",&x,&y);
                    printf("%d\n",Query(1,x+1,y+1));
                }
            }
        }
    }
    return 0;
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值