题意:给定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. 【转】GLSL资料收集

    https://blog.csdn.net/u013467442/article/details/44457869 其中入门资料相当好:https://blog.csdn.net/racehorse/ ...

  2. android hidl

    1.定义.hal接口文件,如: 在vendor/sprd/interface中新建目录hello,其中定义好hidl接口,如: package vendor.sprd.hardware.hello@1 ...

  3. linux 互斥量

    互斥量(mutex)从本质上来说是一把锁,在访问共享资源前对互斥量进行加锁,在访问完后释放互斥量上的锁. 对互斥量进行加锁以后,任何其他试图再次对互斥量加锁的线程都将会被阻塞直到当前线程释放该互斥锁. ...

  4. IDEA基本设置和快捷键大全

    # IDEA基本设置 ## 设置编码格式 1. Configure - Settings - Editor - File Encodings 2. 将三个编码全部设置为UTF-8 ## 启用Ctrl+ ...

  5. Learn Python the hard way, ex45 对象、类、以及从属关系

    #!/usr/bin/python #coding:utf-8 # animal is-a object(yes,sort of sonfusing)look at the extra credit ...

  6. logstash异常

    logstash异常 1 2 3 Unrecognized VM option 'UseParNewGC' Error: Could not create the Java Virtual Machi ...

  7. 使用SqlBulkCopy 批量操作大量数据

    private void button1_Click(object sender, EventArgs e) { //1.0 构建一个内存表一定要和Users表的结构保持一致,除了自增的主键外 Dat ...

  8. git --> 工作使用流程

    [git]------git开发过程中的使用流程------[WangQi]   001.创建仓库 002.新建项目 003.初始化仓库  这一步不需要做 git init : 文件夹中会多出一个隐藏 ...

  9. oracle--单表查询

    ---单表的查询学习 --查询表的所有数据 select * from 表名;*代表所有 select * from emp; --查询表中指定字段的值 select 字段名1,字段名2,...fro ...

  10. HNUSTOJ-1696 简单验证码识别(模拟)

    1696: 简单验证码识别 时间限制: 2 Sec  内存限制: 128 MB 提交: 148  解决: 44 [提交][状态][讨论版] 题目描述 验证码是Web系统中一种防止暴力破解的重要手段.其 ...