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做的,也可以用二分。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <map>
 6 using namespace std;
 7 const int MAXN = 4e5 + 10;
 8 struct data {
 9     int l , r , pos;
10 }a[MAXN];
11 int ans[MAXN] , n , bit[MAXN] , x[MAXN * 2];
12 map <int , int> mp;
13 bool cmp(data x , data y) {
14     return x.r < y.r;
15 }
16 int sum(int i) {
17     int s = 0;
18     while(i > 0) {
19         s += bit[i];
20         i -= (i&-i);
21     }
22     return s;
23 }
24
25 void add(int i , int x) {
26     while(i <= n*2) {
27         bit[i] += x;
28         i += (i&-i);
29     }
30 }
31
32 int main()
33 {
34     scanf("%d" , &n);
35     int cont = 0;
36     for(int i = 1 ; i <= n ; i++) {
37         scanf("%d %d" , &a[i].l , &a[i].r);
38         x[++cont] = a[i].l;
39         x[++cont] = a[i].r;
40         a[i].pos = i;
41     }
42     sort(x + 1 , x + cont + 1);
43     for(int i = 1 ; i <= cont ; i++) {
44         mp[x[i]] = i;
45     }
46     sort(a + 1 , a + n + 1 , cmp);
47     for(int i = 1 ; i <= n ; i++) {
48         a[i].l = mp[a[i].l];
49         a[i].r = mp[a[i].r];
50     }
51     for(int i = 1 ; i <= n ; i++) {
52         ans[a[i].pos] = i - 1 - sum(a[i].l);
53         //cout << a[i].l << "  " << a[i].r << "    " << sum(a[i].l) << endl;
54         add(a[i].l , 1);
55     }
56     for(int i = 1 ; i <= n ; i++) {
57         printf("%d\n" , ans[i]);
58     }
59 }
时间: 2024-08-04 23:36:27

Educational Codeforces Round 10 D. Nested Segments (树状数组)的相关文章

CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组

题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点值离散化,按照左端点从大到小排序,顺着这个顺序处理所有线段,那么满足在它内部的线段一定是之前已经扫到过的.用树状数组判断有多少是在右端点范围内. 1 #include <iostream> 2 #include <vector> 3 #include <algorithm>

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

看着题意:[1,i]中等于a[i]的个数要大于[,jn]中等于a[j]的个数 且i<j,求有多少对这样的(i,j)  ,i<j但是 i前面的合法个数 要大于j后面的 看起来很像逆序数的样子,所以很容易往树状数组想去,但是处理就看个人了,像我比赛的时候就处理得非常的麻烦,虽做出了但是花时间也多,经过杰哥的教育,其实正着塞进树状数组 反着来找就可以了,灰常的简单清晰明了,贴一发纪念我的搓比 int n; int aa[1000000 + 55]; int bb[1000000 + 55]; int

Codeforces Round #301 (Div. 2)(树状数组+离散化)

E. Infinite Inversions time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output There is an infinite sequence consisting of all positive integers in the increasing order: p?=?{1,?2,?3,?...}. We per

Codeforces Round #220 (Div. 2) D 树状数组 &amp;&amp; 二分

/*题目*/ 题意:给了n,m,然后一个包含m个数的数组nnum,数组默认从小到大排序,然后是 n个操作,输入一个数x,若x为0,把0加到这个字符串的末尾,若x为1,把1加到这个字符串的末尾,若x为-1,那么把字符串里的 下标 与 nnum数组里的元素相等的  给删除,字符串一开始是空的,问你最后字符串里有什么,若为空 就输出 POOR STACK 这题目看这操作一般都很容易联想到线段树,树状数组,一开始我建了个树状数组,但是超时了,毕竟操作很多,而且 在删除操作里,若nnum数组很大,等同于1

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

D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes input standard input output standard output Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present her wit

Codeforces Round #225 (Div. 1) C 树状数组 || 线段树

看到这题很开心啊,有印象跟以前做过的很像,貌似最近就做过一个,以时间戳为区间来建立树状数组,然后一开始我以为题意是,给x点加val,它以下的所有节点都加-val:所以一开始就以 加 和 减 建立了两个树状数组,最后 减去就是答案,写完发现跟案例对不上啊,读了题目也没发现读错了,对于那句话 我理解错了,后来看了 这个: http://blog.csdn.net/keshuai19940722/article/details/18967661 仔细看看处理部分,我还以为分奇偶性有规律呢,后来才发现读

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

题目大意: n波人去k*k的电影院看电影. 要尽量往中间坐,往前坐. 直接枚举,贪心,能坐就坐,坐在离中心近期的地方. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #define maxn 1000005 #define lowbit(x) (x&(-x)) using namespace std; struct BIT { int sum

Codeforces 374D Inna and Sequence 二分+树状数组

题目链接:点击打开链接 给定n个操作,m长的序列a 下面n个数 if(co>=0)则向字符串添加一个co (开始是空字符串) else 删除字符串中有a的下标的字符 直接在序列上搞,简单模拟 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h&

Codeforces Gym 100114 H. Milestones 离线树状数组

H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description The longest road of the Fairy Kingdom has n milestones. A long-established tradition defines a specific color for milestones in each region, with a