Educational Codeforces Round 10-D. Nested Segments

来源:互联网 发布:免费听音乐的软件 编辑:程序博客网 时间:2024/05/15 03:56

原题链接

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
41 82 34 75 6
output
3010
input
33 41 52 6
output
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; }


0 0
原创粉丝点击