TOJ 4105 Lines Counting(离线树状数组)
4105. Lines Counting
Time Limit: 2.0 Seconds Memory Limit: 150000K
Total Runs: 152 Accepted Runs: 47
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.
Sample Input
3
1 3
2 4
3 5
2
1 2 3 4
1 4 5 6
Sample Output
2
1
题目链接:TOJ 4105
题意就是在给你N条在X轴上的线段,求左端点在L1~R1且右端点在L2~R2的线段条数,其实这题跟NBUT上一道题很像,问你在区间L1~R1中,值在L2~R2中有几个数,只是这题在起点计数回退时可能多退几个位子,因为线段的起点和终点坐标可能有重复的,都不能算进去,因此要用while语句来操作。离线树状数组是什么个意思呢?就是要满足区间减法,这样的话就可以这样计数:在遇到询问左端点时时减去$[起点,询问区间左端点-1]$对该询问产生的影响值count1,在遇到询问右端点时加上$[起点,询问区间右端点]$对该询问的影响值count2,这样可以发现count1其实是count2的子集,一加一减一定会把count1抵消掉,只留下刚好符合询问区间的答案了。画了个图助于理解

代码:
#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 100010;
struct Line
{
int l, r;
bool operator<(const Line &rhs)const
{
if (l != rhs.l)
return l < rhs.l;
return r < rhs.r;
}
};
struct query
{
int k, r1, r2, flag, id;
query(int _k = 0, int _r1 = 0, int _r2 = 0, int _flag = 0, int _id = 0): k(_k), r1(_r1), r2(_r2), flag(_flag), id(_id) {}
bool operator<(const query &rhs)const
{
return k < rhs.k;
}
};
Line line[N];
query Q[N << 1];
int T[N], ans[N]; void init()
{
CLR(T, 0);
CLR(ans, 0);
}
void add(int k, int v)
{
while (k < N)
{
T[k] += v;
k += (k & -k);
}
}
int getsum(int k)
{
int ret = 0;
while (k)
{
ret += T[k];
k -= (k & -k);
}
return ret;
}
int main(void)
{
int n, m, i;
while (~scanf("%d", &n))
{
init();
for (i = 0; i < n; ++i)
scanf("%d%d", &line[i].l, &line[i].r);
sort(line, line + n);
scanf("%d", &m);
int qcnt = 0;
for (i = 0; i < m; ++i)
{
int l1, r1, l2, r2;
scanf("%d%d%d%d", &l1, &r1, &l2, &r2);
Q[qcnt++] = query(l1, l2, r2, 0, i);
Q[qcnt++] = query(r1, l2, r2, 1, i);
}
sort(Q, Q + qcnt);
int x = 0;
for (i = 0; i < qcnt; ++i)
{
while (line[x].l <= Q[i].k && x < n)
add(line[x++].r, 1);
if (Q[i].flag)
ans[Q[i].id] += getsum(Q[i].r2) - getsum(Q[i].r1 - 1);
else
{
while (line[x - 1].l >= Q[i].k && x - 1 >= 0)
{
add(line[x - 1].r, -1);
--x;
}
ans[Q[i].id] -= getsum(Q[i].r2) - getsum(Q[i].r1 - 1);
while (line[x].l <= Q[i].k && x < n)
{
add(line[x].r, 1);
++x;
}
}
}
for (i = 0; i < m; ++i)
printf("%d\n", ans[i]);
}
return 0;
}
TOJ 4105 Lines Counting(离线树状数组)的更多相关文章
- TOJ 4105 Lines Counting (树状数组)
题意:给定N条线段,每条线段的两个端点L和R都是整数.然后给出M个询问,每次询问给定两个区间[L1,R1]和[L2,R2],问有多少条线段满足:L1≤L≤R1 , L2≤R≤R2 ? 题解,采用离线做 ...
- 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 < ...
随机推荐
- 问题 M: 克隆玩具
题目描述 你只有一个A类型玩具,现在有个有两种功能的机器:1. 加工一个A类型的玩具能够再得到一个A类型的玩具和一个B类型的玩具.2. 加工一个B类型的玩具,能得到两个B类型的玩具. 问经过多次加工之 ...
- frombuffer的用法
函数原型为:numpy.ma.frombuffer(buffer, dtype=float, count=-1, offset=0) import numpy s = 'hello world' pr ...
- C#继承机制 访问与隐藏基类成员
(1) 访问基类成员 通过base 关键字访问基类的成员: 调用基类上已被其他方法重写的方法. 指定创建派生类实例时应调用的基类构造函数. 基类访问只能在构造函数.实例方法或实例属性访问器中进 ...
- Spring Boot 2.x零基础入门到高级实战教程
一.零基础快速入门SpringBoot2.0 1.SpringBoot2.x课程全套介绍和高手系列知识点 简介:介绍SpringBoot2.x课程大纲章节 java基础,jdk环境,maven基础 2 ...
- 2017年9月11日 梁勇 java教材 编程练习题 第二章 2.15 键盘 读取两个点的坐标值(小数),控制台输出两点间距离。
package com.swift; import java.util.Scanner; public class PToP { public static void main(String[] ar ...
- CSS的垂直居中和水平居中总结
内联元素居中方案 水平居中设置: 行内元素 设置 text-align:center: Flex布局 设置display:flex;justify-content:center;(灵活运用) 垂直居中 ...
- js中的||、&&与!用法
&&和||在JQuery源代码内尤为使用广泛,由网上找了些例子作为参考,对其用法研究了一下: 1. && function a(){ alert("a&quo ...
- 网络流_Edmond-Karp算法、Dinic算法
转载:网络流基础篇——Edmond-Karp算法 BY纳米黑客 网络流的相关定义: 源点:有n个点,有m条有向边,有一个点很特殊,只出不进,叫做源点. 汇点:另一个点也很特殊, ...
- APCInject
#include <iostream> #include <Windows.h> #include <TlHelp32.h> using namespace std ...
- 第二篇:ssh.invoke_shell() 切换root出现的新问题
接上一篇:按照上一篇的方式,在没有对ssh.invoke_shell()执行后的登录提示符进行判断的话,那边有部分机器就回因为返回为空导致程序卡死. 正常机器 ssh.recv(9999) 命令返 ...