首页 > 其他分享 >CF1359D Yet Another Yet Another Task

CF1359D Yet Another Yet Another Task

时间:2023-11-07 19:44:22浏览次数:26  
标签:le CF1359D max int Another res Yet

貌似没有线段树做法。

记\(s\)为\(a\)的前缀和数组。

对于一个确定的右端点 \(r\) 和左端点 \(l\),它对于答案的贡献是 \(s_r-s_{l-1}-max\{a_i\},l\le i\le r\) ,如果枚举右端点,令 \(c_l=s_{l-1}+max\{a_i\},l\le i\) 。那么其实就是要求 \(1\le k\le r-1\) 的 \(min\{c_k\}\)。

线段树维护 \(c_k\) 即可。

用单调栈求出 \(a_i\) 左边第一个大于自己的数 \(a_p\),然后把 \([p,i-1]\) 的 \(max\{a_i\}\) 覆盖成 \(a_r\) 即可。

然后你发现要维护 \(c\) ,你得维护 \(s\) 的最小值,和 \(max\{a_i\}\) 。

时间复杂度 \(O(nlogn)\) 。

#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10;

int a[N], n, s[N];
int stk[N], top;

struct Node {
	int l, r;
	int s, a, v; // s[i] + a[i]
	int tag;
}tr[N << 2];

void pushup(int u) {
	tr[u].v=min(tr[u<<1].v,tr[u<<1|1].v);
	tr[u].s=min(tr[u<<1].s,tr[u<<1|1].s);
}

void pushdown(int u) {
	if (tr[u].tag != 2e9) {
		tr[u<<1].a=tr[u].tag,tr[u<<1].v=tr[u<<1].s+tr[u].tag;
		tr[u<<1|1].a=tr[u].tag,tr[u<<1|1].v=tr[u<<1|1].s+tr[u].tag;
		tr[u<<1|1].tag=tr[u<<1].tag=tr[u].tag;
		tr[u].tag = 2e9;
	} 
}

void build(int u,int l,int r) {
	tr[u]={l,r};
	tr[u].tag=2e9;
	if (l==r) {
		tr[u].a=1e9;
		tr[u].s=s[l];
		tr[u].v=tr[u].a+tr[u].s;
		tr[u].tag=2e9;
		return;
	}
	int mid = l +r>>1;
	build(u<<1,l,mid),build(u<<1|1,mid+1,r);
	pushup(u);
}

void modify(int u,int L,int R,int k) {
	if (tr[u].l>=L&&tr[u].r<=R) {
		tr[u].a=k;
		tr[u].v=tr[u].s+k;
		tr[u].tag=k;
		return ;
	}
	int mid=tr[u].l+tr[u].r>>1;
	pushdown(u);
	if (L<=mid) modify(u<<1,L,R,k);
	if (R>mid) modify(u<<1|1,L,R,k);
	pushup(u);
}

int query(int u,int L,int R) {
	if (tr[u].l>=L&&tr[u].r<=R) 
		return tr[u].v;
	int mid=tr[u].l+tr[u].r>>1;
	pushdown(u);
	int res=2e9;
	if (L<=mid) res=min(res,query(u<<1,L,R));
	if (R>mid) res=min(res,query(u<<1|1,L,R));
	return res;
}

int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
	for (int i = 1; i <= n; i ++ ) s[i] = s[i - 1] + a[i];
	
	int ans = 0;
	build(1,0,n);
	for (int i = 1; i <= n; i ++ ) {
		while (top && a[stk[top]] <= a[i]) top -- ;
		// [stk[top], i-1]拍成 a[i]+s[k]
		modify(1,stk[top],i-1,a[i]);
		ans=max(ans,s[i]-query(1,0,i-1));
		stk[ ++ top] = i;
	}
	cout << ans << endl;
	return 0;
}

标签:le,CF1359D,max,int,Another,res,Yet
From: https://www.cnblogs.com/zhangchenxin/p/17815782.html

