CF - 652 D Nested Segments
题解:
可以将所有线段按照左端点优先小,其次右端点优先大进行排序。
然后对于第 i 条线段来说, 那么第 i+1 ---- n 的线段左端点都一定在第i条线段的右边, 接下来就需要知道 i+1 ---- n 中的这些线段有多少条的右端点是在第 i 条线段的右端点的左边。
可以通过一个树状数组来维护一个右端点的前缀合。
当我门处理第i条线段的时候, 先将第i条线段的右端点从树状数组里面删除,然后在询问在 r 之前有多少个右端点就是相应的答案了。
代码:
/*
code by: zstu wxk
time: 2019/02/23
*/
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 5e5 + ;
struct Node{
int l, r, id;
bool operator< (const Node & x) const{
if(l == x.l) return r > x.r;
return l < x.l;
}
}A[N];
int b[N];
int k = , n;
int F(int x){
return lower_bound(b+, b++k, x) - b;
}
int tree[N];
void Updata(int x, int c){
while(x <= k){
tree[x] += c;
x += x & (-x);
}
return ;
}
int Query(int x){
int ret = ;
while(x > ){
ret += tree[x];
x -= x & (-x);
}
return ret;
}
int ans[N];
void Ac(){
for(int i = ; i <= n; ++i){
scanf("%d%d", &A[i].l, &A[i].r);
A[i].id = i;
b[++k] = A[i].l;
b[++k] = A[i].r;
}
sort(A+, A++n);
sort(b+, b++k);
k = unique(b+, b++k) - (b+);
for(int i = ; i <= n; ++i)
Updata(F(A[i].r), );
for(int i = ; i <= n; ++i){
Updata(F(A[i].r), -);
ans[A[i].id] = Query(F(A[i].r));
}
for(int i = ; i <= n; ++i)
printf("%d\n", ans[i]);
}
int main(){
while(~scanf("%d", &n)){
Ac();
}
return ;
}
CF - 652 D Nested Segments的更多相关文章
- Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...
- Educational Codeforces Round 10 D. Nested Segments 【树状数组区间更新 + 离散化 + stl】
任意门:http://codeforces.com/contest/652/problem/D D. Nested Segments time limit per test 2 seconds mem ...
- ural1987 Nested Segments
Nested Segments Time limit: 1.0 secondMemory limit: 64 MB You are given n segments on a straight lin ...
- Code Forces 652D Nested Segments(离散化+树状数组)
Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- codeforces 652D D. Nested Segments(离散化+sort+树状数组)
题目链接: D. Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Educational Codeforces Round 10 D. Nested Segments
D. Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- D - Nested Segments CodeForces - 652D (离散化+树桩数组)
D - Nested Segments CodeForces - 652D You are given n segments on a line. There are no ends of some ...
- [离散化+树状数组]CodeForces - 652D Nested Segments
Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- Codeforces 976C Nested Segments
题面: 传送门 C. Nested Segments Input file: standard input Output file: standard output Time limit: 2 secon ...
随机推荐
- Bean Validation完结篇:你必须关注的边边角角(约束级联、自定义约束、自定义校验器、国际化失败消息...)
每篇一句 没有任何技术方案会是一种银弹,任何东西都是有利弊的 相关阅读 [小家Java]深入了解数据校验:Java Bean Validation 2.0(JSR303.JSR349.JSR380)H ...
- 【Python-Django】Jinja2模板引擎配置教程详解!!!!
Jinjia2的官方文档:http://jinja.pocoo.org/docs/2.10/ 1. 安装Jinja2扩展包 $ pip install Jinja2 2. 配置Jinja2模板引擎 T ...
- Linux 常用命令及使用方法
1. type :查询命令 是否属于shell解释器 2. help : 帮助命令3. man : 为所有用户提供在线帮助4. ls : 列表显示目录内的文件及目录 -l 以长格 ...
- plotly之set_credentials_file问题
相信了解可视化的同学们都听说过plotly,笔者也是第一次了解这个网站,然后兴冲冲地设置,但是没想到第一次进行在线账号初始化就出现了问题! python3报错为module 'plotly.tools ...
- Selenium+java - 借助autolt完成上传文件操作
写在前面: 上传文件是每个自动化测试同学会遇到,而且可以说是面试必考的问题,标准控件我们一般用sendkeys()就能完成上传,但是我们的测试网站的上传控件一般为自己封装的,用传统的上传已经不好用了, ...
- Angualr6表单提交验证并跳转
在Angular6中,使用NG-ZRROR作为前端开发框架,在进行表单开发时遇到了一些问题,最后解决了,在此记录. 1.表单构造: 引入forms: import { FormGroup, FormB ...
- 【Java例题】5.3 线性表的使用
3.线性表的使用.使用ArrayList模拟一个一维整数数组.数据由Random类随机产生.进行对输入的一个整数进行顺序查找.并进行冒泡排序. package chapter6; import jav ...
- python3学习-lxml模块
在爬虫的学习中,我们爬取网页信息之后就是对信息项匹配,这个时候一般是使用正则.但是在使用中发现正则写的不好的时候不能精确匹配(这其实是自己的问题!)所以就找啊找.想到了可以通过标签来进行精确匹配岂不是 ...
- 关于 IntelliJ 的 IDEA PyCharm 等更新 2019.2 后中文乱码 的解决方案
关于IntelliJ 的2019.2 更新后的中文乱码解决方案 设置 备用字体 file -> Setting -> Editor ->Font 由于编程常用英文首选字体font默认 ...
- Security Guards (Gym - 101954B)( bfs + 打表 )
题意及思路 题目主要是讲先给出所有guard的位置,再给出所有incidents的位置,求出guard到达每个incident处最小的steps,其中guard每次可以向四周8个方向移动. 思路:对于 ...