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则没有,最后给你起点和终点,问你要是 ...
随机推荐
- 算法复习——LCT(bzoj2049洞穴勘测)
题目: Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连 ...
- 【leetcode dp】629. K Inverse Pairs Array
https://leetcode.com/problems/k-inverse-pairs-array/description/ [题意] 给定n和k,求正好有k个逆序对的长度为n的序列有多少个,0& ...
- HashMap构造函数有哪些
hashMap有4个构造函数: public HashMap(int initialCapacity, float loadFactor) public HashMap(int initialCapa ...
- 如何用Eclipse将普通的JavaWeb项目转为Maven项目
最新自己的第一个项目差不多稳定运行之后 想着将项目转为Maven项目.于是参考网上成功的将自己的普通的项目转为了maven项目,现在记录一下: 0.普通的java项目的结构如下: 1.接下来开始进行正 ...
- Servlet 2.4 规范之第二篇:Servlet接口
Servlet接口是Servlet API的最核心抽象类.所有的servlets都直接实现了这个接口,或者以更通用的方式继承了这个接口的实现类.Servlet API自带了两个实现了Servlet接口 ...
- BZOJ——2096: [Poi2010]Pilots
http://www.lydsy.com/JudgeOnline/problem.php?id=2096 Time Limit: 30 Sec Memory Limit: 162 MBSubmit: ...
- P1717 钓鱼 洛谷
https://www.luogu.org/problem/show?pid=1717 题目描述 话说发源于小朋友精心设计的游戏被电脑组的童鞋们藐杀之后非常不爽,为了表示安慰和鼓励,VIP999决定请 ...
- luogu P1886 滑动窗口(单调队列
题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...
- C# 打印日志
原理其实很简单,就是创建文件夹.创建文件.写入内容 首先判断文件夹.文件是否存在 然后再创建或者追加 不多介绍,直接上代码 public static void BuildLogFile(string ...
- java实现简单的算法
排序大的分类可以分为两种:内排序和外排序.在排序过程中,全部记录存放在内存,则称为内排序,如果排序过程中需要使用外存,则称为外排序.下面讲的排序都是属于内排序. 内排序有可以分为以下几类: (1).插 ...