【十分不错】【离线+树状数组】【TOJ4105】【Lines Counting】
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.
#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】的更多相关文章
- TOJ 4105 Lines Counting(离线树状数组)
4105. Lines Counting Time Limit: 2.0 Seconds Memory Limit: 150000K Total Runs: 152 Accepted Ru ...
- 13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 2224: Boring Counting Time Limit: 3 Sec ...
- 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 ...
- Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...
- HDU 4417 离线+树状数组
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- SPOJ 3267 D-query(离散化+在线主席树 | 离线树状数组)
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...
- 【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)
pid=5654">[HDOJ 5654] xiaoxin and his watermelon candy(离线+树状数组) xiaoxin and his watermelon c ...
- POJ 3416 Crossing --离线+树状数组
题意: 给一些平面上的点,然后给一些查询(x,y),即以(x,y)为原点建立坐标系,一个人拿走第I,III象限的点,另一个人拿II,IV象限的,点不会在任何一个查询的坐标轴上,问每次两人的点数差为多少 ...
- HDU 2852 KiKi's K-Number(离线+树状数组)
题目链接 省赛训练赛上一题,貌似不难啊.当初,没做出.离线+树状数组+二分. #include <cstdio> #include <cstring> #include < ...
- 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) ,问在每个区间里所有 ...
随机推荐
- 单调队列-hdu-4193-Non-negative Partial Sums
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4193 题目大意: 给n个数,a0,a1,...an,求ai,ai+1,...an,a1,a2,... ...
- 那么温暖http合约,入门。
简介 年提出.经过几年的使用与发展,得到不断地完好和扩展.眼下在WWW中使用的是HTTP/1.0的第六版,HTTP/1.1的规范化工作正在进行之中,并且HTTP-NG(Next Generation ...
- 左右HttpClient上传的方法来解决中国的乱码
二手HttpClient人们都知道通过addTextBody方法来加入要上传的文本信息,可是,假设要上传中文的话.或还有中文名称的文件会出现乱码的问题,解决的方法事实上非常easy: 第一步:设置Mu ...
- UESTC-888-Absurdistan Roads(kruskal+floyd)
The people of Absurdistan discovered how to build roads only last year. After the discovery, every c ...
- 使用 AngularJS 和 ReactJS 的经验
1. React 福音 当我们的团队开始寻找一个合适的前端框架的时候,我们考虑了许多选择,最后留下两个选项 —— Angular 和 React. Angular 是目前为止最成熟的方案:它拥有一个庞 ...
- RMAN数据库恢复之控制文件和参数文件恢复
一.控制文件的恢复1.查询控制文件的路径 SQL> SELECT * FROM V$CONTROLFILE; STATUS NAME IS_RECOVERY_DEST_FILE BLOCK_SI ...
- ffmpeg的使用
-i "转换前的视频绝对路径\文件名.mp4" -movflags faststart+rtphint "转换后的视频绝对路径\文件名.mp4" 上面这条语句是 ...
- bootstrap使用中遇到的问题(二)
1.ie8不支持carousel组件, 解决方法:将jquery换为jquery1版本,具体原因不清楚~~~~~ 2.ie8不支持background-color:rgba(); 解决方法:这样写代码 ...
- 新浪微博iOS示例,登录,获取个人信息
1.导入第三方库和系统框架
- 新博客 Fighting
C3 Viso绘图 的 几种关系