题目链接: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(二分/线段树区间最值&单点更新)的更多相关文章

  1. HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧)

    HUD.2795 Billboard ( 线段树 区间最值 单点更新 单点查询 建树技巧) 题意分析 题目大意:一个h*w的公告牌,要在其上贴公告. 输入的是1*wi的w值,这些是公告的尺寸. 贴公告 ...

  2. 【线段树区间最值单点更新模板】BNUOJ 52965 E Excellent Engineers

    http://acm.bnu.edu.cn/v3/external/gym/101512.pdf #include<bits/stdc++.h> using namespace std; ...

  3. hdoj1754 I Hate It【线段树区间最大值维护+单点更新】

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. 【bzoj4695】最假女选手 线段树区间最值操作

    题目描述 给定一个长度为 N 序列,编号从 1 到 N .要求支持下面几种操作:1.给一个区间[L,R] 加上一个数x 2.把一个区间[L,R] 里小于x 的数变成x 3.把一个区间[L,R] 里大于 ...

  5. 【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]里的每个数 ...

  6. 【hdu5306】Gorgeous Sequence 线段树区间最值操作

    题目描述 给你一个序列,支持三种操作: $0\ x\ y\ t$ :将 $[x,y]$ 内大于 $t$ 的数变为 $t$ :$1\ x\ y$ :求 $[x,y]$ 内所有数的最大值:$2\ x\ y ...

  7. hdu6070(分数规划/二分+线段树区间更新,区间最值)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意: 给出一个题目提交序列, 从中选出一个正确率最小的子串. 选中的子串中每个题目当且仅当最 ...

  8. cf834D(dp+线段树区间最值,区间更新)

    题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...

  9. HDU 4819 Mosaic (二维线段树&区间最值)题解

    思路: 二维线段树模板题,马克一下,以后当模板用 代码: #include<cstdio> #include<cmath> #include<cstring> #i ...

随机推荐

  1. PHP Framework

    PHP Framework is built for PHP developers who need elegant toolkit to create full-featured web appli ...

  2. 【shell】获取第10+个位置参数

    转载自:http://www.cnblogs.com/sheldonxu/archive/2012/06/25/2560770.html 在Shell脚本中,可以用$n的方式获取第n个参数,例如,一个 ...

  3. Win8+VS2012 配置OpenGL SuperBible5 环境

    (1)glew: 版本:1.7.0-win32 下载地址:https://sourceforge.net/projects/glew/files/glew/ 安装步骤: 将include文件夹下的.h ...

  4. 【Leetcode-easy】Longest Common Prefix

    思路:每次从字符数组中读取两个字符串比较.需要注意输入字符串为空,等细节. public String longestCommonPrefix(String[] strs) { if(strs==nu ...

  5. [egret+pomelo]实时游戏杂记(1)

    [egret+pomelo]学习笔记(1) [egret+pomelo]学习笔记(2) [egret+pomelo]学习笔记(3) 资料 egret pomelo pomelo捡宝项目 准备工作 1. ...

  6. Starting MySQL.. ERROR! The server quit without updating PID file

    版权声明:本文为博主原创文章,未经博主允许不得转载. 注意:本文出自 “阿飞”的博客 ,如果要转载本文章,请与作者联系! 并注明来源: http://blog.csdn.net/faye0412/ar ...

  7. matlab给图片插入说明文字

    text(cluster().center(),cluster().center(),num2str(),'color','k') 格式是text(x,y,'说明文字') x,y代表位置

  8. 微信video和audio无法自动播放解决方案

    //音频,写法一<audio src="music/bg.mp3" autoplay loop controls>你的浏览器还不支持哦</audio> // ...

  9. linux应用之nginx的源码安装及配置(centos)

    1.准备工作选首先安装这几个软件:GCC,PCRE(Perl Compatible Regular Expression),zlib,OpenSSL.Nginx是C写的,需要用GCC编译:Nginx的 ...

  10. MySQL存储过程示例

    MySQL存储过程: /*自定义结束符*/ DELIMITER $$ /*如果存在同名的存储过程就删除*/ DROP PROCEDURE IF EXISTS prAddBlack$$ /*创建存储过程 ...