CodeForce 356A Knight Tournament(线段树的区间更新+单点询问)

93 篇文章 1 订阅
52 篇文章 0 订阅

Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the message to all knights in the kingdom and they in turn agreed to participate in this grand event.

As for you, you're just a simple peasant. There's no surprise that you slept in this morning and were late for the tournament (it was a weekend, after all). Now you are really curious about the results of the tournament. This time the tournament in Berland went as follows:

  • There are n knights participating in the tournament. Each knight was assigned his unique number — an integer from 1 to n.
  • The tournament consisted of m fights, in the i-th fight the knights that were still in the game with numbers at least li and at most ri have fought for the right to continue taking part in the tournament.
  • After the i-th fight among all participants of the fight only one knight won — the knight number xi, he continued participating in the tournament. Other knights left the tournament.
  • The winner of the last (the m-th) fight (the knight number xm) became the winner of the tournament.

You fished out all the information about the fights from your friends. Now for each knight you want to know the name of the knight he was conquered by. We think that the knight number b was conquered by the knight number a, if there was a fight with both of these knights present and the winner was the knight number a.

Write the code that calculates for each knight, the name of the knight that beat him.

Input

The first line contains two integers nm (2 ≤ n ≤ 3·105; 1 ≤ m ≤ 3·105) — the number of knights and the number of fights. Each of the following m lines contains three integers li, ri, xi (1 ≤ li < ri ≤ nli ≤ xi ≤ ri) — the description of the i-th fight.

It is guaranteed that the input is correct and matches the problem statement. It is guaranteed that at least two knights took part in each battle.

Output

Print n integers. If the i-th knight lost, then the i-th number should equal the number of the knight that beat the knight number i. If the i-th knight is the winner, then the i-th number must equal 0.

Example
Input
4 3
1 2 1
1 3 3
1 4 4
Output
3 1 4 0 
Input
8 4
3 5 4
3 7 6
2 8 8
1 8 1
Output
0 8 4 6 4 8 6 1 
Note

Consider the first test case. Knights 1 and 2 fought the first fight and knight 1 won. Knights 1 and 3 fought the second fight and knight 3 won. The last fight was between knights 3 and 4, knight 4 won.



题解:

题意:

有n个骑士,m场战斗,每场战斗有l,r,x,表示下标为l到r的骑士中胜者为x,最后让你输出每个人被谁打败了,如果是人生大赢家就输出0

思路:

比赛一开始以为是一个不路径压缩的并查集,后来一想这么大的区间更新肯定要超时就想着线段树了,把要更新的区间倒过来进行区间覆盖就好了(注意去掉x)

代码:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<deque>
#define M (t[k].l+t[k].r)/2
#define lson k*2
#define rson k*2+1
#define ll long long
#define INF 100861111;
using namespace std;
struct node
{
    int l,r;
    int tag;//lazy tag
    int v;//值
}t[300005*4];
void Build(int l,int r,int k)
{
    t[k].l=l;
    t[k].r=r;
    t[k].tag=0;
    t[k].v=0;
    if(l==r)
        return;
    int mid=M;
    Build(l,mid,lson);
    Build(mid+1,r,rson);
}
void pushdown(int k)//更新子区间,下推标记
{
    if(t[k].tag)
    {
        t[lson].tag=t[rson].tag=t[k].tag;
        t[lson].v=t[rson].v=t[k].tag;
        t[k].tag=0;
    }
}
void update(int l,int r,int k,int v)//区间更新
{
    if(t[k].l==l&&t[k].r==r)
    {
        t[k].v=v;
        t[k].tag=v;
        return;
    }
    pushdown(k);
    int mid=M;
    if(r<=mid)
    {
        update(l,r,lson,v);
    }
    else if(l>mid)
        update(l,r,rson,v);
    else
    {
        update(l,mid,lson,v);
        update(mid+1,r,rson,v);
    }
}
int query(int x,int k)//单点询问
{
    if(t[k].l==t[k].r)
    {
        return t[k].v;
    }
    pushdown(k);
    int mid=M;
    if(x<=mid)
        return query(x,lson);
    else
        return query(x,rson);
}
struct edge
{
    int l,r;
    int x;
}q[300005];//存更新区间和信息
int main()
{
    int i,j,k,n,m;
    scanf("%d%d",&n,&m);
    for(i=0;i<m;i++)
    {
        scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].x);
        if(q[i].l>q[i].r)//可能有坑
            swap(q[i].l,q[i].r);
    }
    Build(1,n,1);
    for(i=m-1;i>=0;i--)//反过来处理更新
    {
        if(q[i].l!=q[i].x)//如果不是胜者在最右边的情况
        update(q[i].l,q[i].x-1,1,q[i].x);
        if(q[i].r!=q[i].x)//如果不是胜者在最左边的情况
        update(q[i].x+1,q[i].r,1,q[i].x);
    }
    int temp=query(1,1);//特殊处理第一个人
    if(temp==1)
        printf("0");
    else
        printf("%d",temp);
    for(i=2;i<=n;i++)//处理后面的人
    {
        int temp=query(i,1);
        if(temp==i)
            printf(" 0");
        else
            printf(" %d",temp);
    }
    printf("\n");
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值