BZOJ2957 楼房重建
2016-07-22 12:06
281 查看
题意:给定m(m <= 100000)个操作,每次将坐标轴上横坐标为x(1 <= x <= n <= 100000)的楼的高度设为y,之后回答在(0,0)能看到几个楼。
分析:
7.19考试题,当时全场基本都写得O(nm)大暴力,gzz神犇用骗分法竟然AC了,但在BZOJ上TLE了,只能说考试时候数据水了。
这题有两种做法,一种是分块,还有一种是线段树。
由于以前没写过分块,所以就尝试了尝试写分块。
分块的思路很简单,每个块内维护一个“能看见的序列”,修改时重新维护一遍当前块,查询时,用当前能看到的最小斜率在块内二分,更新答案,并更新能看到的最小斜率。
#include <cstdio>
#include <algorithm>
const int N = 100005, sz = 400;
int n,m,x,y,c[255];
double a
,b
;
int main() {
scanf("%d%d", &n, &m);
while(m--) {
scanf("%d%d", &x, &y);
int bl = (x+sz-1)/sz, ans = 0; double mx = 0;
a[x] = (double)y/x, c[bl] = 0;
for(int j=(bl-1)*sz+1;j<=bl*sz;j++) if(!c[bl]||a[j]>b[(bl-1)*sz+c[bl]]) b[(bl-1)*sz+(++c[bl])]=a[j];
for(int j = 1; j <= 250; j++) {
int k = std::upper_bound(b+(j-1)*sz+1, b+(j-1)*sz+c[j]+1, mx)-b;
ans += (j-1)*sz+c[j]-k+1;
if(k != (j-1)*sz+c[j]+1) mx = b[(j-1)*sz+c[j]];
}
printf("%d\n", ans);
}
return 0;
}
分析:
7.19考试题,当时全场基本都写得O(nm)大暴力,gzz神犇用骗分法竟然AC了,但在BZOJ上TLE了,只能说考试时候数据水了。
这题有两种做法,一种是分块,还有一种是线段树。
由于以前没写过分块,所以就尝试了尝试写分块。
分块的思路很简单,每个块内维护一个“能看见的序列”,修改时重新维护一遍当前块,查询时,用当前能看到的最小斜率在块内二分,更新答案,并更新能看到的最小斜率。
#include <cstdio>
#include <algorithm>
const int N = 100005, sz = 400;
int n,m,x,y,c[255];
double a
,b
;
int main() {
scanf("%d%d", &n, &m);
while(m--) {
scanf("%d%d", &x, &y);
int bl = (x+sz-1)/sz, ans = 0; double mx = 0;
a[x] = (double)y/x, c[bl] = 0;
for(int j=(bl-1)*sz+1;j<=bl*sz;j++) if(!c[bl]||a[j]>b[(bl-1)*sz+c[bl]]) b[(bl-1)*sz+(++c[bl])]=a[j];
for(int j = 1; j <= 250; j++) {
int k = std::upper_bound(b+(j-1)*sz+1, b+(j-1)*sz+c[j]+1, mx)-b;
ans += (j-1)*sz+c[j]-k+1;
if(k != (j-1)*sz+c[j]+1) mx = b[(j-1)*sz+c[j]];
}
printf("%d\n", ans);
}
return 0;
}
相关文章推荐
- [BZOJ2038][2009国家集训队][莫队][分块]小z的袜子
- Codeforces538F A Heap of Heaps【分块+差分求前缀和】
- 大矩阵的分块乘法及matlab实现
- Hadoop分块和分片
- bzoj-2741 L
- bzoj-3585 mex
- bzoj-2051 A Problem For Fun
- 算法马拉松8(差和问题)
- 1290 Counting Diff Pairs
- 【Violet 6】【BZOJ2724】蒲公英
- 【BZOJ4320】Homework
- 【BZOJ4216】Pig
- 【BZOJ2741】FOTILE模拟赛 L
- [2009国家集训队]小Z的袜子(hose) 分块做法
- NBUT 1457 分块
- codeforces #307 E. GukiZ and GukiZiana (分块)
- Codeforces Round #307 (Div. 2)E. GukiZ and GukiZiana(分块)
- 【分块】 CF 551 E GukiZ and GukiZiana
- Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana (分块)
- Codeforces Gym 100589A Queries on the Tree(树状数组+分块)