题目链接: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. SGU 275 To xor or not to xor (高斯消元)

    题目链接 题意:有n个数,范围是[0, 10^18],n最大为100,找出若干个数使它们异或的值最大并输出这个最大值. 分析: 一道高斯消元的好题/ 我们把每个数用二进制表示,要使得最后的异或值最大, ...

  2. android线程池

    线程池的基本思想还是一种对象池的思想,开辟一块内存空间,里面存放了众多(未死亡)的线程,池中线程执行调度由池管理器来处理.当有线程任务时,从池中取一个,执行完成后线程对象归池,这样可以避免反复创建线程 ...

  3. volicity语法学习和总结

    Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码定义的对象.当Velocit ...

  4. Asp.net正则获取html内容

    1.获取div内容 string str = "tt<u>ss</u><div id=\"test\"><div>< ...

  5. js 写成类的形式 js 静态变量 js方法 属性 json类

    function ClassStudentList() { //[{"Cid":"0d","Students":[{"Sid&qu ...

  6. [反汇编练习] 160个CrackMe之006

    [反汇编练习] 160个CrackMe之006. 本系列文章的目的是从一个没有任何经验的新手的角度(其实就是我自己),一步步尝试将160个CrackMe全部破解,如果可以,通过任何方式写出一个类似于注 ...

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

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

  8. 【JSP】JSP检查字符串是否为数字

    //判断是否是正整数 function IsNum(s) { if(s!=null){ var r,re; re = /\d*/i; //\d表示数字,*表示匹配多个数字 r = s.match(re ...

  9. Zend Framework 入门(3)—错误处理

    undefined 说明脚本中一些对象没有设定 你应在在使用该对象前进行判断 例如: if(typeof(aobject) == "undefined"){   //错误处理 }

  10. cgroup隔离的知识点

    tasks中写入的是线程号 cgroup.procs是进程号 ===================CPU隔离===================== 主机CPU核数: cat /proc/cpui ...