Codeforces 483D Interesting Array【思维+线段树】

本文介绍了一个算法问题,即构建一个有趣的数组,该数组需满足特定条件。通过位运算和前缀和技巧,文章详细阐述了如何高效地找到满足条件的数组,或者判断不存在这样的数组。

D. Interesting Array
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers liriqi (1 ≤ li ≤ ri ≤ n) meaning that value  should be equal to qi.

Your task is to find any interesting array of n elements or state that such array doesn't exist.

Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as "&", in Pascal — as "and".

Input

The first line contains two integers nm (1 ≤ n ≤ 1051 ≤ m ≤ 105) — the number of elements in the array and the number of limits.

Each of the next m lines contains three integers liriqi (1 ≤ li ≤ ri ≤ n0 ≤ qi < 230) describing the i-th limit.

Output

If the interesting array exists, in the first line print "YES" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n](0 ≤ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them.

If the interesting array doesn't exist, print "NO" (without the quotes) in the single line.

Examples
input
3 1
1 3 3
output
YES
3 3 3
input
3 2
1 3 3
1 3 2
output
NO

题目大意:


已知有M个信息,表示【L,R】区间内的与的值为val.问是否存在这样一个数组,如果存在,输出,如果不存在输出NO.


思路:


①我们知道,每个查询都是单独的,所以我们如果知道了【L,R】区间与的值为val,那么我们顺其自然,直接将这个区间上的值都或上val.


②那么我们可以维护一个数组sum【i】【j】,表示j这个位子上,第i位是否有1,那么对于m个已知信息,我们可以O(m*30)去维护这个数组,在sum【i】【L】上+1.在sum【i】【R+1】上-1.然后我们O(n*30)遍历这个数组,就能够知道一个数组a【】的值。


③那么我们对于这个数组a【】进行m次查询,如果每个区间的与的值都和已知信息相对,那么结果就是这个数组,否则就是不存在这样的一个解。


Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
struct node
{
    int l,r,val;
}q[150000];
int INF;
int a[150000];
int sum[32][150000];
int now[32];
/**********************************/
#define lson l,m,rt*2
#define rson m+1,r,rt*2+1
int tree[150000*4];
void pushup(int rt)
{
    tree[rt]=tree[rt*2]&tree[rt*2+1];
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        tree[rt]=a[l];
        return ;
    }
    int m=(l+r)/2;
    build(lson);build(rson);pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(l>=L&&r<=R)
    {
        return tree[rt];
    }
    pushup(rt);
    int m=(l+r)/2;
    int ans=INF;
    if(L<=m)ans=(ans&(query(L,R,lson)));
    if(R>m)ans=(ans&(query(L,R,rson)));
    pushup(rt);
    return ans;
}
/**********************************/
int main()
{
    int n,m;
    for(int i=0;i<=30;i++)INF+=(1<<i);
    while(~scanf("%d%d",&n,&m))
    {
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=m;i++)
        {
            int L,R,val;
            scanf("%d%d%d",&L,&R,&val);
            q[i].l=L;q[i].r=R,q[i].val=val;
            for(int j=0;j<=30;j++)
            {
                if((val&((1<<j)))>0)
                {
                    sum[j][L]++;
                    sum[j][R+1]--;
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<=30;j++)
            {
                now[j]+=sum[j][i];
            }
            int num=0;
            for(int j=0;j<=30;j++)
            {
                if(now[j]>0)num+=(1<<j);
            }
            a[i]=num;
        }
        build(1,n,1);
        int flag=1;
        for(int i=1;i<=m;i++)
        {
            if(query(q[i].l,q[i].r,1,n,1)!=q[i].val)flag=0;
        }
        if(flag==0)printf("NO\n");
        else
        {
            printf("YES\n");
            for(int i=1;i<=n;i++)
            {
                printf("%d ",a[i]);
            }
            printf("\n");
        }
    }
}










### 关于C++实现线段树思维导图 线段树是一种高效的数据结构,主要用于解决区间查询和更新问题。以下是围绕C++中实现线段树的相关知识点构建的一个思维导图框架: #### 1. **线段树的基础概念** - 定义:线段树是一种二叉树结构,用于处理数组上的范围查询和修改操作[^3]。 - 特点: - 每个节点表示一个区间。 - 叶子节点对应数组中的单个元素。 - 非叶子节点表示其两个子区间的并集。 #### 2. **线段树的核心功能** - 区间求和/最大值/最小值:通过递归方式计算指定区间的值。 - 单点更新:更改某个位置的值,并相应调整受影响的父节点。 - 区间更新:批量修改某一段区域内的值。 #### 3. **C++实现的关键要素** - 构造函数初始化列表:在线段树类定义中,利用构造函数初始化列表完成必要的变量设置[^5]。 - 动态内存分配:通常使用`std::vector<int>`作为底层容器存储节点数据。 - 延迟标记(Lazy Propagation):优化大规模区间更新操作,减少冗余计算。 ```cpp #include <iostream> #include <vector> using namespace std; class SegmentTree { private: vector<int> tree; int n; void build(int node, int start, int end, const vector<int>& arr) { if (start == end) { tree[node] = arr[start]; } else { int mid = (start + end) / 2; build(2 * node, start, mid, arr); build(2 * node + 1, mid + 1, end, arr); tree[node] = tree[2 * node] + tree[2 * node + 1]; // 修改此处可支持其他运算 } } public: SegmentTree(const vector<int>& arr) : n(arr.size()), tree(4 * n, 0) { build(1, 0, n - 1, arr); // 根节点编号为1 } int query(int node, int start, int end, int l, int r) { if (r < start || l > end) return 0; // 不相交的情况 if (l <= start && end <= r) return tree[node]; // 完全包含 int mid = (start + end) / 2; return query(2 * node, start, mid, l, r) + query(2 * node + 1, mid + 1, end, l, r); } void update(int node, int start, int end, int idx, int val) { if (start == end) { tree[node] += val; } else { int mid = (start + end) / 2; if (idx <= mid) { update(2 * node, start, mid, idx, val); } else { update(2 * node + 1, mid + 1, end, idx, val); } tree[node] = tree[2 * node] + tree[2 * node + 1]; } } }; ``` #### 4. **学习资源推荐** - 推荐书籍:《算法竞赛入门经典——训练指南》,其中详细讲解了线段树的应用场景及其变种形式[^2]。 - 在线教程:LeetCode 和 Codeforces 平台提供了丰富的练习题目以及社区讨论[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值