Educational Codeforces Round 10 D. Nested Segments 【树状数组区间更新 + 离散化 + stl】
任意门:http://codeforces.com/contest/652/problem/D
D. Nested Segments
2 seconds
256 megabytes
standard input
standard output
You are given n segments on a line. There are no ends of some segments that coincide. For each segment find the number of segments it contains.
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of segments on a line.
Each of the next n lines contains two integers li and ri ( - 109 ≤ li < ri ≤ 109) — the coordinates of the left and the right ends of the i-th segment. It is guaranteed that there are no ends of some segments that coincide.
Print n lines. The j-th of them should contain the only integer aj — the number of segments contained in the j-th segment.
4
1 8
2 3
4 7
5 6
3
0
1
0
3
3 4
1 5
2 6
0
1
1
题意概括:
有 N 个区间,求每个区间有多少个存在的子区间。
例如第一个样例:
4
1 8
2 3
4 7
5 6 【1,8】有 3 个,他们发别是 【2,3】,【4,7】,【5,6】;
【2,3】没有;
【4,7】有 1 个,【5,6】;【5,6】没有;
注意:
一、只是有部分相交的区间不在考虑范围内,模拟一下样例二就明白了。
3
3 4
1 5
2 6
二、端点不重合,这个很重要!!!
解题思路:
由题意可知这是离线操作,涉及区间查询和修改,线段树或树状数组。
其次数据范围不小,要考虑离散化。
这道题如何离散化?
二维 pair 储存原数据 + vector 排序&&去重;
树状数组要维护一些什么呢?我们怎么记录区间内有几个符合条件的线段呢?
一开始自己模拟样例一后是想到标记左端点,然后求其区间和,刚好区间和 减去他自己就是答案,不够这种想法是很片面的。
因为有只有部分相交的情况:例如样例二

