【Codeforces 652 D Nested Segments】+ 树状数组 + 离散化
来源:互联网 发布:中控科门禁软件下载 编辑:程序博客网 时间:2024/05/18 20:07
D. Nested Segments
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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.
Output
Print n lines. The j-th of them should contain the only integer aj — the number of segments contained in the j-th segment.
Examples
Input
4
1 8
2 3
4 7
5 6
Output
3
0
1
0
Input
3
3 4
1 5
2 6
Output
0
1
1
D. Nested Segments
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
Input
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.
Output
Print n lines. The j-th of them should contain the only integer aj — the number of segments contained in the j-th segment.
Examples
Input
4
1 8
2 3
4 7
5 6
Output
3
0
1
0
Input
3
3 4
1 5
2 6
Output
0
1
1
树状数组 + 离散化 原来 + 起来这么好用~ amzaing~
AC代码:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int MAXN = 2e5 + 10;struct node{ int x,y,id;}st[MAXN];int sum[MAXN],N,ans[MAXN];bool cmp(node a,node b){return a.y < b.y;}bool cmp1(node a,node b){ if(a.x != b.x) return a.x > b.x; else return a.y < b.y;}void up(int a){ while(a <= N){ sum[a]++; a += a & (-a); }}int qu(int a){ int cut = 0; while(a > 0){ cut += sum[a]; a -= a & (-a); } return cut;}int main(){ while(scanf("%d",&N) != EOF){ memset(sum,0,sizeof(sum)); for(int i = 1 ; i <= N; i++){ scanf("%d %d",&st[i].x,&st[i].y); st[i].id = i; } sort(st + 1 ,st + 1 + N,cmp); for(int i = 1 ; i <= N; i++) st[i].y = i; // 离散右端点,便于的运用树状数组求解 sort(st + 1,st + 1 + N,cmp1); for(int i = 1 ; i <= N; i++){ ans[st[i].id] = qu(st[i].y); // 查询 up(st[i].y); // 求和 } for(int i = 1 ; i <= N; i++) printf("%d\n",ans[i]); } return 0;}
- Codeforces 652D Nested Segments 【树状数组 + 离散化】
- CodeForces 652D Nested Segments(树状数组+离散化)
- CodeForces 652D Nested Segments(离散化,树状数组)
- 【Codeforces 652 D Nested Segments】+ 树状数组 + 离散化
- Codeforces 652D Nested Segments【离散化+思维+树状数组】
- Nested Segments codeforces 652D 树状数组 +离散化
- CodeForces 652D Nested Segments(树状数组 离散化)
- Nested Segments CodeForces 652D 树状数组+离散化
- CodeForces 652D Nested Segments 树状数组
- Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
- CodeForces 652D Nested Segments (树状数组)
- Codeforces 652D Nested Segments 树状数组离线处理
- Code Forces 652D Nested Segments(离散化+树状数组)
- CodeForce 652D Nested Segments 树状数组
- 初识树状数组 Educational Codeforces Round 10 D - Nested Segments
- codeforces 652D Nested Segments
- cf#ECR10-D. Nested Segments-树状数组+二分
- 【Educational Codeforces Round 10D】【树状数组】Nested Segments 每条线段内部有多少条线段
- linux c读取tty和消息队列的使用demo
- 利用编辑距离计算文本相似性
- UC/OS II 学习笔记(一)
- python中的单链表操作
- 判断一个数是否为2的N次方问题
- 【Codeforces 652 D Nested Segments】+ 树状数组 + 离散化
- [工具代码]使JTextfield具有默认提示文字的功能
- 想看TCP/IP卷一
- Java实现多线程断点下载(下载过程中可以暂停)
- 浅淡安卓json的三种解析
- leetcode(410):Split Array Largest Sum
- C 预处理器
- WEBRTC视频网络传输到解码输出
- 卷积神经网路、Mxnet、Alxnet及Lenet