Lightoj 1135-count The multiples of 3 segment tree

Source: Internet
Author: User






http://www.lightoj.com/volume_showproblem.php?problem=1135



Test Instructions: given two operations, one for all elements of the interval plus 1, a query interval can be divisible by 3 of the number of how many.



idea: The requirement is divisible by 3, we can record 3 states, the current interval modulus 3 more than 1 of the remaining 2 of 0, then on a number increase, the direct exchange of the number of different remainder can be.


/** @Date    : 2016-12-06-20.00
  * @Author  : Lweleth ([email protected])
  * @Link    : https://github.com/
  * @Version :
  */

#include<bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

struct yuu
{
    int l, r;
    int add;
    int m0, m1, m2;
}tt[N << 2];

void pushup(int p)
{
    tt[p].m0 = tt[p << 1].m0 + tt[p << 1 | 1].m0;
    tt[p].m1 = tt[p << 1].m1 + tt[p << 1 | 1].m1;
    tt[p].m2 = tt[p << 1].m2 + tt[p << 1 | 1].m2;
}

void pushdown(int p)
{
    if(tt[p].add != 0)
    {
        tt[p].add %= 3;
        ///
        tt[p << 1].add += tt[p].add;
        if(tt[p].add == 2)
        {
            swap(tt[p << 1].m0 , tt[p << 1].m1);
            swap(tt[p << 1].m0 , tt[p << 1].m2);
        }
        else if(tt[p].add == 1)
        {
            swap(tt[p << 1].m0 , tt[p << 1].m2);
            swap(tt[p << 1].m1 , tt[p << 1].m0);
        }
        ///
        tt[p << 1 | 1].add += tt[p].add;
        if(tt[p].add == 2)
        {
            swap(tt[p << 1 | 1].m0 , tt[p << 1 | 1].m1);
            swap(tt[p << 1 | 1].m0 , tt[p << 1 | 1].m2);
        }
        else if(tt[p].add == 1)
        {
            swap(tt[p << 1 | 1].m0 , tt[p << 1 | 1].m2);
            swap(tt[p << 1 | 1].m1 , tt[p << 1 | 1].m0);
        }
        tt[p].add = 0;
    }
}

void build(int l, int r, int p)
{
    tt[p].l = l;
    tt[p].r = r;
    tt[p].add = tt[p].m0 = tt[p].m2 = tt[p].m1 = 0;
    if(l == r)
    {
        tt[p].m0 = 1;
        return ;
    }
    int mid = (l + r) >> 1;
    build(l , mid, p << 1);
    build(mid + 1, r, p << 1 | 1);
    pushup(p);
}

void updata(int l, int r, int v, int p)
{
    if(l <= tt[p].l && r >= tt[p].r)
    {
        tt[p].add += v;
        swap(tt[p].m0 , tt[p].m2);
        swap(tt[p].m1 , tt[p].m0);
        return ;
    }
    pushdown(p);
    int mid = (tt[p].l + tt[p].r) >> 1;
    if(l <= mid)
        updata(l, r, v, p << 1);
    if(r > mid)
        updata(l, r, v, p << 1 | 1);
    pushup(p);
}

int query(int l, int r, int p)
{
    if(l <= tt[p].l && r >= tt[p].r)
    {
        return tt[p].m0;
    }
    pushdown(p);
    int mid = (tt[p].l + tt[p].r) >> 1;
    int ans = 0;
    if(l <= mid)
        ans += query(l, r, p << 1);
    if(r > mid)
        ans += query(l, r, p << 1 | 1);
    return ans;
}
int main()
{
    int T;
    int cnt = 0;
    cin >> T;
    while(T--)
    {
        int n, q;
        scanf("%d%d", &n, &q);
        build(1, n, 1);
        printf("Case %d:\n", ++cnt);
        while(q--)
        {
            int t, x, y;
            scanf("%d%d%d", &t ,&x ,&y);
            if(t)
                printf("%d\n", query(x+1, y+1, 1));
            else
                updata(x+1, y+1, 1, 1);
        }
    }
    return 0;
}


Lightoj 1135-count The multiples of 3 segment tree


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.