# 题目大意 GSS3 - Can you answer these queries III 需要你维护一种数据结构,支持两种操作: 单点修改 求一个区间的最大子段和 # 解题思路 一个区间的最大子段和(GSS),只能通过三种方式转移而来. 左儿子的最大子段和 右儿子的最大子段和 左儿子的最大右子段和+右儿子的最大左子段和 那这就比较好办了.只需要维护四个东西就可以了 sum,区间和 gss,最大子段和 gssl,最大左子段和 gssr,最大右子段和 emmm,比较可做. # 代码 #inclu…
//在gss1的基础上加了修改操作,一样的做法,加一个modify函数就可以了 #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; ; int n,m,type,l,r; int L,R,M,S; struct Tree { int l,r,mid; int sum,lmaxn,…
SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50000) integers between -10000 and 10000. On this sequence you have to apply M (M <= 50000) operations: modify the i-th element in the sequence or for giv…
「SPOJ 3105」Power Modulo Inverted 传送门 题目大意: 求关于 \(x\) 的方程 \[a^x \equiv b \;(\mathrm{mod}\; p) \] 的最小自然数解,不保证 \(a,p\) 互质 如果保证 \(a,p\) 互质,那么可以直接使用 \(\texttt{BSGS}\) 算法通过本题. 对于这道题目,我们考虑将式子变形 令 \(t=\gcd(a,p)\),则有 \[\frac{a}{t}a^{x-1} \equiv \frac{b}{t} \;…
gss2调了一下午,至今还在wa... 我的做法是:对于询问按右区间排序,利用splay记录最右的位置.对于重复出现的,在splay中删掉之前出现的位置所在的节点,然后在splay中插入新的节点.对于没有出现过的,直接插入.询问时直接统计区间的最大子段和. gss2没能调出bug,所以看了一下以下的gss3,发现跟gss1基本一样.直接上代码 以上的做法是错的,对于这种数据就过不了.姿势不对,囧 44 -2 3 -211 4 GSS Can you answer these queries II…
GSS3 - Can you answer these queries III You are given a sequence A of N (N <= 50000) integers between -10000 and 10000. On this sequence you have to apply M (M <= 50000) operations: modify the i-th element in the sequence or for given x y print max{…
SP1716 GSS3 - Can you answer these queries III 题意翻译 n 个数,q 次操作 操作0 x y把A_xAx 修改为yy 操作1 l r询问区间[l, r] 的最大子段和 依旧是维护最大子段和,还是再敲一遍比较好. code: #include<iostream> #include<cstdio> #define ls(o) o<<1 #define rs(o) o<<1|1 using namespace std…
Can you answer these queries III 题目:洛谷 SPOJ [题目描述] 给定长度为N的数列A,以及M条指令,每条指令可能是以下两种之一: 1.“0 x y”,把A[x]改成y: 2.“1 x y”,查询区间[x,y]中的最大连续子段和. [输入格式] 第一行,N: 第二行,N个数,即A[1]...A[N]: 第三行,M: 第4至第M+3行,每行三个整数,分别表示“0或1”,x,y,即指令. [输出格式] i行,i表示“1 x y”的数量,每行1个整数,即指令2的答案…
Can you answer these queries III(luogu) Description 维护一个长度为n的序列A,进行q次询问或操作 0 x y:把Ax改为y 1 x y:询问区间[l,r]的最大子段和 数据范围:n,q<=5e4,-1e4<=Ai<=1e4; Solution 线段树处理区间最大子段和 res为区间最大子串和 sum为区间和 prel和prer分别为从区间左端点和右端点开始的最大子串和 Code #include <cstdio> #incl…
传送门 Luogu 解题思路 区间最大子段和板子题. 考虑用线段树来做. 对于一个线段树节点所包含区间,它的最大子段和有两种情况,包含中点与不包含. 不包含的情况直接从左右子树转移. 对于包含的情况: 我们对每个节点维护两个值:开头是左端点的最大子段和,结尾是右端点的最大子段和. 那么包含中点的情况可以用上面两个东西转移. 那么这两个东西又怎么维护呢... 他们也有包含与不包含中点的情况,只要记一下节点的区间和就可以了,具体方法同上. 于是便搞定了这道题. 细节注意事项 咕咕咕 参考代码 #in…
Time Limit: 330MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Description You are given a sequence A of N (N <= 50000) integers between -10000 and 10000. On this sequence you have to apply M (M <= 50000) operations: modify the i-th…
题目链接 给出n个数, 2种操作, 一种是将第x个数改为y, 第二种是询问区间[x,y]内的最大连续子区间. 开4个数组, 一个是区间和, 一个是区间最大值, 一个是后缀的最大值, 一个是前缀的最大值. 合并起来好麻烦...... #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <…
[题目分析] GSS1的基础上增加修改操作. 同理线段树即可,多写一个函数就好了. [代码] #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <map> #include <set> #include <queue> #include <string> #include <iostream&…
题意 概括为主席树区间加区间询问 题解 记录一下标记永久化的方法.每个点存add和sum两个标记,表示这个区间整个加多少,区间和是多少(这个区间和不包括祖先结点区间加) 然后区间加的时候,给路上每结点的sum更新,然后到达完整区间后更新add.询问的时候把路径上所有结点(不包括自己)的add加起来乘以区间长度,再加上sum 大概思想就是:区间内的加法用sum维护,区间外的加法询问的时候再考虑贡献 #include <algorithm> #include <cstdio> usin…
题意翻译 nnn 个数, qqq 次操作 操作0 x y把 AxA_xAx​ 修改为 yyy 操作1 l r询问区间 [l,r][l, r][l,r] 的最大子段和 题目描述 You are given a sequence A of N (N <= 50000) integers between -10000 and 10000. On this sequence you have to apply M (M <= 50000) operations: modify the i-th ele…
题意翻译 nnn 个数, qqq 次操作 操作0 x y把 AxA_xAx​ 修改为 yyy 操作1 l r询问区间 [l,r][l, r][l,r] 的最大子段和 感谢 @Edgration 提供的翻译 题目描述 You are given a sequence A of N (N <= 50000) integers between -10000 and 10000. On this sequence you have to apply M (M <= 50000) operations:…
GSS3 Description 动态维护最大子段和,支持单点修改. Solution 设 \(f[i]\) 表示以 \(i\) 为结尾的最大子段和, \(g[i]\) 表示 \(1 \sim i\) 的最大子段和,那么 \[f[i] = max(f[i - 1] + a[i], a[i])\] \[g[i] = max(g[i - 1], f[i])\] 发现只跟前一项有关.我们希望使用矩阵乘法的思路,但是矩阵乘法通常只能适用于递推问题.因此我们引入广义矩阵乘法. 矩阵乘法问题可分治的原因在于…
思路:用TREE记录节点的最大连续和,LEF记录左边开始的最大连续和,RIG记右边开始的最大连续和 然后处理的时候就是比较左边最大,右边最大  中间区间的问题 其中这个query 只能膜拜了... 大大缩减了时间.放弃治疗的我只会用三个函数 传参就要传出翔. flag == -1表示要左边最大 flag == 1 表示要右边最大 然后ans在找到的区间的同时进行比较. 我已可入灵魂 #include <iostream> #include <cstdio> #include <…
题面 题解 相信大家写过的传统做法像这样:(这段代码蒯自Karry5307的题解) struct SegmentTree{ ll l,r,prefix,suffix,sum,maxn; }; //... inline void update(ll node) { ll res; tree[node].sum=tree[node<<1].sum+tree[(node<<1)|1].sum; tree[node].maxn=max(tree[node<<1].maxn,tr…
区间连续不重复子段最大值,要维护历史的最大值和当前的最大值,打两个lazy,离线 #include<cstdio> #include<cstring> #include<algorithm> #define maxn 150000 #define rep(i,l,r) for(int i=l;i<=r;i++) #define LL long long using namespace std; typedef struct { LL nmax,hmax,nlazy…
线段树操作. 维护一个区间最大连续子段和,左最大连续子段和,右最大连续子段和即可. 最后不知道怎么搞,query的时候返回了个结构体. #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int N=50005; int n,q,a[N],opt,x,y; struct Segtree{int l,r,lmx,rmx,mx,sum;}t[N<<…
问题描述 [LG-SP1716](https://www.luogu.org/problem/SP1716] 题解 GSS 系列的第三题,在第一题的基础上带单点修改. 第一题题解传送门 在第一题的基础上,增加一个单点修改就完事了. \(\mathrm{Code}\) #include<bits/stdc++.h> using namespace std; template <typename Tp> void read(Tp &x){ x=0;char ch=1;int f…
题目链接 之前用线段树写了一遍,现在用\(ddp\)再写一遍. #include <cstdio> #define lc (now << 1) #define rc (now << 1 | 1) inline int max(int a, int b){ return a > b ? a : b; } const int INF = 2147483647 >> 2; const int MAXN = 50010; inline int read(){…
题目描述 给出了序列A[1],A[2],-,A[N]. (a[i]≤15007,1≤N≤50000).查询定义如下: 查询(x,y)=max{a[i]+a[i+1]+-+a[j]:x≤i≤j≤y}. 给定M个查询,程序必须输出这些查询的结果. 输入输出格式 输入格式: 输入文件的第一行包含整数N. 在第二行,N个数字跟随. 第三行包含整数M. M行跟在后面,其中第1行包含两个数字xi和yi. 输出格式: 您的程序应该输出M查询的结果,每一行一个查询. 题解 查询区间内的最大子段和,我们用线段树来…
传送门 解题思路 和GSS1相似,但需要巨恶心的分类讨论,对于x1<=y1< x2< =y2 这种情况 , 最大值应该取[x1,y1]的右端最大+[y1+1,x2-1]的和+[x2,y2]的左端最大.对于x1< =x2< =y1<=y2,用四种情况,第一种是[x1,x2-1]的右端最大+[x2,y2]的左端最大,第二种是[x1,y1]的右端最大+[y1+1,y2]的左端最大,第三种是[x2,y1]的最大值,第四种是[x1,x2-1]的右端最大+[x2,y1]的和+[y1…
\[ Preface \] 没有 Preface. \[ Description \] 维护一个长度为 \(n\) 的数列 \(A\) ,需要支持以下操作: 0 x y 将 \(A_x\) 改为 \(y\) . 1 x y 求 \(\max\limits_{x \leq l \leq r \leq y}{\sum_{i=l}^rA[i]}\) . \[ Solution \] 区间最大子段和 是一个非常经典的问题. 对于 整体最大子段和 来说,一般有 \(O(n)\) 的 贪心 和 分治 做法,…
题目描述 You are given a sequence \(A\) of \(N (N <= 50000)\) integers between \(-10000\) and \(10000\). On this sequence you have to apply \(M (M <= 50000)\) operations: modify the \(i\)-th element in the sequence or for given \(x\) \(y\) print \(max\{…
都说这题是 GSS 系列中最难的,今天做了一下,名副其实 首先你可以想到各种各样的在线乱搞想法,线段树,主席树,平衡树,等等,但发现都不太可行. 注意到题目也没有说强制在线,因此可以想到离线地去解决这道题. 我们把询问按照右端点从小到大排序.假设当前询问的右端点为 \(i\). 定义 \(s_j\) 为 \([j,i]\) 中不重复数字的和.我们建一棵线段树维护 \(s_j\) 的最大值. 这样修改起来就比较自然了,当右端点从 \(i-1\) 变到 \(i\) 的时候,记 \(a_i\) 上一次…
传送门 (分析见正睿2018.10.1笔记) 代码 #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cctype> #include<cmath> #include<cstdlib> #include<queue> #include<ctime…
Can you answer these queries III SPOJ - GSS3 这道题和洛谷的小白逛公园一样的题目. 传送门: 洛谷 P4513 小白逛公园-区间最大子段和-分治+线段树区间合并(单点更新.区间查询) 代码: #include<bits/stdc++.h> using namespace std; typedef long long ll; ; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1…