zoukankan      html  css  js  c++  java
  • D

    Problem description

    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.

    Examples

    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(表示n个骑士,编号为1~n)和m(m行比赛数据),其中l,r,x表示[l,r]中除x编号外都被x打败,最后第m行数据的x是最终的胜利者,其"被打败的编号"为0,要求输出每个骑士(1~n)被打败的骑士编号。注:输入数据已经保证每个骑士都参加战斗。显然题目给的数据范围很大,两重循环就TLE。怎么优化呢?做法:将编号1~n仍在set容器中,采用lower_bound()找到容器中l元素(迭代器)的位置,然后在区间[l,r]中除x外将被打败的骑士编号标记起来(即s[i]=x,s数组中元素初始值要全部清0),再删除容器中已被打败的骑士编号,最后输出每个骑士被打败的骑士编号即可。

    AC代码(1871ms):

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 3e5+5;
     4 int n,m,l,r,x,cnt,s[maxn];
     5 set<int> st;
     6 set<int>::iterator it,its,tmp[maxn];
     7 int main(){
     8     cin>>n>>m;
     9     for(int i=1;i<=n;++i)st.insert(i);//将编号1~n全都扔到set容器中
    10     memset(s,0,sizeof(s));//s数组存放下标i被编号x打败,初始值注意清0
    11     while(m--){
    12         cin>>l>>r>>x;cnt=0;
    13         its=st.lower_bound(l);//找到不小于l的迭代器的位置
    14         for(it=its;it!=st.end()&&(*it<=r);++it)//[l,r]区间中除x外都已被x打败
    15             if(*it!=x){s[*it]=x;tmp[cnt++]=it;}
    16         for(int i=0;i<cnt;++i)//删除已被打败的编号
    17             st.erase(tmp[i]);
    18     }
    19     for(int i=1;i<=n;++i)
    20         cout<<s[i]<<(i==n?"
    ":" ");
    21     return 0;
    22 }
  • 相关阅读:
    简介.Net对象序列化.txt
    如何在Web页面退出前提示用户保存数据?
    如何将图片存储到数据库中
    页面回车键响应,onkeydown事件
    用C#创建Windows服务(Windows Services)
    解决“Visual Studio 要求设计器使用文件中的第一个类。移动类代码使之成为文件中的第一个类,然后尝试重新加载设计器。”方法
    动态创建htm元素并添加到document中
    如何在Asp.net的Header中添加/title/Meta tages/CSS
    无法打开项目文件,“d:\web\webapp.csproj”,此安装不支持该项目类型
    用Intelligencia.UrlRewriter实现URL重写
  • 原文地址:https://www.cnblogs.com/acgoto/p/9157528.html
Copyright © 2011-2022 走看看