题目链接

我们将线段按照右端点从小到大排序, 如果相同, 那么按照左端点从大到小排序。

然后对每一个l, 查询之前有多少个l比他大, 答案就是多少。因为之前的r都是比自己的r小的, 如果l还比自己大的话, 那么自己就可以包含它。 记得离散化。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
struct node
{ int l, r, id;
bool operator < (node a) const
{
if(r == a.r)
return l>a.l;
return r<a.r;
}
}q[200005];
int b[200005], ans[200005], sum[200005<<2];
void update(int p, int l, int r, int rt) {
if(l == r) {
sum[rt] ++;
return ;
}
int m = l+r>>1;
if(p<=m)
update(p, lson);
else
update(p, rson);
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
int query(int L, int R, int l, int r, int rt){
if(L<=l&&R>=r){
return sum[rt];
}
int m = l+r>>1, ret = 0;
if(L<=m)
ret += query(L, R, lson);
if(R>m)
ret += query(L, R, rson);
return ret;
}
int main()
{
int n;
cin>>n;
for(int i = 0; i < n; i++) { scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
b[i] = q[i].l;
}
sort(q, q+n);
sort(b, b+n);
for(int i = 0; i < n; i++) {
int x = lower_bound(b, b+n, q[i].l)-b+1;
ans[q[i].id] = query(x, n, 1, n, 1);
update(x, 1, n, 1);
}
for(int i = 0; i<n; i++) {
printf("%d\n", ans[i]);
}
return 0;
}

codeforces 652D . Nested Segments 线段树的更多相关文章

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

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

  2. [离散化+树状数组]CodeForces - 652D Nested Segments

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

  3. CodeForces 652D Nested Segments

    离散化+树状数组 先对坐标离散化,把每条线段结尾所在点标1, 询问某条线段内有几条线段的时候,只需询问这段区间的和是多少,询问结束之后再把这条线段尾部所在点标为0 #include<cstdio ...

  4. URAL-1987 Nested Segments 线段树简单区间覆盖

    题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1987 题意:给定n条线段,每两条线段要么满足没有公共部分,要么包含.给出m个询问,求当前 ...

  5. Buses and People CodeForces 160E 三维偏序+线段树

    Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...

  6. CodeForces 877E DFS序+线段树

    CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...

  7. [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)

    [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...

  8. [Codeforces 1199D]Welfare State(线段树)

    [Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...

  9. [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)

    [Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...

随机推荐

  1. select Into用法

    1. insert into 表(列1,列2) select 列1,列2 from 表2 where.... 2.select * into 表2 from 表 1where... 上面写法标识将表1 ...

  2. 13个mysql数据库的实用SQL小技巧

    此文章为转载 使用CASE来重新定义数值类型 SELECT id,title, (CASE date WHEN '0000-00-00' THEN '' ELSE date END) AS date ...

  3. 关于C语言指针几个容易混淆的概念

    前言: 大多数学习过C/C++或者正在学习的同学在对指针概念把握时,总是感觉不太明了,小弟我也不例外啊,于是翻开资料复习整理一下,并把自己的学习心得拿出来供大家分享,讨论. 基本概念掠过,主要来探讨一 ...

  4. Linux下进程的文件访问权限

    本文转自 http://blog.csdn.net/chosen0ne/article/details/10581883 对进程校验文件访问权限包括两个部分,一是确定进程的角色(属于哪个用户或者组), ...

  5. 安装Boost

    @echo off set BOOST_ROOT=C:\boost_1_59_0 pushd %BOOST_ROOT% cd tools\build call bootstrap.bat gcc b2 ...

  6. Win32汇编开始 Hello Asm

    今天开始学习Win32汇编 因为自己很多都是Windows方面 所以 接触一下Win32汇编 . ;.386指令集 .model flat,stdcall ;工作模式 option casemap:n ...

  7. SQLServer 2008 R2 清空日志文件

    USE [master]GOALTER DATABASE FH2_SJH SET RECOVERY SIMPLE WITH NO_WAITGOALTER DATABASE FH2_SJH SET RE ...

  8. struts2笔记12-声明式异常

    1.配置异常处理 <action name="save" class="com.test.actions.ProductAction" method=&q ...

  9. USB 3.1 Type-C

    [時報記者任珮云台北報導]微軟.英特爾.蘋果今年將新款PC介面升級至USB 3.1規格,Wintel陣營今年新款PC產品亦將全面採用USB 3.1介面,里昂證出具最新的報告指出,在新趨勢帶動下,台廠的 ...

  10. zookeeper watch 节点

    zjtest7-redis:/root/zk# cat a1.pl use ZooKeeper; use AnyEvent; use AE; use Data::Dumper; use IO::Soc ...