Niuke.com-Xiangtan University School Competition Reproduces Question H (Line Segment Tree Dyeing Problem)

Link: https://www.nowcoder.com/acm/contest/105/H
Source: Niuke.com

The n buckets are arranged in order, and we number the buckets with 1~n. There are two operations:
1 lrc put a ball of color c into each bucket in the lrc interval [l,r] (1≤l,r≤n,l≤r,0≤c≤60)
2 lr query How many balls of different colors are in the bucket in the interval [l,r] (1≤l,r≤n,l≤r)

Enter description:

There are multiple sets of data, for each set of data: 
the first line has two integers n, m (1≤n, m≤100000) and
the next m lines represent m operations, the format is as shown in the title.

Output description:

For each operation number 2, output an integer representing the result of the query.

Example 1

enter

10 10
1 1 2 0
1 3 4 1
2 1 4
1 5 6 2
2 1 6
1 7 8 1
2 3 8
1 8 10 3
2 1 10
2 3 8

output

2
3
2
4
3 

Ideas:
Seeing that the color is <= 60, it is clear that binary is used. . The writing is similar to poj2777, except that poj2777 is an overlay color, and this is an added color. .
Forget that the << operator doesn't support the ll range. . Looking for a long time wrong. . . Just replace it with fast power directly.
If you have written this type of question before, it is very simple to write.

Implementation code;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
#define ll long long
const ll M = 1e5+10;
ll powl(ll n,ll m)
{
    ll ans = 1;
    while(m > 0)
    {
        if(m & 1)ans = (ans * n);
        m = m >> 1;
        n = (n * n);
    }
    return ans;
}

ll n,w,e;
ll color[M*4],lazy[M*4];
void pushup(ll rt){
   color[rt] = color[rt*2]|color[rt*2+1];
}

void build(ll l,ll r,ll rt){
    if(l==r){
        color[rt] = 0;
        return ;
    }
    ll m = (l+r)/2;
    build(l,m,rt*2);
    build(m+1,r,rt*2+1);
    pushup(rt);
}

void pushdown(ll rt){
    if(lazy[rt]){
        lazy[rt*2] |= lazy[rt];
        lazy[rt*2+1] |= lazy[rt];
        color[rt*2] |= lazy[rt*2];
        color[rt*2+1] |= lazy[rt*2+1];
        lazy[rt] = 0;
    }
}

void update(ll L,ll R,ll l,ll r,ll x,ll rt){
     if(L<=l&&r<=R){
        lazy[rt] |= powl(2,x-1);
        color[rt] |= powl(2,x-1);
        return ;
     }
     pushdown(rt);
     ll m = (l+r)/2;
     if(L<=m)  update(L,R,l,m,x,rt*2);
     if(R>m) update(L,R,m+1,r,x,rt*2+1);
     pushup(rt);
}

ll query(ll L,ll R,ll l,ll r,ll rt){
    if(L<=l&&r<=R){
        return color[rt];
    }
    ll ans1=0,ans2=0,ans;
    ll m = (l+r)/2;
    pushdown(rt);
    if(L<=m) ans1+=query(L,R,l,m,rt*2);
    if(R>m)  ans2+=query(L,R,m+1,r,rt*2+1);
    years = years1| years2;
    return years;
}

void getsum(ll x){
    ll ans = 0;
    while(x){
        if(x%2==1) ans++;
        x/=2;
    }
    printf("%lld\n",ans);
}

intmain ()
{
    ll L,R,x;
    int c;
    while(scanf("%lld%lld",&n,&w)!=EOF){
        memset(lazy,0,sizeof(lazy));
        build(1,n,1);
        while(w--){
        scanf("%lld",&c);
        if(c==1){
            scanf("%lld%lld%lld",&L,&R,&x);
            if(L>R) swap(L,R);
            x++;
            update(L,R,1,n,x,1);
        }
        else{
            scanf("%lld%lld",&L,&R);
            if(L>R) swap(L,R);
            ll cnt = query(L,R,1,n,1);
             //cout<<cnt<<endl;
            getsum(cnt);
        }
    }
    }
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324906291&siteId=291194637