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 ...
随机推荐
- css的基本操作学习--css样式,选择器,hover,文字属性,文本属性,背景
什么是css? 通配符选择器 <head> /* *通配符选择器 匹配任何元素 */ *{ margin: 0; padding: 0; } </head> css样式有三种 ...
- web开发如何使用高德地图API(四)通过AMap.Marker自定义标点
说两句: 以下内容除了我自己写的部分,其他部分在高德开放平台都有(可点击外链访问). 我所整理的内容以实际项目为基础希望更有针对性的,更精简. 点击直奔主题. 准备工作: 首先,注册开发者账号,成为高 ...
- HDU 3401 Trade
Trade Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 3401 ...
- [cogs461] [网络流24题#10] 餐巾 [网络流,最小费用最大流]
建图:从源点向第一层连边,第一层表示当天用掉多少餐巾,第二层表示当天需要多少餐巾,所以注意购买餐巾的边容量为无穷大,要从源点开始连向第二层的点,每天可能有剩余,在第一层内表示为流入第二天的节点.具体见 ...
- nyoj_111_分数加减法_201311281341
分数加减法 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 编写一个C程序,实现两个分数的加减法 输入 输入包含多行数据 每行数据是一个字符串, ...
- 洛谷 P1378 油滴扩展
P1378 油滴扩展 题目描述 在一个长方形框子里,最多有N(0≤N≤6)个相异的点,在其中任何一个点上放一个很小的油滴,那么这个油滴会一直扩展,直到接触到其他油滴或者框子的边界.必须等一个油滴扩展完 ...
- http协议的再次理解
1.Tomcat是根据server.xml的配置启动的.根目录下conf/server.xml. 2.Tomcat是根据server.xml的配置启动的.根目录下conf/server.xml. 3. ...
- clear out all variables without closing terminal
clear out all variables without closing terminal https://unix.stackexchange.com/questions/172655/cle ...
- mvn 仓库
http://mvnrepository.com/artifact/org.postgresql/postgresql/9.3-1103-jdbc41 ------------------------ ...
- javascript打开本地应用
function openShell(){ if(window.ActiveXObject){ var cmd = new ActiveXObject('WScript.Shell') cmd.Run ...