题目链接: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. 剑指Offer:栈的压入、弹出序列【31】

    剑指Offer:栈的压入.弹出序列[31] 题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈 ...

  2. 在win7下使用git和gitlab进行code review

    1.安装 Git-2.6.3-64-bit.exe  下载地址:http://pan.baidu.com/s/1hqGvwnq 2.根据收到的邮件进入gitlab网站,并修改密码登陆 3.新建一个文件 ...

  3. HDU - 4990 Reading comprehension 【矩阵快速幂】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4990 题意 初始的ans = 0 给出 n, m for i in 1 -> n 如果 i 为奇 ...

  4. 排序List集合

    这两天写代码过程中遇到一个需求,需要按照某个字段排序List集合,自己实现了一半,发现网上有个更好的版本,就采用了这个,记录下来. 使用这个工具类要注意一个就是 如果你按照age 字段排序,那么age ...

  5. HTML font: 12px/1.5 Arial; 是什么意思

    意思是: 字体12像素 行高 1.5em 字体 Arial 可以参考下面资料: 我们常用的font属性有下面这六种: font-style设定斜体 如:font-style: italic; font ...

  6. certbot申请SSL证书及中间证书问题

    首先是到https://certbot.eff.org/上申请证书,由于我们使用的web服务器是基于erlang的cowboy的,在主页上没有选项可以支持,因此在Software下拉项中选择" ...

  7. Early Media and Music on Hold

    Early media refers to any media that is played to the initial caller’s phone before the remote party ...

  8. vmware 三种网络模式图解及分区挂载

  9. python 之闭包,装饰器,生成器

    一.可迭代的对象(也就是可以用for循环,generator 是生成器的意思) 怎么判断是否可迭代呢?通俗的方法建立一个for循环 高大上的方法是: In [40]: from collections ...

  10. poj1821 Fence(单调队列优化dp)

    地址 一排N个木板,M个工匠站在不同位置$S_i$,每个人可以粉刷覆盖他位置的.最长长度为$L_i$木板段,每刷一个有$P_i$报酬.同一木板只刷一次.求最大报酬. 根据每个人的位置dp,设$f[i] ...