이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define vec vector
const int MOD = 1e9+7;
int MX = 10005;
void add(int32_t& a, int val) {
a += val;
a %= MOD;
}
void mul(int& a, int val) {
a *= val;
a %= MOD;
}
int32_t main() {
int N;
cin >> N;
vec<int> a(N), b(N);
bool flag = true;
for(int i = 0; i<N; i++) {
cin >> a[i];
flag &= a[i] <=2;
};
for(int i = 0; i<N; i++) {
cin >> b[i];
flag &= b[i] <=2;
};
if(flag) MX = 4;
set<int> vals;
for(int i = 0; i<N; i++) {
vals.insert(a[i]);
vals.insert(b[i]);
}
int id = 0;
vec<int> id_to_val(MX);
map<int, int> val_to_id;
for(int val : vals) {
val_to_id[val] = id;
id_to_val[id++] = val;
}
for(int i = 0; i<N; i++) {
a[i] = val_to_id[a[i]];
b[i] = val_to_id[b[i]];
}
vec<vec<int32_t>> mxcnt_left(N+2, vec<int32_t>(MX));
vec<vec<int32_t>> mxcnt_right(N+2, vec<int32_t>(MX));
mxcnt_left[0][0] = 1;
for(int i = 1; i<=N; i++) {
for(int j = 0; j<MX; j++) {
add(mxcnt_left[i][max(a[i-1], j)], mxcnt_left[i-1][j]);
add(mxcnt_left[i][max(b[i-1], j)], mxcnt_left[i-1][j]);
}
}
mxcnt_right[N+1][0] = 1;
for(int i = N; i>=1; i--) {
for(int j = 0; j<MX; j++) {
add(mxcnt_right[i][max(a[i-1], j)], mxcnt_right[i+1][j]);
add(mxcnt_right[i][max(b[i-1], j)], mxcnt_right[i+1][j]);
}
}
vec<vec<int32_t>> mnmx_cnt(N+2, vec<int32_t>(MX));
for(int i = 1; i<=N; i++) {
int32_t rightsum = 0;
for(int j = MX-1; j>=0; j--) {
add(rightsum, mxcnt_right[i+1][j]);
add(mnmx_cnt[i][j], (int)(mxcnt_left[i-1][j]*(int)rightsum)%MOD);
}
int32_t leftsum = 0;
for(int j = MX-1; j>=0; j--) {
add(mnmx_cnt[i][j], ((int)mxcnt_right[i+1][j]*(int)leftsum)%MOD);
add(leftsum, mxcnt_left[i-1][j]);
}
}
int32_t ans = 0;
for(int i = 1; i<=N; i++) {
for(int j = a[i-1]; j<MX; j++) {
add(ans, ((int)mnmx_cnt[i][j]*(int)(id_to_val[j]-id_to_val[a[i-1]]))%MOD);
}
for(int j = b[i-1]; j<MX; j++) {
add(ans, ((int)mnmx_cnt[i][j]*(int)(id_to_val[j]-id_to_val[b[i-1]]))%MOD);
}
}
cout << ans << '\n';
}
# | 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... |