【十分不错】【离线+树状数组】【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) ,问在每个区间里所有 ...
随机推荐
- IOS开发之---触摸和手势
Touch:在与设备的多点触摸屏交互时生成. 响应者对象 响应者对象就是可以响应事件并对事件作出处理.在iOS中,存在UIResponder类,它定义了响应者对象的所有方法.UIApplication ...
- Android应用程序组件Content Provider简要介绍和学习计划
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6946067 在Android系统中,Conte ...
- 使用 React 和 Flux 创建一个记事本应用
React,来自 Facebook,是一个用来创建用户界面的非常优秀的类库.唯一的问题是 React 不会关注于你的应用如何处理数据.大多数人把 React 当做 MV* 中的 V.所以,Facebo ...
- RequireJs运行原理
在require中,根据AMD(Asynchronous Module Definition)的思想,即异步模块加载机制,其思想就是把代码分为一个一个的模块来分块加载,这样无疑可以提高代码的重用. 在 ...
- ADO.net基础学习总结(二)
将连接字符串放入配置文件中 1.添加一个“应用程序配置文件:app.config” <?xml version="1.0" encoding="utf-8" ...
- html的target用法
_blank -- 在新窗口中打开链接 _parent -- 在父窗体中打开链接 _self -- 在当前窗体打开链接,此为默认值 _top -- 在当前窗体打开链接,并替换当前的整个窗体(框架页), ...
- ios 添加多个target 管理 多个版本文件
1. 添加一个Target 这里是添加一个Test 项目 这里添加新的target Test与Release 也是同上的操作
- 沼跃鱼早已看穿了一切 C/C++
沼跃鱼早已看穿了一切 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 593 Solved: 229[Submit][Status][Web Boa ...
- C#控制台程序本地化应用(Localization)
本地化在百度百科上的解释如下: “软件本地化是指将某一产品的用户界面(UI)和辅助材料(文档资料和在线帮助菜单)从其原产国语言向另一种语言转化,使之适应某一外国语言和文化的过程.” 其原理主要是把相同 ...
- 国外大神Leo-G的 DevopsWiki
https://raw.githubusercontent.com/Leo-G/DevopsWiki/master/README.md 总结的太好了,直接把md文件贴过来好了!慢慢学习!分享给大家,觉 ...