codeforces 652D . Nested Segments 线段树
题目链接
我们将线段按照右端点从小到大排序, 如果相同, 那么按照左端点从大到小排序。
然后对每一个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 线段树的更多相关文章
- codeforces 652D Nested Segments 离散化+树状数组
题意:给你若干个区间,询问每个区间包含几个其它区间 分析:区间范围比较大,然后离散化,按右端点排序,每次更新树状数组中的区间左端点,查询区间和 注:(都是套路) #include<cstdio& ...
- [离散化+树状数组]CodeForces - 652D Nested Segments
Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- CodeForces 652D Nested Segments
离散化+树状数组 先对坐标离散化,把每条线段结尾所在点标1, 询问某条线段内有几条线段的时候,只需询问这段区间的和是多少,询问结束之后再把这条线段尾部所在点标为0 #include<cstdio ...
- URAL-1987 Nested Segments 线段树简单区间覆盖
题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1987 题意:给定n条线段,每两条线段要么满足没有公共部分,要么包含.给出m个询问,求当前 ...
- Buses and People CodeForces 160E 三维偏序+线段树
Buses and People CodeForces 160E 三维偏序+线段树 题意 给定 N 个三元组 (a,b,c),现有 M 个询问,每个询问给定一个三元组 (a',b',c'),求满足 a ...
- CodeForces 877E DFS序+线段树
CodeForces 877E DFS序+线段树 题意 就是树上有n个点,然后每个点都有一盏灯,给出初始的状态,1表示亮,0表示不亮,然后有两种操作,第一种是get x,表示你需要输出x的子树和x本身 ...
- [Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路)
[Codeforces 1197E]Culture Code(线段树优化建图+DAG上最短路) 题面 有n个空心物品,每个物品有外部体积\(out_i\)和内部体积\(in_i\),如果\(in_i& ...
- [Codeforces 1199D]Welfare State(线段树)
[Codeforces 1199D]Welfare State(线段树) 题面 给出一个长度为n的序列,有q次操作,操作有2种 1.单点修改,把\(a_x\)修改成y 2.区间修改,把序列中值< ...
- [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)
[Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...
随机推荐
- C# 中的委托和事件详解
C# 中的委托和事件 文中代码在VS2005下通过,由于VS2003(.Net Framework 1.1)不支持隐式的委托变量,所以如果在一个接受委托类型的位置直接赋予方法名,在VS2003下会报错 ...
- php安装配置文件 源码和yum版
源码安装 ./configure --prefix=/usr/local/services/php \--with-config-file-path=/usr/local/service/php/et ...
- Unicode解码、URL编码/解码
+ (NSString *) stringByReplaceUnicode:(NSString *)string { NSMutableString *convertedString = [strin ...
- TCP/IP详解之:IGMP和DNS
第13章 IGMP:Internet组管理协议 IGMP用于支持主机和路由器进行多播: IGMP是IP层的一部分,IGMP报文通过IP数据报进行传输: IGMP报文长度为固定8 Byte: 报文中,I ...
- json对象和json字符串转换方法
在WEB数据传输过程中,json是以文本,即字符串的轻量级形式传递的,而客户端一般用JS操作的是接收到的JSON对象,所以,JSON对象和JSON字符串之间的相互转换.JSON数据的解析是关键. 先明 ...
- easyui跨iframe属性datagrid
1.问题 如何刷新easyui父级tab页中iframe嵌套页中的datagrid? 2.解决方法 (1) parent.$("iframe[title='tabtitle']") ...
- 使用Python把Gtest XML测试结果转换为HTML格式
在最近的测试中,使用gtest测试框架对c语言代码进行测试,结果以XML文件来保存,但是测试结果的查阅和分析非常不方便.便想着把xml的结果直接转为HTML文件,方便和Jenkins系统对接显示.因现 ...
- eclipse 改包名
转载自: http://www.2cto.com/kf/201304/206747.html 1.在项目上右键,选择android tools->rename application packa ...
- 限制TextBox输入,只能输入整数
public class TextBoxInt : TextBox { public TextBoxInt() { KeyDown += TextBoxInt_KeyDown; TextChanged ...
- ASP.NET缓存中Cache过期的三种策略
原文:ASP.NET缓存中Cache过期的三种策略 我们在页面上添加三个按钮并双击按钮创建事件处理方法,三个按钮使用不同的过期策略添加ASP.NET缓存. <asp:Button ID=&quo ...