题意:给定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. Oracle JET(二)Oracle JET使用

    Oracle JET 开发分为 Web 应用程序开发和移动应用程序开发(WebApp). Oracle JET Web 应用程序开发入门: 使用方法三种: 使用 Oracle JET Yeoman 生 ...

  2. React-Native 之 GD (十三)数据持久化(realm) 及 公共Cell

    1.数据持久化 数据持久化是移动端的一个重要部分,刚发现 Realm 原来已经支持 React-Native 了 步骤一: 引入 realm $ npm install realm --save 步骤 ...

  3. Linux添加目录到环境变量以及添加Sublime Text到环境变量

    本文主要介绍了Linux添加目录到环境变量以及添加Sublime Text到环境变量,通过具体的解释说明,让我们从中学到Linux添加目录到环境变量以及添加Sublime Text到环境变量的精髓所在 ...

  4. 20160513--js 弹出窗口带有iframe控件 备忘

    需要引用JQuery. /*! * 主 题:<页面弹出窗口> * 说 明:用于页面弹出的窗口. * 功能描述: * 1.生成弹出窗口,窗口内包括iframe控件: * 2.窗口弹出时,生成 ...

  5. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第1节 Scanner类_1-API概述和使用步骤

    官方翻译的中文版本

  6. 测开之路九十八:js变量和语句

    这里为了方便调试,在jsbin网站上面编写js脚本:https://jsbin.com/?js,console 可以点击增加/减少对应展示分页,Console为控制台部分,Output为页面部分 变量 ...

  7. Week13 - 376. Wiggle Subsequence

    Week13 - 376. Wiggle Subsequence A sequence of numbers is called a wiggle sequence if the difference ...

  8. [2019杭电多校第一场][hdu6578]Blank(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6578 计数问题想到dp不过分吧... dp[i][j][k][w]为第1-i位置中4个数最后一次出现的 ...

  9. opencv中画圆circle函数和椭圆ellipse函数

    1.      void ellipse(InputOutputArray img, Point center, Size axes, double angle, double startAngle, ...

  10. BZOJ 1100 &&luogu 3454(计算几何+KMP)

    题面 给定一个多边形,求对称轴数量. 分析 初看这似乎是一道计算几何的题目,但是如果暴力枚举对称轴,再去判断对称轴两边的边和角是否相等,时间复杂度为\(O(n^2)\),显然会TLE 问题转换 顺时针 ...