Buses and People CodeForces 160E 三维偏序+线段树
Buses and People CodeForces 160E 三维偏序+线段树
题意
给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a<a', b'<b, c'<c 的最小 c 对应的元组编号。
解题思路
三维偏序问题,是我第一次做,查的题解。
一位大佬是这么说的,原博客首先,离线处理所有询问,将这 N+M 个元组按照 a 从小到达进行排序,若有相同的 a,则给定元组应该排在询问元组之前。排序后即可保证对于任意一个询问元组,答案一定出现在该元组以前的给定元组中。因为要求的是最小的满足条件的 C,应该在 C 上建立线段树。对 C 进行离散化操作,在 (b,c) 上建立线段树,以 C 为下标,B 为权值。线段树中维护 B 的最大值,询问时在线段树上二分即可。
代码实现(看代码很容易)
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=2e5+7;
struct node{
int st, ed, t, id;
bool friend operator < (node a, node b)
{
return a.st==b.st ? a.id < b.id : a.st < b.st;
}
}a[maxn];
vector<int> v;
int tot;
int maxx[maxn<<2], id[maxn<<2];
int ans[maxn>>1];
int n, m;
void update(int rt, int l, int r, int pos, int val, int ID)
{
if(l==r)
{
//if(maxx[rt] < val)
{
maxx[rt]=val;
id[rt]=ID;
}
return ;
}
int mid=(l+r)>>1;
if(pos<=mid)
update(rt<<1, l, mid, pos, val, ID);
else
update(rt<<1|1, mid+1, r, pos, val, ID);
maxx[rt]=max(maxx[rt<<1], maxx[rt<<1|1]);
}
int query(int rt, int l, int r, int x, int y, int val)
{
if(l==r) return id[rt];
int mid=(l+r)>>1;
int ret=-1;
if(y<=mid)
{
if(maxx[rt<<1] >= val) ret=query(rt<<1, l, mid, x, y, val);
}
else if(x > mid)
{
if(maxx[rt<<1|1] >= val) ret=query(rt<<1|1, mid+1, r, x, y, val);
}
else
{
if( maxx[rt<<1] >= val ) ret=query(rt<<1, l, mid, x, mid, val);
if(ret==-1 && maxx[rt<<1|1] >= val) ret=query(rt<<1|1, mid+1, r, mid+1, y, val);
}
return ret;
}
int main()
{
scanf("%d%d", &n, &m);
for(int i=1; i<=n+m; i++)
{
scanf("%d%d%d", &a[i].st, &a[i].ed, &a[i].t);
a[i].id=i;
v.push_back(a[i].t);
}
sort(v.begin() , v.end());
v.erase( unique(v.begin() , v.end() ), v.end() );
tot=v.size() ;
sort(a+1, a+n+m+1);
for(int i=1; i<=n+m; i++)
{
a[i].t = lower_bound(v.begin() , v.end() , a[i].t) - v.begin() +1;
}
for(int i=1; i<=n+m; i++)
{
if(a[i].id<=n)
update(1, 1, tot, a[i].t, a[i].ed, a[i].id);
else
ans[a[i].id - n]=query(1, 1, tot, a[i].t, tot, a[i].ed);
}
for(int i=1; i<=m; i++)
{
printf("%d ", ans[i]);
}
return 0;
}
Buses and People CodeForces 160E 三维偏序+线段树的更多相关文章
- 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 ...
随机推荐
- font-awesome样式只显示方框
这是一个踩过的坑:使用font-awesome中的css样式库时,比如fa-user-circle-o,显示的不是一个用户图标,而是一个方框. 怎么回事呢? 进入css文件,发现: 咦,这些文件呢?我 ...
- .NET界面控件DevExpress v19.1.3重磅来袭
DevExpress Universal Subscription(又名DevExpress宇宙版或DXperience Universal Suite)是全球使用广泛的.NET用户界面控件套包,De ...
- 用selenium启动chrome浏览器
python 3.7 pycharm 1.安装selenium pip3 install selenium 2.下载与chrome匹配的chromdriver.exe,放到项目的解释器路径下,跟pyt ...
- java8 时间日期操作包总结
- java IO操作分类
- 使用idea对XML的增删改查
XML:是一种可扩展标记性的语言,与java语言无关,它可以自定义标签. 1.首先需要到导入Dom4j架包,与自己所时候的ide关联 2.编写自己的xml文件,入上图所示(里面的所有元素及元素中的属性 ...
- Altera培训SignalTap II的使用--笔记
培训的内容有点多(啰嗦)(笔记为截图) 听课笔记:Altera培训SignalTap II的使用--笔记
- onmouseover和onmouseout鼠标移入移出切换图片的几种实现方法
鼠标移入移出图片切换很常见的,那我们就来说说他的写法 第一种方法,也是最简单的一种,在html:里就可实现 <img class="img" src="img/do ...
- [CSP-S模拟测试]:简单的操作(二分图+图的直径)
题目描述 从前有个包含$n$个点,$m$条边,无自环和重边的无向图. 对于两个没有直接连边的点$u,v$,你可以将它们合并.具体来说,你可以删除$u,v$及所有以它们作为端点的边,然后加入一个新点$x ...
- 按ECS退出全屏模式
<!DOCTYPE html><html><meta http-equiv="Content-Type" content="text/htm ...