题目链接

参考博客

题意:

  给n个线段,对于每个线段问它覆盖了多少个线段。

思路:

  由于线段端点是在2e9范围内,所以要先离散化到2e5内(左右端点都离散化了,而且实际上离散化的范围是4e5),然后对右端点升序排序:

  例如 2 3

     5 6

     4 7

     1 8

  这样的话,如果对i<j,a[ i ].l >= a[ j ].l ,那么第 j 组一定包含了第 i 组,算完第一组sum(3)-sum(2-1),把a[1].l加入到树状数组中,再算第二组,以此类推算到第三组时,sum(7)=2(1~7的和是2,因为前面加了两个数  2和5),sum(4-3)=sum(3)=1(因为前面加入的数中只有一个2 是在范围1~3的) ,所以第三组的答案是2-1=1 。

#include<iostream>
#include<cstdio>
#include <cctype>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<string>
#include<cmath>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<map>
using namespace std;
#define ll long long
#define mem(a,x) memset(a,x,sizeof(a))
#define se second
#define fi first
const ll mod=;
const int INF= 0x3f3f3f3f;
const int N=4e5+; int n,cnt=;
int c[N],ans[N],b[N*];
struct node
{
int l,r,id;
}a[N]; bool cmp1(node x,node y)
{
return x.r<y.r || x.r==y.r&&x.l<y.l;
} int lowbit(int x){
return x&-x;
} void add(int x,int k)
{
for(int i=x;i<=n*;i+=lowbit(i)) c[i]+=k;
}
int sum(int x)
{
int sum=;
for(int i=x;i>;i-=lowbit(i)) sum+=c[i];
return sum;
}
int main()
{
cin>>n;
for(int i=;i<=n;i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
a[i].id=i;
b[++cnt]=a[i].l;
b[++cnt]=a[i].r;
}
sort(b+,b++cnt);
for(int i=;i<=n;i++)
{
a[i].l=lower_bound(b+,b++cnt,a[i].l)-b;
a[i].r=lower_bound(b+,b++cnt,a[i].r)-b;
}
sort(a+,a++n,cmp1); for(int i=;i<=n;i++)
{
ans[a[i].id]=sum(a[i].r)-sum(a[i].l -);
add(a[i].l, );
}
for(int i=;i<=n;i++)
cout<<ans[i]<<endl;
}

D. Nested Segments(树状数组、离散化)的更多相关文章

  1. Educational Codeforces Round 10 D. Nested Segments (树状数组)

    题目链接:http://codeforces.com/problemset/problem/652/D 给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间. 我是先把每个区间 ...

  2. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

  4. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  5. Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化

    D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...

  6. CodeForces-652D:Nested Segments(树状数组+离散化)

    You are given n segments on a line. There are no ends of some segments that coincide. For each segme ...

  7. BZOJ-1227 虔诚的墓主人 树状数组+离散化+组合数学

    1227: [SDOI2009]虔诚的墓主人 Time Limit: 5 Sec Memory Limit: 259 MB Submit: 914 Solved: 431 [Submit][Statu ...

  8. POJ 2299 树状数组+离散化求逆序对

    给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...

  9. [HDOJ4325]Flowers(树状数组 离散化)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4325 关于离散化的简介:http://blog.csdn.net/gokou_ruri/article ...

  10. Bzoj 1901: Zju2112 Dynamic Rankings 主席树,可持久,树状数组,离散化

    1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6321  Solved: 2628[Su ...

随机推荐

  1. element ui + sortablejs实现表格的行列拖拽

    <template> <div class="container"> <el-table :data="tableData" bo ...

  2. CentOS下安装Tomcat

    CentOS版本:CentOS-7-x86_64-Minimal-1810 1.安装JDK 详情查看:CentOS下安装JDK-rpm文件.CentOS安装JDK-tar.gz文件 2.下载tomca ...

  3. python数据分析2之numpy

    源代码 # -*- coding: utf-8 -*- """ Spyder Editor This is a temporary script file. " ...

  4. td宽度自适应 窄的地方自动收缩

    .layui-table td,.layui-table th { white-space: nowrap; width: auto; min-width: 0px!important; } 不换行! ...

  5. a++与++a,谈谈C++的参数传递

    先看一段代码: #include<iostream> using namespace std; void func(int a, int b) { cout << a < ...

  6. Python之虚拟环境virtualenv、pipreqs生成项目依赖第三方包

    virtualenv简介 含义: virtual:虚拟,env:environment环境的简写,所以virtualenv就是虚拟环境,顾名思义,就是虚拟出来的一个新环境,比如我们使用的虚拟机.doc ...

  7. JS实现简单的图片透明度循环变化(渐变)

    找了好多,都是由100到0就结束了,到头来自己魔改,以下就是源码. div中加入img,js添加函数,完事(调用),取名后面加个1是为了避免冲突 <!DOCTYPE HTML> <h ...

  8. 客开监控(BE/UI/BP)插件停用与启用

    1.单据界面右键属性,获取当前客开监控页面URL连接:http://172.16.168.15/U9/erp/display.aspx?lnk=UFSoft.UBF.Cust.CustManager& ...

  9. php GD 和图像处理函数, 用 STHUPO.TTF 字体向图像写入文本

    php GD 和图像处理函数,   用  STHUPO.TTF 字体向图像写入文本 注意: 01)   imagettftext() 这个函数不能使用相对路径, 要想使用相对路径要先使用  puten ...

  10. H5页面跳转与传值

    页面之间的跳转经常使用a标签,使用mvc框架的都是通过访问controller的请求方法,返回请求页面.但本次开发,前端与后台完全分离,前端APP使用HBuider来开发,后台数据就无法使用mvc框架 ...