ABCDE
F
离散化之后建图,dfs找环或叶子结点
——————————————————
G
没有什么可以有思维的。
就是遍历右节点,线段树每个叶子结点是1-i, 2-i, 3- i…i-i
维护的是max
然后我是想从顶往下找到左边叶子,这样不行,因为不知道rt<<1|1在不在范围内。所以还是结点维护的是max和leftnode,从底往上传递。
void pushup(int rt) {
if (maxx[rt << 1] >= maxx[rt << 1 | 1]) {
maxx[rt] = maxx[rt << 1];
pos[rt] = pos[rt << 1];
}
else {
maxx[rt] = maxx[rt << 1 | 1];
pos[rt] = pos[rt << 1 | 1];
}
}
void pushdown(int rt) {
if (lazy[rt]) {
lazy[rt << 1] += lazy[rt];
lazy[rt << 1 | 1] += lazy[rt];
maxx[rt << 1] += lazy[rt];
maxx[rt << 1 | 1] += lazy[rt];
lazy[rt] = 0;
}
}
void build(int l, int r, int rt) {
lazy[rt] = 0;
if (l == r) {
maxx[rt] = 0;
pos[rt] = l;
return;
}
int m = (l + r) >> 1;
build(l, m, rt << 1);
build(m + 1, r, rt << 1 | 1);
pushup(rt);
}
void update(int L, int R, ll c, int l, int r, int rt) {
if (L <= l && r <= R) {
lazy[rt] += c;
maxx[rt] += c;
return;
}
pushdown(rt);
int m = (l + r) >> 1;
if (m >= L)update(L, R, c, l, m, rt << 1);
if (m < R)update(L, R, c, m + 1, r, rt << 1 | 1);
pushup(rt);
}
int leftans = 0;
ll maxxans = 0;
void query(int L, int R, int l, int r, int rt)
{
if (L <= l && r <= R) {
if (maxx[rt] > maxxans) {
maxxans = maxx[rt];
leftans = pos[rt];
}
return;
}
pushdown(rt);
int m = (l + r) >> 1;
if (m >= L)query(L, R, l, m, rt << 1);
if (m < R)query(L, R, m + 1, r, rt << 1 | 1);
}
for (int i = 1; i <= m; i++) {
update(1, i, k, 1, m, 1);
for (int j = 0; j < leftt[i].size(); j++) {
update(1, leftt[i][j].left, leftt[i][j].p, 1, m, 1);
}
query(1, i, 1, m, 1);
if (maxxans > maxans) {
maxans = maxxans;
rmax = i;
lmax = leftans;
}
}