codeforces 652D D. Nested Segments(离散化+sort+树状数组)
题目链接:
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 题意:给n个线段,对于每个线段问它覆盖了多少个线段;
思路:由于线段端点是在e9范围内,所以要先离散化到2e5内,只离散化一个端点即可,我离散化的是右端点,然后在对左端点排序(保证l[i]>=l[j]),然后再按顺序query右端点(保证r[i]<r[j])并update,就可以得到结果了,感觉线段树和树状数组真是丧心病狂;还有以前不会离散化,昨天睡觉的时候居然自己就领悟了,哈哈哈哈哈,开心;
AC代码:
/*2014300227 652D - 24 GNU C++11 Accepted 187 ms 5948 KB */
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+;
int n,sum[N],ans[N];
struct node
{
int l,r,id;
};
node qu[N];
bool cmp1(node x,node y)
{
if(x.r==y.r)return x.l<y.l;
return x.r<y.r;
}
bool cmp2(node x,node y)
{
if(x.l==y.l)return x.r<y.r;//注意此处的大小关系,因为我把右端点相等的离散化成不等的了;
return x.l>y.l;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x)
{
while(x<=n)
{
sum[x]++;
x+=lowbit(x);
}
}
int query(int x)
{
int s=;
while(x>)
{
s+=sum[x];
x-=lowbit(x);
}
return s;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d%d",&qu[i].l,&qu[i].r);
qu[i].id=i;
}
sort(qu+,qu+n+,cmp1);//按右端点排序以离散化
for(int i=;i<=n;i++)
{
qu[i].r=i;//离散化
}
sort(qu+,qu+n+,cmp2);
for(int i=;i<=n;i++)
{
ans[qu[i].id]=query(qu[i].r);
update(qu[i].r);
}
for(int i=;i<=n;i++)
{
printf("%d\n",ans[i]);
} return ;
}
codeforces 652D D. Nested Segments(离散化+sort+树状数组)的更多相关文章
- Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)
http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...
- CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)
题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...
- Codeforces 777E(离散化+dp+树状数组或线段树维护最大值)
E. Hanoi Factory time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- cf 61 E. Enemy is weak 离散化+树状数组
题意: 给出一个数组,数组的每一个元素都是不一样的,求出对于3个数组下标 i, j, k such that i < j < k and ai > aj > ak where ...
- Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)
[题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...
- POJ 2299 Ultra-QuickSort (离散化)+【树状数组】
<题目链接> 题目大意: 给你一段序列,问你如果每次只交换该序列相邻的两个元素,最少需要交换多少步才能够使该序列变为升序排列. 解题分析: 不难发现,其实本题就是让我们求原始序列的逆序对, ...
- POJ 2299 Ultra-QuickSort 离散化加树状数组求逆序对
http://poj.org/problem?id=2299 题意:求逆序对 题解:用树状数组.每读入一个数x,另a[x]=1.那么a数列的前缀和s[x]即为x前面(或者说,再x之前读入)小于x的个数 ...
- CodeForces 380C Sereja and Brackets(扫描线+树状数组)
[题目链接] http://codeforces.com/problemset/problem/380/C [题目大意] 给出一个括号序列,求区间内左右括号匹配的个数. [题解] 我们发现对于每个右括 ...
- HDU 5862 Counting Intersections (离散化+扫描线+树状数组)
题意:给你若干个平行于坐标轴的,长度大于0的线段,且任意两个线段没有公共点,不会重合覆盖.问有多少个交点. 析:题意很明确,可是并不好做,可以先把平行与x轴和y轴的分开,然后把平行y轴的按y坐标从小到 ...
随机推荐
- 安装Hadoop 1.1.2 (三 安装配置Hadoop)
1 tar -zxvf hadoop-1.1.2.tar.gz 2 在hadoop/conf目录 (1) 编辑 hadoop-env.sh export JAVA_HOME=/usr/java/jdk ...
- spring mvc注解和spring boot注解
1 spring mvc和spring boot之间的关系 spring boot包含spring mvc.所以,spring mvc的注解在spring boot总都是可以用的吗? spring b ...
- cocos2d-x CCControl控件
感谢点评与关注.欢迎转载与分享.勤奋努力,持之以恒! CCControlSlider 滑动条 void HelloWorld::myInit10() { CCSize size = CCDirecto ...
- linux下查找最耗iowait的进程
抓哪个进程干坏事前要先停掉syslogservice syslog stop 打开block dump:echo 1 > /proc/sys/vm/block_dump 统计:dmesg | e ...
- 谷歌浏览器chrome上的firepath ----chropath
图标如下 功能:获取相对xpath,绝对xpath和CSS选择器.在devtools面板中编辑,检查并验证XPath和CSS选择器. 使用方法
- 视图的创建与使用 Sql Server View
创建教材的三个数据表Student.Course及SC. create database S_T Use S_T CREATE TABLE Student (Sno CHAR(9), Sname CH ...
- Data Structure Array: Maximum sum such that no two elements are adjacent
http://www.geeksforgeeks.org/maximum-sum-such-that-no-two-elements-are-adjacent/ #include <iostre ...
- C#判断VS是否处于设计模式
public class CheckDesingModel { public static bool IsDesingMode() { bool ReturnFlag = false; if (Lic ...
- 苹果企业账号发布APP详解——通过自己网站分发应用
一.通过企业账号申请证书 1 Certificate Signing Request (CSR)文件 在Mac系统中进入“钥匙串访问”,选择“钥匙串访问”-“证书助理”-“从证书颁发机构请求证书…”, ...
- uCGUI 按键窗口切换机制(更新篇)
在之前文章中,讲述了一个低内存使用量的的窗口切换机制.有人会问,低内存使用量是多低呢,我这里举个例子.我有一个项目中使用到本切换机制,128*64的单色屏,初步计算有105个窗口(后面还会增加),总内 ...