正文
增加 18 行删除 18 行
5 行未改动678910111213141516171819202122232425262728293021 行未改动5253545556575859606162636465666722 行未改动9091929394959697989910010110210310410510610710810911011111211311411511642 行未改动5 行未改动进行 $q$ 次查询,求区间内的一个子区间满足其为区间内的最大子段和且该区间尽可能地靠左。### 思路为了方便,我们用 $a,b,c,d$ 分别来表示最大子段和、区间和、最大前缀和、最大后缀和,$u,v$ 表示合并的左右区间,$f$ 表示合并后的大区间。1. 最大子段和合并:有三种情况,如图:要么完全包含在左区间或右区间,要么由左区间的后缀与右区间的前缀拼接而成,对这三种情况取最大值就是合并后的最大子段和。2. 区间和合并:直接将两个区间和相加即可。3. 最大前缀和合并:有两种情况,第一种最大前缀和就是 $c_u$,第二种是 $b_u+c_v$,然后对两种情况取最大值。4. 最大后缀和合并:同最大前缀和合并一样,有两种情况,第一种是 $d_v$,第二种是 $b_v+d_u$,对两种情况取最大值即可。综上可以得出:$$\begin{aligned}a_f &= \max(a_u, a_v, d_u + c_v) \\b_f &= b_u + b_v \\21 行未改动#define Kei_love_ARIS return#define cin_fast ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)#define fi first#define se second#define int long long#define in(a) a = read()#define rep(i , a , b) for(int i = a ; i <= b ; i ++)using namespace std;typedef long long ll;const int N = 5e5 + 5;const int inf = 0x3f3f3f3f;const long long INF = 0x3f3f3f3f3f3f3f3f;inline int read() {int x = 0;char ch = getchar();bool f = 0;22 行未改动ans.a = max(max(a , other.a) , d + other.c);ans.l = ans.r = inf;if(ans.a == a) {ans.l = l , ans.r = r;}if(ans.a == d + other.c) {int nl = rs , nr = other.ls;if(nl < ans.l) ans.l = nl , ans.r = nr;else if(nl == ans.l) ans.r = min(ans.r , nr);}if(ans.a == other.a){int nl = other.l , nr = other.r;if(nl < ans.l) ans.l = nl , ans.r = nr;else if(nl == ans.l) ans.r = min(ans.r , nr);}Kei_love_ARIS ans;}}d[N];int find(int x) {if(fa[x] != fa[fa[x]]){int root = find(fa[x]);d[x] = d[x] + d[fa[x]];fa[x] = root;}Kei_love_ARIS fa[x];}struct Q{42 行未改动5 行未改动678910111213141516171819202122232425262728293021 行未改动5253545556575859606162636465666722 行未改动9091929394959697989910010110210310410510610710810911011111211311411511642 行未改动5 行未改动进行 $q$ 次查询,求区间内的一个子区间满足其为区间内的最大子段和且该区间尽可能地靠左。### 思路为了方便,我们用 $a,b,c,d$ 分别来表示最大子段和、区间和、最大前缀和、最大后缀和,$u,v$ 表示合并的左右区间,$f$ 表示合并后的大区间。1. 最大子段和合并:有三种情况,如图:要么完全包含在左区间或右区间,要么由左区间的后缀与右区间的前缀拼接而成,对这三种情况取最大值就是合并后的最大子段和。2. 区间和合并:直接将两个区间和相加即可。3. 最大前缀和合并:有两种情况,第一种最大前缀和就是 $c_u$,第二种是 $b_u+c_v$,然后对两种情况取最大值。4. 最大后缀和合并:同最大前缀和合并一样,有两种情况,第一种是 $d_v$,第二种是 $b_v+d_u$,对两种情况取最大值即可。综上可以得出:$$\begin{aligned}a_f &= \max(a_u, a_v, d_u + c_v) \\b_f &= b_u + b_v \\21 行未改动#define Kei_love_ARIS return#define cin_fast ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)#define fi first#define se second#define int long long#define in(a) a = read()#define rep(i , a , b) for(int i = a ; i <= b ; i ++)using namespace std;typedef long long ll;const int N = 5e5 + 5;const int inf = 0x3f3f3f3f;const long long INF = 0x3f3f3f3f3f3f3f3f;inline int read() {int x = 0;char ch = getchar();bool f = 0;22 行未改动ans.a = max(max(a , other.a) , d + other.c);ans.l = ans.r = inf;if(ans.a == a) {ans.l = l , ans.r = r;}if(ans.a == d + other.c) {int nl = rs , nr = other.ls;if(nl < ans.l) ans.l = nl , ans.r = nr;else if(nl == ans.l) ans.r = min(ans.r , nr);}if(ans.a == other.a){int nl = other.l , nr = other.r;if(nl < ans.l) ans.l = nl , ans.r = nr;else if(nl == ans.l) ans.r = min(ans.r , nr);}Kei_love_ARIS ans;}}d[N];int find(int x) {if(fa[x] != fa[fa[x]]){int root = find(fa[x]);d[x] = d[x] + d[fa[x]];fa[x] = root;}Kei_love_ARIS fa[x];}struct Q{42 行未改动