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 ...
随机推荐
- [CF852D] Exploration plan
问题描述 The competitors of Bubble Cup X gathered after the competition and discussed what is the best w ...
- 表格 td 设置宽度无效问题
现在有个需求,就是表格的列不固定,都是动态加载的,想给每一列设置宽度,但是设置 width:100xp,没有效果,不过设置min-width:100px 就有效果了,table的宽度为 td的宽度和 ...
- 微信小程序细节部分
微信小程序和HTML区别: 1.开发工具不同,H5的开发工具+浏览器Device Mode预览,小程序的开发基于自己的开发者工具 2.开发语言不同,小程序自己开发了一套WXML标签语言和WXSS样式语 ...
- Vuex-全局状态管理【简单小案例】
前言: Vuex个人见解: 1.state :所有组件共享.共用的数据.理解为不是一个全局变量,不能直接访问以及操作它.2.mutations : 如何操作 state 呢?需要有一个能操作state ...
- handy源码阅读(六):tcp类
首先是tcpconn和tcpserver类: struct TcpConn : public std::enable_shared_from_this<TcpConn>, private ...
- break、continue、return的使用
跳转控制语句: java中的goto是保留字,目前不能使用,虽然没有了goto语句可以增强程序的安全性,但是也带来很多不便. 比如说:我们想让某个循环到某一步的时候就结束,现在就做不了这个事情了.为了 ...
- 【bzoj3343】教主的魔法
*题目描述: 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.…….N. 每个人的身高一开始 ...
- Python_015(面向对象(接口类,抽象类,多态,封装)
一.抽象类与接口类 1.抽象类:抽象即类似或者说比较像的部分,继承描述的是父类与子类的一种关系,要找出这种关系,必须先抽象再继承; a:抽象分成两个层次: 1)由对象->类:将两个有相似地方的对 ...
- Linux内核源码分析
Linux源码下载: https://www.kernel.org/ https://git.kernel.org/ Linux内核源码阅读以及工具(转): https://blog.csdn.net ...
- es之IK分词器
1:默认的分析器-- standard 使用默认的分词器 curl -XGET 'http://hadoop01:9200/_analyze?pretty&analyzer=standard' ...