P2048 [NOI2010]超级钢琴 [堆+st表]
考虑只能取长度为 [L,R] 的,然后不难想到用堆搞。
搞个前缀和的st表,里面维护的是一个 最大值的位置
struct rmq {
int mx[N][20] ;
void qwq(int n) {
rep(i , 1 , n) mx[i][0] = i ;
for(int j = 1 ; (1 << j) <= n ; j ++)
for(int i = 1 ; i + (1 << j) - 1 <= n ; i ++) {
int x = mx[i][j - 1] , y = mx[i + (1 << j - 1)][j - 1] ;
mx[i][j] = sum[x] > sum[y] ? x : y ;
}
}
int qry(int l , int r) {
int k = log2(r - l + 1) ;
int x = mx[l][k] , y = mx[r - (1 << k) + 1][k] ;
return sum[x] > sum[y] ? x : y ;
}
} st ;
考虑 对于任意的 \(i\) , 放进去一个 \([i+L-1 , min(i+R-1 , n)]\)
每次从中间分裂就好了,堆自动取max,这题没了
code
// by Isaunoya
#include <bits/stdc++.h>
using namespace std;
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
#define int long long
const int _ = 1 << 21;
struct I {
char fin[_], *p1 = fin, *p2 = fin;
inline char gc() {
return (p1 == p2) && (p2 = (p1 = fin) + fread(fin, 1, _, stdin), p1 == p2) ? EOF : *p1++;
}
inline I& operator>>(int& x) {
bool sign = 1;
char c = 0;
while (c < 48) ((c = gc()) == 45) && (sign = 0);
x = (c & 15);
while ((c = gc()) > 47) x = (x << 1) + (x << 3) + (c & 15);
x = sign ? x : -x;
return *this;
}
inline I& operator>>(double& x) {
bool sign = 1;
char c = 0;
while (c < 48) ((c = gc()) == 45) && (sign = 0);
x = (c - 48);
while ((c = gc()) > 47) x = x * 10 + (c - 48);
if (c == '.') {
double d = 1.0;
while ((c = gc()) > 47) d = d * 0.1, x = x + (d * (c - 48));
}
x = sign ? x : -x;
return *this;
}
inline I& operator>>(char& x) {
do
x = gc();
while (isspace(x));
return *this;
}
inline I& operator>>(string& s) {
s = "";
char c = gc();
while (isspace(c)) c = gc();
while (!isspace(c) && c != EOF) s += c, c = gc();
return *this;
}
} in;
struct O {
char st[100], fout[_];
signed stk = 0, top = 0;
inline void flush() {
fwrite(fout, 1, top, stdout), fflush(stdout), top = 0;
}
inline O& operator<<(int x) {
if (top > (1 << 20)) flush();
if (x < 0) fout[top++] = 45, x = -x;
do
st[++stk] = x % 10 ^ 48, x /= 10;
while (x);
while (stk) fout[top++] = st[stk--];
return *this;
}
inline O& operator<<(char x) {
fout[top++] = x;
return *this;
}
inline O& operator<<(string s) {
if (top > (1 << 20)) flush();
for (char x : s) fout[top++] = x;
return *this;
}
} out;
#define pb emplace_back
#define fir first
#define sec second
template < class T > inline void cmax(T & x , const T & y) {
(x < y) && (x = y) ;
}
template < class T > inline void cmin(T & x , const T & y) {
(x > y) && (x = y) ;
}
int n , k , L , R ;
const int N = 5e5 + 10 ;
int sum[N] ;
struct rmq {
int mx[N][20] ;
void qwq(int n) {
rep(i , 1 , n) mx[i][0] = i ;
for(int j = 1 ; (1 << j) <= n ; j ++)
for(int i = 1 ; i + (1 << j) - 1 <= n ; i ++) {
int x = mx[i][j - 1] , y = mx[i + (1 << j - 1)][j - 1] ;
mx[i][j] = sum[x] > sum[y] ? x : y ;
}
}
int qry(int l , int r) {
int k = log2(r - l + 1) ;
int x = mx[l][k] , y = mx[r - (1 << k) + 1][k] ;
return sum[x] > sum[y] ? x : y ;
}
} st ;
struct element {
int o , l , r , t ;
element() {}
element(int _o , int _l , int _r) : o(_o) , l(_l) , r(_r) , t(st.qry(_l , _r)) {}
bool operator < (const element & other) const {
return sum[t] - sum[o - 1] < sum[other.t] - sum[other.o - 1] ;
}
};
priority_queue < element > q ;
signed main() {
#ifdef _WIN64
freopen("testdata.in" , "r" , stdin) ;
#endif
in >> n >> k >> L >> R ;
rep(i , 1 , n) {
in >> sum[i] ;
sum[i] += sum[i - 1] ;
}
st.qwq(n) ;
rep(i , 1 , n)
if(i + L - 1 <= n) q.push(element(i , i + L - 1 , min(i + R - 1 , n))) ;
int ans = 0 ;
rep(i , 1 , k) {
int o = q.top().o , l = q.top().l , r = q.top().r , t = q.top().t ; q.pop() ;
ans += sum[t] - sum[o - 1] ;
if(l != t) q.push(element(o , l , t - 1)) ;
if(r != t) q.push(element(o , t + 1 , r)) ;
}
out << ans << '\n' ;
return out.flush(), 0;
}
P2048 [NOI2010]超级钢琴 [堆+st表]的更多相关文章
- Bzoj 2006: [NOI2010]超级钢琴 堆,ST表
2006: [NOI2010]超级钢琴 Time Limit: 20 Sec Memory Limit: 552 MBSubmit: 2222 Solved: 1082[Submit][Statu ...
- bzoj 2006: [NOI2010]超级钢琴【st表+堆】
设计一个五元组(i,l,r,p,v),表示在以i为左端点,右端点落在(l,r)中的情况下,取最大值v时右端点落在p.把这个五元组塞到优先队列里,以v排序,每次取出一个,然后把这个取过的五元组分成两个( ...
- 【题解】 bzoj2006: [NOI2010]超级钢琴 (ST表+贪心)
题面戳我 Solution 不会,看的题解 Attention 哇痛苦,一直不会打\(ST\)表,我是真的菜啊qwq 预处理 Log[1]=0;two[0]=1; for(int i=2;i<= ...
- P2048 [NOI2010]超级钢琴(RMQ+堆+贪心)
P2048 [NOI2010]超级钢琴 区间和--->前缀和做差 多次查询区间和最大--->前缀和RMQ 每次取出最大的区间和--->堆 于是我们设个3元组$(o,l,r)$,表示左 ...
- [BZOJ 2006] [NOI 2010]超级钢琴(贪心+ST表+堆)
[BZOJ 2006] [NOI 2010]超级钢琴(贪心+ST表+堆) 题面 给出一个长度为n的序列,选k段长度在L到R之间的区间,一个区间的值等于区间内所有元素之的和,使得k个区间的值之和最大.区 ...
- 洛谷 P2048 [NOI2010]超级钢琴 解题报告
P2048 [NOI2010]超级钢琴 题目描述 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为 ...
- 【题解】P2048 [NOI2010]超级钢琴
[题解][P2048 NOI2010]超级钢琴 一道非常套路的题目.是堆的套路题. 考虑前缀和,我们要是确定了左端点,就只需要在右端区间查询最大的那个加进来就好了.\(sum_j-sum_{i-1} ...
- [洛谷P2048] [NOI2010] 超级钢琴
洛谷题目链接:[NOI2010]超级钢琴 题目描述 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号 ...
- BZOJ2006:超级钢琴(ST表+堆求前K大区间和)
Description 小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i个音符的美妙度 ...
随机推荐
- (三)maven创建部署javaweb
http://how2j.cn/k/idea/idea-maven-web/1356.html#nowhere 这个网站讲的很详细了,下载很慢的话按照他的提示配置阿里云即可,很快 另外可能会出现各种j ...
- C++中STL库函数的基本运用
学了这么长时间的STL库,现在我觉得是有必要对过去的题目和所遇到的问题做一下整理了,以便于之后更好的展开练习: 一. 为什么要用STL库? 1.简单粗暴(省事). 2.便于解决复杂的问题(在贪心题目中 ...
- 全网最全小白搭建Hexo+Gitee/Coding
全网最全小白搭建Hexo+Gitee/Coding 本站内容已全部转移到https://www.myyuns.ltd,具体请移步到www.myyuns.ltd查看
- .NET Core之单元测试(三):Mock框架Moq的使用
编写一个API 新增一个接口 public interface IFoo { bool Ping(string ip); } 接口实现 public class Foo : IFoo { public ...
- 线段树学习----C语言
/* 线段树学习:如果一个节点为i,那么他的左孩子为2I+1,右孩子为2i+2: */ #include<stdio.h> #define min(a,b) a<b?a:b; ]; ...
- FakeLogonScreen抓取Windows凭证
FakeLogonScreen抓取Windows凭证 实践中使用的配置 攻击者: 操作系统: Kali Linux 2020.1 IP: 192.168.1.13 目标: 作业系统: Windows ...
- Webpack之optimization.splitChunks代码分割插件的配置
SplitChunkPlugin插件配置参数详解 对引入的库代码(例如:lodash.jQuery等)进行代码的分割进行优化 若配置时只写chunks:"all",其余则为默认配置 ...
- vue-cli3点滴
1.如果你不在构造函数中声明private的变量,那么久会提示错误. 2.
- windows下修改tomcat的startup.bat脚本文件后台运行
1.修改startup.bat文件 rem Get remaining unshifted command line arguments and save them in the set CMD_LI ...
- vue路由+vue-cli实现tab切换
第一步:搭建环境 安装vue-cli cnpm install -g vue-cli安装vue-router cnpm install -g vue-router使用vue-cli初始化项目 vue ...