이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int add(int x, int y) { x += y; return x >= mod ? x - mod : x; }
int sub(int x, int y) { x -= y; return x < 0 ? x + mod : x; }
void sadd(int& x, int y) { x = add(x, y); }
void ssub(int& x, int y) { x = sub(x, y); }
struct Fenwick {
int n;
vector<int> fenw;
Fenwick() { }
Fenwick(int _n) {
n = _n;
fenw.resize(n);
for(int i = 0; i < n; i++) fenw[i] = 0;
}
void update(int x, int val) { x++; for(int i = x; i < n; i += (i & -i)) sadd(fenw[i], val); }
int get(int r) {
r++;
int ans = 0;
for(int i = r; i >= 1; i -= (i & -i)) sadd(ans, fenw[i]);
return ans;
}
int get(int l, int r) {
if(l > r) return 0;
return sub(get(r), get(l - 1));
}
};
const int N = 5e5 + 10, M = 26;
int n, m;
int a[N], b[N];
int dp[N][M][2];
int important[N][2];
int first[N];
int pref[N][M][2];
vector<Fenwick> fw(M);
vector<int> down[N], up[N];
set<int> s;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for(int i = 0; i < m; i++) {
cin >> a[i] >> b[i];
if(a[i] < b[i]) down[b[i]].push_back(a[i]);
else up[a[i]].push_back(b[i]);
}
for(int i = n; i >= 1; i--) {
auto it = s.upper_bound(i);
if(it == s.begin()) important[i][0] = 0;
else {
it--;
important[i][0] = *it;
}
for(int j : down[i]) s.insert(j);
}
s.clear();
for(int i = n; i >= 1; i--) {
auto it = s.upper_bound(i);
if(it == s.begin()) important[i][1] = 0;
else {
it--;
important[i][1] = *it;
}
for(int j : up[i]) s.insert(j);
}
for(int i = 1; i <= n; i++) {
if(important[i][0] > important[i][1]) first[i] = 1;
else {
first[i] = 2;
swap(important[i][0], important[i][1]);
}
}
for(int i = 0; i < M; i++) fw[i] = Fenwick(n + 2);
for(int c = 0; c < M; c++) {
dp[0][c][0] = dp[0][c][1] = 1;
fw[c].update(0, 1);
}
// for(int i = 1; i <= n; i++) cout << i << ": " << important[i][0] << " " << important[i][1] << " " << first[i] << "\n";
for(int i = 1; i <= n; i++) {
for(int c = 0; c < M; c++) {
dp[i][c][0] = fw[c].get(important[i][0], i - 1);
dp[i][c][1] = fw[c].get(important[i][1], important[i][0] - 1);
// if(important[i][0] == 0) sadd(dp[i][c][0], 1);
// if(important[i][1] == 0 && important[i][0] > 0) sadd(dp[i][c][1], 1);
}
pref[i][0][0] = dp[i][0][0];
pref[i][0][1] = dp[i][0][1];
for(int c = 1; c < M; c++) pref[i][c][0] = add(pref[i][c - 1][0], dp[i][c][0]);
for(int c = 1; c < M; c++) pref[i][c][1] = add(pref[i][c - 1][1], dp[i][c][1]);
for(int nwc = 0; nwc < M; nwc++) {
fw[nwc].update(i, add(sub(pref[i][M - 1][0], pref[i][nwc][0]), (nwc > 0 ? pref[i][nwc - 1][0] : 0)));
if(first[i] == 1) fw[nwc].update(i, sub(pref[i][M - 1][1], pref[i][nwc][1]));
else if(first[i] == 2 && nwc > 0) fw[nwc].update(i, pref[i][nwc - 1][1]);
}
}
// for(int i = 1; i <= n; i++) {
// for(int j = 0; j < M; j++) cout << dp[i][j][0] + dp[i][j][1] << " ";
// cout << "\n";
// }
int ans = 0;
for(int c = 0; c < M; c++) {
sadd(ans, dp[n][c][0]);
sadd(ans, dp[n][c][1]);
}
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... |