#include<bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
#define __lg(x) 63-__builtin_clzll(x)
#define pow2(x) (1LL<<x)
void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
#ifdef local
void CHECK();
void setio(){
freopen("/Users/iantsai/cpp/input.txt","r",stdin);
freopen("/Users/iantsai/cpp/output.txt","w",stdout);
}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
#define TOI_is_so_de ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);setio();
const int mxn = 5e5+5, mod = 1e9+7;
int n, l[mxn], r[mxn], f[mxn], inv[mxn];
bool vis[mxn];
vector<pii>ord;
queue<int> to;
int fpow(int a, int b){
int res = 1;
while(b){
if(b&1) res = res*a%mod;
a=a*a%mod;
b >>= 1;
}
return res;
}
struct BIT{
int bit[mxn];
void init(){
for(int i=0;i<mxn;i++){
bit[i] = 0;
}
}
void modify(int p,int v){
for(;p<mxn;p+=p&-p){
bit[p] += v;
}
}
int query(int p){
int res = 0;
for(;p;p-=p&-p){
res += bit[p];
}
return res;
}
}bt;
signed main(){
TOI_is_so_de;
cin >> n;
f[0] = 1;
inv[0] = 1;
for(int i=1;i<=n;i++){
f[i] = f[i-1] * 2 % mod;
inv[i] = fpow(f[i], mod-2);
}
int val = f[n-1], ans = 0;
for(int i=1;i<=n;i++){
cin >> l[i];
}
for(int i=1;i<=n;i++){
cin >> r[i];
}
for(int H = 1000; H > 0; H--){
bt.init();
ord.clear();
for(int i=1;i<=n;i++){
ord.pb({1 + (l[i] >= H), i});
}
for(int i=1;i<=n;i++){
ord.pb({1 + (r[i] >= H), i});
}
set<int>st;
sort(all(ord));
reverse(all(ord));
int prev = -1;
for(auto [h, id] : ord){
while(sz(to) and h!=prev){
auto v = to.front();
to.pop();
if(vis[v]){
st.insert(v);
vis[v] = 0;
}
else{
bt.modify(v, 1);
vis[v] = 1;
}
}
auto it = st.lower_bound(id);
int temp = val;
if(it == st.begin()){
int x = bt.query(id - 1);
int val2 = f[x];
temp = temp * inv[x] % mod * (val2 - 1) % mod;
}
if(it == st.end()){
int x = bt.query(mxn-1) - bt.query(id);
int val2 = f[x];
temp = temp * inv[x] % mod * (val2 - 1) % mod;
}
to.push(id);
prev = h;
ans = (ans + temp) % mod;
}
while(sz(to)){
auto v = to.front();
to.pop();
vis[v] = 0;
}
debug(H, ans);
}
cout << ans << '\n';
#ifdef local
CHECK();
#endif
}
/*
input:
*/
#ifdef local
void CHECK(){
cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
function<bool(string,string)> compareFiles = [](string p1, string p2)->bool {
std::ifstream file1(p1);
std::ifstream file2(p2);
if (!file1.is_open() || !file2.is_open())return false;
std::string line1, line2;
while (getline(file1, line1) && getline(file2, line2)) {
if (line1 != line2)return false;
}
int cnta=0,cntb=0;
while(getline(file1,line1))cnta++;
while(getline(file2,line2))cntb++;
return cntb-cnta<=1;
};
bool check = compareFiles("output.txt","expected.txt");
if (check) cerr<<"ACCEPTED\n";
else cerr<<"WRONG ANSWER!\n";
}
#else
#endif
Compilation message
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:35:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:36:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
4696 KB |
Output is correct |
2 |
Correct |
79 ms |
4188 KB |
Output is correct |
3 |
Correct |
81 ms |
4184 KB |
Output is correct |
4 |
Correct |
75 ms |
4696 KB |
Output is correct |
5 |
Correct |
77 ms |
4700 KB |
Output is correct |
6 |
Correct |
80 ms |
4188 KB |
Output is correct |
7 |
Correct |
79 ms |
4188 KB |
Output is correct |
8 |
Correct |
74 ms |
4700 KB |
Output is correct |
9 |
Correct |
77 ms |
4188 KB |
Output is correct |
10 |
Correct |
78 ms |
4188 KB |
Output is correct |
11 |
Incorrect |
81 ms |
4184 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
6748 KB |
Output is correct |
2 |
Correct |
80 ms |
4184 KB |
Output is correct |
3 |
Correct |
81 ms |
4188 KB |
Output is correct |
4 |
Correct |
78 ms |
4188 KB |
Output is correct |
5 |
Correct |
79 ms |
4184 KB |
Output is correct |
6 |
Correct |
80 ms |
4188 KB |
Output is correct |
7 |
Correct |
75 ms |
4696 KB |
Output is correct |
8 |
Correct |
79 ms |
4696 KB |
Output is correct |
9 |
Correct |
73 ms |
4696 KB |
Output is correct |
10 |
Correct |
74 ms |
4696 KB |
Output is correct |
11 |
Correct |
79 ms |
4188 KB |
Output is correct |
12 |
Correct |
79 ms |
4184 KB |
Output is correct |
13 |
Correct |
79 ms |
4188 KB |
Output is correct |
14 |
Correct |
81 ms |
4188 KB |
Output is correct |
15 |
Correct |
85 ms |
4700 KB |
Output is correct |
16 |
Correct |
96 ms |
4696 KB |
Output is correct |
17 |
Correct |
89 ms |
4188 KB |
Output is correct |
18 |
Correct |
83 ms |
4696 KB |
Output is correct |
19 |
Correct |
88 ms |
4188 KB |
Output is correct |
20 |
Correct |
85 ms |
4184 KB |
Output is correct |
21 |
Correct |
83 ms |
4384 KB |
Output is correct |
22 |
Correct |
91 ms |
4388 KB |
Output is correct |
23 |
Correct |
85 ms |
4700 KB |
Output is correct |
24 |
Correct |
81 ms |
4696 KB |
Output is correct |
25 |
Correct |
85 ms |
4700 KB |
Output is correct |
26 |
Correct |
87 ms |
4700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
6748 KB |
Output is correct |
2 |
Correct |
80 ms |
4184 KB |
Output is correct |
3 |
Correct |
81 ms |
4188 KB |
Output is correct |
4 |
Correct |
78 ms |
4188 KB |
Output is correct |
5 |
Correct |
79 ms |
4184 KB |
Output is correct |
6 |
Correct |
80 ms |
4188 KB |
Output is correct |
7 |
Correct |
75 ms |
4696 KB |
Output is correct |
8 |
Correct |
79 ms |
4696 KB |
Output is correct |
9 |
Correct |
73 ms |
4696 KB |
Output is correct |
10 |
Correct |
74 ms |
4696 KB |
Output is correct |
11 |
Correct |
79 ms |
4188 KB |
Output is correct |
12 |
Correct |
79 ms |
4184 KB |
Output is correct |
13 |
Correct |
79 ms |
4188 KB |
Output is correct |
14 |
Correct |
81 ms |
4188 KB |
Output is correct |
15 |
Correct |
85 ms |
4700 KB |
Output is correct |
16 |
Correct |
96 ms |
4696 KB |
Output is correct |
17 |
Correct |
89 ms |
4188 KB |
Output is correct |
18 |
Correct |
83 ms |
4696 KB |
Output is correct |
19 |
Correct |
88 ms |
4188 KB |
Output is correct |
20 |
Correct |
85 ms |
4184 KB |
Output is correct |
21 |
Correct |
83 ms |
4384 KB |
Output is correct |
22 |
Correct |
91 ms |
4388 KB |
Output is correct |
23 |
Correct |
85 ms |
4700 KB |
Output is correct |
24 |
Correct |
81 ms |
4696 KB |
Output is correct |
25 |
Correct |
85 ms |
4700 KB |
Output is correct |
26 |
Correct |
87 ms |
4700 KB |
Output is correct |
27 |
Correct |
2013 ms |
5836 KB |
Output is correct |
28 |
Correct |
1957 ms |
5812 KB |
Output is correct |
29 |
Correct |
1927 ms |
5344 KB |
Output is correct |
30 |
Correct |
1923 ms |
5600 KB |
Output is correct |
31 |
Correct |
1952 ms |
5584 KB |
Output is correct |
32 |
Correct |
1951 ms |
6104 KB |
Output is correct |
33 |
Correct |
1871 ms |
5516 KB |
Output is correct |
34 |
Correct |
2053 ms |
5840 KB |
Output is correct |
35 |
Correct |
2071 ms |
5596 KB |
Output is correct |
36 |
Correct |
1817 ms |
5860 KB |
Output is correct |
37 |
Correct |
1969 ms |
5600 KB |
Output is correct |
38 |
Correct |
1977 ms |
5340 KB |
Output is correct |
39 |
Correct |
1978 ms |
5480 KB |
Output is correct |
40 |
Correct |
2085 ms |
5480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
4696 KB |
Output is correct |
2 |
Correct |
79 ms |
4188 KB |
Output is correct |
3 |
Correct |
81 ms |
4184 KB |
Output is correct |
4 |
Correct |
75 ms |
4696 KB |
Output is correct |
5 |
Correct |
77 ms |
4700 KB |
Output is correct |
6 |
Correct |
80 ms |
4188 KB |
Output is correct |
7 |
Correct |
79 ms |
4188 KB |
Output is correct |
8 |
Correct |
74 ms |
4700 KB |
Output is correct |
9 |
Correct |
77 ms |
4188 KB |
Output is correct |
10 |
Correct |
78 ms |
4188 KB |
Output is correct |
11 |
Incorrect |
81 ms |
4184 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
4376 KB |
Output is correct |
2 |
Correct |
77 ms |
4384 KB |
Output is correct |
3 |
Correct |
75 ms |
4740 KB |
Output is correct |
4 |
Correct |
85 ms |
4184 KB |
Output is correct |
5 |
Correct |
85 ms |
4184 KB |
Output is correct |
6 |
Correct |
81 ms |
4436 KB |
Output is correct |
7 |
Correct |
76 ms |
4700 KB |
Output is correct |
8 |
Correct |
88 ms |
4392 KB |
Output is correct |
9 |
Correct |
89 ms |
4188 KB |
Output is correct |
10 |
Correct |
1980 ms |
5344 KB |
Output is correct |
11 |
Correct |
2070 ms |
5340 KB |
Output is correct |
12 |
Correct |
2070 ms |
5340 KB |
Output is correct |
13 |
Correct |
1947 ms |
5340 KB |
Output is correct |
14 |
Execution timed out |
5060 ms |
44448 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
4696 KB |
Output is correct |
2 |
Correct |
79 ms |
4188 KB |
Output is correct |
3 |
Correct |
81 ms |
4184 KB |
Output is correct |
4 |
Correct |
75 ms |
4696 KB |
Output is correct |
5 |
Correct |
77 ms |
4700 KB |
Output is correct |
6 |
Correct |
80 ms |
4188 KB |
Output is correct |
7 |
Correct |
79 ms |
4188 KB |
Output is correct |
8 |
Correct |
74 ms |
4700 KB |
Output is correct |
9 |
Correct |
77 ms |
4188 KB |
Output is correct |
10 |
Correct |
78 ms |
4188 KB |
Output is correct |
11 |
Incorrect |
81 ms |
4184 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |