CF 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
Copy
3 1
1 3 3
output
Copy
YES
3 3 3
input
Copy
3 2
1 3 3
1 3 2
output
Copy
NO

题意:

构造出一个序列,满足下面m个要求:

每一个要求的组成是l,r,q三个数,表示从a[l]&a[l+1]....&a[r]的值为q,问能不能构造出这样的序列。

思路:

sum[i]表示该区间的&的值,一开始处理每个要求的时候,我们都让它直接满足该要求,sum[i]|q来满足。最后我们再看看每个sum[i]的值是否还为原来的值,如果不是,则无法构造。可以的话,那么我们查询到叶子节点就是构造的序列。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1e5+5;
int sum[maxn<<2],lazy[maxn<<2];
int l[maxn],r[maxn],c[maxn];
int now;
void pushdown(int i)
{
    if(lazy[i])
    {
        sum[2*i]|=lazy[i];
        sum[2*i+1]|=lazy[i];
        lazy[2*i]|=lazy[i];
        lazy[2*i+1]|=lazy[i];
        lazy[i]=0;
    }
}
void update(int i,int l,int r,int x,int y,int c)
{
    if(x<=l&&r<=y)
    {
        sum[i]|=c;
        lazy[i]|=c;
        return;
    }
    pushdown(i);
    int mid=(l+r)/2;
    if(x<=mid) update(2*i,l,mid,x,y,c);
    if(y>mid) update(2*i+1,mid+1,r,x,y,c);
    sum[i]=sum[2*i]&sum[2*i+1];
    return;
}
void query(int i,int l,int r,int x,int y)
{
    if(x<=l&&r<=y)
    {
        now&=sum[i];
        return;
    }
    pushdown(i);
    int mid=(l+r)/2;
    if(x<=mid) query(2*i,l,mid,x,y);
    if(y>mid) query(2*i+1,mid+1,r,x,y);
    sum[i]=sum[2*i]&sum[2*i+1];
    return;
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&l[i],&r[i],&c[i]);
            update(1,1,n,l[i],r[i],c[i]);
        }
        for(int i=1;i<=m;i++)
        {
            now=(1<<30)-1;
            query(1,1,n,l[i],r[i]);
            if(now!=c[i])
            {
                cout<<"NO"<<endl;
                return 0;
            }
        }
        cout<<"YES"<<endl;
        for(int i=1;i<=n;i++)
        {
            now=(1<<30)-1;
            query(1,1,n,i,i);
            cout<<now<<" ";
        }
    }
    return 0;
}


### 关于C++线段树实现中的错误分析 在线段树的实现过程中,可能会遇到多种类型的错误。以下是常见的几种原因及其可能引发的问题: #### 1. **未初始化变量** 如果在构建线段树之前没有对数组或其他存储结构进行清零处理,则可能导致残留数据干扰计算结果[^3]。 例如,在某些情况下,`sum` 或 `lazy` 数组如果没有被正确初始化为零,后续的操作会受到先前数据的影响。 ```cpp // 初始化操作 memset(sum, 0, sizeof(sum)); memset(lazy, 0, sizeof(lazy)); ``` #### 2. **懒惰标记(Lazy Propagation)问题** 当使用带延迟更新的线段树时,忘记下传懒惰标记或者错误地实现了 `push_down` 函数,都会导致查询结果不准确[^2]。 ```cpp void push_down(int p, int l, int r) { if (lazy[p]) { int mid = (l + r) >> 1; lazy[left(p)] += lazy[p]; sum[left(p)] += (mid - l + 1) * lazy[p]; lazy[right(p)] += lazy[p]; sum[right(p)] += (r - mid) * lazy[p]; lazy[p] = 0; // 清除当前节点的懒惰标记 } } ``` #### 3. **边界条件处理不当** 对于区间的划分和递归终止条件,如果不小心设置错误,可能会导致无限递归或错过目标区间[^4]。 例如,在以下代码片段中,如果 `if(tree[i].l > X || tree[i].r < X)` 条件写错,就无法正常退出递归。 ```cpp void find(int i) { if (tree[i].l > X || tree[i].r < X) return ; ans += tree[i].cnt; find(i * 2); find(i * 2 + 1); } ``` #### 4. **内存分配不足** 由于线段树通常需要两倍甚至四倍的空间来存储节点信息,因此如果数组大小定义过小,就会发生越界访问。 建议按照实际需求合理规划空间大小,比如将数组容量设为 `N << 2`。 ```cpp const int MAX_N = 1e6 + 5; int segTree[MAX_N << 2]; // 开辟足够的空间 ``` #### 5. **逻辑错误** 有时算法本身的逻辑存在缺陷,例如在解决特定题目时未能充分考虑特殊情况。以 CF527C Glass Carving 为例,若未维护好最大矩形面积的变化过程,则难以得出正确答案[^5]。 --- ### 总结 综上所述,C++ 中线段树实现可能出现的错误主要包括但不限于以上几点。开发者应仔细检查每一处细节,并通过调试工具验证程序行为是否符合预期。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值