当前位置:网站首页>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";
}
边栏推荐
- Code neatness -- function
- 单片机之IIC通信协议「建议收藏」
- Startup and shutdown of Oracle Database
- Translation (4): matching rules for automatic text completion
- 互联网的发展为产业的变革和转型提供了新的解决方案
- 炼金术(3): 怎样做好1个业务流程的接口对接
- 吴恩达《机器学习》课程总结(13)_聚类
- LabVIEW continuous sampling and limited sampling mode
- 云厂商为什么都在冲这个KPI?
- Alchemy (9): simple but not simple, never-ending test -- always_ run
猜你喜欢
![计数质数[枚举 -> 空间换时间]](/img/11/c52e1dfce8e35307c848d12ccc6454.png)
计数质数[枚举 -> 空间换时间]

表单form 和 表单元素(input、select、textarea等)

Character interception triplets of data warehouse: substrb, substr, substring

Transmitting and receiving antenna pattern

Sword finger offer 61 Shunzi in playing cards

Promise是什么

Flutter series: Transformers in flutter

Local visualization tool connects to redis of Alibaba cloud CentOS server
![[microservices sentinel] sentinel data persistence](/img/9f/2767945db99761bb35e2bb5434b44d.png)
[microservices sentinel] sentinel data persistence

MySQL企业级参数调优实践分享
随机推荐
供应链高效管理供应商
炼金术(9): 简约而不简单,永不停歇的测试 -- always_run
MySQL分表查询之Merge存储引擎实现
The limits of Technology (11): interesting programming
炼金术(2): 为什么要用issue管理软件
Function and usage of malloc function in C language
Request object, response object, session object
一个人可以到几家证券公司开户?开户安全吗
Mise en œuvre du pool de Threads: les sémaphores peuvent également être considérés comme de petites files d'attente
券商买股票用什么app是比较好的,比较安全的
request对象、response对象、session对象
What is promise
MATLB|基于复杂网络的配电系统微电网优化配置
Thread pool implementation: semaphores can also be understood as small waiting queues
zotero文献管理工具安装使用
[读书摘要] 学校的英文阅读教学错在哪里?--经验主义和认知科学的PK
Comprehensive evaluation of free, easy-to-use and powerful open source note taking software
MATLB|改进的前推回代法求解低压配电网潮流
Matlb| improved forward push back method for solving power flow of low voltage distribution network
炼金术(3): 怎样做好1个业务流程的接口对接