TOJ 4105 Lines Counting (树状数组)
题意:给定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 (树状数组)的更多相关文章
- TOJ 4105 Lines Counting(离线树状数组)
4105. Lines Counting Time Limit: 2.0 Seconds Memory Limit: 150000K Total Runs: 152 Accepted Ru ...
- [BZOJ4756][Usaco2017 Jan]Promotion Counting 树状数组
4756: [Usaco2017 Jan]Promotion Counting Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 305 Solved: ...
- HDU 4358 Boring counting 树状数组+思路
研究了整整一天orz……直接上官方题解神思路 #include <cstdio> #include <cstring> #include <cstdlib> #in ...
- 【十分不错】【离线+树状数组】【TOJ4105】【Lines Counting】
On the number axis, there are N lines. The two endpoints L and R of each line are integer. Give you ...
- hdu 3887 Counting Offspring dfs序+树状数组
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 2224: Boring Counting Time Limit: 3 Sec ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- HDU 5862 Counting Intersections(离散化 + 树状数组)
Counting Intersections Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- HDU 3887 Counting Offspring(DFS序+树状数组)
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
随机推荐
- 阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第5节 final关键字_1_final关键字概念与四种用法
英文的含义和程序中的含义是相同的.最终的,不可改变的
- Junit 3.8源码分析
JUnit背景介绍 JUnit是由Erich Gamma和Kent Beck 编写的一个回归测试框架(regression testing framework).Junit测试是程序员测试,即所谓的白 ...
- Linux 文件创建、插入、替换
创建文件 touch newfile.txt 插入字符串 echo "aaa" >>/newfile.txt 替换字符串 sed -i "s/aaa/ccc/ ...
- Vue 基础 day02
Vue Devtools 安装 https://chrome.google.com/webstore/search/vue%20devtools?hl=zh-CN 需要翻墙 过滤器 概念: Vue.j ...
- mysql 多表查询 以及 concat 、concat_ws和 group_concat
left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录inner join(等值连接) 只返 ...
- py学习日记
From:<python编程从入门到实践> 持续更新中... 都在代码里了 第一到七章: """ Author:CruelKing Time:7/27/201 ...
- BitMap的原理和实现
相关概念 基础类型 在java中: byte -> 8 bits -->1字节 char -> 16 bit -->2字节 short -> 16 bits --> ...
- 三:GC回收机制
jvm垃圾回收机制: jvm中有个垃圾回收线程,它是低优先级的,当虚拟机空闲或堆内存不足时,它就会去清除不可达对象. GC是如何去判断对象是否能被回收的 早期GC判断对象是否能被回收时用的引用计数法, ...
- c知识点总结2
函数 int func(int x){ //x:形式参数 .... } int main(){ .... int res=func(z); //z:实际参数 } 实参与形参具有不同的存储单元, 实参与 ...
- 前端写代码自动刷新神器Browsersync
Browsersync能让浏览器实时.快速响应您的文件更改(html.js.css.sass.less等)并自动刷新页面. 更重要的是 Browsersync可以同时在PC.平板.手机等设备下进项调试 ...