湘潭大学校赛H-统计颜色 线段树

 

 

链接:https://www.nowcoder.com/acm/contest/105/H
来源:牛客网

n个桶按顺序排列,我们用1~n给桶标号。有两种操作:
1 l r c 区间[l,r]中的每个桶中都放入一个颜色为c的球 (1≤l,r ≤n,l≤r,0≤c≤60)
2 l r   查询区间[l,r]的桶中有多少种不同颜色的球     (1≤l,r ≤n,l≤r)

ac代码:

#define _CRT_SECURE_NO_WARNINGS
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<string>
#include<stack>
#include<ctime>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<sstream>
#include<iostream>
#include<functional>
#include<algorithm>
#include<memory.h>
using namespace std;
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
#define rep(i,t,n)  for(int i =(t);i<=(n);++i)
#define per(i,n,t)  for(int i =(n);i>=(t);--i)
#define mp make_pair
#define pb push_back
#define mmm(a,b) memset(a,b,sizeof(a))
typedef long long ll;
const int maxn = 1e5 + 5;
ll tree[maxn * 4], lazy[maxn * 4];
void pushdown(ll root) {
    tree[root << 1] |= lazy[root];
    tree[root << 1 | 1] |= lazy[root];
    lazy[root << 1] |= lazy[root];
    lazy[root << 1 | 1] |= lazy[root];
    lazy[root] = 0;
}
void update(ll l, ll r, ll L, ll R, ll root, ll clr) {
    if (l <= L&&r >= R) {
        tree[root] |= clr;
        lazy[root] |= clr;
        return;
    }
    ll mid = L + R >> 1;
    if (lazy[root])pushdown(root);
    if (r <= mid)update(l, r, L, mid, root << 1, clr);
    else if (l > mid)update(l, r, mid+1, R, root << 1 | 1, clr);
    else {
        update(l, mid, L, mid, root << 1, clr);
        update(mid + 1, r, mid + 1, R, root << 1 | 1, clr);
    }
    tree[root] = tree[root << 1] | tree[root << 1 | 1];

 }
ll query(ll l, ll r, ll L, ll R, ll root, ll clr) {
    if (l <= L&&r >= R) {
        
        return tree[root];
    }
    ll mid = L + R >> 1;
    if (lazy[root])pushdown(root);
    if (r <= mid)return query(l, r, L, mid, root << 1, clr);
    else if (l > mid)return query(l, r, mid+1    , R, root << 1 | 1, clr);
    else {
        return query(l, mid, L, mid, root << 1, clr)| query(mid + 1, r, mid + 1, R, root << 1 | 1, clr);;
        
    }
    tree[root] = tree[root << 1] | tree[root << 1 | 1];

}
int main()
{
    ios::sync_with_stdio(false);
    int n, m;
    while (cin >> n >> m) {
        mmm(tree, 0);
        mmm(lazy, 0);
        rep(i, 1, m) {
            ll x, l, r, c;
            cin >> x;
            if (x == 1) {
                cin >> l >> r >> c;
                update(l, r, 1, n, 1, 1ll << c);

            }
            if (x == 2) {
                cin >> l >> r;
                ll ans = query(l, r, 1, n,1, 1);
                int res = 0;
                while (ans) {
                    if (ans & 1)res++;
                    ans >>= 1;
                }
                cout << res << endl;
            }
        }
    }
    


    
}

 

 

tle代码

 

#define _CRT_SECURE_NO_WARNINGS
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<cstdio>
#include<string>
#include<stack>
#include<ctime>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<sstream>
#include<iostream>
#include<functional>
#include<algorithm>
#include<memory.h>
//#define INF 0x3f3f3f3f
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
#define rep(i,t,n)  for(int i =(t);i<=(n);++i)
#define per(i,n,t)  for(int i =(n);i>=(t);--i)
#define mp make_pair
#define pb push_back
#define mmm(a,b) memset(a,b,sizeof(a))
//std::ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
void smain();
#define ONLINE_JUDGE
int main() {
//    ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    long _begin_time = clock();
#endif
    smain();
#ifndef ONLINE_JUDGE
    long _end_time = clock();
    printf("time = %ld ms.", _end_time - _begin_time);
#endif
    return 0;
}
#define  _CRT_SECURE_NO_WARNINGS

const int maxn = 1e5 + 5;
int a[maxn], n, q, l, r, val;
typedef long long ll;
struct node {
    int l, r;
    long long    sum, lazy;
    void update(long long x) {
        //sum += (ll)1 * (r - l + 1)*x;
        //lazy += x;
        sum |= x;
        lazy |= x;
    }
}tree[maxn * 4];
void push_up(int x) {
    //tree[x].sum = tree[x << 1].sum + tree[x << 1 | 1].sum;
    tree[x].sum = tree[x << 1].sum | tree[x << 1 | 1].sum;
}
void push_down(int x) {
    int lazyval = tree[x].lazy;
    if (lazyval) {
        tree[x << 1].update(lazyval);
        tree[x << 1 | 1].update(lazyval);
        tree->lazy = 0;
    }
}
void build(int x, int l, int r) {
    tree[x].l = l; tree[x].r = r;
    tree[x].sum = tree[x].lazy = 0;
    if (l == r) {
        tree[x].sum = 0;
    }
    else {
        int mid = l + r >> 1;
        build(x << 1, l, mid);
        build(x << 1 | 1, mid + 1, r);
        push_up(x);
    }
}
void update(int x, int l, int r, long long val) {
    int L = tree[x].l, R = tree[x].r;
    if (l <= L&&r >= R) {
        tree[x].update(val);
    }
    else {
        push_down(x);
        int mid = L + R >> 1;
        if (mid >= l)update(x << 1, l, r, val);
        if (r > mid)update(x << 1 | 1, l, r, val);
        push_up(x);
    }
}
long long query(int x, int l, int r) {
    int L = tree[x].l, R = tree[x].r;
    if (l <= L&&r >= R) {
        return tree[x].sum;
    }
    else {
        push_down(x);
        long long ans = 0;
        int mid = L + R >> 1;
        if (mid >= l)ans |= query(x << 1, l, r);
        if (r > mid)ans |= query(x << 1 | 1, l, r);
        push_up(x);
        return ans;
    }
}

void Run() {

}

void smain() {
    cin >> n >> q; {
        build(1, 1, n);
        rep(i, 1, q) {
            int x;
            scanf("%d", &x);
            //cin >> x;
            if (x == 1) {
                int l, r, c;
                scanf("%d%d%d", &l, &r,&c);
                //cin >> l >> r >> c;
                update(1, l, r, 1 << c);

            }
            else {
                int l, r;
                
                scanf("%d%d", &l, &r);
                //cin >> l >> r;
                ll res= query(1, l, r);
                int cnt=0;

                while (res) {
                    if (res & 1)cnt++;
                    res >>= 1;
                }
                printf("%d\n", cnt);
                //cout << cnt << endl;
            }
        }
    }
}

 

posted @ 2018-05-02 14:31  SuuTTT  阅读(279)  评论(0编辑  收藏  举报