Educational Codeforces Round 10 D. Nested Segments
2 seconds
256 megabytes
standard input
standard output
You are given n segments on a line. There are no ends of some segments that coincide. For each segment find the number of segments it contains.
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of segments on a line.
Each of the next n lines contains two integers li and ri ( - 109 ≤ li < ri ≤ 109) — the coordinates of the left and the right ends of the i-th segment. It is guaranteed that there are no ends of some segments that coincide.
Print n lines. The j-th of them should contain the only integer aj — the number of segments contained in the j-th segment.
4
1 8
2 3
4 7
5 6
3
0
1
0
3
3 4
1 5
2 6
0
1
1
离散化+ 线段树或树状数组维护前缀和
想按照左端点进行排序,然后逆序 对右端点进行查询,插入.这样可以保证线段的包含关系。写的有点挫,一开始始终re后来把线段树开到8*maxn就过了。。
/* ***********************************************
Author :guanjun
Created Time :2016/3/28 15:34:10
File Name :cfedu10d.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 211000
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
struct Node{
int x,y;
int id;
}Nod[maxn];
bool cmp(Node a,Node b){
return a.x<b.x;
}
struct node{
int l,r;
ll sum;
int c;
}nod[maxn*];
int ans[maxn];
void push_up(int i){
nod[i].sum=nod[i<<].sum+nod[i<<|].sum;
}
void build(int i,int l,int r){
nod[i].l=l;
nod[i].r=r;
nod[i].c=;
if(l==r){
nod[i].sum=;
return ;
}
int mid=(l+r)/;
build(i<<,l,mid);
build(i<<|,mid+,r);
push_up(i);
}
void update(int i,int k,int x){
if(nod[i].l==k&&nod[i].r==k){
nod[i].sum+=x;
return ;
}
int mid=(nod[i].l+nod[i].r)/;
if(k<=mid)update(i<<,k,x);
else update(i<<|,k,x);
push_up(i);
}
ll quary(int i,int l,int r){
if(nod[i].l==l&&nod[i].r==r){
return nod[i].sum;
}
int mid=(nod[i].l+nod[i].r)/;
ll ans=;
if(r<=mid)ans+=quary(i<<,l,r);
else if(l>mid)ans+=quary(i<<|,l,r);
else {
ans+=quary(i<<,l,mid);
ans+=quary(i<<|,mid+,r);
}
push_up(i);
return ans;
}
int num[*maxn];
map<int,int>mp;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int n,q;
ll z;
while(cin>>n){
mp.clear();
for(int i=;i<=n;i++){
scanf("%d%d",&Nod[i].x,&Nod[i].y);
num[i]=Nod[i].x;
num[i+n]=Nod[i].y;
Nod[i].id=i;
}
sort(num+,num++(*n));
int cnt=;
for(int i=;i<=*n;i++){
mp[num[i]]=cnt;
cnt++;
}
build(,,mp[num[*n]]);
sort(Nod+,Nod++n,cmp);
for(int i=n;i>=;i--){
ans[Nod[i].id]=quary(,,mp[Nod[i].y]);
update(,mp[Nod[i].y],);
}
for(int i=;i<=n;i++){
printf("%d\n",ans[i]);
}
}
return ;
}
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 ...
- Educational Codeforces Round 10 D. Nested Segments (树状数组)
题目链接:http://codeforces.com/problemset/problem/652/D 给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间. 我是先把每个区间 ...
- CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...
- Educational Codeforces Round 10
A:Gabriel and Caterpillar 题意:蜗牛爬树问题:值得一提的是在第n天如果恰好在天黑时爬到END,则恰好整除,不用再+1: day = (End - Begin - day0)/ ...
- Educational Codeforces Round 10 A. Gabriel and Caterpillar 模拟
A. Gabriel and Caterpillar 题目连接: http://www.codeforces.com/contest/652/problem/A Description The 9-t ...
- Educational Codeforces Round 10 C. Foe Pairs 水题
C. Foe Pairs 题目连接: http://www.codeforces.com/contest/652/problem/C Description You are given a permu ...
- Educational Codeforces Round 10 B. z-sort 构造
B. z-sort 题目连接: http://www.codeforces.com/contest/652/problem/B Description A student of z-school fo ...
- Educational Codeforces Round 10 E - Pursuit For Artifacts (强联通缩点 + 回溯)
题目链接:http://codeforces.com/contest/652/problem/E 给你n个点m个边,x和y双向连接,要是z是1表示这条边上有宝藏,0则没有,最后给你起点和终点,问你要是 ...
随机推荐
- 洛谷P3327 - [SDOI2015]约数个数和
Portal Description 共\(T(T\leq5\times10^4)\)组数据.给出\(n,m(n,m\leq5\times10^4)\),求\[\sum_{i=1}^n\sum_{j= ...
- Ionic2如何下拉刷新和上拉加载
下拉刷新: <ion-content> <ion-refresher (ionRefresh)="doRefresh($event)"> <ion-r ...
- PHP实现当前文件夹下所有文件和文件夹的遍历
<?php function myScandir($dir){ static $flag=''; //设置缩进显示格式 $files = scandir($dir);//读取当前文件夹的文件 $ ...
- linux与windows查看占用端口的进程ID并杀死进程
有时候tomcat出现端口被占用,需要查出进程ID并杀死进程. 1.查找占用端口的进程ID(windows与linux一样 8005也可以加上引号 grep可以用findstr替换) 6904就 ...
- chromedriver错误信息提示
The open chrome driver window displays: Starting ChromeDriver (v2.8.241075) on port 10820 [8804:7492 ...
- 洛谷——P1746 离开中山路
P1746 离开中山路 题目背景 <爱与愁的故事第三弹·shopping>最终章. 题目描述 爱与愁大神买完东西后,打算坐车离开中山路.现在爱与愁大神在x1,y1处,车站在x2,y2处.现 ...
- iOS 内存管理实践
内存管理实践 尽管基本的概念在内存管理策略文章中简单得阐述了,但是还有一些实用的步骤让你更容易管理内存:有助于确保你的程序最大限度地减少资源需求的同时,保持可靠和强大. 使用“访问器方法”让内存管理更 ...
- Unity3d插件]EasyTouch简单使用方法
EasyTouch使用 EasyTouch 文件夹[-] 一.效果图 二.操作步骤 1.官方文档上的步骤 2.翻译一下以上的步骤 3.依据官方的这些提示.自己来做一个属于自己的人物遥感控制 对于移动平 ...
- 系统重装 如何转换GPT的磁盘格式为MBR或者反过来
使用分区助手专业版可以让磁盘在GPT和MBR之间进行转换 一般把磁盘全部格式化并清除分区,剩下的都会是可用空间,还是需要重建MBR来把磁盘转换成MBR格式的 转换会设置操作系统类型
- grunt 试用笔记
Gruntjs是JavaScript项目的构建工具,也是基于node的一个命令行工具.很多开源JS项目都是使用它搭建.如jQuery.Qunit.CanJS等.它有以下作用 合并JS文件压缩JS文件单 ...