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. nginx 配置代理某个路径

    location /test{ proxy_pass http://localhost:8765/test; proxy_set_header Host $http_host; } 其中红色的那句可以 ...

  2. weblogic nmap扫描脚本

     CVE-2018-2894 / Nmap利用脚本,可批量批量快速扫描getshell.检测漏洞.利用漏洞 地址:https://github.com/Rvn0xsy/nse_vuln/tree/ma ...

  3. 每天一条linux命令(1):ls命令

    ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linu ...

  4. openjudge-NOI 2.6-2988 计算字符串距离

    题目链接:http://noi.openjudge.cn/ch0206/2988/ 题解: 首先,题目有误,少了一个添加操作 和求解LCS之类的思路类似 f[i][j]表示a序列中1..i的部分和b序 ...

  5. 中国区的Azure添加到 VSTS 的 Service Endpoint

    把中国区的Azure添加到 VSTS (Visual Studio Team System) 的 Service Endpoint. 这个是使用 VSTS 自动部署到中国区Azure的前置条件. Se ...

  6. java之正则表达式、日期操作

    正则表达式和日期操作 正则表达式简介 正则表达式就是使用一系列预定义的特殊字符来描述一个字符串的格式规则,然后使用该格式规则匹配某个字符串是否符合格式要求. 作用:比如注册邮箱,邮箱有用户名和密码,一 ...

  7. hive学习(四) hive的函数

    1.内置运算符 1.1关系运算符 运算符 类型 说明 A = B 所有原始类型 如果A与B相等,返回TRUE,否则返回FALSE A == B 无 失败,因为无效的语法. SQL使用”=”,不使用”= ...

  8. JavaScript 中typeof、instanceof 与 constructor 的区别?

    typeof.instanceof 与 constructor 详解 typeof  一元运算符 返回一个表达式的数据类型的字符串,返回结果为js基本的数据类型,包括number,boolean,st ...

  9. 七牛刷新接口PHP实现

    <?php require_once '../autoload.php'; use Qiniu\Auth; use Qiniu\Http\Client; $accessKey = 'access ...

  10. 转:asm.js 和 Emscripten 入门教程

    转:http://www.ruanyifeng.com/blog/2017/09/asmjs_emscripten.html asm.js 和 Emscripten 入门教程 作者: 阮一峰 日期:  ...