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

给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间。

我是先把每个区间看作整体,按照R从小到大排序。然后从最小的R开始枚举每个区间,要是枚举到这个区间L的时候,计算之前枚举的区间有多少个Li在L之后,那么这些Li大于L的区间的数量就是答案。那我每次枚举的时候用树状数组add(L , 1) 说明在L这个位置上出现了区间,之后枚举的时候计算L之前的和,然后i - 1 - sum(L)这个就是答案。(跟用树状数组计算逆序对有点类似,自己模拟一下就明白了)。

但是区间的L和R很大,区间的个数又不是很多。所以我用离散化,区间的大小包含1到2n这些数。我用map做的,也可以用二分。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int MAXN = 4e5 + ;
struct data {
int l , r , pos;
}a[MAXN];
int ans[MAXN] , n , bit[MAXN] , x[MAXN * ];
map <int , int> mp;
bool cmp(data x , data y) {
return x.r < y.r;
}
int sum(int i) {
int s = ;
while(i > ) {
s += bit[i];
i -= (i&-i);
}
return s;
} void add(int i , int x) {
while(i <= n*) {
bit[i] += x;
i += (i&-i);
}
} int main()
{
scanf("%d" , &n);
int cont = ;
for(int i = ; i <= n ; i++) {
scanf("%d %d" , &a[i].l , &a[i].r);
x[++cont] = a[i].l;
x[++cont] = a[i].r;
a[i].pos = i;
}
sort(x + , x + cont + );
for(int i = ; i <= cont ; i++) {
mp[x[i]] = i;
}
sort(a + , a + n + , cmp);
for(int i = ; i <= n ; i++) {
a[i].l = mp[a[i].l];
a[i].r = mp[a[i].r];
}
for(int i = ; i <= n ; i++) {
ans[a[i].pos] = i - - sum(a[i].l);
//cout << a[i].l << " " << a[i].r << " " << sum(a[i].l) << endl;
add(a[i].l , );
}
for(int i = ; i <= n ; i++) {
printf("%d\n" , ans[i]);
}
}

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. CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组

    题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...

  4. Educational Codeforces Round 10 D. Nested Segments

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

  5. Codeforces Round #365 (Div. 2) D 树状数组+离线处理

    D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...

  6. Codeforces Round #261 (Div. 2) D 树状数组应用

    看着题意:[1,i]中等于a[i]的个数要大于[,jn]中等于a[j]的个数 且i<j,求有多少对这种(i,j)  ,i<j可是 i前面的合法个数 要大于j后面的 看起来非常像逆序数的样子 ...

  7. Codeforces Beta Round #10 B. Cinema Cashier (树状数组)

    题目大意: n波人去k*k的电影院看电影. 要尽量往中间坐,往前坐. 直接枚举,贪心,能坐就坐,坐在离中心近期的地方. #include <cstdio> #include <ios ...

  8. Educational Codeforces Round 10

    A:Gabriel and Caterpillar 题意:蜗牛爬树问题:值得一提的是在第n天如果恰好在天黑时爬到END,则恰好整除,不用再+1: day = (End - Begin - day0)/ ...

  9. Codeforces 946G Almost Increasing Array (树状数组优化DP)

    题目链接   Educational Codeforces Round 39 Problem G 题意  给定一个序列,求把他变成Almost Increasing Array需要改变的最小元素个数. ...

随机推荐

  1. RAPIDXML 中文手册,根据官方文档完整翻译!

    简介:这个号称是最快的DOM模型XML分析器,在使用它之前我都是用TinyXML的,因为它小巧和容易上手,但真正在项目中使用时才发现如果分析一个比较大的XML时TinyXML还是表现一般,所以我们决定 ...

  2. 【Todo】JS跨域访问问题的解决

    做双十一,需要在主会场页面,嵌入我们产品的JS豆腐块.而这个豆腐块需要调用我们后端的数据接口,涉及跨域访问. 参考 http://www.cnblogs.com/2050/p/3191744.html ...

  3. POJ 2947 Widget Factory (高斯消元 判多解 无解 和解集 模7情况)

    题目链接 题意: 公司被吞并,老员工几乎全部被炒鱿鱼.一共有n种不同的工具,编号1-N(代码中是0—N-1), 每种工具的加工时间为3—9天 ,但是现在老员工不在我们不知道每种工具的加工时间,庆幸的是 ...

  4. 《SPFA算法的优化及应用》——姜碧野(学习笔记)

    一.核心性质:三角不等式.最短路满足d[v]<=d[u]+w(u,v) 二.SPFA两种实现: 常见的是基于bfs的,这是直接对bellman-ford用队列维护.根据最短路的长度最长为(n-1 ...

  5. 深入理解移动web开发之PPI,Pixel,DevicePixelRatio(转)

    如果你是一个开始接触移动Web开发的前端工程师,那么你或许也遇到了和我曾经遇到的过问题:有太多新的概念需要掌握,太多相似的概念需要区分.没关系,我将用两篇文章的篇幅来解决这些问题.上篇文章关于解释和区 ...

  6. Linux Shell编程(2): for while

    ; i < ; i++)) do echo "current number is $i" done SERVICES="80 22 25 110 8000 23 2 ...

  7. wifi详解(二)

    1        Wifi模块解析和启动流程 1.1      框架分析 WIFI整体框架如图所示: 首先,用户程序使用WifiManager类来管理Wifi模块,它能够获得Wifi模块的状态,配置和 ...

  8. linux命令——磁盘管理pwd

    Linux中用 pwd 命令来查看”当前工作目录“的完整路径(绝对路径). 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录.在不太确定当前位置时,就会使用pwd来判定当前目录在文件系统内的 ...

  9. asmcmd报错

    在进入asm的命令行时报错: 报错内容如下 [oracle@kel dbs]$ asmcmd asmcmd: command disallowed by current instance type 从 ...

  10. 你是怎么理解“MVC”的

    MVC就是三个字母的组合,M-模型, V-视图, C-控制器. 这些在百度上随便一索就可以索到,而且网上对这三个部分的解释又过于笼统,使人没法完全理解MVC的含义.   这里我简单的谈谈我对MVC这三 ...