이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<stdio.h>
#include<stdint.h>
#include<vector>
#include<algorithm>
#include<utility>
#include<set>
#include<map>
#include<queue>
#include<stack>
using namespace std;
const int64_t INF = 1e9 + 777;
const int64_t mINF = 1e9;
const int64_t MOD = 1e9 + 7;
const int nbit = 11;
const int ndig = 10;
const int nchar = 26;
const int D = 4;
int dr[D] = {0, 1, 0, -1};
int dc[D] = {1, 0, -1, 0};
struct Solution
{
int n;
int m;
vector<vector<int> > s;
vector<vector<int> > g;
Solution() {}
void solve()
{
cin >> n >> m;
s.resize(n); g.resize(n);
for(int i = 0; i < m; i++)
{
int a; int b;
cin >> a >> b;
a--; b--;
if(a < b) s[a].push_back(b);
if(a > b) g[b].push_back(a);
}
set<int> sm;
set<int> gr;
vector<int> sums(nchar, 0);
vector<int> sumg(nchar, 0);
vector<vector<int> > dp(n, vector<int>(nchar, 1));
for(int i = n - 2; i >= 0; i--)
{
sm.insert(i + 1);
gr.insert(i + 1);
for(int j = 0; j < nchar; j++)
{
sums[j] = modadd(sums[j], dp[i + 1][j]);
sumg[j] = modadd(sumg[j], dp[i + 1][j]);
}
for(int j = 0; j < (int) s[i].size(); j++)
{
int r = s[i][j];
while(!gr.empty())
{
int id = *(gr.begin());
if(id > r) break; // i -> r no greater
for(int k = 0; k < nchar; k++)
{
sumg[k] = modsub(sumg[k], dp[id][k]);
}
gr.erase(gr.begin());
}
}
for(int j = 0; j < (int) g[i].size(); j++)
{
int r = g[i][j];
while(!sm.empty())
{
int id = *(sm.begin());
if(id > r) break; // i -> r no smaller
for(int k = 0; k < nchar; k++)
{
sums[k] = modsub(sums[k], dp[id][k]);
}
sm.erase(sm.begin());
}
}
int x = 0;
for(int j = 0; j < nchar; j++)
{
dp[i][j] = modadd(dp[i][j], x);
x = modadd(x, sums[j]);
}
int y = 0;
for(int j = nchar - 1; j >= 0; j--)
{
dp[i][j] = modadd(dp[i][j], y);
y = modadd(y, sumg[j]);
}
}
int ans = 0;
for(int i = 0; i < nchar; i++)
{
ans = modadd(ans, dp[0][i]);
}
cout << ans << "\n";
}
int modsub(int t1, int t2)
{
int res = t1 - t2;
if(res < 0) res += MOD;
return res;
}
int modadd(int t1, int t2)
{
int res = t1 + t2;
if(res >= MOD) res -= MOD;
return res;
}
int modmul(int t1, int t2)
{
int64_t res = 1LL * t1 * t2;
return (res % MOD);
}
bool BIT(int& mask, int& j)
{
return (mask & MASK(j));
}
int MASK(int j)
{
return (1 << j);
}
};
void __startup()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
}
int main()
{
__startup();
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
Solution().solve();
}
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... |