이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ent "\n"
const int maxn = 1e6 + 100;
const ll INF = (ll)1e18 + 100;
const int inf = 1e9 + 100;
const int MOD = 1e9 + 7;
const int maxl = 26;
const int P = 31;
int n;
int a[maxn][2];
vector<int> dp[maxn];
void upd(int &x, int y){
x = (x + y) % MOD;
}
void test(){
cin >> n;
vector<int> s = {-1};
for(int i = 1; i <= n; i++){
cin >> a[i][0];
}
for(int i = 1; i <= n; i++){
cin >> a[i][1];
s.push_back(a[i][0]);
s.push_back(a[i][1]);
}
sort(s.begin(), s.end());
s.resize(unique(s.begin(), s.end()) - s.begin());
for(int i = 1; i <= n; i++){
a[i][0] = lower_bound(s.begin(), s.end(), a[i][0]) - s.begin();
a[i][1] = lower_bound(s.begin(), s.end(), a[i][1]) - s.begin();
if(a[i][0] > a[i][1]) swap(a[i][0], a[i][1]);
}
dp[0] = vector<int>(s.size(), 0);
dp[0][0] = 1;
for(int i = 1; i <= n; i++){
dp[i] = dp[i-1];
for(int j = s.size()-1; j >= 0; j--){
ll x = dp[i][j];
dp[i][j] = 0;
upd(dp[i][max(j, a[i][1])], x);
upd(dp[i][max(j, a[i][0])], x);
}
}
vector<int> d = vector<int>(s.size(), 0);
d[0] = 1; ll ans = 0;
for(int i = n; i > 0; i--){
ll sum1 = 0, sum2 = 0;
for(int j = s.size() - 1; j >= 0; j--){
sum1 = (sum1 + dp[i-1][j]) % MOD;
ll cnt = (1ll * d[j] * sum1 + dp[i-1][j] * sum2) % MOD;
sum2 = (sum2 + d[j]) % MOD;
if(j > a[i][1]){
ans = (ans + 1ll * (s[j] - s[a[i][1]]) * cnt) % MOD;
} if(j > a[i][0]){
ans = (ans + 1ll * (s[j] - s[a[i][0]]) * cnt) % MOD;
}
}
for(int j = s.size()-1; j >= 0; j--){
ll x = d[j];
d[j] = 0;
upd(d[max(j, a[i][1])], x);
upd(d[max(j, a[i][0])], x);
}
}
cout << ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t; t = 1;
while(t--) test();
cout << ent;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |