[Codeforces 482B] Interesting Array

Posted evenbao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Codeforces 482B] Interesting Array相关的知识,希望对你有一定的参考价值。

[题目链接]

         https://codeforces.com/contest/482/problem/B

[算法]

        显然 , 当qi二进制表示下第j位为1时 , [li,ri]中每个数二进制表示下的第j为也为1

        根据这个性质 , 计算出要求的序列a, 然后用线段树检验序列是否合法即可

        时间复杂度 : O(NlogN)

[代码]

         

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
#define MAXLOG 31

struct query
{
        int l , r , q;
} a[MAXN];

int n , m;
int value[MAXN],cnt[MAXN];

struct SegmentTree
{
        struct Node
        {
                int l , r , val;
        } Tree[MAXN << 2];
        inline void build(int index,int l,int r)
        {
                Tree[index].l = l;
                Tree[index].r = r;
                if (l == r) 
                {
                        Tree[index].val = value[l];
                        return;
                }
                int mid = (l + r) >> 1;
                build(index << 1,l,mid);
                build(index << 1 | 1,mid + 1,r);
                update(index);
        }
        inline void update(int index)
        {
                Tree[index].val = Tree[index << 1].val & Tree[index << 1 | 1].val;
        }
        inline int query(int index,int l,int r)
        {
                if (Tree[index].l == l && Tree[index].r == r) return Tree[index].val;
                int mid = (Tree[index].l + Tree[index].r) >> 1;
                if (mid >= r) return query(index << 1,l,r);
                else if (mid + 1 <= l) return query(index << 1 | 1,l,r);
                else return query(index << 1,l,mid) & query(index << 1 | 1,mid + 1,r);
        }
} T;

template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
    T f = 1; x = 0;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == -) f = -f;
    for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - 0;
    x *= f;
}

int main()
{
        
        read(n); read(m);
        for (int i = 1; i <= m; i++)
        {
                read(a[i].l);
                read(a[i].r);
                read(a[i].q);
        }
        for (int i = 0; i < MAXLOG; i++)
        {
                for (int j = 0; j <= n; j++) cnt[j] = 0;
                for (int j = 1; j <= m; j++)
                {
                        if (a[j].q & (1 << i))
                        {
                                cnt[a[j].l]++;
                                cnt[a[j].r + 1]--;        
                        }        
                } 
                for (int j = 1; j <= n; j++) 
                {
                        cnt[j] += cnt[j - 1];
                        if (cnt[j] > 0)
                                value[j] |= (1 << i);
                }
        } 
        T.build(1,1,n);
        for (int i = 1; i <= m; i++)
        {
                if (T.query(1,a[i].l,a[i].r) != a[i].q)
                {
                        printf("NO
");
                        return 0;
                }
        }
        printf("YES
");
        for (int i = 1; i <= n; i++) printf("%d ",value[i]);
        printf("
");
        
        return 0;
    
}

 

以上是关于[Codeforces 482B] Interesting Array的主要内容,如果未能解决你的问题,请参考以下文章

UIButton的圆角

HCIE实验复习

windows系统查找文件-通配符的使用

ElementUI el-checkbox实现全选反选单选

CentOS网卡无法联网,ethtool显示Link detected: no Speed: Unknown! Duplex: Unknown! (255)

codeforces上怎么看测试数据