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

离散化+ 线段树或树状数组维护前缀和

想按照左端点进行排序,然后逆序 对右端点进行查询,插入.这样可以保证线段的包含关系。写的有点挫,一开始始终re后来把线段树开到8*maxn就过了。。

/* ***********************************************
Author :guanjun
Created Time :2016/3/28 15:34:10
File Name :cfedu10d.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 211000
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
struct Node{
int x,y;
int id;
}Nod[maxn];
bool cmp(Node a,Node b){
return a.x<b.x;
}
struct node{
int l,r;
ll sum;
int c;
}nod[maxn*];
int ans[maxn];
void push_up(int i){
nod[i].sum=nod[i<<].sum+nod[i<<|].sum;
}
void build(int i,int l,int r){
nod[i].l=l;
nod[i].r=r;
nod[i].c=;
if(l==r){
nod[i].sum=;
return ;
}
int mid=(l+r)/;
build(i<<,l,mid);
build(i<<|,mid+,r);
push_up(i);
}
void update(int i,int k,int x){
if(nod[i].l==k&&nod[i].r==k){
nod[i].sum+=x;
return ;
}
int mid=(nod[i].l+nod[i].r)/;
if(k<=mid)update(i<<,k,x);
else update(i<<|,k,x);
push_up(i);
}
ll quary(int i,int l,int r){
if(nod[i].l==l&&nod[i].r==r){
return nod[i].sum;
}
int mid=(nod[i].l+nod[i].r)/;
ll ans=;
if(r<=mid)ans+=quary(i<<,l,r);
else if(l>mid)ans+=quary(i<<|,l,r);
else {
ans+=quary(i<<,l,mid);
ans+=quary(i<<|,mid+,r);
}
push_up(i);
return ans;
}
int num[*maxn];
map<int,int>mp;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int n,q;
ll z;
while(cin>>n){
mp.clear();
for(int i=;i<=n;i++){
scanf("%d%d",&Nod[i].x,&Nod[i].y);
num[i]=Nod[i].x;
num[i+n]=Nod[i].y;
Nod[i].id=i;
}
sort(num+,num++(*n));
int cnt=;
for(int i=;i<=*n;i++){
mp[num[i]]=cnt;
cnt++;
}
build(,,mp[num[*n]]);
sort(Nod+,Nod++n,cmp);
for(int i=n;i>=;i--){
ans[Nod[i].id]=quary(,,mp[Nod[i].y]);
update(,mp[Nod[i].y],);
}
for(int i=;i<=n;i++){
printf("%d\n",ans[i]);
}
}
return ;
}

Educational Codeforces Round 10 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. Educational Codeforces Round 10 D. Nested Segments (树状数组)

    题目链接:http://codeforces.com/problemset/problem/652/D 给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间. 我是先把每个区间 ...

  4. CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组

    题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...

  5. Educational Codeforces Round 10

    A:Gabriel and Caterpillar 题意:蜗牛爬树问题:值得一提的是在第n天如果恰好在天黑时爬到END,则恰好整除,不用再+1: day = (End - Begin - day0)/ ...

  6. Educational Codeforces Round 10 A. Gabriel and Caterpillar 模拟

    A. Gabriel and Caterpillar 题目连接: http://www.codeforces.com/contest/652/problem/A Description The 9-t ...

  7. Educational Codeforces Round 10 C. Foe Pairs 水题

    C. Foe Pairs 题目连接: http://www.codeforces.com/contest/652/problem/C Description You are given a permu ...

  8. Educational Codeforces Round 10 B. z-sort 构造

    B. z-sort 题目连接: http://www.codeforces.com/contest/652/problem/B Description A student of z-school fo ...

  9. Educational Codeforces Round 10 E - Pursuit For Artifacts (强联通缩点 + 回溯)

    题目链接:http://codeforces.com/contest/652/problem/E 给你n个点m个边,x和y双向连接,要是z是1表示这条边上有宝藏,0则没有,最后给你起点和终点,问你要是 ...

随机推荐

  1. 最长递增子序列(cogs 731)

    «问题描述:给定正整数序列x1,..., xn.(1)计算其最长递增子序列的长度s.(2)计算从给定的序列中最多可取出多少个长度为s的递增子序列.(3)如果允许在取出的序列中多次使用x1和xn,则从给 ...

  2. 森林 BZOJ 3123

    题解: 第k大直接用主席树解决 合并利用启发式合并,将较小的连接到较大的树上 #include<cmath> #include<cstdio> #include<cstd ...

  3. ORACLE的impdp和expdp命令【登录、创建用户、授权、导入导出】

    使用EXPDP和IMPDP时应该注意的事项: EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用. EXPDP和IMPDP是服务端的工具程序,他们只能在ORACLE服务端使用, ...

  4. java中执行JS脚本

    package 测试包; import javax.script.*; public class SSSSSSSSS { public SSSSSSSSS() { // TODO Auto-gener ...

  5. objective-c中#import和@class的区别

    在Objective-C中,可以使用#import和@class来引用别的类型, 但是你知道两者有什么区别吗? @class叫做forward-class,  你经常会在头文件的定义中看到通过@cla ...

  6. android应用开发之View的大小计量单位(px、dpi、dp、dip、sp)

    http://blog.csdn.net/ljianhui/article/details/43601495?ref=myread 一.像素(px)与屏幕分辨率 1)px(Pixels ,像素):对应 ...

  7. AppCompatActivity

    刚开始看HelloWorld的目录结构然后就发现Android Studio中的是 import android support.v7.app.AppcompatActivity; public cl ...

  8. (CF)Codeforces445A DZY Loves Chessboard(纯实现题)

    转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接:http://codeforces.com/problemset/pro ...

  9. Mac电脑解压文件unrar用密码问题解决

    下载了一个rar文件,有密码的,你懂的. 但是在mac上面,用unrar解压,只能解出空文件:用izip解压,直接停在那里不动. 只好上网搜索.找到了办法. 用brew 安装了命令行版本的 unrar ...

  10. deepin os 15.4 切换jdk版本

    sudo update-alternatives --config javasudo update-alternatives --config javacsudo update-alternative ...