zoj 3299(区间改动+离散化)
题意:有n个由小木块组成的长条木块要掉下来。给出木块的左右区间,然后有给了m个木板的左右区间和高度用来接住木块,由于木块是由小木块接触组成的,也就是木板能够接住一部分的木块。剩下的会继续掉落,问最后每一个木板上有多少个小木块。
题解:这道题用线段树可解,还有还有一个比較机智的做法。
先说线段树,左右区间到3×1e7,假设用线段树解决须要离散化。
把木板从低到高排序后用一个线段树flag维护每一个区间相应的木板编号。这样高的木板就能够覆盖低木板的编号,然后用还有一个线段树sum维护每一个长条木块在每一个区间内的数量。由于有可能n个长条木块区间有重叠。所以一个区间内不会仅仅有一排小木块,那么每更新一个长条木块,相应的区间的长条木块数量加1。
最后查询每一个区间全部的小木块数量就是相应的长条木块数量乘区间长度。一開始一直MLE,然后把离散用的map换成二分查找就水过去了。
下面是线段树做法的代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 100001;
struct Board {
int l, r, h, id;
}boa[N];
int n, m, l[N], r[N];
long long res[N];
int sum[N << 4], flag[N << 4];
vector<int> a;
bool cmp(const Board& a, const Board& b) {
return a.h < b.h;
}
void pushdown(int k) {
if (flag[k]) {
flag[k * 2] = flag[k * 2 + 1] = flag[k];
flag[k] = 0;
}
if (sum[k]) {
sum[k * 2] += sum[k];
sum[k * 2 + 1] += sum[k];
sum[k] = 0;
}
}
void modify1(int k, int left, int right, int l1, int r1, int x) {
if (l1 <= left && right <= r1) {
flag[k] = x;
return;
}
pushdown(k);
int mid = (left + right) / 2;
if (l1 < mid)
modify1(k * 2, left, mid, l1, r1, x);
if (r1 > mid)
modify1(k * 2 + 1, mid, right, l1, r1, x);
}
void modify2(int k, int left, int right, int l1, int r1) {
if (l1 <= left && right <= r1) {
sum[k]++;
return;
}
pushdown(k);
int mid = (left + right) / 2;
if (l1 < mid)
modify2(k * 2, left, mid, l1, r1);
if (r1 > mid)
modify2(k * 2 + 1, mid, right, l1, r1);
}
void query(int k, int left, int right) {
if (flag[k]) {
res[flag[k]] += (long long)sum[k] * (a[right] - a[left]);
return;
}
if (left + 1 == right)
return;
pushdown(k);
int mid = (left + right) / 2;
query(k * 2, left, mid);
query(k * 2 + 1, mid, right);
}
int main() {
while (scanf("%d%d", &n, &m) == 2) {
a.clear();
memset(sum, 0, sizeof(sum));
memset(flag, 0, sizeof(flag));
memset(res, 0, sizeof(res));
for (int i = 1; i <= n; i++) {
scanf("%d%d", &l[i], &r[i]);
a.push_back(l[i]);
a.push_back(r[i]);
}
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &boa[i].l, &boa[i].r, &boa[i].h);
boa[i].id = i;
a.push_back(boa[i].l);
a.push_back(boa[i].r);
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
int cnt = a.size();
sort(boa + 1, boa + 1 + m, cmp);
for (int i = 1; i <= m; i++) {
int pos1 = lower_bound(a.begin(), a.end(), boa[i].l) - a.begin();
int pos2 = lower_bound(a.begin(), a.end(), boa[i].r) - a.begin();;
modify1(1, 0, cnt - 1, pos1, pos2, boa[i].id);
}
for (int i = 1; i <= n; i++) {
int pos1 = lower_bound(a.begin(), a.end(), l[i]) - a.begin();
int pos2 = lower_bound(a.begin(), a.end(), r[i]) - a.begin();;
modify2(1, 0, cnt - 1, pos1, pos2);
}
query(1, 0, cnt - 1);
for (int i = 1; i <= m; i++)
printf("%lld\n", res[i]);
printf("\n");
}
return 0;
}
还有一种做法是看别人的题解的,速度又快代码又短(就喜欢写代码短的╮(╯▽╰)╭),不须要离散化。把全部木块和木板所在区间从左到右扫描一边,结合绘图非常好理解。
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std;
const int N = 100005;
struct Board {
int h, l, r;
long long num;
}boa[N << 2];
struct Node {
int st, id, v;//区间起点。区分木块还是木板。区分左右端点
}node[N << 2];
int n, m;
map<int,int> mp;//存当前处理区间内全部的木板,左值是木板高度,右值是木板相应编号
bool cmp(const Node& a, const Node& b) {
if (a.st != b.st)
return a.st < b.st;
return a.v < b.v;
}
int main() {
while (scanf("%d%d", &n, &m) == 2) {
mp.clear();
int cnt = 0, l, r;
for (int i = 1; i <= n; i++) {
scanf("%d%d", &l, &r);
node[++cnt].st = l, node[cnt].id = 0, node[cnt].v = 1;
node[++cnt].st = r, node[cnt].id = 0, node[cnt].v = -1;
}
for (int i = 1; i <= m; i++) {
scanf("%d%d%d", &boa[i].l, &boa[i].r, &boa[i].h);
boa[i].num = 0;
node[++cnt].st = boa[i].l, node[cnt].id = i, node[cnt].v = 1;
node[++cnt].st = boa[i].r, node[cnt].id = i, node[cnt].v = -1;
}
sort(node + 1, node + 1 + cnt, cmp);
int pre = node[0].st, cnt1 = 0;//当前处理区间起点,当前区间的长条木块数量
for (int i = 1; i <= cnt; i++) {
if (mp.rbegin() != mp.rend()) {//map自己主动排序。所以map里最后一个值一定是最大的。也就是最高的木板
int id = mp.rbegin() -> second;//右值是编号
boa[id].num += (long long)(node[i].st - pre) * cnt1;//小木块数量是长条木块乘区间长度
}
if (!node[i].id) {//当前处理区间是长条木块
if (node[i].v == 1)
cnt1++;//长条木块数量加1
else
cnt1--;
}
else {//木板
if (node[i].v == 1)//左端点增加木板
mp[boa[node[i].id].h] = node[i].id;
else//右端点去掉木板
mp.erase(boa[node[i].id].h);
}
pre = node[i].st;//更新处理区间起点
}
for (int i = 1; i <= m; i++)
printf("%lld\n", boa[i].num);
printf("\n");
}
return 0;
}zoj 3299(区间改动+离散化)的更多相关文章
- poj 2528(区间改动+离散化)
题意:有一个黑板上贴海报.给出每一个海报在黑板上的覆盖区间为l r,问最后多少个海报是可见的. 题解:由于l r取值到1e7,肯定是要离散化的,但普通的离散化会出问题.比方[1,10],[1,4],[ ...
- ZOJ 3299 线段树 离散化
本来是个很简单的题目,难住我的主要是这么几点 1.它所有的点都是坐标,不是实际的砖块,1,3指的是1-2 2-3的砖块...后来就是用1 代表1-2 ,2代表2-3.....,这样的话,每次读入的数据 ...
- hiho1079 线段树区间改动离散化
题目链接: hihocoder1079 代码: #include<iostream> #include<cstdio> #include<cstring> #inc ...
- POJ-2528 Mayor's posters (线段树区间更新+离散化)
题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...
- POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)
题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...
- ZOJ 2301 Color the Ball 线段树(区间更新+离散化)
Color the Ball Time Limit: 2 Seconds Memory Limit: 65536 KB There are infinite balls in a line ...
- hiho一下21周 线段树的区间修改 离散化
离散化 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在回国之后,重新过起了朝7晚5的学生生活,当然了,他们还是在一直学习着各种算法~ 这天小Hi和小Ho ...
- POJ 2528 Mayor's posters(线段树/区间更新 离散化)
题目链接: 传送门 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Description The citizens of By ...
- POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 59239 Accepted: 17157 ...
随机推荐
- OpenGl的源程序,运行就提示,"计算机丢失 glut32.dll文件"
转自:http://www.cppblog.com/longzxr/archive/2009/12/04/102565.html?opt=admin 今天调试OpenGl的源程序,编译通过,但一运行就 ...
- nolock的使用
在SQL Server 2005数据库查询时,为了提高查询的性能,我们往往会在表后面加一个nolock,或者是with(nolock),让数据库在查询时不锁定表,从而提高查询的速度.本文我们就介绍SQ ...
- Android 4.4 KitKat升级率已经接近18%(2014-07-09 07:29)
腾讯数码讯(编 译:张秀梅)按照惯例, 每个月的第一个星期的星期一谷歌都会发布最新一期Android版本分布图.从去年十月末谷歌发布Android 4.4 KitKat以来,截止到目前为止Androi ...
- 给文件夹添加Everyone用户
DOC命令 C# code 1. cacls C:dming /g everyone:f /e /t 这样可以添加 2. cacls C:Program Files客友软件 /g everyone:f ...
- 【Python】Python Mako模板使用
参考资料: Mako Templates for Python官网:http://www.makotemplates.org/ Python模板库Mako的用法:http://my.oschina.n ...
- 学习笔记4-Action参数绑定
参数绑定功能默认是开启的,其原理是把URL中的参数(不包括模块.控制器和操作名)和操作方法中的参数进行绑定. 要启用参数绑定功能,首先确保你开启了URL_PARAMS_BIND设置: 'URL_PAR ...
- Python socket – network programming tutorial
原文:https://www.binarytides.com/python-socket-programming-tutorial/ --------------------------------- ...
- docker下搭建gitlab
[root@localhost ~]# docker run \ > --name='gitlab' \ > -itd \ > --link gitlab_mysql:mysql \ ...
- 算法笔记_040:二进制幂(Java)
目录 1 问题描述 2 解决方案 2.1 从左至右二进制幂 2.2 从右至左二进制幂 1 问题描述 使用n的二进制表示,计算a的n次方. 2 解决方案 2.1 从左至右二进制幂 此方法计算a的n次 ...
- Python list替换元素
替换直接对应位置赋值 假设现在班里仍然是3名同学: >>> L = ['Adam', 'Lisa', 'Bart'] 现在,Bart同学要转学走了,碰巧来了一个Paul同学,要更新班 ...