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/) 题面 有 ...
随机推荐
- asp.net 两个页面之前传递数据
.在两个表单之间传递数据 看下面的代码: 对于WebForm1: private void Page_Load(object sender, System.EventArgs e) { ArrayLi ...
- HTML4如何让一个DIV居中对齐?float输入日志标题
float:left,right clear:both 如何让一个DIV居中对齐? 第一步:设置外层的DIV的text-align:center; 第二步:设置里层的DIV的margin:auto 以 ...
- javaweb一周总结(菜鸟)
我的试用期开始了. 这是我的第一篇博客,这也是我作为java工程师的第六天,主要是为了记录一周内出现的问题以及一些工作上的解答,吐槽一句工作的确和想的不一样之后直接记录下吧. 由于拥有工作平台看不到底 ...
- shell编程001
1.shell中如何进行算术计算 A=1; B=2 (1)let C=$A+$B (2)C=$[$A+$B] (3)C=$(($A+$B)) (4)C=`expr $A + $B` (注意运算符前 ...
- scroll运用、图片悬浮
scroll 滚动条 长话短说进入正题: scrollTOP==0 内容置于顶部: scrollTOP()>=$(document).height-$(window).height 内容置于底部 ...
- PHP面试题之算法解析
面试中经常被问到会什么算法,这里整合一些常见的算法及它们的实现原理.下面的例子都是经过测试可用的,如果有什么问题请告知!! 本人小白,如果有更好的实现方式,敬请赐教,感激不尽!!!! 冒泡排序,快速排 ...
- #CI的MVC实现
CI的MVC实现 CI被标榜为一款简单易用的框架,经过一段时间的了解后,它的小而精给让我印象深刻.麻雀虽小五脏俱全,一个框架产品包含太多的特性,这篇文章就说说CI中是如何实现MVC的? 执行流程 根据 ...
- python 初学笔记 (一)
初学python第一天,希望自己真正了解计算机语言,并且做出成效. 写下学习笔记,记录学习进度,娱乐学习,不断成长. python详细介绍: python是什么?运用到哪里?有哪些在使用它? pyth ...
- jquery ajax提交及请求
jQuery.ajax({ url: dataURL, success: function(results) { var parsedJson = jQuery.parseJSON(results); ...
- MVC 创建带图片的<A></A>标签
<a href="@Url.Action("Detail", "Product", new { messageId = item.message ...