【十分不错】【离线+树状数组】【TOJ4105】【Lines Counting】
来源:互联网 发布:淘宝怎么设置卖家区域 编辑:程序博客网 时间:2024/06/06 15:42
On the number axis, there are N lines. The two endpoints L and R of each line are integer. Give you M queries, each query contains two intervals: [L1,R1] and [L2,R2], can you count how many lines satisfy this property: L1≤L≤R1 and L2≤R≤R2?
Input
First line will be a positive integer N (1≤N≤100000) indicating the number of lines. Following the coordinates of the N lines' endpoints L and R will be given (1≤L≤R≤100000). Next will be a positive integer M (1≤M≤100000) indicating the number of queries. Following the four numbers L1,R1,L2 and R2 of the M queries will be given (1≤L1≤R1≤L2≤R2≤100000).
Output
For each query output the corresponding answer.
双区间查询
第一个区间就裸的[1...l1-1] [l2..r2]
[1.......r1] [l2..r2]
用vector (Q[i])维护~(i为l1-1,r1的值)
表示 [1...i][Q[i][j].l2...Q[i][j].r2] 表示上述~
第二个区间 当枚举i的时候,内循环枚举线段到s[p].x<=i,s[p].y加入a[]来维护终止点
因为起止点排好序了,
一定满足Q[i]的[1....i]
a[]树状数组来维护好l2,r2.
原理理解后 代码很简单。。
直接贴朱神的代码了
复杂度 比较难表述 但是显然是接受范围
(当初考虑l,r不好存。。开数组不好存,才知道可以表示在数组内就好)
#include <stdio.h>#include <ctype.h>#include <string.h>#include <stdlib.h>#include <limits.h>#include <math.h>#include <algorithm>#include <vector>using namespace std;const int N=100000;int a[100005];int inline lowbit(int x){return x&(-x);}void add(int p,int val){while(p<=N){a[p]=(a[p]+val);p+=lowbit(p);}}int sum(int p){int ans=0;while(p>0){ans=(ans+a[p]);p-=lowbit(p);}return ans;}struct Query{int l,r;int oriid;int orip;Query(){}Query(int a,int b,int c,int d):l(a),r(b),oriid(c),orip(d){}};vector<Query> qs[100005];int ans[100005][2];struct Pair{int x,y;Pair(){}Pair(int a,int b):x(a),y(b){}bool operator<(const Pair&b)const{return x<b.x;}}side[100005];int main(){int n,m;while(~scanf("%d",&n)){for(int i=0;i<n;i++){scanf("%d%d",&side[i].x,&side[i].y);}sort(side,side+n);for(int i=0;i<=N;i++) qs[i].clear();scanf("%d",&m);int maxq=0;for(int i=0;i<m;i++){int a,b,c,d;scanf("%d%d%d%d",&a,&b,&c,&d);qs[a-1].push_back(Query(c,d,i,0));qs[b].push_back(Query(c,d,i,1));maxq=max(maxq,(max(a,b)));}memset(a,0,sizeof(a));int sp=0;for(int i=0;i<=maxq;i++){while(sp<n&&side[sp].x==i){add(side[sp].y,1);sp++;}for(int j=0;j<qs[i].size();j++){int ans1=sum(qs[i][j].l-1);int ans2=sum(qs[i][j].r);ans[qs[i][j].oriid][qs[i][j].orip]=ans2-ans1;}}for(int i=0;i<m;i++){printf("%d\n",ans[i][1]-ans[i][0]);}} return 0;}
0 0
- 【十分不错】【离线+树状数组】【TOJ4105】【Lines Counting】
- HDU 4358 Boring counting(离线 + 树状数组)
- sdut2610---Boring Counting(离线+树状数组+离散化)
- HDU4638【离线树状数组】
- 树状数组+离线+hdu4605
- Hduoj3874 【离线+树状数组】
- hdu3874 离线树状数组
- HDU4630(树状数组,离线)
- HDU 4358 Boring counting(树的遍历+树状数组+离散化+离线处理)
- HDU 4358 Boring counting(树状数组离线操作+欧拉序列)
- HDU 4358-I - Boring counting-dfs序+离线+树状数组/线段树
- hdu 5124 lines(树状数组)
- HDU Boring counting 树状数组
- HDU3450-Counting Sequences【树状数组
- Counting Black(树状数组)
- hdu 4638 (树状数组+离线)
- hdu2492(树状数组+离线处理)
- hdu3874之离线树状数组
- 无法将网络更改为桥接状态:没有未桥接的主机网络适配器
- JAVA字符串的比较
- 红黑树的插入
- 用GPicSync软件为照片添加拍摄地点的坐标信息
- Java设计模式之备忘录模式
- 【十分不错】【离线+树状数组】【TOJ4105】【Lines Counting】
- 我的大学生涯
- IOS 开发推荐常用lib
- Android 5.1 API 22 所有sdk文件下载地址
- java初级笔记_GUI
- error C1083: 无法打开源文件:No such file or directory
- 如何判断数组是否已初始化,定义了维数
- java初级笔记_Socket
- 爬虫框架Scrapy的安装与简介