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 ...
随机推荐
- 常用GDB命令行调试命令
po po是print-object的简写,可用来打印所有NSObject对象.使用举例如下: (gdb) po self <LauncherViewController: 0x552c570& ...
- 使用Kubeadm创建k8s集群之节点部署(三十一)
前言 本篇部署教程将讲述k8s集群的节点(master和工作节点)部署,请先按照上一篇教程完成节点的准备.本篇教程中的操作全部使用脚本完成,并且对于某些情况(比如镜像拉取问题)还提供了多种解决方案.不 ...
- OLE--SWT高级控件
OLE和ActiveX控件的支持 OLE(Object Link Embeded)是指在程序之间链接和嵌入对象数据.通过OLE技术可以在一个应用程序中执行其他的应用程序. 而ActiveX ...
- Android | Sqlite3
Android 数据库创建及使用: 创建: package he3.sd.dao; import android.content.Context; import android.database.sq ...
- Usaco Training [2.1] The Castle 搜索
传送门 题目的输出的4个信息 前两个很容易,dfs,bfs都可以,图怎么建都可以 后两个在搜索的时候记录belong[i][j]和已有的size即可 代码应该比不少题解清晰吧 #include < ...
- java订单生成工具类
欢迎来到付宗乐个人博客网站.本个人博客网站提供最新的站长新闻,各种互联网资讯. 还提供个人博客模板,最新最全的java教程,java面试题.在此我将尽我最大所能将此个人博客网站做的最好! 谢谢大家,愿 ...
- Tomcat源码分析 (五)----- Tomcat 类加载器
在研究tomcat 类加载之前,我们复习一下或者说巩固一下java 默认的类加载器.楼主以前对类加载也是懵懵懂懂,借此机会,也好好复习一下. 楼主翻开了神书<深入理解Java虚拟机>第二版 ...
- 访问CGI程序时不添加 /cgi-bin/ 目录也可访问
配置如下 <VirtualHost *:80> DocumentRoot D:\web_root\test ServerName www.test.com <Directory /& ...
- Swift项目引入第三方库的方法
以下,将创建一个Swift项目,然后引入3个库: Snappy 简化autolayout代码的库 Alamofire HTTP网络库,AFNetworking作者写的 SDWebImage 图片加载, ...
- net core 3.0 之Grpc新特性小试牛刀
相信微服务大家伙都有听说和知道,好处弊端咱也不多说了,Grpc算是一个比较全面的微服务框架,也得到微软的支持 总结下来就是,跨平台,可靠,通信快,扩展性强,网络消耗小,模板多语言通用 光说好处,没 ...