51nod1287(二分/线段树区间最值&单点更新)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1287
题意:中文题诶~
解法1:b[i] 存储 max(a[0], ....., a[i]),显然 b 是单调不减的,所以直接二分 x,再更新 a 和 b 数组即可;
代码:
#include <iostream>
#include <stdio.h>
using namespace std; const int MAXN = 5e4 + ;
int a[MAXN], b[MAXN], x; int main(void){
int n, m;
scanf("%d%d", &n, &m);
for(int i = ; i < n; i++){
scanf("%d", &a[i]);
if(i == ) b[i] = a[i];
else b[i] = max(b[i - ], a[i]);
}
while(m--){
scanf("%d", &x);
if(x <= a[] || x > b[n - ]) continue;
int pos = lower_bound(b, b + n, x) - b;
a[pos - ]++;
b[pos - ] = max(b[pos - ], a[pos - ]);
}
for(int i = ; i < n; i++){
printf("%d\n", a[i]);
}
return ;
}
解法2:用线段树维护区间最大值,再更新一下 a 数组即可;
代码:
#include <iostream>
#include <stdio.h>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std; const int MAXN = 5e4 + ;
int Max[MAXN << ];
int a[MAXN]; void push_up(int rt){
Max[rt] = max(Max[rt << ], Max[rt << | ]);
} void build(int l, int r, int rt){
if(l == r){
Max[rt] = a[l];
return;
}
int mid = (l + r) >> ;
build(lson);
build(rson);
push_up(rt);
} void update(int p, int x, int l, int r, int rt){
if(l == r){
Max[rt] += x;
return;
}
int mid = (l + r) >> ;
p <= mid ? update(p, x, lson) : update(p, x, rson);
push_up(rt);
} int query(int x, int l, int r, int rt){
if(l == r) return l;
int mid = (l + r) >> ;
return Max[rt << ] >= x ? query(x, lson) : query(x, rson);
} int main(void){
int n, m, x;
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
}
build(, n , );
while(m--){
scanf("%d", &x);
if(x > Max[] || x <= a[]) continue;
int index = query(x, , n, );
a[index - ] += ;
update(index - , , , n, );
}
for(int i = ; i <= n; i++){
printf("%d\n", a[i]);
}
return ;
}
51nod1287(二分/线段树区间最值&单点更新)的更多相关文章
- HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧)
HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧) 题意分析 题目大意:一个h*w的公告牌,要在其上贴公告. 输入的是1*wi的w值,这些是公告的尺寸. 贴公告 ...
- 【线段树区间最值单点更新模板】BNUOJ 52965 E Excellent Engineers
http://acm.bnu.edu.cn/v3/external/gym/101512.pdf #include<bits/stdc++.h> using namespace std; ...
- hdoj1754 I Hate It【线段树区间最大值维护+单点更新】
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 【bzoj4695】最假女选手 线段树区间最值操作
题目描述 给定一个长度为 N 序列,编号从 1 到 N .要求支持下面几种操作:1.给一个区间[L,R] 加上一个数x 2.把一个区间[L,R] 里小于x 的数变成x 3.把一个区间[L,R] 里大于 ...
- 【bzoj4355】Play with sequence 线段树区间最值操作
题目描述 维护一个长度为N的序列a,现在有三种操作: 1)给出参数U,V,C,将a[U],a[U+1],...,a[V-1],a[V]都赋值为C. 2)给出参数U,V,C,对于区间[U,V]里的每个数 ...
- 【hdu5306】Gorgeous Sequence 线段树区间最值操作
题目描述 给你一个序列,支持三种操作: $0\ x\ y\ t$ :将 $[x,y]$ 内大于 $t$ 的数变为 $t$ :$1\ x\ y$ :求 $[x,y]$ 内所有数的最大值:$2\ x\ y ...
- hdu6070(分数规划/二分+线段树区间更新,区间最值)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意: 给出一个题目提交序列, 从中选出一个正确率最小的子串. 选中的子串中每个题目当且仅当最 ...
- cf834D(dp+线段树区间最值,区间更新)
题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...
- HDU 4819 Mosaic (二维线段树&区间最值)题解
思路: 二维线段树模板题,马克一下,以后当模板用 代码: #include<cstdio> #include<cmath> #include<cstring> #i ...
随机推荐
- 编写你的第一个web应用程序1
在shell中运行以下命令来检查django是否已安装及其版本 python -m django --version 如果django已经安装,你应该看到安装的版本号,如果还没有安装,你会看到一个‘n ...
- ZOJ - 4016 Mergeable Stack 【LIST】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 题意 模拟栈的三种操作 第一种 push 将指定元素压入指 ...
- 《CSS权威指南(第三版)》---第六章 文本属性
本章主要的内容是: 1.文本缩进: text-indent.行内元素无法缩进,一般用左内边距或外边距来创造这种效果. 2.文本对齐:text-align .只应用于块状元素. 3.行高:一般line- ...
- hihocoder #1040 矩形判断(计算几何问题 给8个点的坐标,能否成为一个矩形 【模板思路】)
#1040 : 矩形判断 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 给出平面上4条线段,判断这4条线段是否恰好围成一个面积大于0的矩形. 输入 输入第一行是一个整数T ...
- Too many open files解决方案及原理
以下是我解决Too many open files异常时学习的知识的理解和总结,如有不正确指出,敬请指出! 此问题中文搜索雷同,你可以尝试以下关键字:"file descriptor lea ...
- 使用gdb调试c/c++代码
转自 http://blog.csdn.net/haoel/article/details/2879 GDB概述———— GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具.或许,各位比较 ...
- 用gdisk调整gpt/ext4分区大小
主机: CentOS release 6.4 (Final) 目的:从/home分区分出100G来创建新分区/vm 参考: http://ryanclouser.com/?p=66 http://fa ...
- C#高性能Socket服务器SocketAsyncEventArgs的实现(IOCP)
网址:http://blog.csdn.net/zhujunxxxxx/article/details/43573879 引言 我一直在探寻一个高性能的Socket客户端代码.以前,我使用Socket ...
- mysql密码过期的修改方法(your password has expired)
今天打开SQLyog提示密码过期:Your password has expired 解决方法: 1. 启动MySQL服务 2. 启动MySQL后台 3. 执行以下命令 step 1: S ...
- springmvc的简单介绍以及springmvc组件的介绍
Spring web mvc框架 什么是springmvc Springmvc是spring框架的一个模块,spring和springmvc无需中间整合层整合 Springmvc是一个基于mvc的we ...