专题二树形结构 E - Can you answer these queries V
2022/2/6 23:42:53
本文主要是介绍专题二树形结构 E - Can you answer these queries V,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
- 题目
You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| <= 10000 , 1 <= N <= 10000 ). A query is defined as follows: Query(x1,y1,x2,y2) = Max { A[i]+A[i+1]+...+A[j] ; x1 <= i <= y1 , x2 <= j <= y2 and x1 <= x2 , y1 <= y2 }. Given M queries (1 <= M <= 10000), your program must output the results of these queries.
Input
The first line of the input consist of the number of tests cases <= 5. Each case consist of the integer N and the sequence A. Then the integer M. M lines follow, contains 4 numbers x1, y1, x2 y2.
Output
Your program should output the results of the M queries for each test case, one query per line.
Example
Input: 2 6 3 -2 1 -4 5 2 2 1 1 2 3 1 3 2 5 1 1 1 1 1 1 1 Output: 2 3 1
- 思路
这题没有修改,和A相比的区别主要是前后端点不定
A题:https://www.cnblogs.com/Benincasa/p/15866592.html
两个区间不相交时,最大连续和是x1,y1的最大后缀和,加上y1,x2的总和,再加上x2,y2的最大前缀和
两个区间相交时有四种情况,一种情况下和不相交的时候方法一样,另外三种分别是x2,y1的最大连续和,x1,y1的最大连续和,x2,y2的最大连续和 - 代码
lmin是之前写别的思路的时候加的,没啥用
#include<cstdio> #include<iostream> #include<string> #include<cstring> #include<algorithm> using namespace std; #define clear(a,x) memset(a,x,sizeof(a)) #define ll long long const int M=4e5; struct Node { int lsum, lmin, rsum, sum, maxx; }ss[M]; int a[M << 2]; int n,q,ca,ans; void up(int p) { ss[p].sum = ss[p << 1].sum + ss[(p << 1) + 1].sum; ss[p].lsum = max(ss[p << 1].lsum, ss[p << 1].sum + ss[p << 1 | 1].lsum); ss[p].lmin = min(ss[p << 1].lmin, ss[p << 1].sum + ss[p << 1 | 1].lmin); ss[p].rsum = max(ss[p << 1 | 1].rsum, ss[p << 1 | 1].sum + ss[p << 1].rsum); ss[p].maxx = max(max(ss[p << 1].maxx, ss[p << 1 | 1].maxx), ss[p << 1].rsum + ss[p << 1 | 1].lsum); } void build(int s, int t, int p) { if (s == t) { ss[p].lsum = ss[p].lmin = ss[p].rsum = ss[p].maxx = ss[p].sum = a[s]; return; } int m = s + ((t - s) >> 1); build(s, m, p << 1), build(m + 1, t, p << 1 | 1); up(p); } //void change(int x, int c, int s, int t, int p) //{ // int m = s + ((t - s) >> 1); // if (s == x && s == t) { // ss[p].maxx = ss[p].lsum = ss[p].rsum = ss[p].sum = c; // return; // } // if (x <= m) change(x, c, s, m, p << 1); else change(x, c, m+1, t, p << 1 | 1); // up(p); //} Node query(int l, int r, int s, int t, int p){ if (l <= s && t <= r) return ss[p]; int m = s + ((t - s) >> 1); if (m >= r) return query(l, r, s, m, p << 1) ; else if (m < l) return query(l, r, m + 1, t, p << 1 | 1); else { Node ls = query(l, r, s, m, p << 1); Node rs = query(l, r, m + 1, t, p << 1 | 1); Node ans ; ans.maxx = max(max(ls.maxx, rs.maxx), ls.rsum + rs.lsum); ans.lsum = max(ls.lsum, ls.sum + rs.lsum); ans.lmin = min(ls.lmin, ls.sum + rs.lmin); ans.rsum = max(rs.rsum, rs.sum + ls.rsum); ans.sum = ls.sum + rs.sum; return ans; } } int main() { scanf("%d",&ca); while(ca--) { scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d",&a[i]); } build(1,n,1); scanf("%d",&q); int x1,y1,x2,y2; while (q--) { ans = 0; cin >> x1 >> y1 >> x2 >> y2; if(y1 < x2) { ans = query(x1,y1,1,n,1).rsum + query(x2,y2,1,n,1).lsum + query(y1,x2,1,n,1).sum; ans = ans - a[y1] - a[x2]; } else { int a1 = query(x2,y1,1,n,1).maxx; int a2 = query(x1,x2,1,n,1).rsum + query(y1,y2,1,n,1).lsum + query(x2,y1,1,n,1).sum; a2 = a2 - a[y1] - a[x2]; int a3 = query(x1,x2,1,n,1).rsum + query(x2,y1,1,n,1).lsum - a[x2]; int a4 = query(x2,y1,1,n,1).rsum + query(y1,y2,1,n,1).lsum - a[y1]; ans = max(max(max(a1, a2), a3), a4); } cout << ans << endl; } } return 0; }
这篇关于专题二树形结构 E - Can you answer these queries V的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23增量更新怎么做?-icode9专业技术文章分享
- 2024-11-23压缩包加密方案有哪些?-icode9专业技术文章分享
- 2024-11-23用shell怎么写一个开机时自动同步远程仓库的代码?-icode9专业技术文章分享
- 2024-11-23webman可以同步自己的仓库吗?-icode9专业技术文章分享
- 2024-11-23在 Webman 中怎么判断是否有某命令进程正在运行?-icode9专业技术文章分享
- 2024-11-23如何重置new Swiper?-icode9专业技术文章分享
- 2024-11-23oss直传有什么好处?-icode9专业技术文章分享
- 2024-11-23如何将oss直传封装成一个组件在其他页面调用时都可以使用?-icode9专业技术文章分享
- 2024-11-23怎么使用laravel 11在代码里获取路由列表?-icode9专业技术文章分享
- 2024-11-22怎么实现ansible playbook 备份代码中命名包含时间戳功能?-icode9专业技术文章分享