相关文章

  • How can I move a MySQL database from one server to another?
     Myfavoritewayistopipeasqldumpcommandtoasqlcommand.Youcandoalldatabasesoraspecificone.So,forinstance,mysqldump-uuser-ppasswordmyDatabase|mysql-hremoteserver-uremoteuser-premoteserverpasswordYoucandoalldatabaseswithmysq......
  • [PG] Another example of FCSA
    functionactualargumentsandcadidatesT=(193341,23,23)C=[(193341,1700,1700),(1700,1700,1700),(1043,1700,1700)]querytypeinformation,selectoid,typname,typcategorytypcat,typispreferredpreferredfrompg_typewhereoidin(193341,2......
  • 修改token有效期工具 Another Redis Desktop Manager
     1、获取到redis的host和密码登录2、根据要使用的token查询出数据,修改TTL字段值未-1保存即可。 ......
  • CF1073G Yet Another LCP Problem
    一道*2600调了一年,代码细节是有点粪了,但自己菜也是挺菜的。/oh/oh考虑容斥,令\(f(A)=\sum\limits_{i,j\inA}\operatorname{lcp}(i,j)\),那么答案就是\(f(A\cupB)-f(A)-f(B)\)(这里的并表示可重集合并)。令\(A=\{a_1,a_2,\cdots,a_m\}\),并且\(a_1\lea_2\le\cdots\lea_m\),......
  • 题解 CF903G【Yet Another Maxflow Problem】
    加边\(A_n\stackrel{0}{\to}A_{n+1}\),\(B_0\stackrel{0}{\to}B_1\)。称形如\(A_i\toA_{i+1}\)的边为左部边,形如\(B_j\toB_{j+1}\)的边为右部边,形如\(A_i\toB_j\)的边为中间边。根据最大流最小割定理,将最大流问题转化为最小割问题求解。显然,至少存在一组最小割,包含恰好......
  • #结论#CF1776G Another Wine Tasting Event
    题目给定一个长度为\(2n-1\)的字符串,问一组使得\(n\)个长度不小于\(n\)的区间中字母W的个数相等的字母W的个数分析首先结论就是\(\max_{i=1}^n\{cW[i\dotsi+n-1]\}\)一定是合法解以这组解为基准,左右端点如果向外扩展那么个数一定会更多或者不变,而此时由于当前字母......
  • [920] Copy the font style from one cell in a table of a Word document to another
    TocopythefontstylefromonecellinatableofaWorddocumenttoanothercellusingPythonandthepython-docxlibrary,youcanaccessthefontpropertiesofthesourcecellandapplythemtothetargetcell.Here'showyoucandoit:First,ma......
  • Codeforces Round 893 (Div. 2) C. Yet Another Permutation Problem
    有一个\(gcd\)游戏,按以下步骤进行:选择一个\(n\)的排列\(p_1,p_2,\cdots,p_n\)。对于每个\(i\),\(d_i=gcd(p_i,p_{i\%n+1})\)排列\(p\)的\(score\)为数组\([d_1,d_2,\cdots,d_n]\)中不同数的个数。给一个\(n\),需要构造一个排列\(p\),使得\(sco......
  • [910] Copy a file to another directory with a new name in Python
    TocopyafiletoanotherdirectorywithanewnameinPython,youcanusetheshutillibrary.Here'showyoucandoit:importshutil#Specifythesourcefilepathsource_file='path/to/source/file.txt'#Specifythedestinationdirect......
  • 洛谷 P7830 [CCO2021] Through Another Maze Darkly
    洛谷传送门被联考创出shit了。考虑一种极限情况:每个点指向父亲。那么这种情况我们会顺着欧拉序完整地把整棵树都走一遍。但是初始的时候不一定每个点都指向父亲。发现我们走过\(O(n^2)\)步就能到达上面的极限情况。比较显然,因为每次扩展至少使一个点从不指向父亲变成指向父......