微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Codeforces Round #275 (Div. 1) B. Interesting Array (线段树)

题目链接:http://codeforces.com/contest/482/problem/B

题意:给定一个长度为n个序列,要求满足m个条件:[L,R]区间内的异或和为x

分享图片

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <queue>
#include <climits>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define lson l,m,rt << 1
#define rson m + 1,r,rt << 1 | 1
using namespace std;
typedef long long ll;
static const int MAX_N = 1e5 + 5;
static const int N = 1005;
static const ll Mod = 2009;
static const int inf = (1 << 30) - 1;
struct Node{
    int l,x;
}p[MAX_N];
int n;
int sum[MAX_N << 2],lazy[MAX_N << 2];
void push_up(int rt){
    sum[rt] = sum[rt << 1] & sum[rt << 1 | 1];
}
void push_down(int rt){
    if(lazy[rt]){
        sum[rt << 1] |= lazy[rt];
        sum[rt << 1 | 1] |= lazy[rt];
        lazy[rt << 1] |= lazy[rt];
        lazy[rt << 1 | 1] |= lazy[rt];
        lazy[rt] = 0;
    }
}
void update(int l,int r,int rt,int L,int R,int x){
    if(l >= L && r <= R){
        sum[rt] |= x;
        lazy[rt] |= x;
        return ;
    }
    push_down(rt);
    int m = l + r >> 1;
    if(m >= L) update(lson,L,R,x);
    if(R > m) update(rson,x);
    push_up(rt);
}
int query(int l,int R){
    if(l >= L && r <= R) return sum[rt];
    push_down(rt);
    int m = l + r >> 1;
    int ans = inf;
    if(m >= L) ans &= query(lson,R);
    if(R > m) ans &= query(rson,R);
    push_up(rt);
    return ans;
}
void output(int l,int rt){
    if(l == r){
        printf("%d%c",sum[rt],l == n ? \n :  );
        return ;
    }
    push_down(rt);
    int m = l + r >> 1;
    output(lson);
    output(rson);
}
void solve(){
//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
    int m;
    scanf("%d%d",&n,&m);
    for(int i = 0; i < m; ++i){
        scanf("%d%d%d",&p[i].l,&p[i].r,&p[i].x);
        update(1,n,1,p[i].l,p[i].r,p[i].x);
    }
    bool fg = true;
    for(int i = 0; i < m; ++i){
        if(p[i].x != query(1,p[i].r)){
            fg = false;
            break;
        }
    }
    if(!fg) puts("NO");
    else{
        puts("YES");
        output(1,1);
    }
}
int main() {
    solve();
    return 0;
}
View Code

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。