-
Notifications
You must be signed in to change notification settings - Fork 0
/
abc179_d.cpp
257 lines (233 loc) · 5.8 KB
/
abc179_d.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.cpp"
#else
#define dbg(...)
#endif
#define endl '\n'
#define fastio \
ios_base::sync_with_stdio(0); \
cin.tie(0);
// #define int long long
#define len(__x) (int)__x.size()
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using vll2d = vector<vll>;
using vi = vector<int>;
using vi2d = vector<vi>;
using pii = pair<int, int>;
using vii = vector<pii>;
using vc = vector<char>;
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define rep(i, begin, end) \
for (__typeof(begin) i = (begin) - ((begin) > (end)); \
i != (end) - ((begin) > (end)); \
i += 1 - 2 * ((begin) > (end)))
int lg2(ll x) {
return __builtin_clzll(1) - __builtin_clzll(x);
}
// vector<string> dir({"LU", "U", "RU", "R", "RD", "D",
// "LD", "L"}); int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0}; int
// dy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
vector<string> dir({"U", "R", "D", "L"});
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
const ll OO = 1e18;
const int oo = 1e9;
int T(1);
const ll MOD = 998244353;
template <ll _mod = MOD>
struct mint {
ll value;
static const ll MOD_value = _mod;
mint(ll v = 0) {
value = v % _mod;
if (value < 0) value += _mod;
}
mint(ll a, ll b) : value(0) {
*this += a;
*this /= b;
}
mint& operator+=(mint const& b) {
value += b.value;
if (value >= _mod) value -= _mod;
return *this;
}
mint& operator-=(mint const& b) {
value -= b.value;
if (value < 0) value += _mod;
return *this;
}
mint& operator*=(mint const& b) {
value = (ll)value * b.value % _mod;
return *this;
}
friend mint mexp(mint a, ll e) {
mint res = 1;
while (e) {
if (e & 1) res *= a;
a *= a;
e >>= 1;
}
return res;
}
friend mint inverse(mint a) { return mexp(a, _mod - 2); }
mint& operator/=(mint const& b) {
return *this *= inverse(b);
}
friend mint operator+(mint a, mint const b) {
return a += b;
}
mint operator++(int) {
return this->value = (this->value + 1) % _mod;
}
mint operator++() {
return this->value = (this->value + 1) % _mod;
}
friend mint operator-(mint a, mint const b) {
return a -= b;
}
friend mint operator-(mint const a) { return 0 - a; }
mint operator--(int) {
return this->value = (this->value - 1 + _mod) % _mod;
}
mint operator--() {
return this->value = (this->value - 1 + _mod) % _mod;
}
friend mint operator*(mint a, mint const b) {
return a *= b;
}
friend mint operator/(mint a, mint const b) {
return a /= b;
}
friend std::ostream& operator<<(std::ostream& os,
mint const& a) {
return os << a.value;
}
friend bool operator==(mint const& a, mint const& b) {
return a.value == b.value;
}
friend bool operator!=(mint const& a, mint const& b) {
return a.value != b.value;
}
};
struct Lnode {
mint<MOD> v;
bool assign;
Lnode() : v(), assign() {} // Neutral element
Lnode(mint<MOD> _v, bool a = 0) : v(_v), assign(a){};
};
using Qnode = mint<MOD>;
using Unode = Lnode;
struct LSegTree {
int n, ql, qr;
vector<Qnode> st;
vector<Lnode> lz;
/*-8<--------------------------------------------------------------->8-*/
Qnode merge(Qnode lv, Qnode rv, int nl, int nr) {
return lv + rv;
}
void prop(int i, int l, int r) {
if (lz[i].assign) {
st[i] = lz[i].v * (r - l + 1);
if (l != r) lz[tol(i)] = lz[tor(i)] = lz[i];
} else {
st[i] += lz[i].v * (r - l + 1);
if (l != r)
lz[tol(i)].v += lz[i].v, lz[tor(i)].v += lz[i].v;
}
lz[i] = Lnode();
}
void applyV(int i, Unode v) {
if (v.assign) {
lz[i] = v;
} else {
lz[i].v += v.v;
}
}
/*-8<--------------------------------------------------------------->8-*/
LSegTree() {}
LSegTree(int _n) : n(_n), st(_n << 2), lz(_n << 2) {}
bool disjoint(int l, int r) { return qr < l or r < ql; }
bool contains(int l, int r) {
return ql <= l and r <= qr;
}
int tol(int i) { return i << 1; }
int tor(int i) { return i << 1 | 1; }
void build(vector<Qnode>& v) { build(v, 1, 0, n - 1); }
void build(vector<Qnode>& v, int i, int l, int r) {
if (l == r) {
st[i] = v[l];
return;
}
int m = midpoint(l, r);
build(v, tol(i), l, m);
build(v, tor(i), m + 1, r);
st[i] = merge(st[tol(i)], st[tor(i)], l, r);
}
void upd(int l, int r, Unode v) {
ql = l, qr = r;
upd(1, 0, n - 1, v);
}
void upd(int i, int l, int r, Unode v) {
prop(i, l, r);
if (disjoint(l, r)) return;
if (contains(l, r)) {
applyV(i, v);
prop(i, l, r);
return;
}
int m = midpoint(l, r);
upd(tol(i), l, m, v);
upd(tor(i), m + 1, r, v);
st[i] = merge(st[tol(i)], st[tor(i)], l, r);
}
Qnode qry(int l, int r) {
ql = l, qr = r;
return qry(1, 0, n - 1);
}
Qnode qry(int i, int l, int r) {
prop(i, l, r);
if (disjoint(l, r)) return Qnode();
if (contains(l, r)) return st[i];
int m = midpoint(l, r);
return merge(qry(tol(i), l, m), qry(tor(i), m + 1, r),
l, r);
}
};
auto run() {
int N, K;
cin >> N >> K;
vector<pii> S(K);
for (auto& [a, b] : S) cin >> a >> b;
sort(all(S));
LSegTree st(N);
st.upd(0, 0, Lnode(1));
for (int i = 0; i < N; i++) {
for (auto [l, r] : S) {
if (i + l >= N) break;
st.upd(l + i, min(N - 1, i + r), st.qry(i, i));
if (i + r >= N) break;
}
}
cout << st.qry(N - 1, N - 1) << '\n';
}
int32_t main(void) {
#ifndef LOCAL
fastio;
#endif
// cin >> T;
for (int t = 1; t <= T; t++) {
run();
}
}
// AC, dp, data structures