Educational Codeforces Round 10 D. Nested Segments (树状数组)
题目链接: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 (树状数组)的更多相关文章
- Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...
- 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 ...
- CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...
- Educational Codeforces Round 10 D. Nested Segments
D. Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 ...
- Codeforces Round #261 (Div. 2) D 树状数组应用
看着题意:[1,i]中等于a[i]的个数要大于[,jn]中等于a[j]的个数 且i<j,求有多少对这种(i,j) ,i<j可是 i前面的合法个数 要大于j后面的 看起来非常像逆序数的样子 ...
- Codeforces Beta Round #10 B. Cinema Cashier (树状数组)
题目大意: n波人去k*k的电影院看电影. 要尽量往中间坐,往前坐. 直接枚举,贪心,能坐就坐,坐在离中心近期的地方. #include <cstdio> #include <ios ...
- Educational Codeforces Round 10
A:Gabriel and Caterpillar 题意:蜗牛爬树问题:值得一提的是在第n天如果恰好在天黑时爬到END,则恰好整除,不用再+1: day = (End - Begin - day0)/ ...
- Codeforces 946G Almost Increasing Array (树状数组优化DP)
题目链接 Educational Codeforces Round 39 Problem G 题意 给定一个序列,求把他变成Almost Increasing Array需要改变的最小元素个数. ...
随机推荐
- Codeforces Round #232 (Div. 2) B. On Corruption and Numbers
题目:http://codeforces.com/contest/397/problem/B 题意:给一个n ,求能不能在[l, r]的区间内的数字相加得到, 数字可多次重复.. 比赛的时候没有想出来 ...
- iOS开发:Swift多线程NSThread的使用
一:创建线程,NSThread创建线程常用的三种方式: //1:手动创建启动 let thread:NSThread = NSThread(target: self, selector:"d ...
- BZOJ_1624_ [Usaco2008_Open]_Clear_And_Present_Danger_寻宝之路_(最短路_Floyd)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1025 给出\(n\)个点以及之间的边的长度,给出必须访问的点的顺序,求最短路线长度. 分析 用 ...
- 连接Excel时出现未指定的错误
使用 strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended ...
- 移动对meta的定义
以下是meta每个属性详解 尤其要注意的是content里多个属性的设置一定要用分号+空格来隔开,如果不规范将不会起作用. 一.<meta http-equiv="Content-Ty ...
- Authentication with SignalR and OAuth Bearer Token
Authentication with SignalR and OAuth Bearer Token Authenticating connections to SignalR is not as e ...
- Fedora22(Gnome桌面)安装Chrome
1.从官网上(http://www.google.cn/chrome/)下载到相对应系统版本的rpm包.这里我的是: google-chrome-stable_current_x86_64.rpm 此 ...
- python学习之self,cls,staticmethod,classmethod
一.总体说明 python类里会出现这三个单词,self和cls都可以用别的单词代替,类的方法有三种, 一是通过def定义的 普通的一般的,需要至少传递一个参数,一般用self,这样的方法必须通过一个 ...
- git 记录
在官网有详细的教程http://git-scm.com/book/zh/%E8%B5%B7%E6%AD%A5 查看分支和日志的两个工具:gitk 和 tig ,两个都有 --all 参数,可以查看所有 ...
- 重读gets()与is函数的用法
这是从百度百科上查找的资料: gets(): 从stdin流中读取字符串,直至接受到换行符或EOF时停止,并将读取的结果存放在buffer指针所指向的字符数组中.换行符不作为读取串的内容,读取的换行符 ...