• 线段树成段更新模板


    #include <bits/stdc++.h>
    
    struct Tree {
    	Tree *lc,*rc;
    	int l,r;
    	int v,lazy;
    	Tree(int l=0,int r=0)
    		:l(l),r(r),v(0),lazy(0) {}
    	void build() {
    		if (l == r) return ;
    		int mid = l+r>>1;
    		lc = new Tree(l,mid);
    		rc = new Tree(mid+1,r);
    		lc->build();
    		rc->build();
    	}
    	void modify(int x,int L,int R) {
    		if (L<=l && r<=R) {
    			v |= x;
    			lazy |= x;
    			return ;
    		}
    		if (R<l || r<L) return ;
    		down();
    		lc->modify(x,L,R);
    		rc->modify(x,L,R);
    		v = lc->v & rc->v;
    	}
    	void down() {
    		if (lazy == 0) return ;
    		lc->lazy |= lazy;
    		lc->v |= lazy;
    		rc->lazy |= lazy;
    		rc->v |= lazy;
    		lazy = 0;
    	}
    	int query(int L,int R) {
    		if (L<=l && r<=R) return v;
    		if (R<l || r<L) return (1ll<<31)-1;
    		down();
    		return lc->query(L,R) & rc->query(L,R);
    	}
    	void show() {
    		if (l == r) {
    			printf("%d ",v);
    			return ;
    		}
    		down();
    		lc->show();
    		rc->show();
    	}
    };
    
    const int N = 100000 + 5;
    int n,m;
    int L[N],R[N],Q[N];
    
    int main() {
    	scanf("%d%d",&n,&m);
    	Tree *tree = new Tree(1,n);
    	tree->build();
    	for (int i = 0; i < m; ++ i) {
    		scanf("%d%d%d",L+i,R+i,Q+i);
    		tree->modify(Q[i],L[i],R[i]);
    	}
    	for (int i = 0; i < m; ++ i) {
    		if (tree->query(L[i],R[i]) != Q[i]) {
    			puts("NO"); return 0;
    		}
    	}
    	puts("YES");
    	tree->show();
    	puts("");
    	return 0;
    }
    

      by 铭神

    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <algorithm>
    using namespace std;
    typedef __int64 ll;
    
    const int MOD = (int)1e9+7;
    const int N = 100000+5;
    
    struct Segtree {
    #define lson rt<<1, l, mid
    #define rson rt<<1|1, mid+1, r
        int node[N<<2], mark[N<<2];
    
        void down(int rt) {
            if(mark[rt]) {
                mark[rt<<1] |= mark[rt];
                mark[rt<<1|1] |= mark[rt];
                node[rt<<1] |= mark[rt];
                node[rt<<1|1] |= mark[rt];
                mark[rt] = 0;
            }
        }
    
        void up(int rt) {
            node[rt] = node[rt<<1] & node[rt<<1|1];
        }
    
        void update(int rt, int l, int r, int L, int R, int x) {
            if(L <= l && R >= r) {
                mark[rt] |= x;
                node[rt] |= x;
                return ;
            }
            down(rt);
            int mid = l+r>>1;
            if(L <= mid)    update(lson, L, R, x);
            if(R > mid) update(rson, L, R, x);
            up(rt);
        }
    
        int query(int rt, int l, int r, int L, int R) {
            if(L <= l && R >= r) return node[rt];
            down(rt);
            int mid = l+r>>1;
            int ret1 = -1, ret2 = -1;
            if(L <= mid) ret1 = query(lson, L, R);
            if(R > mid  )ret2 = query(rson, L, R);
            up(rt);
            if(ret1 == -1)  return ret2;
            if(ret2 == -1)  return ret1;
            return ret1&ret2;
        }
    }tree;
    
    int l[N], r[N], q[N];
    
    int main() {
        int n, m;
        scanf("%d%d", &n, &m);
        for(int i = 1;i <= m; i++) {
            scanf("%d%d%d", &l[i], &r[i], &q[i]);
            tree.update(1, 1, n, l[i], r[i], q[i]);
        }
        for(int i = 1;i <= m; i++) {
            if(tree.query(1, 1, n, l[i], r[i]) != q[i])
                return puts("NO") , 0;
        }
        puts("YES");
        for(int i = 1;i <= n; i++)
            printf("%d ", tree.query(1, 1, n, i, i));
        puts("");
        return 0;
    }
    

      by 杰哥

    http://codeforces.com/problemset/problem/482/B

  • 相关阅读:
    用户输入
    hashlib 加密
    vue-cli项目配置mock数据(新版本)
    vue-cli 本地开发mock数据使用方法
    使用nodeJs安装Vue-cli
    购物车实现三
    购物车实现二
    购物车实现一
    极限开发与敏捷开发
    敏捷软件开发-极限编程
  • 原文地址:https://www.cnblogs.com/get-an-AC-everyday/p/5145022.html
Copyright © 2020-2023  润新知