Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

GYM104491B Standard Problem

B. Standard Problem

注意计算的方案数是选择哪些区间的方案数,而不是构成的不下降序列的方案数,考虑对于每一种选择区间的方案确定一种构成序列的方案并令只能这样选择数。我们令每一个区间选择的数为当前能选择的最小的数,即若当前最后一个数 $k$ 有 $k \le l _ i$ 那么选择 $l _ i$ ,若 $k > l _ i$ 那么选择 $k$ ,那么更新的时候就会有 $[1, l _ i]$ 对 $l _ i$ 有贡献, $[l _ i + 1, r _ i]$ 的每一个数对自己有贡献。维护区间最大值和最大值个数,需要实现单点修改和区间加。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <cstdio>
using namespace std;
typedef long long L;
const int N = 2e5 + 10, mod = 998244353;
struct Node
{
L v, tag;
int l, r, c;
} tr[N << 2];
int n, m;
void build (int l = 0, int r = m, int x = 1)
{
tr[x].l = l, tr[x].r = r;
tr[x].v = 0, tr[x].c = l == 0, tr[x].tag = 0;
if (l == r) return;
int mid = l + r >> 1;
build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1);
}
void pushup (Node &x, Node l, Node r)
{
x.l = l.l, x.r = r.r;
if (l.v == r.v) x.v = l.v, x.c = (l.c + r.c) % mod;
else if (l.v > r.v) x.v = l.v, x.c = l.c;
else if (l.v < r.v) x.v = r.v, x.c = r.c;
}
void add (int x, L k) { tr[x].v += k, tr[x].tag += k; }
void pushdown (int x)
{
add(x << 1, tr[x].tag), add(x << 1 | 1, tr[x].tag);
tr[x].tag = 0;
}
Node query (int l, int r, int x = 1)
{
if (tr[x].l >= l && tr[x].r <= r) return tr[x];
pushdown(x);
int mid = tr[x].l + tr[x].r >> 1;
if (r <= mid) return query(l, r, x << 1);
if (l > mid) return query(l, r, x << 1 | 1);
Node res; pushup(res, query(l, r, x << 1), query(l, r, x << 1 | 1));
return res;
}
void change (int t, L v, int c, int x = 1)
{
if (tr[x].l == tr[x].r)
{
tr[x].v = v, tr[x].c = c;
return;
}
pushdown(x);
int mid = tr[x].l + tr[x].r >> 1;
if (t <= mid) change(t, v, c, x << 1);
if (t > mid) change(t, v, c, x << 1 | 1);
pushup(tr[x], tr[x << 1], tr[x << 1 | 1]);
}
void modify (int l, int r, L k, int x = 1)
{
if (tr[x].l > r || tr[x].r < l) return;
if (tr[x].l >= l && tr[x].r <= r)
return add(x, k);
pushdown(x);
modify(l, r, k, x << 1), modify(l, r, k, x << 1 | 1);
pushup(tr[x], tr[x << 1], tr[x << 1 | 1]);
}
int main ()
{
int T; scanf("%d", &T);
while (T--)
{
scanf("%d%d", &n, &m);
build();
for (int i = 1, l, r, v; i <= n; ++i)
{
scanf("%d%d%d", &l, &r, &v);
Node t = query(0, l);
t.v += v;
change(l, t.v, t.c);
if (l ^ r) modify(l + 1, r, v);
}
Node res = query(0, m);
printf("%lld %d\n", res.v, res.c);
}
return 0;
}