题意: 构造一个序列,满足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 <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 100007 int sum[*N],mark[*N]; void pushup(int rt)
{
sum[rt] = sum[*rt]&sum[*rt+];
} void pushdown(int l,int r,int rt)
{
if(mark[rt])
{
mark[*rt] |= mark[rt];
mark[*rt+] |= mark[rt];
sum[*rt] |= mark[rt];
sum[*rt+] |= mark[rt];
mark[rt] = ;
}
} void build(int l,int r,int rt)
{
mark[rt] = sum[rt] = ;
if(l == r)
return;
int mid = (l+r)/;
build(l,mid,*rt);
build(mid+,r,*rt+);
} void update(int l,int r,int aa,int bb,int val,int rt)
{
if(aa <= l && bb >= r)
{
sum[rt] |= val;
mark[rt] |= val;
return;
}
pushdown(l,r,rt);
int mid = (l+r)/;
if(aa <= mid) update(l,mid,aa,bb,val,*rt);
if(bb > mid) update(mid+,r,aa,bb,val,*rt+);
pushup(rt);
} int query(int l,int r,int aa,int bb,int rt)
{
if(aa <= l && bb >= r)
return sum[rt];
pushdown(l,r,rt);
int mid = (l+r)/;
if(bb <= mid) return query(l,mid,aa,bb,*rt);
else if(aa > mid) return query(mid+,r,aa,bb,*rt+);
else return (query(l,mid,aa,bb,*rt)&query(mid+,r,aa,bb,*rt+));
} void print(int l,int r,int rt)
{
if(l == r)
{
printf("%d ",sum[rt]);
return;
}
pushdown(l,r,rt);
int mid = (l+r)/;
print(l,mid,*rt);
print(mid+,r,*rt+);
} struct Q
{
int l,r,val;
}q[N]; int main()
{
int n,m,i;
while(scanf("%d%d",&n,&m)!=EOF)
{
build(,n,);
for(i=;i<=m;i++)
{
scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].val);
update(,n,q[i].l,q[i].r,q[i].val,);
}
for(i=;i<=m;i++)
{
if(query(,n,q[i].l,q[i].r,) != q[i].val)
break;
}
if(i == m+)
puts("YES"),print(,n,),puts("");
else
puts("NO");
}
return ;
}

Codeforces Round #275 Div.1 B Interesting Array --线段树的更多相关文章

  1. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

  2. Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq

    B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...

  3. Codeforces Round #530 (Div. 2) F (树形dp+线段树)

    F. Cookies 链接:http://codeforces.com/contest/1099/problem/F 题意: 给你一棵树,树上有n个节点,每个节点上有ai块饼干,在这个节点上的每块饼干 ...

  4. Codeforces Round #546 (Div. 2) E 推公式 + 线段树

    https://codeforces.com/contest/1136/problem/E 题意 给你一个有n个数字的a数组,一个有n-1个数字的k数组,两种操作: 1.将a[i]+x,假如a[i]+ ...

  5. Codeforces Round #271 (Div. 2) F. Ant colony 线段树

    F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并

    D. Developing Game   Pavel is going to make a game of his dream. However, he knows that he can't mak ...

  7. Codeforces Round #406 (Div. 2) D. Legacy (线段树建图dij)

    D. Legacy time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  8. Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP

    D. The Bakery   Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...

  9. Codeforces Round #316 (Div. 2) C. Replacement(线段树)

    C. Replacement time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. Hibernate框架的总结

    hibernate 简介:hibernate是一个开源框架,它是对象关联关系映射的框架,它对JDBC做了轻量级的封装,而我们java程序员可以使用面向对象的思想来操纵数据库.hibernate核心接口 ...

  2. webform(内置对象)

    一.内置对象 (一)Response - 响应请求对象1.定义:Response对象用于动态响应客户端请示,控制发送给用户的信息,并将动态生成响应.Response对象只提供了一个数据集合cookie ...

  3. 2013学习总结----JavaScript

    javascript面向对象,实现的几种方式 1:直接使用JSON对象 var o1={ "a":1, "b":2, "c":functio ...

  4. 如果一个游戏上面加一个透明层,js能不能实现 点击透明层的任意点 而正常玩游戏

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 我的一个javascript项目的重构历程

    一个月前,组内的一个内部使用的浏览器比价插件的前端部分交给我来维护,作为一个老司机我是拒绝的,自己的代码都是坑,还要去给别人填坑,搞笑地说. 呵呵,能拒绝么.... 好好享受吧,骚年...... 第一 ...

  6. Log4net中的调错

    在使用log4net时,感觉最麻烦的就是配置文件了,为了使用方便,我不得不先准备好一个完整的配置文件方案,测试了输出到文本.控制台.windows事件.SQL Server数据库都没有问题,但输出到o ...

  7. Cannot export AX project in AX7

    I tried to export project from VS.  I succeed before.  But today I got a Microsoft Visual Studio err ...

  8. NPOI Excel类

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using NPOI.HSSF.Us ...

  9. Java 中Comparator 的使用,实现集合排序

    目标:实现对Person 对象的年龄,从小到大排序 1.实现排序 package com.app; import java.util.ArrayList; import java.util.Colle ...

  10. 带缓存的输入输出-bufferedinputstream类与bufferedoutputstream类

    package hengzhe.cn.o1; import java.io.*; /* * 带缓存的输入输出-bufferedinputstream类与bufferedoutputstream类 * ...