线段树(good)Codeforces Round #275 (Div. 2)D
来源:互联网 发布:怎么样让淘宝号升心快 编辑:程序博客网 时间:2024/04/28 21:39
题意: 构造一个序列,满足m个形如:[l,r,c] 的条件。 [l,r,c]表示[l,r]中的元素按位与(&)的和为c。
解法: 线段树维护,sum[rt]表示要满足到现在为止的条件时该子树的按位与和至少为多少。
更新时,如果val的pos位为1,那么整个区间的按位与和pos位也应该为1,否则与出来就不对了。(这是本题解题的核心)
那么此时更新 sum[rt] |= val 即可。然后再check一遍看是否满足所有条件即可。
#include<iostream>#include<cstdio>#include<string>#include<cstring>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>using namespace std;const int maxn=100010;typedef long long LL;int N,M;struct node{ int l,r; LL q;}qu[maxn];struct IntervalTree{ int sum[maxn<<2],setv[maxn<<2]; void build(int o,int l,int r) { sum[o]=setv[o]=0; if(l==r)return; int mid=(l+r)>>1; build(o<<1,l,mid); build(o<<1|1,mid+1,r); } void pushdown(int o) { if(setv[o]) { setv[o<<1]|=setv[o]; setv[o<<1|1]|=setv[o]; sum[o<<1]|=setv[o]; sum[o<<1|1]|=setv[o]; setv[o]=0; } } void pushup(int o) { sum[o]=sum[o<<1]&sum[o<<1|1]; } void update(int o,int l,int r,int q1,int q2,int val) { if(q1<=l&&r<=q2) { sum[o]|=val; setv[o]|=val; return; } pushdown(o); int mid=(l+r)>>1; if(q1<=mid)update(o<<1,l,mid,q1,q2,val); if(q2>mid)update(o<<1|1,mid+1,r,q1,q2,val); pushup(o); } int query(int o,int l,int r,int q1,int q2) { if(q1<=l&&r<=q2)return sum[o]; pushdown(o); int mid=(l+r)>>1; if(q2<=mid)return query(o<<1,l,mid,q1,q2); else if(q1>mid) return query(o<<1|1,mid+1,r,q1,q2); else return (query(o<<1,l,mid,q1,q2)&query(o<<1|1,mid+1,r,q1,q2)); } void print(int o,int l,int r) { if(l==r) { printf("%d ",sum[o]); return; } pushdown(o); int mid=(l+r)>>1; print(o<<1,l,mid); print(o<<1|1,mid+1,r); }}tree;int main(){ while(scanf("%d%d",&N,&M)!=EOF) { tree.build(1,1,N); for(int i=1;i<=M;i++) { scanf("%d%d%d",&qu[i].l,&qu[i].r,&qu[i].q); tree.update(1,1,N,qu[i].l,qu[i].r,qu[i].q); } int flag=1; for(int i=1;i<=M;i++) { if(tree.query(1,1,N,qu[i].l,qu[i].r)!=qu[i].q) { flag=0; break; } } if(!flag)printf("NO\n"); else {printf("YES\n");tree.print(1,1,N);printf("\n");} } return 0;}
0 0
- 线段树(good)Codeforces Round #275 (Div. 2)D
- Codeforces Round #275 (Div. 2) D题 (线段树)
- Codeforces Round #275 (Div. 2) D 线段树
- Codeforces Round #365 (Div. 2) D (线段树)
- Codeforces Round #283 (Div. 2)D(good)
- Codeforces Round #271 (Div. 2)(dp,线段树good)(很好的一场cf)
- Codeforces Round #197 (Div. 2)(d线段树)
- Codeforces Round #266 (Div. 2)(good)
- Codeforces Round #279 (Div. 2)(good)
- Codeforces Round #166 (Div. 2) D - Good Substrings
- Codeforces Round #162 (Div. 2), problem: (D) Good Sequences
- Codeforces Round #166 (Div. 2)---D. Good Substrings(字符串)
- Codeforces Round #258 (Div. 2) D. Count Good Substrings
- Codeforces Round #258 (Div. 2)-D. Count Good Substrings
- 贪心(最大团)||dp+线段树(Codeforces Round #296 (Div. 2)D. Clique Problem)
- Codeforces Round #291 (Div. 2)D. R2D2 and Droid Army (线段树+二分)
- 线段树+二分(Codeforces Round #291 (Div. 2)D. R2D2 and Droid Army)
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake(线段树+dp)
- Lambda表达式详解
- char * a, char ** a, char * a[], char a[][], char * a[][], char ** a[][], char * a [][][], and so on
- 【ThinkingInC++】76、设计模式
- svn使用
- should be mapped with insert="false" update="false"的解决方法 和对象产生默认值的解决方法
- 线段树(good)Codeforces Round #275 (Div. 2)D
- JS匿名函数&闭包相关
- std::vector删除重复元素和查找
- Unity3d脚本执行顺序详解
- Oracle VM VirtualBox CentOS 5.9 安装教程(超级详细图文教程)
- Android 异步消息处理机制(一) 让你深入理解 Looper、Handler、Message三者关系
- 业主圈地建“私家花园”
- socket,ASN.1,webservice,soap,json之间的关系。
- UVA - 193 Graph Coloring