Educational Codeforces Round 10-D. Nested Segments
来源:互联网 发布:免费听音乐的软件 编辑:程序博客网 时间:2024/05/15 03:56
原题链接
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.
41 82 34 75 6
3010
33 41 52 6
011
把线段按照左端点从大到小排序,若左端点相同则按右端点从小到大排序,那么node[i]线段一定不可能包含node[i+1]线段.离散化右端点.遍历排好序的所有线段i的右端点,找到1..i-1有多少线段的右端点小于等于node[i],则为node[i]包含的线段,可用树状数组来维护.
#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#include <vector>#include <map>#include <cmath>#include <queue>#include <ctime> #define maxn 1000005#define INF 1e18using namespace std;typedef long long ll;struct Node{Node(){}Node(int a, int b){l = a;r = b;} friend bool operator < (const Node &a, const Node&b){return a.l > b.l || (a.l == b.l && a.r < b.r);}int l, r, h, i;}node[200005];int num[200005], p[200005];void Update(int i, int d){while(i <= d){p[i]++;i += i & -i;}}int Query(int i){int ans = 0;while(i > 0){ans += p[i];i -= i & -i;}return ans;}bool temp(const Node&a, const Node&b){return a.i < b.i;}int main(){//freopen("in.txt", "r", stdin);int n;scanf("%d", &n);for(int i = 1; i <= n; i++){scanf("%d%d", &node[i].l, &node[i].r);num[i] = node[i].r;node[i].i = i;}sort(node+1, node+n+1);sort(num+1, num+1+n);int d = unique(num+1, num+1+n) - num;d--;for(int i = 1; i <= n; i++){int e = lower_bound(num+1, num+d+1, node[i].r) - num;node[i].h = Query(e);Update(e, d);}sort(node+1, node+n+1, temp);for(int i = 1; i <= n; i++) printf("%d\n", node[i].h); return 0; }
- Educational Codeforces Round 10 D. Nested Segments
- Educational Codeforces Round 10 D Nested Segments
- Educational Codeforces Round 10 D.Nested Segments
- Educational Codeforces Round 10-D. Nested Segments
- 初识树状数组 Educational Codeforces Round 10 D - Nested Segments
- Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
- 【Educational Codeforces Round 10D】【树状数组】Nested Segments 每条线段内部有多少条线段
- Educational Codeforces Round 4 D. The Union of k-Segments
- Educational Codeforces Round 4-D. The Union of k-Segments
- codeforces 652D Nested Segments
- Codeforces 612D Educational Codeforces Round 4 D. The Union of k-Segments 离散化+map
- CodeForces 652D Nested Segments 树状数组
- CodeForces D. Nested Segments【逆序对类型】
- Educational Codeforces Round 4 612D The Union of k-Segments(stl)
- Educational Codeforces Round 4 D. The Union of k-Segments(★)
- Educational Codeforces Round 4 D The Union of k-Segments (扫描线)
- Educational Codeforces Round 21-D
- Educational Codeforces Round 5 D
- Ajax工作原理
- java 几种加载驱动的方法
- 获取file域选择的图片显示在页面
- Springboot摘要
- [leetcode]257. Binary Tree Paths -- JavaScript 代码
- Educational Codeforces Round 10-D. Nested Segments
- 多线程环境下使用c++输出流和printf函数的区别
- 【JZOJ 3072】掷骰子
- php静态方法典型题分析
- 前端速学成财:第十一课-实战演练:用gulp 构建前端页面(1)---静态构建
- java笔记之线程
- 火神的鱼
- 摇一摇听歌的小Demo
- 图的深度遍历