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. 常州模拟赛d3t3 两只怪物心心相印

    题目背景 从前我是一位无名的旅人,旅途中我得到了某样东西:贤者之石.我因此得到悠久的时光和漂泊的生命.1897年冬天,我一时兴起舍弃了旅人的生活. 贤者之石创造出来的,是货真价实的黄金.我的名声传遍了 ...

  2. linux网络性能评估

    Linux网络性能评估 参考自:自学it网,http://www.zixue.it/. 网络性能评估(1)通过ping命令检测网络的连通性.(2)通过netstat -i 组合检测网络接口状况.(3) ...

  3. golang-uuid

    uuid第三方库可以在github上找,我在这使用的是:github.com/satori/go.uuid PS:不知道哪里原因,使用go mod 管理包,下载的包和github上的不是完全一样,也特 ...

  4. 湘潭oj1203/邀请赛A题 数论+java大数

    求 n%1+n%2+n%3+n%4+.........n%n=,n<=10^12次. 开始时盲目地找规律,结果一无所获.后来经学长点拨,天资愚钝,搞了半天才明白. 先上图: 对于该题,在求区间( ...

  5. 取得mib oidname oid 对应关系表

    snmptranslate -Tz -m ALL > d:\2.txt 取得所有名称与OID的对应表,很有用

  6. 42.QT-QSqlQuery类操作SQLite数据库(创建、查询、删除、修改)详解

    Qt 提供了 QtSql 模块来提供平台独立的基于 SQL 的数据库操作.这里我们所说的“平台 独立”,既包括操作系统平台,也包括各个数据库平台,Qt支持以下几种数据库: QT自带SQLITE数据库, ...

  7. HNOI_2002 营业额统计(Splay)

    此题可以用STL的multiset解决,也可以手打一棵伸展树(Splay)来求前驱与后驱. 使用multiset: #include<iostream> #include<set&g ...

  8. Ubuntu官方Wiki教程资源

    前言:通常学习一样新知识时,最快的方式是通过搜索引擎然后以最快的方式拿枪上战场,如果接下来还一直依赖搜索引擎去打,那么你会发现自己永远都在打游击:那么如果要解决这个问题,必须要学会系统的学习,只有连贯 ...

  9. Shiro源代码分析之两种Session的方式

    1.Shiro默认的Session处理方式 <!-- 定义 Shiro 主要业务对象 --> <bean id="securityManager" class=& ...

  10. time is always a factor, time is always now!!!!

    https://www.linkedin.com/pulse/time-always-now-joe-alderman ---------------------------------------- ...