CodeForces 356A_(set应用,线段树)
3 seconds
256 megabytes
standard input
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.
The first line contains two integers n, m (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 ≤ n; li ≤ 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.
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.
4 3
1 2 1
1 3 3
1 4 4
3 1 4 0
8 4
3 5 4
3 7 6
2 8 8
1 8 1
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应用,线段树)的更多相关文章
- Buses and People CodeForces 160E 三维偏序+线段树
Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)
[Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...
- [Codeforces 1199D]Welfare State(线段树)
[Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...
- [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)
[Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- Codeforces 482B Interesting Array(线段树)
题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...
- codeforces 383C Propagating tree 线段树
http://codeforces.com/problemset/problem/383/C 题目就是说, 给一棵树,将一个节点的值+val, 那么它的子节点都会-val, 子节点的子节点+val. ...
- CodeForces 228D. Zigzag(线段树暴力)
D. Zigzag time limit per test 3 seconds memory limit per test 256 megabytes input standard input out ...
- Codeforces 626G Raffles(贪心+线段树)
G. Raffles time limit per test:5 seconds memory limit per test:256 megabytes input:standard input ou ...
随机推荐
- Mysql中文乱码以及导出为sql语句和Excel问题解决
Mysql中文乱码以及导出为sql语句和Excel问题解决 这几天基于Heritrix写了一个爬虫,用到mysql,在导入导出数据时,遇到一些乱码问题,好不容易解决了,记录一下,以备查看.一.导出数据 ...
- 洛谷 P1469 找筷子
题目描述 经过一段时间的紧张筹备,电脑小组的“RP餐厅”终于开业了,这天,经理LXC接到了一个定餐大单,可把大家乐坏了!员工们齐心协力按要求准备好了套餐正准备派送时,突然碰到一个棘手的问题,筷子!CX ...
- 动物统计加强版 Trie 树
动物统计加强版 时间限制:3000 ms | 内存限制:150000 KB 难度:4 描述 在美丽大兴安岭原始森林中存在数量繁多的物种,在勘察员带来的各种动物资料中有未统计数量的原始动物的名单 ...
- 一次MySQL(INNODB存储引擎) 死锁捉虫记
前言 任何系统不管在什么阶段都需要关注生产环境错误日志,最近几个月内,发现偶尔会出现数据库死锁情况.以前碰到的数据库类错误大部分是SQL语法造成的错误,来到新东家之后才第一次碰到死锁情况,以前是搞游戏 ...
- gap lock/next-key lock浅析Basic-Paxos协议日志同步应用
http://www.cnblogs.com/renolei/p/4673842.html 当InnoDB在判断行锁是否冲突的时候, 除了最基本的IS/IX/S/X锁的冲突判断意外, InnoDB还将 ...
- [转]数据库查询 sysobjects
sysobjects sysobjects是系统自建的表,里面存储了在数据库内创建的每个对象(约束.默认值.日志.规则.存储过程等),各在表中占一行.只有在 tempdb 内,每个临时对象才在该表中占 ...
- web项目log日志查看分析->流程理解
1.DEBUG [2017-07-10 11:38:41,705][] org.springframework.web.servlet.DispatcherServlet:865 - Dispatch ...
- HDU 5467
第一次写LCT,各种模板加入...以后都只遇到有新意的题目再更新了 这道题就是LCT,但是,难在一个回退的操作.这时,可以通过改变执行顺序,先把要回退后再做的操作先执行了,再回退到之前的执行.这时,建 ...
- poj 1664 放苹果 (划分数)
题意:中文题目,不解释... 题解: 第一种方法是暴力深搜:枚举盘子1~n放苹果数量的所有情况,不需要剪枝:将每次枚举的情况,即每个盘的苹果数量,以字典序排序,然后存进set里 以此去重像" ...
- NS3网络仿真(2):first.py
1 安装基本模块 11 安装Python 12 安装PTVS 13 加入对python-279的支持 2 在vs2013下编译NS3 3 编译NetAnim 4 在vs2 ...