이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define MAX 500005
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
ll n, m, k, tt;
pair<pll, ll> inp[MAX];
ll dp[MAX][30], sum[MAX][30];
inline void update(ll i, ll j) {
sum[i][j] = (sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + dp[i][j] + 2 * MOD) % MOD;
assert(sum[i][j] >= 0);
}
inline ll query(ll i1, ll j1, ll i2, ll j2) {
return (sum[i2][j2] - sum[i2][j1 - 1] - sum[i1 - 1][j2] + sum[i1 - 1][j1 - 1] + 2 * MOD) % MOD;
}
struct Node {
ll v;
Node() { v = 0; }
};
Node Merge(Node l, Node r) {
Node ret;
ret.v = max(l.v, r.v);
return ret;
}
struct SEG {
Node seg[MAX << 2];
void update(ll s, ll e, ll i, ll v, ll n) {
if(s == e) { seg[n].v = max(seg[n].v, v); return; }
ll m = (s + e) >> 1;
if(i <= m) update(s, m, i, v, n << 1);
else update(m + 1, e, i, v, n << 1 | 1);
seg[n] = Merge(seg[n << 1], seg[n << 1 | 1]);
}
Node query(ll s, ll e, ll l, ll r, ll n) {
if(s > r || e < l) return Node();
if(l <= s && e <= r) return seg[n];
ll m = (s + e) >> 1;
return Merge(query(s, m, l, r, n << 1), query(m + 1, e, l, r, n << 1 | 1));
}
};
SEG seg1, seg2;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(ll i = 1; i <= m; ++i) {
cin >> inp[i].first.first >> inp[i].first.second;
inp[i].second = inp[i].first.first < inp[i].first.second;
if(inp[i].first.first > inp[i].first.second) swap(inp[i].first.first, inp[i].first.second);
}
sort(inp + 1, inp + m + 1);
for(ll i = 1; i <= 26; ++i) dp[1][i] = 1, update(1, i);
for(ll i = 2, p = 1; i <= n; ++i) {
while(p <= m && inp[p].first.first < i) {
(inp[p].second ? seg1 : seg2).update(1, n, inp[p].first.second, inp[p].first.first, 1);
++p;
}
ll prv1 = seg1.query(1, n, i, n, 1).v + 1, prv2 = seg2.query(1, n, i, n, 1).v + 1;
for(ll c = 1; c <= 26; ++c) {
if(i > prv1 && c > 1) dp[i][c] += query(prv1, 1, i - 1, c - 1);
if(i > prv2 && c < 26) dp[i][c] += query(prv2, c + 1, i - 1, 26);
dp[i][c] %= MOD;
update(i, c);
}
}
ll ans = 0;
for(ll i = 1; i <= n; ++i) for(ll j = 1; j <= 26; ++j) ans = (ans + dp[i][j]) % MOD;
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |