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 三维偏序+线段树的更多相关文章

  1. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  2. [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)

    [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...

  3. [Codeforces 1199D]Welfare State(线段树)

    [Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...

  4. [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)

    [Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...

  5. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  6. Codeforces 482B Interesting Array(线段树)

    题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...

  7. codeforces 383C Propagating tree 线段树

    http://codeforces.com/problemset/problem/383/C 题目就是说,  给一棵树,将一个节点的值+val, 那么它的子节点都会-val, 子节点的子节点+val. ...

  8. CodeForces 228D. Zigzag(线段树暴力)

    D. Zigzag time limit per test 3 seconds memory limit per test 256 megabytes input standard input out ...

  9. Codeforces 626G Raffles(贪心+线段树)

    G. Raffles time limit per test:5 seconds memory limit per test:256 megabytes input:standard input ou ...

随机推荐

  1. vue中的v-model原理,与组件自定义v-model

    VUE中的v-model可以实现双向绑定,但是原理是什么呢?往下看看吧 根据官方文档的解释,v-model其实是一个语法糖,它会自动的在元素或者组件上面解析为 :value="" ...

  2. C#(.net)实现用apache activemq传递SQLite的数据

    版权声明:本文为搜集借鉴各类文章的原创文章,转载请注明出处:http://www.cnblogs.com/2186009311CFF/p/6382623.html. C#(.net)实现用apache ...

  3. centos搭建lamp环境参考(根据腾讯云实验室)

    1.安装MYSQL 使用 yum 安装 MySQL: yum install mysql-server -y 安装完成后,启动 MySQL 服务: service mysqld restart 设置 ...

  4. [CF938E]Max History题解

    题面 >CF传送门< >洛谷传送门< 解法 显而易见,对于一个数\(a_i\),若果它出现在\(f\)序列中,必定\(a_i\)之前的元素要小于\(a_i\),我们设\(cnt ...

  5. RAM 大全-DRAM, SRAM, SDRAM的关系与区别

    ROM和RAM指的都是半导体存储器,ROM是Read Only Memory的缩写,RAM是Random Access Memory的缩写.ROM在系统停止供电的时候仍然可以保持数据,而RAM通常都是 ...

  6. UOJ418. 【集训队作业2018】三角形

    http://uoj.ac/problem/418 题解 考虑激活每个节点时,它的每个儿子都是放满的. 那每一次的操作我们可以用一个二元组来表示\((w_i-\sum w_{son},\sum w_{ ...

  7. Coffee Chicken

    Coffee Chicken 字符串斐波那契 输出第s[n]个字符串的第k位及后十位 暴力算出前20项,超过20,跑dfs #include<bits/stdc++.h> using na ...

  8. Oracle---智斗ORA01427

    下面是我在做更新的时候遇到报ORA-01427,单行子查询返回多行值,原因是红色部分返回了多行值 UPDATE IN_MO IM      SET IM.BOM_ID =             (S ...

  9. hibernate class cast exception from object to ...

    http://stackoverflow.com/questions/22548325/java-lang-classcastexception-cannot-be-cast-to-java-lang ...

  10. HDU6599 (字符串哈希+回文自动机)

    题意: 求有多少个回文串的前⌈len/2⌉个字符也是回文串.(两组解可重复)将这些回文串按长度分类,分别输出长度为1,2,...,n的合法串的数量. 题解:https://www.cnblogs.co ...