当前位置:网站首页>795 div.2 D. Max GEQ sum monotone stack
795 div.2 D. Max GEQ sum monotone stack
2022-06-28 00:34:00 【Strezia】
1691D
1800
The question
Give a sequence a 1 , a 2 , . . . , a n a_1, a_2,...,a_n a1,a2,...,an, Ask if all i , j ( 1 ≤ i ≤ j ≤ n , ) i,j(1\leq i\leq j\leq n,) i,j(1≤i≤j≤n,), Satisfy max ( a i , . . . a j ) ≥ a i + . . . + a j \max(a_i,...a_j)\geq a_i+...+a_j max(ai,...aj)≥ai+...+aj. ( n ≤ 2 e 5 , − 1 0 9 ≤ a i ≤ 1 0 9 ) (n\leq 2e5,-10^9\leq a_i\leq 10^9) (n≤2e5,−109≤ai≤109)
Ideas
It is observed that there is n 2 n^2 n2 Species value , On the left side, there are at most n n n Species value , So we consider from 1 → n 1\to n 1→n enumeration a i a_i ai, And find an interval [ L , R ] [L,R] [L,R] Satisfy a i a_i ai Is the largest number , On this basis, maximize ∑ a [ L , R ] \sum a[L,R] ∑a[L,R].
First step , Find the corresponding [ L i , R i ] [L_i,R_i] [Li,Ri] Value range . That is, find the first greater than... On the left and right respectively a i a_i ai Number of numbers , It can be realized by two monotone stacks . For example, when calculating the right side , Maintain a monotonically increasing stack , Traverse back and forth , If at present a i a_i ai Greater than the value corresponding to the top element of the stack , The first position on the right side of the stack top element that is larger than it is i i i. Similar on the other side .
The second step , Maximize ∑ a [ L , R ] \sum a[L,R] ∑a[L,R]. Just maintain a prefix and an array , And find [ L , i − 1 ] [L,i-1] [L,i−1] And [ i , R ] [i, R] [i,R] Subtract the maximum value in . You can use a segment tree or ST surface , Note that the prefix and array subscript are from 0 Start .
Code
int n, a[maxn], L[maxn], R[maxn];
int pre[maxn];
struct segment_tree {
int l, r;
int minn, maxx;
#define l(x) tree[x].l
#define r(x) tree[x].r
#define mn(x) tree[x].minn
#define mx(x) tree[x].maxx
}tree[maxn << 2];
void pushup(int p) {
mn(p) = min(mn(p<<1), mn(p<<1|1));
mx(p) = max(mx(p << 1), mx(p << 1 | 1));
}
void build(int p, int l, int r) {
l(p) = l, r(p) = r;
if(l == r) {
mn(p) = mx(p) = pre[l];
return;
}
int mid = (l + r) >> 1;
build(p << 1, l, mid);
build(p << 1 | 1, mid + 1, r);
pushup(p);
}
int query_min(int p, int l, int r) {
if(l(p) >= l && r(p) <= r) {
return mn(p);
}
int mid = (l(p) + r(p)) >> 1;
int ans = (1ll<<62);
if(l <= mid) ans = min(ans, query_min(p << 1, l, r));
if(r > mid) ans = min(ans, query_min(p << 1 | 1, l, r));
return ans;
}
int query_max(int p, int l, int r) {
if(l(p) >= l && r(p) <= r) {
return mx(p);
}
int mid = (l(p) + r(p)) >> 1;
int ans = -(1ll<<62);
if(l <= mid) ans = max(ans, query_max(p << 1, l, r));
if(r > mid) ans = max(ans, query_max(p << 1 | 1, l, r));
return ans;
}
void solve() {
int last = 0;
cin >> n;
pre[0] = 0;
for(int i = 1; i <= n; i++) {
cin >> a[i];
pre[i] = pre[i-1] + a[i];
}
a[n+1] = a[0] = INF;
stack<int> s;
for(int i = 1; i <= n + 1; i++) {
while(s.size() && a[s.top()] < a[i]) {
R[s.top()] = i;
s.pop();
}
s.push(i);
}
while(s.size()) s.pop();
for(int i = n; i >= 0; i--) {
while(s.size() && a[s.top()] < a[i]) {
L[s.top()] = i;
s.pop();
}
s.push(i);
}
while(s.size()) s.pop();
build(1, 1, n);
for(int i = 1; i <= n; i++) {
int mnn = query_min(1, L[i], i-1);
if(L[i] == 0) mnn = min(mnn, 0LL);
int mxx = query_max(1, i, R[i] - 1);
if(mxx - mnn > a[i]) {
cout << "NO\n";
return;
}
}
cout << "YES\n";
}
边栏推荐
- Sentinel
- The limits of Technology (11): interesting programming
- JVM的内存模型简介
- Character interception triplets of data warehouse: substrb, substr, substring
- The Internet industry has derived new technologies, new models and new types of industries
- RecyclerView实现分组效果,多种实现方式
- Request object, response object, session object
- internship:业务流程初识
- Mise en œuvre du pool de Threads: les sémaphores peuvent également être considérés comme de petites files d'attente
- 炼金术(2): 为什么要用issue管理软件
猜你喜欢
![用两个栈实现队列[两次先进后出便是先进先出]](/img/de/07297816f1a44d41389bb45d012c80.png)
用两个栈实现队列[两次先进后出便是先进先出]

Alchemy (1): identify prototype, demo and MVP in project development

HCIP/HCIE Routing&Switching / Datacom备考宝典系列(十九)PKI知识点全面总结(公钥基础架构)

MongoDB-在windows电脑本地安装一个mongodb的数据库

flutter系列之:flutter中的变形金刚Transform

Instructions for vivado FFT IP

Promise是什么

Modern programming languages: zig
![Using two stacks to implement queues [two first in first out is first in first out]](/img/de/07297816f1a44d41389bb45d012c80.png)
Using two stacks to implement queues [two first in first out is first in first out]

夏日的晚会
随机推荐
Which securities company is better and safer to open an account for stock speculation
Alchemy (1): identify prototype, demo and MVP in project development
SCU|通过深度强化学习进行微型游泳机器人的步态切换和目标导航
What is promise
RNA-seq入门实战(一):上游数据下载、格式转化和质控清洗
flutter系列之:flutter中的变形金刚Transform
股市小白在网上股票开户安全吗?
Sword finger offer 61 Shunzi in playing cards
[digital ic/fpga] detect the position of the last matching sequence
华泰证券在网上开户安全吗?
单片机之IIC通信协议「建议收藏」
How many securities companies can a person open an account? Is it safe to open an account
Software engineering job design (1): [personal project] implements a log view page
【无标题】
zotero文献管理工具安装使用
What are the ways to combine the points system with marketing activities
一个人可以到几家证券公司开户?开户安全吗
现代编程语言:Rust (铁锈,一文掌握钢铁是怎样生锈的)
【论文阅读|深读】SDNE:Structural Deep Network Embedding
[black apple series] m910x perfect black apple system installation tutorial – 2 making system USB disk -usb creation