A. Knight Tournament
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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 rihave 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 

n个骑士,m场战斗,知道m场战斗的(l,r,x)(从l到r中未出局的骑士中x胜出),求每个骑士被谁战胜。

1).STL_SET 的应用
#include <stdio.h>
#include <string.h>
#include <math.h>
#include<iostream>
#include <algorithm>
#include<set>
using namespace std;
#define MAXN 300005
set<int>s;
set<int>::iterator it_l,it,tmp[MAXN]; int ans[MAXN];
int main()
{
int n,m,l,r,x,num;
while(cin>>n>>m)
{
s.clear();
memset(ans,,sizeof(ans));
for(int i=;i<=n;i++)
s.insert(i);
for(int i=;i<m;i++)
{
cin>>l>>r>>x;
it_l=s.lower_bound(l);
num=;
for(it=it_l;*it<=r&&it!=s.end();it++)
{
if(*it!=x)
{
ans[*it]=x;
tmp[num++]=it;
}
}
for(int j=;j<num;j++)
s.erase(tmp[j]);
}
for(int i=;i<n;i++)
cout<<ans[i]<<" ";
cout<<ans[n]<<endl;
}
return ;
}
2).线段树。最开始用单点更新超时,看题解说是倒着区间更新。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include<iostream>
#include <algorithm>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1 int n,m;
struct Node
{
int lazy;
int l,r;
int con;
} tree[<<]; void pushdown(int rt)
{
if(tree[rt].lazy>)
{
tree[rt<<].lazy=tree[rt<<|].lazy=tree[rt].lazy;
tree[rt<<].con=tree[rt<<|].con=tree[rt].lazy;
tree[rt].lazy=;
}
} void build(int l,int r,int rt)
{
tree[rt].l=l;
tree[rt].r=r;
tree[rt].lazy=;
tree[rt].con=;
if(l==r)
return;
int mid=(l+r)/;
build(lson);
build(rson);
} int query(int x,int l,int r,int rt)
{
if(l==r)
{
return tree[rt].lazy;
}
pushdown(rt);
int mid=(r+l)>>;
if(x>mid)
query(x,rson);
else
query(x,lson);
} void update(int L,int R,int l,int r,int rt,int con)
{
if(L==l&&R==r)
{
tree[rt].lazy=con;
return;
}
pushdown(rt);
int mid=(l+r)/;
if(L>mid)
update(L,R,rson,con);
else if(R<=mid)
update(L,R,lson,con);
else
{
update(L,mid,lson,con);
update(mid+,R,rson,con);
}
} int l[],r[],con[]; int main()
{ scanf("%d%d",&n,&m);
build(,n,);
for(int i=; i<m; i++)
scanf("%d%d%d",&l[i],&r[i],&con[i]);
for(int i=m-;i>=;i--)
{
if(con[i]>l[i])
update(l[i],con[i]-,,n,,con[i]);
if(con[i]<r[i])
update(con[i]+,r[i],,n,,con[i]);
}
for(int i=; i<=n; i++)
{
printf("%d",query(i,,n,));
if(i==n)
printf("\n");
else
printf(" ");
}
return ;
}

 

CodeForces 356A_(set应用,线段树)的更多相关文章

  1. Buses and People CodeForces 160E 三维偏序+线段树

    Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...

  2. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  3. [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)

    [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...

  4. [Codeforces 1199D]Welfare State(线段树)

    [Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...

  5. [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)

    [Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...

  6. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  7. Codeforces 482B Interesting Array(线段树)

    题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...

  8. codeforces 383C Propagating tree 线段树

    http://codeforces.com/problemset/problem/383/C 题目就是说,  给一棵树,将一个节点的值+val, 那么它的子节点都会-val, 子节点的子节点+val. ...

  9. CodeForces 228D. Zigzag(线段树暴力)

    D. Zigzag time limit per test 3 seconds memory limit per test 256 megabytes input standard input out ...

  10. Codeforces 626G Raffles(贪心+线段树)

    G. Raffles time limit per test:5 seconds memory limit per test:256 megabytes input:standard input ou ...

随机推荐

  1. [tsA1491][2013中国国家集训队第二次作业]家族[并查集]

    m方枚举,并查集O(1)维护,傻逼题,,被自己吓死搞成神题了... #include <bits/stdc++.h> using namespace std; struct tri { i ...

  2. Java分代垃圾回收机制:年轻代/年老代/持久代(转)

    虚拟机中的共划分为三个代:年轻代(Young Generation).年老点(Old Generation)和持久代(Permanent Generation).其中持久代主要存放的是Java类的类信 ...

  3. N天学习一个linux命令之scp

    用途 通过ssh通道,不同主机之间复制文件 用法 scp [options] [user@host:]file1 [user2@host2:]file2 常用参数 -1使用 ssh 1协议 -2使用s ...

  4. Swift基本常识点

    import Foundation // 单行注释 // 多行注释(支持嵌套,OC是不支持的) // 常量let,初始化之后就不可改变. // 常量的具体类型可以自动识别,等号后面是什么类型,它就是什 ...

  5. mysql 日期计算,今天,明天,本周,下周,本月,下月

    --今天 DATE_FORMAT(BIRTH_DATE,'%Y-%m-%d') = CURDATE() --明天 DATE_FORMAT(BIRTH_DATE,'%Y-%m-%d') = TIMEST ...

  6. 学习笔记——DISTINCT

    DISTINCT印象中向来被人诟病,说它效率低下.但网上那些SQL 面试题答案,却时有用之.其中 COUNT(DISTINCT 句式,我以前很少用,这里做个笔记. 为管理岗位业务培训信息,建立3个表: ...

  7. android按钮被点击文字颜色变化效果

    有的时候做应用需要点击按钮时文字颜色也跟着变,松开后又还原,目前发现两种解决方案:第一用图片,如果出现的地方比较多,那么图片的量就相当可观:第二,也就是本文讲到的.废话少说,先贴图片,再上代码. 正常 ...

  8. bazel编译tensorflow 生成libtensorflow_inference.so 和 libandroid_tensorflow_inference_java.jar

    bazel build -c opt //tensorflow/contrib/android:libtensorflow_inference.so --crosstool_top=//externa ...

  9. Java-java-com-util-common-service:ServiceException.java

    ylbtech-Java-java-com-util-common-service:ServiceException.java 1.返回顶部 1. package com.shineyoo.manag ...

  10. Java项目打包发布

    Java项目打包发布 如果只想发布为一个可执行的jar包,使用eclipse的Export功能就可以了 使用eclipse的Export功能,将项目中的所有package打包为一个pet.jar文件, ...