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. GeoTrust 企业(OV)型 增强版(EV) 多域名(SAN/UC) SSL证书

    GeoTrust 企业(OV)型 增强版(EV) 多域名(SAN/UC)SSL证书(GeoTrust True BusinessID With EV Multi-Domain(SAN/UC) SSL ...

  2. kendo Grid json解析的问题

    新建立了一个 页面,在grid中使用了dropdownlist,总是显示companyId is not defined ,以前这个问题解决过了,忘记记录了额,现在不知道怎么办了 下面的这个解决方法是 ...

  3. [bzoj1855][Scoi2010]股票交易_动态规划_单调队列

    股票交易 bzoj-1855 Scoi-2010 题目大意:说不明白题意系列++...题目链接 注释:略. 想法:这个题还是挺难的. 动态规划没跑了 状态:dp[i][j]表示第i天手里有j个股票的最 ...

  4. ASP.NET MVC 源码分析(一)

    ASP.NET MVC 源码分析(一) 直接上图: 我们先来看Core的设计: 从项目结构来看,asp.net.mvc.core有以下目录: ActionConstraints:action限制相关 ...

  5. 洛谷——P2639 [USACO09OCT]Bessie的体重问题Bessie's We…

    https://www.luogu.org/problem/show?pid=2639 题目描述 Bessie像她的诸多姊妹一样,因为从Farmer John的草地吃了太多美味的草而长出了太多的赘肉. ...

  6. Python3-paramiko-SFTP上传文件夹到多台远程服务器

    # coding: utf-8 import paramiko import re import os from time import sleep # 定义一个类,表示一台远端linux主机 # 参 ...

  7. Mac下使用OpenMP

    Mac下使用OpenMP,修改Build Options 下面的compiler for c/c++/objective-C 为 LLVM GCC 4.2 - Language 则可以找到Enable ...

  8. 977. Squares of a Sorted Array

    题目描述: Given an array of integers A sorted in non-decreasing order, return an array of the squares of ...

  9. [Angular] Using ngTemplateOutlet to create dynamic template

    I can use <tamplete> syntax and a entry component as a container to create a dynamic component ...

  10. 设置用root用户telnet到linux系统

    默认情况下,ROOT用户不能以telnet方式连接Linux操作系统,而且也是不安全的.但从技术上来讲,是可以实现的. #mv /etc/securetty /etc/securetty.bak 保存 ...