D. Nested Segments

题目连接:

http://www.codeforces.com/contest/652/problem/D

Description

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.

Sample Input

4

1 8

2 3

4 7

5 6

Sample Output

3

0

1

0

Hint

题意

给你n个线段,让你输出每个线段完全包含多少个其他的线段

保证任意两个线段的端点不重合

题解:

先离散化一波

然后离线树状数组维护一波就好了

for循环暴力扫左端点,然后树状数组去查询小于右端点的线段有多少个,就完了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 4e5+7;
pair<pair<int,int>,int>p[maxn];
int ans[maxn];
vector<int> Q;
map<int,int> H;
int a[maxn];
int lowbit(int x){return x&(-x);}
void update(int x,int v)
{
for(int i=x;i<maxn;i+=lowbit(i))
a[i]+=v;
}
int get(int x)
{
int tot = 0;
for(int i=x;i;i-=lowbit(i))
tot+=a[i];
return tot;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&p[i].first.first,&p[i].first.second);
Q.push_back(p[i].first.first),Q.push_back(p[i].first.second);
p[i].second=i;
}
sort(Q.begin(),Q.end());
Q.erase(unique(Q.begin(),Q.end()),Q.end());
for(int i=0;i<Q.size();i++)
H[Q[i]]=i+1;
for(int i=1;i<=n;i++)
{
p[i].first.first=H[p[i].first.first];
p[i].first.second=H[p[i].first.second];
update(p[i].first.second,1);
}
sort(p+1,p+1+n);
for(int i=1,j=1;i<maxn;i++)
{
while(j<=n&&p[j].first.first==i)
{
ans[p[j].second]=get(p[j].first.second);
update(p[j].first.second,-1);
j++;
}
if(j==n+1)break;
}
for(int i=1;i<=n;i++)
printf("%d\n",ans[i]-1);
}

Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化的更多相关文章

  1. 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 ...

  2. Educational Codeforces Round 10 D. Nested Segments

    D. Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  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. CodeForces-652D:Nested Segments(树状数组+离散化)

    You are given n segments on a line. There are no ends of some segments that coincide. For each segme ...

  6. Educational Codeforces Round 8 E. Zbazi in Zeydabad 树状数组

    E. Zbazi in Zeydabad 题目连接: http://www.codeforces.com/contest/628/problem/D Description A tourist wan ...

  7. Codeforces 703D Mishka and Interesting sum 离线+树状数组

    链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...

  8. Codeforces 220B - Little Elephant and Array 离线树状数组

    This problem can be solve in simpler O(NsqrtN) solution, but I will describe O(NlogN) one. We will s ...

  9. Codeforces Round #401 (Div. 1) C(set+树状数组)

    题意: 给出一个序列,给出一个k,要求给出一个划分方案,使得连续区间内不同的数不超过k个,问划分的最少区间个数,输出时将k=1~n的答案都输出 比赛的时候想的有点偏,然后写了个nlog^2n的做法,T ...

随机推荐

  1. 【C++自我精讲】基础系列六 PIMPL模式

    [C++自我精讲]基础系列六 PIMPL模式 0 前言 很实用的一种基础模式. 1 PIMPL解释 PIMPL(Private Implementation 或 Pointer to Implemen ...

  2. 一个爬取https和http通用的工具类(JDK自带的URL的用法)

    今天在java爬取天猫的时候因为ssl报错,所以从网上找了一个可以爬取https和http通用的工具类.但是有的时候此工具类爬到的数据不全,此处不得不说python爬虫很厉害. package cn. ...

  3. perl6 Socket

    Perl6 中的SOCKET就是相当于Perl5 的 IO::Socket::INET. 官方介绍如下: #下面是客户端multi method new( :$host, :$port, :, :$e ...

  4. Cookie、Session 和 自定义分页

    cookie Cookie的由来 大家都知道HTTP协议是无状态的. 无状态的意思是每次请求都是独立的,它的执行情况和结果与前面的请求和之后的请求都无直接关系,它不会受前面的请求响应情况直接影响,也不 ...

  5. Python3 多进程

    多进程(multiprocessing)的用法和多线程(threading)类似,里面的函数也一样,start()为启动函数,join() 等待该进程运行结束,每一个进程也是由它的父进程产生 1.简单 ...

  6. python算法之近似熵、互近似熵算法

    理论基础 近似熵? 定义:近似熵是一个随机复杂度,反应序列相邻的m个点所连成折线段的模式的互相近似的概率与由m+1个点所连成的折线段的模式相互近似的概率之差. 作用:用来描述复杂系统的不规则性,越是不 ...

  7. python基础===用9种方式生成新的对象

    class Point: def __init__(self, x, y): self.x = x self.y = y point1 = Point(1, 2) point2 = eval(&quo ...

  8. 关于select联动的两种做法

    第一种方法: function dong(){      var getSheng = document.getElementById("sheng");      var get ...

  9. 在Ubuntu上安装Redis MySQL MongoDB memcached Nginx

    1.安装Redis sudo apt-get install redis-server 2.安装MySQL sudo apt-get install mysql-server 3.安装MongoDB ...

  10. cgic实现输入文件名,打开文件的功能

    a.c文件 #include <stdio.h>#include <stdlib.h>#include <string.h>#include <stdarg. ...