CodeForces 356A Knight Tournament(线段树成段更新)

题意:n个人m次比赛,每次比赛有a,b,c三个数,代表[a,b]区间的获胜者为c,每次比赛失败者不在比赛,求最终每位选手输给谁;

思路:线段树区间更新单点查询,更新各区间的次序与题目所给次序相反;(注意数据范围)

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[5000010],b[5000010],c[5000010];
int lazy[5000010],tree[5000010];
int n,m;
void pushup(int pos)
{
    tree[pos]=tree[2*pos]|tree[2*pos+1];
}
void pushdown(int pos)
{
    if(lazy[pos])
    {
        lazy[2*pos]=lazy[pos];
        lazy[2*pos+1]=lazy[pos];
        tree[2*pos]=lazy[pos];
        tree[2*pos+1]=lazy[pos];
        lazy[pos]=0;
    }
}
void build(int l,int r,int pos)
{
    int mid=(l+r)/2;
    lazy[pos]=0;
    if(l==r)
    {
        tree[pos]=0;        
        return;
    }
    build(l,mid,2*pos);
    build(mid+1,r,2*pos+1);
}
void update(int L,int R,int val,int l,int r,int pos)
{
    int mid=(l+r)/2;
    if(L<=l&&r<=R)
    {
        tree[pos]=val;
        lazy[pos]=val;
        return;
    }
    pushdown(pos);
    if(L<=mid) update(L,R,val,l,mid,2*pos);
    if(mid<R) update(L,R,val,mid+1,r,2*pos+1);
}
int query(int k,int l,int r,int pos)
{
    int mid=(l+r)/2;
    if(l==r)
    {
        return tree[pos];
    }
    pushdown(pos);
    if(mid>=k) return query(k,l,mid,2*pos);
    return query(k,mid+1,r,2*pos+1);
}
int main()
{
    int i,j,k;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        build(1,n,1);
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&a[i],&b[i],&c[i]);
        }
        for(i=m-1;i>=0;i--)
        {
            if(c[i]>a[i]) update(a[i],c[i]-1,c[i],1,n,1);
            if(c[i]<b[i]) update(c[i]+1,b[i],c[i],1,n,1);
        }
        for(i=1;i<=n;i++)
        {
            printf("%d ",query(i,1,n,1));
        }printf("
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/dashuzhilin/p/4535178.html