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;
}


【十分不错】【离线+树状数组】【TOJ4105】【Lines Counting】的更多相关文章

  1. TOJ 4105 Lines Counting(离线树状数组)

    4105.   Lines Counting Time Limit: 2.0 Seconds   Memory Limit: 150000K Total Runs: 152   Accepted Ru ...

  2. 13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 2224: Boring Counting Time Limit: 3 Sec   ...

  3. SPOJ DQUERY - D-query (莫队算法|主席树|离线树状数组)

    DQUERY - D-query Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query ...

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

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

  5. HDU 4417 离线+树状数组

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. SPOJ 3267 D-query(离散化+在线主席树 | 离线树状数组)

    DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...

  7. 【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)

    pid=5654">[HDOJ 5654] xiaoxin and his watermelon candy(离线+树状数组) xiaoxin and his watermelon c ...

  8. POJ 3416 Crossing --离线+树状数组

    题意: 给一些平面上的点,然后给一些查询(x,y),即以(x,y)为原点建立坐标系,一个人拿走第I,III象限的点,另一个人拿II,IV象限的,点不会在任何一个查询的坐标轴上,问每次两人的点数差为多少 ...

  9. HDU 2852 KiKi's K-Number(离线+树状数组)

    题目链接 省赛训练赛上一题,貌似不难啊.当初,没做出.离线+树状数组+二分. #include <cstdio> #include <cstring> #include < ...

  10. CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组

    题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...

随机推荐

  1. svn之——linux下清除svn的用户名和密码

    问题:之前用的svn账号权限不够,需要使用别的账号,所以提出需求——怎么使用新的svn账号进行操作 方法一: linux下删除~/.subversion/auth即可清除之前的用户名和密码:rm -r ...

  2. MySQL Workbench导出数据库

    步骤: 1. 打开mysql workbench,进入需要导出的数据库,点击左侧栏的[Management]tab键. 2. 点选要输出的数据库 点击[Data Export] 选在要输出的数据库 选 ...

  3. sun.misc.BASE64Encoder找不到包,解决方法

    右键项目->属性->java bulid path->jre System Library->access rules->resolution选择accessible,以 ...

  4. 实现怎样支持Android重力感应器Sensor编程

    添加当重力变化时的处理函数 在创建监听器时调用的函数 doSomething(x, y, z) 是自己定义的方法. 当手机倾斜方向改变时,监听器会调用该方法. 我们要做的,就是填充该方法,用于在重力发 ...

  5. 查看linux系统的开机时间/重启历史记录

    查看linux系统的开机时间/重启历史记录1.who -b命令[root@rusky opt]# who -b ---查看最后一次(上次)系统启动的时间 system boot Dec 27 05:0 ...

  6. 3GP文件格式研究

    需要看的文档 http://www.3gpp.org/ftp/Specs/archive/26_series/ 3GPP TS 26.233 3GPP TS 26.243 3GPP TS 26.244 ...

  7. SqlServer2008数据库透明加密

    前几天研究了一下sql数据库的透明加密,记下来加深一下理解. 用脚本创建文件夹 --查文件夹有没有 EXEC master.dbo.xp_fileexist 'D:\DATA\storedcerts' ...

  8. CAShapeLayer + UIBezierPath

    UIBezierPath: UIBezierPath是在 UIKit 中的一个类,继承于NSObject,可以创建基于矢量的路径.使用此类可以定义常见的圆形.多边形等形状 .我们使用直线.弧(arc) ...

  9. iOS学习笔记-死锁deadlock理解

    1.首先看一下官方文档的解释,这个block的队列是同步执行的,不像异步,这个方法直到block执行完毕才会返回 2.主线程一旦开启,就要先把自己的代码执行完成之后,才去执行加入到主队列中的任务 De ...

  10. Z - 不容易系列之(3)―― LELE的RPG难题

    Description          人称“AC女之杀手”的超级偶像LELE最近忽然玩起了深沉,这可急坏了众多“Cole”(LELE的粉丝,即"可乐"),经过多方打探,某资深C ...