题目传送门

题解:

可以将所有线段按照左端点优先小,其次右端点优先大进行排序。

然后对于第 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的更多相关文章

  1. Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化

    D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...

  2. 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 ...

  3. ural1987 Nested Segments

    Nested Segments Time limit: 1.0 secondMemory limit: 64 MB You are given n segments on a straight lin ...

  4. Code Forces 652D Nested Segments(离散化+树状数组)

     Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  5. codeforces 652D D. Nested Segments(离散化+sort+树状数组)

    题目链接: D. Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  6. Educational Codeforces Round 10 D. Nested Segments

    D. Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  7. D - Nested Segments CodeForces - 652D (离散化+树桩数组)

    D - Nested Segments CodeForces - 652D You are given n segments on a line. There are no ends of some ...

  8. [离散化+树状数组]CodeForces - 652D Nested Segments

    Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  9. Codeforces 976C Nested Segments

    题面: 传送门 C. Nested Segments Input file: standard input Output file: standard output Time limit: 2 secon ...

随机推荐

  1. 【iOS】NSLog 打印 BOOL 类型值

    这个问题以前没在意,刚偶然打印,发现有些问题,上网查了下,发现是这么搞的: NSLog(@"%@", isEqual?@"YES":@"NO" ...

  2. Java 之MVC动态分页完美实现

    一个分页小技术有时也是让人挠头,在这里完全前端实现方式与Java的实现方式,我们提供给你完全的编码参考,希望能够帮到你哦(:) 内容导读 1.程序结构 2.JSP页面设计 3.分页主要编码 4.运行效 ...

  3. Core CLR Host 源码简单分析

    在定制 CLR Host的时候,可以通过调用如下代码,来获取当前需要被宿主的程序调用入口: hr = Host->CreateDelegate( domainId, L"Main,Ve ...

  4. UE4 本地化不起作用 SetCurrentCulture

    UE4 本地化 FInternationalization::Get ().SetCurrentCulture ( TEXT ( "en" ) ) FInternationaliz ...

  5. HashMap、Hash Table、ConcurrentHashMap

    这个这个...本王最近由于开始找实习工作了,所以就在牛客网上刷一些公司的面试题,大多都是一些java,前端HTML,js,jquery,以及一些好久没有碰的算法题,说实话,有点难受,其实在我不知道的很 ...

  6. MySQL-5.7.21非图形化下载、安装、连接问题记录

    1.安装包下载链接:https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-winx64.zip 官网:https://www.mysql.co ...

  7. 第四次作业;创建raid5,源码编译安装;磁盘配额

    创建raid5 格式化 ext4 创建物理卷: 创建卷组: 创建逻辑卷: 格式化  ext4 挂载 开机自启动 创建raid配置文件 源码编译安装: 创建本地yum仓库 umount /dev/sr0 ...

  8. 使用mybatis实现分页查询示例代码分析

    *******************************************分页查询开始*************************************************** ...

  9. Linux--shell重定向与文件处理命令--02

    一.IO重定向 1.数据输入:键盘---标准输入,但并不是唯一输入方式 ” | passwd –stdin username #同时添加用户和密码 while line;do 循环体...$line ...

  10. Java网络编程 -- 网络协议

    OSI网络七层协议 为使不同计算机厂家的计算机能够互相通信,以便在更大的范围内建立计算机网络,有必要建立一个国际范围的网络体系结构标准.OSI网络七层协议就是在这个基础上制定出来的,其从最底层开始依次 ...