题目链接:http://codeforces.com/problemset/problem/652/D

大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段。

方法是对所有线段的端点值离散化,按照左端点从大到小排序,顺着这个顺序处理所有线段,那么满足在它内部的线段一定是之前已经扫到过的。用树状数组判断有多少是在右端点范围内。

 #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#include <ctime>
#include <set>
using namespace std; const int N=4e5+;
int a[N];
int b[N];
int lowbit(int x) {
return x&(-x);
}
int get(int x) {
int ret=;
while (x) {
ret+=a[x];
x-=lowbit(x);
}
return ret;
}
void add(int x,int add) {
while (x<N) {
a[x]+=add;
x+=lowbit(x);
}
}
struct Seg{
int l,r;
int id; Seg() { } Seg(int l, int r, int id) : l(l), r(r), id(id) { }
bool operator < (const Seg & o) const {
return l>o.l;
}
}seg[N];
int ans[N];
int main () {
int n;
scanf("%d",&n);
int t=;
for (int i=;i<=n;i++) {
scanf("%d %d",&seg[i].l,&seg[i].r);
b[t++]=seg[i].l;
b[t++]=seg[i].r;
seg[i].id=i;
}
sort(b,b+t);
int k=unique(b,b+t)-b;
for (int i=;i<=n;i++) {
seg[i].l=lower_bound(b,b+t,seg[i].l)-b+;
seg[i].r=lower_bound(b,b + t,seg[i].r)-b+;
}
sort(seg+,seg++n);
for (int i=;i<=n;i++) {
int id=seg[i].id;
ans[id]=get(seg[i].r);
add(seg[i].r,);
}
for (int i=;i<=n;i++) {
printf("%d\n",ans[i]);
}
return ;
}

CF 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

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

  4. Educational Codeforces Round 10 D. Nested Segments (树状数组)

    题目链接:http://codeforces.com/problemset/problem/652/D 给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间. 我是先把每个区间 ...

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

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

  6. codeforces 652D Nested Segments 离散化+树状数组

    题意:给你若干个区间,询问每个区间包含几个其它区间 分析:区间范围比较大,然后离散化,按右端点排序,每次更新树状数组中的区间左端点,查询区间和 注:(都是套路) #include<cstdio& ...

  7. CodeForces-652D:Nested Segments(树状数组+离散化)

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

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

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

  9. D. Nested Segments(树状数组、离散化)

    题目链接 参考博客 题意: 给n个线段,对于每个线段问它覆盖了多少个线段. 思路: 由于线段端点是在2e9范围内,所以要先离散化到2e5内(左右端点都离散化了,而且实际上离散化的范围是4e5),然后对 ...

随机推荐

  1. Java面试03|并发及锁

    1.synchronized与Lock的区别 使用synchronized这个关键字实现的同步块有一些缺点: (1)锁只有一种类型 (2)线程得到锁或者阻塞 (3)Lock是在Java语言层面基于CA ...

  2. Macaca 自动化框架 [Python 系列]

    介绍 Macaca是一套完整的自动化测试解决方案,基于node.js开发.由阿里巴巴公司开源: 地址:http://macacajs.github.io/macaca/ 特点: 同时支持PC端和移动端 ...

  3. spring知识点全部复习

    一. 专业术语 侵入式设计 引入框架,对现有的类的结构有影响,即需要实现或继承某些特定类.如:Struts框架 非侵入式设计 引入框架,对现有的类结构没有影响.如:Hibernate.Spring 控 ...

  4. docker X509 证书错误的终极解决办法

    最近在做Docker相关的东西,发现只要一pull镜像,就出现如下的ERROR x509: certificate signed by unknown authority. 调查后发现,是公司IT把h ...

  5. ThinkPhp框架:验证码功能

    Think\Verify类可以支持验证码的生成和验证功能. 为了显示这个验证码功能,第一要有控制器,再就是有方法,然后是显示的页面. 一.最简单的方式生成验证码 (1)我们还是继续在那个控制器编写方法 ...

  6. Burpsuite暴力破解

    神器:burpsuite 闲话不多说,直接开搞 1.打开文件BurpLoader.jar,进入Proxy--Options,启用代理 2.打开浏览器(IE),进入Internet选项-连接-局域网设置 ...

  7. 企业架构(TOGAF)学习

    自从听了公司内部的一堂<企业架构设计>培训,顿时觉得如获至宝. 先说下笔者,笔者是一名二流本科毕业,工作三年,基层的软件开发工程师,梦想着有朝一日成长成一名架构师.可是笔者对于如何成长成一 ...

  8. ng-clip angualr 的copy功能

    每次写博客都想由衷的给我的老大膜拜一番!以前刚开始做angular的项目的时候就有说要有点击复制的功能因为当时菜啊,不懂啊.也就没做,今天老大又给了我一个资料!“ng-clip”.跟着老大最大的收获就 ...

  9. vpn的实现原理

    由于公共IP的短缺,我们在组建局域网时,通常使用保留地址作为内部IP,(比如最常用的C类保留地址:192.168.0.0-192.168.255.255)这些地址是不会被互联网分配的,因此它们在互联网 ...

  10. SQL AlawaysOn 之四:故障转移集群

    声明,故障转移集群,仅安装在SQL服务器中,域服务器不能和SQL服务器一起加入集群. 1.添加故障转移集群,下一步 2.安装 3.在域控制服务器上的管理工具里打开故不障转移集群管理器,选择创建集群 4 ...