【1,5】= 1+1+1-1 = 2 答案错误(把部分相交的考虑进去了)
但这道题的解决方法就是标记一个端点,如果标记左端点则按右端点顺序遍历,如果标记右端点则按左端点顺序遍历。
固定其中一个端点后求区间和,并且在查询完毕之后要消除当前区间对后面区间的影响,因为我们按照其中一个端点的顺序遍历,如果不消除当前固定端点区间的影响,那么后面就会有部分相交了。
例如说我标记左端点,按照右端点的顺序遍历,如果我遍历右端点为 6 之后没有把【2, 6】的影响消除,那么当我遍历到右端点为 5 的时候就会把只有部分相交的区间也记录进去了。
解决了以上两个大问题,剩下的就是代码实现的事了。
AC code:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAXN = 4e5+; int t[MAXN]; //树状数组
pair< pair<int, int>, int> p[MAXN]; //记录区间左右端点和
vector<int> Q;
map<int, int> mmp;
int ans[MAXN];
int N; int lowbit(int x){return x&(-x);}
void Update(int x, int val)
{
for(int i = x; i < MAXN; i+=lowbit(i)){
t[i]+=val;
}
} int query(int x)
{
int res = ;
for(int i = x; i; i-=lowbit(i))
res+=t[i];
return res;
} int main()
{
scanf("%d", &N);
for(int i = ; i <= N; i++){
scanf("%d %d", &p[i].first.first, &p[i].first.second);
Q.push_back(p[i].first.first);
Q.push_back(p[i].first.second);
p[i].second = i;
}
sort(Q.begin(), Q.end()); //离散化
Q.erase(unique(Q.begin(),Q.end()), Q.end()); //去重
for(int i = ; i < Q.size(); i++){
mmp[Q[i]] = i+; //新旧端点的映射关系
}
for(int i = ; i <= N; i++){
p[i].first.first = mmp[p[i].first.first]; //更新区间的左右端点
p[i].first.second = mmp[p[i].first.second];
Update(p[i].first.second, ); //更新区间!!!精妙之处在于标记的该区间的其中一个端点!!!(题目条件端点不重合)
}
sort(p+, p++N); //排序等级:左端点 > 右端点 > 区间编号
for(int i = , j = ; i < MAXN; i++){ //遍历左端点,注意范围是 1~MAXN
while(j <= N && p[j].first.first == i){ //左端点相同的区间
ans[p[j].second] = query(p[j].first.second);
Update(p[j].first.second, -); //消除当前区间对后面区间的影响
j++;
}
if(j == N+) break; //遍历完成
}
for(int i = ; i <= N; i++)
printf("%d\n", ans[i]-);
return ;
}
Educational Codeforces Round 10 D. Nested Segments 【树状数组区间更新 + 离散化 + stl】的更多相关文章
- Educational Codeforces Round 10 D. Nested Segments (树状数组)
题目链接:http://codeforces.com/problemset/problem/652/D 给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间. 我是先把每个区间 ...
- Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...
- CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...
- Educational Codeforces Round 10 D. Nested Segments
D. Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 【poj2155】Matrix(二维树状数组区间更新+单点查询)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?
开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...
- Codeforces Round #365 (Div. 2) D 树状数组+离线处理
D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...
- Codeforces Round #261 (Div. 2) D 树状数组应用
看着题意:[1,i]中等于a[i]的个数要大于[,jn]中等于a[j]的个数 且i<j,求有多少对这种(i,j) ,i<j可是 i前面的合法个数 要大于j后面的 看起来非常像逆序数的样子 ...
- NBOJv2 1050 Just Go(线段树/树状数组区间更新单点查询)
Problem 1050: Just Go Time Limits: 3000 MS Memory Limits: 65536 KB 64-bit interger IO format: % ...
随机推荐
- CenctOS6 and CenctOS7 多种姿势解决忘记密码
-----linux---- 忘记密码啦!!! 忘记密码教程!!! 教你们忘记密码(我原来密码就是123456,忘记是不可能的!假装忘记的样子 0.0) 现在我们忘记密码了!对忘记密码了.我忘记密码了 ...
- 日志logback
http://tengj.top/2017/04/05/springboot7/ ------------------ logback使用指南. 公司配置 <?xml version=" ...
- Nginx设置日志分割方法
目标:nginx cronolog日志分割配置文档,每分钟分割一次NGINX访问日志. 大体步骤如下: 1.nginx日志配置 access_log /var/log/nginx/access.log ...
- innosetup区分正常状态和静默安装状态(通过传递的参数)
命令行运行程序,如: myprogram.exe /abc /bcd 如果我们想获取其中的参数,“/abc”.“/bcd” 1. 直接使用innosetup自带的方法, GetCmdTail() ...
- mongodb insert()、save()的区别
mongodb 的 insert().save() ,区别主要是:若存在主键,insert() 不做操作,而save() 则更改原来的内容为新内容. 存在数据: { _id : 1, " ...
- vsftpd配置文件解析
对vsftpd配置文件详细解答. 1.默认配置: 1>允许匿名用户和本地用户登陆. anonymous_enable=YES local_enable=YES 2>匿名用户使用的登陆名为f ...
- JS代码格式化排版工具,web文本编辑器
js格式化代码工具:http://www.cnblogs.com/blodfox777/archive/2008/10/09/1307462.html web文本编辑器 :http://www.div ...
- 如何看linux是32位还是64位--转
地址:http://hi.baidu.com/hehongrong/item/20c296bcf8d834432aebe3b2 如何看linux是32位还是64位 如何看linux是32位还是64位 ...
- Win2D 官方文章系列翻译 - 预乘 Alpha
本文为个人博客备份文章,原文地址: http://validvoid.net/win2d-premultiplied-alpha/ 在计算机绘图中有两种表示颜色值不透明度的方法.Win2D 中两种方法 ...
- Java网络编程二
向web站点发送get post请求,并从web站点取得响应 通相应的set方法,在调用connect()方法前,可以设置请求的各种参数,实现同浏览器类似的访问,调用connect()后可以调用相应的 ...