Code Forces 652D Nested Segments(离散化+树状数组)
2 seconds
256 megabytes
standard input
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.
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.
Print n lines. The j-th
of them should contain the only integer aj —
the number of segments contained in the j-th segment.
4
1 8
2 3
4 7
5 6
3
0
1
0
3
3 4
1 5
2 6
0
1
1
离散化+树状数组。
把所有区间按照右端点排序,然后统计左端点和右端点之间的已经包含的左端点个数,用树状数组求区间和会很快
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <math.h> using namespace std;
#define MAX 2*100000
struct Node
{
int l,r;
int pos;
}a[MAX+5];
int n;
int num[2*MAX+5];
int c[2*MAX+5];
int ans[MAX+5];
int s;
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int num)
{
while(x<=s)
{
c[x]+=num;
x+=lowbit(x);
}
}
int sum(int x)
{
int _sum=0;
while(x>0)
{
_sum+=c[x];
x-=lowbit(x);
}
return _sum;
}
int cmp(Node a,Node b)
{
return a.r<b.r;
}
int main()
{
scanf("%d",&n);
memset(c,0,sizeof(c));
int cnt=0;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
a[i].pos=i;
num[cnt++]=a[i].l;
num[cnt++]=a[i].r;
}
sort(num,num+cnt);
for(int i=1;i<=n;i++)
{
a[i].l=lower_bound(num,num+cnt,a[i].l)-num+1;
a[i].r=lower_bound(num,num+cnt,a[i].r)-num+1;
}
sort(a+1,a+n+1,cmp);
s=a[n].r;
for(int i=1;i<=n;i++)
{
int num=sum(a[i].r)-sum(a[i].l-1);
ans[a[i].pos]=num;
update(a[i].l,1);
}
for(int i=1;i<=n;i++)
{
printf("%d\n",ans[i]);
}
return 0;
}
Code Forces 652D Nested Segments(离散化+树状数组)的更多相关文章
- codeforces 652D Nested Segments 离散化+树状数组
题意:给你若干个区间,询问每个区间包含几个其它区间 分析:区间范围比较大,然后离散化,按右端点排序,每次更新树状数组中的区间左端点,查询区间和 注:(都是套路) #include<cstdio& ...
- CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...
- 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 ...
- Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...
- CodeForces-652D:Nested Segments(树状数组+离散化)
You are given n segments on a line. There are no ends of some segments that coincide. For each segme ...
- D. Nested Segments(树状数组、离散化)
题目链接 参考博客 题意: 给n个线段,对于每个线段问它覆盖了多少个线段. 思路: 由于线段端点是在2e9范围内,所以要先离散化到2e5内(左右端点都离散化了,而且实际上离散化的范围是4e5),然后对 ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- CodeForces 540E - Infinite Inversions(离散化+树状数组)
花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...
- Ultra-QuickSort(归并排序+离散化树状数组)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 50517 Accepted: 18534 ...
随机推荐
- 转:Python操作SQLServer示例
注:此文也是转载,2018年1月发现此文阅读量过万,略感不安.当时只是为了自己存档学习,未粘此文的原始连接.如有侵权,通过即删除,敬请谅解! 从网上找的,估计原文是:Python操作SQLServer ...
- 修改MySQL的默认密码的四种小方法
投稿:lijiao 字体:[增加 减小] 类型:转载 时间:2015-09-05我要评论 对于windows平台来说安装完MySQL后,系统就已经默认生成了许可表和账户,下文中就教给大家如何修改MyS ...
- sublime text全局搜索,查找对应类插件
windows平台下的操作. 1.你必须先安装package controller 否则请先安装 2. 图1 一.如果Preferences > Browse Packages菜单下没有Pa ...
- php对gzip的使用(开启)
gzip是一种压缩算法,在网络通信过程中,经常用到gzip压缩算法.比如一个文本文件,大小为100M,使用gzip压缩之后,大小可能会变成几M.在网络传输过程中,传10M和传100M,消耗的时间和带宽 ...
- SharePoint 2010 讨论板列表内容的读取细节处理
list.Folder表示subject,属于特殊列表,使用list.Folder遍历项,而不是使用list.Items;list.Items表示reply;list.Items[0]["P ...
- 学习 TList 类的实现[4]
现在准备一步步地模拟 TList 类, 建立一个自己的 TMyList. 首先, 这个类中应该包括前面提到的那个 Pointer 数组(TPointerList)的指针(PPointerList): ...
- 利用jstack 找到异常代码
1.top找出耗时pid进程或ps -ef |grep xxx 找出pid 2.ps p 3036 -L -o pcpu,pid,tid,time,tname,cmd 3036为pid 3.prin ...
- 第四章 Spring.Net 如何管理您的类___对象的作用域
Spring.Net 中对象的作用域,就是描述对象的部署模式 ,Spring.Net 中对象可以通过两种模式布署: ① singleton(单例模式) ② 非singleton 也叫非单例模式(或者叫 ...
- Collabration Web Application Screenshot(English Language) Free download now!
The screenshots of english language version collabration web application which is as following: Incl ...
- JavaScript------自定义string.replaceAll()方法
代码:: 注意:原始的replace()方法只能替换第一个字符串check String.prototype.replaceAll = function (s1, s2) { return this. ...