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. hdu 3478 Catch(染色 dfs 或 bfs )

    Problem Description A thief is running away! We can consider the city to N–. The tricky thief starts ...

  2. C,C#,C++中&&和||,&和|的联系和区别

    本文来自:http://www.cnblogs.com/GT_Andy/archive/2010/03/30/1921805.html 两者计算结果相同(针对各自的运算对象),只是性能上有差别而已. ...

  3. swift中通知的使用

    1.发通知.(以这条通知为例,通知名字:gameOverNotification.通知参数:title) NSNotificationCenter.defaultCenter().postNotifi ...

  4. Swift中的设计模式

    设计模式(Design Pattern)是 对软件设计中普遍存在的各种问题,所提出的解决方案.这个术语是由埃里希·伽玛等人(Erich Gamma,Richard Helm,Ralph Johnson ...

  5. Qt开始学习的一些问题

    1.需要将qmake.moc和qvfb的路径加入到系统的环境变量: qmake.moc:export PATH=$PATH:/usr/local/Trolltech/QtEmbedded-4.6.1- ...

  6. android使用友盟第三方自动更新/手动更新

    最近项目用到自动更新,发现一个方便快捷的第三方组件非常好用,只需要几行代码搞定,非常适合新手使用.分享给大家一起学习. 本文档帮助您以最快速最简洁的方式集成使用自动更新组件. 一.申请友盟开发者账号创 ...

  7. 【Android 错误记录】installation error: INSTALL_PARSE_FAILED_NO_CERTIFICATES 错误

    在运行android应用的时候报出了如题的错误: installation error: INSTALL_PARSE_FAILED_NO_CERTIFICATES 原因之一:在src源文件目录下面,有 ...

  8. isNUll ,对于sql中的一个表的description的NULL和空格的处理

    select client.clientname ,description,'client2'= case when client.Description IS NULL then  client.c ...

  9. #pragma 预处理指令

    Linux C 编程一站式学习 #pragma 预处理指示供编译器实现一些非标准的特性,C 标准没有规定 #pragma 后面应该写什么以及起什么作用,由编译器自己规定.有的编译器用 #pragma ...

  10. C++有没有string转化int的函数,怎样转换

    有两种方法1. c++中string到int的转换 1) 在C标准库里面,使用atoi: #include <cstdlib>#include <string> std::st ...