题意:给定N条线段,每条线段的两个端点L和R都是整数。然后给出M个询问,每次询问给定两个区间[L1,R1]和[L2,R2],问有多少条线段满足:L1≤L≤R1 , L2≤R≤R2 ?

题解,采用离线做法,先将所有线段和询问区间全部保存。然后将每个询问[L1,R1][L2,R2]拆分成两个,L1-1, [L2,R2]和 R1,[L2,R2]。x, [L2,R2]表示起点 L <= x,终点 R满足 L2 <= R <= R2的线段条数,则每个询问的答案就是 R1,[L2,R2]的值 - L1-1, [L2,R2]的值。那么下面就需要查询x,[L2,R2]的值了。

将所有线段按照起点排序;将所有新生成的询问按照x排序。之后是一遍扫描,对每个询问,循环直到线段起点L>x,否则树状数组中线段终点位置处值加1。此时计算树状数组第R2位置的前缀和减去L1-1位置处的前缀和即为答案。详见代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define N 100100
struct Line{
int s, t;
bool operator < (const Line a) const {
return s < a.s;
}
}p[N];
struct Range{
int s, id;
Line t;
bool operator < (const Range a) const {
return s < a.s;
}
}rg[*N];
int v[N];
int lb(int x){return x&-x;}
void add(int id){
while(id < N){
v[id]++;
id += lb(id);
}
}
int get(int id){
if(id == ) return ;
return v[id]+get(id-lb(id));
}
int ans[N];
int main(){
int n, m, i, j;
while(~scanf("%d", &n)){
for(i = ; i < n; i++) scanf("%d %d", &p[i].s, &p[i].t);
scanf("%d", &m);
int s1, t1, s2, t2, c = ;
for(i = ; i < m; i++){
scanf("%d %d %d %d", &s1, &t1, &s2, &t2);
rg[c].s = s1-;
rg[c].t.s = s2, rg[c].t.t = t2;
rg[c].id = c++; rg[c].s = t1;
rg[c].t.s = s2, rg[c].t.t = t2;
rg[c].id = c++;
}
sort(p, p+n);
sort(rg, rg+c);
memset(ans, ,sizeof(ans));
memset(v, , sizeof(v));
for(int i = , j = ; i < c; i++)
{
while(j < n && p[j].s <= rg[i].s)
add(p[j++].t);
int tm = get(rg[i].t.t) - get(rg[i].t.s-);
if(rg[i].id&) ans[rg[i].id/] += tm;
else ans[rg[i].id/] -= tm;
}
for(int i = ; i < m; i++) printf("%d\n", ans[i]);
}
return ;
}

TOJ 4105 Lines Counting (树状数组)的更多相关文章

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

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

  2. [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组

    4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 305  Solved: ...

  3. HDU 4358 Boring counting 树状数组+思路

    研究了整整一天orz……直接上官方题解神思路 #include <cstdio> #include <cstring> #include <cstdlib> #in ...

  4. 【十分不错】【离线+树状数组】【TOJ4105】【Lines Counting】

    On the number axis, there are N lines. The two endpoints L and R of each line are integer. Give you ...

  5. hdu 3887 Counting Offspring dfs序+树状数组

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

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

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

  7. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  8. HDU 5862 Counting Intersections(离散化 + 树状数组)

    Counting Intersections Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  9. HDU 3887 Counting Offspring(DFS序+树状数组)

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

随机推荐

  1. 史上最详细的XGBoost实战

    史上最详细的XGBoost实战 0. 环境介绍 Python 版 本: 3.6.2 操作系统 : Windows 集成开发环境: PyCharm 1. 安装Python环境 安装Python 首先,我 ...

  2. C++中的集合和字典

    https://blog.csdn.net/sinat_39037640/article/details/74080509

  3. HttpWebRequest、WebClient、RestSharp、HttpClient区别和用途

    HttpWebRequest 已经不推荐直接使用了,这已经作为底层机制,不适合业务代码使用,比如写爬虫的时候WebClient 不想为http细节处理而头疼的coder而生,由于内部已经处理了通用设置 ...

  4. pycharm中git配置(coding.net为例)

    1.在coding.net注册一个账号 2.登陆coding.net 3.新建项目->输入项目名称.项目描述->初始化仓库选择readme.md并且添加一个appachev2的开源许可证- ...

  5. log4j 不输出mybatis的sql

    有可能导入多个日志工具jar,导致mybatis不知道使用哪种日志.修改你的mybatis配置,添加setting,指定日志类型为log4j. <configuration> <!- ...

  6. Delphi XE2 之 FireMonkey 入门(6) - TLine、TEllipse、TCircle、TPie、TArc、TRectangle、TRoundRect、TCalloutRectangle

    它们都是继承自 TShape 类, 共同拥有如下属性: Fill            : TBrush;      //填充 Stroke          : TBrush;      //边线( ...

  7. python2.7+appium第一个脚本(使用夜神模拟器)

    搭建好环境后,可以开始准备脚本的编写工作 目录 1.安装夜神模拟器 2.使用uiautomatorviewer定位 3.运行第一个脚本 1.安装夜神模拟器 第一步:官网下载夜神模拟器,完成安装 双击下 ...

  8. 类HashSet

    /* * Collection * * List * 有序(存储顺序和取出顺序一致),可以重复 * * Set * 无序(存储顺序和取出顺序不一致),唯一 * * */ import java.uti ...

  9. python3.5+django2.0快速入门(二)

    昨天写了python3.5+django2.0快速入门(一)今天将讲解配置数据库,创建模型,还有admin的后台管理. 配置数据库 我们打开mysite/mysite/settings.py这个文件. ...

  10. stl应用

    http://codeforces.com/problemset/problem/1154/E E. Two Teams time limit per test 2 seconds memory li ...