QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#894170 | #8712. Flooding Wall | sitablechair | 0 | 1ms | 3840kb | C++23 | 4.6kb | 2025-02-11 13:32:36 | 2025-02-11 13:32:37 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
#define ldb long double
#define endl '\n'
#define For(i,l,r) for(int i=l;i<=r;i++)
#define ForD(i,r,l) for(int i=r;i>=l;i--)
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define sz(x) (signed)x.size()
#define unq(x) x.resize(unique(all(x))-x.begin())
#define F "TASK"
#define fio freopen(F".INP","r",stdin);freopen(F".OUT","w",stdout);
#ifdef NCGM
#include"debug.h"
#else
#define debug(...) "fr";
#endif
using namespace std;
const int N=10000,MOD=1e9+7,INV=500000004;
struct SegTree {
ll tr[N*4];
void reset(int id,int l,int r) {
tr[id]=0;
if (l==r) return;
int mid=l+r>>1;
reset(id*2,l,mid);
reset(id*2+1,mid+1,r);
}
void update(int id,int l,int r,int u,int x) {
if (l>u || r<u) return;
if (l==r) return void(tr[id]=x);
int mid=l+r>>1;
update(id*2,l,mid,u,x);
update(id*2+1,mid+1,r,u,x);
tr[id]=tr[id*2]*tr[id*2+1]%MOD;
}
ll query(int id,int l,int r,int u,int v) {
if (l>v || r<u) return 1;
if (l>=u && r<=v) return tr[id];
int mid=l+r>>1;
return query(id*2,l,mid,u,v)*query(id*2+1,mid+1,r,u,v)%MOD;
}
} st;
int n,a[N],b[N],c[N],lft[N][2],lft1[N][2],rgt[N][2],pw[N*2];
vector<pair<int,int>> in[N*2];
vector<ll> big;
ll ans=0;
void solve() {
cin >> n;
pw[0]=1;
For(i,1,n) pw[i]=pw[i-1]*2%MOD;
For(i,1,n) {
cin >> a[i];
big.pb(a[i]);
}
For(i,1,n) {
cin >> b[i];
big.pb(b[i]);
}
sort(all(big));
unq(big);
For(i,1,n) in[lower_bound(all(big),a[i])-big.begin()+1].pb({i,0});
For(i,1,n) in[lower_bound(all(big),b[i])-big.begin()+1].pb({i,1});
For(i,1,n) c[i]=0;
st.reset(1,1,n);
ll cc=1;
For(i,1,n-1) cc=cc*2%MOD;
For(i,1,sz(big)+1) {
for(auto el: in[i-1]) {
c[el.ff]++;
st.update(1,1,n,el.ff,c[el.ff]);
}
for(auto el: in[i-1]) {
if (el.ff>1 && el.ff<n) {
ans=(ans+1LL*(el.ss?b[el.ff]:a[el.ff])*st.query(1,1,n,1,el.ff-1)%MOD*pw[n-el.ff]%MOD)%MOD;
ans=(ans+1LL*(el.ss?b[el.ff]:a[el.ff])*st.query(1,1,n,el.ff+1,n)%MOD*pw[el.ff-1]%MOD)%MOD;
ans=(ans-1LL*(el.ss?b[el.ff]:a[el.ff])*st.query(1,1,n,1,el.ff-1)%MOD*st.query(1,1,n,el.ff+1,n)%MOD)%MOD;
}
else ans=(ans+1LL*(el.ss?b[el.ff]:a[el.ff])*cc%MOD)%MOD;
}
for(auto el: in[i-1]) {
if (el.ff>1) lft[el.ff][el.ss]=st.query(1,1,n,1,el.ff-1);
else lft[el.ff][el.ss]=1;
if (el.ff<n) rgt[el.ff][el.ss]=st.query(1,1,n,el.ff+1,n);
else rgt[el.ff][el.ss]=1;
}
for(auto el: in[i]) {
if (el.ff>1) lft1[el.ff][el.ss]=st.query(1,1,n,1,el.ff-1);
else lft1[el.ff][el.ss]=1;
}
}
For(i,1,n) c[i]=0;
For(i,1,sz(big)) {
for(auto el: in[i-1]) c[el.ff]++;
for(auto el: in[i]) {
ll tmp=1;
ForD(j,el.ff-1,1) {
if (tmp==0) break;
if (a[j]>big[i-1]) ans=(ans+big[i-1]*tmp%MOD*pw[j-1]%MOD*rgt[el.ff][el.ss]%MOD*(el.ff-j-1)%MOD)%MOD;
else if (a[j]==big[i-1]) {
ans=(ans+big[i-1]*tmp%MOD*(pw[j-1]-lft1[j][0])%MOD*rgt[el.ff][el.ss]%MOD*(el.ff-j-1)%MOD)%MOD;
ans=(ans+big[i-1]*tmp%MOD*lft1[j][0]%MOD*pw[n-el.ff]%MOD*(el.ff-j-1)%MOD)%MOD;
}
if (b[j]>big[i-1]) ans=(ans+big[i-1]*tmp%MOD*pw[j-1]%MOD*rgt[el.ff][el.ss]%MOD*(el.ff-j-1)%MOD)%MOD;
else if (b[j]==big[i-1]) {
ans=(ans+big[i-1]*tmp%MOD*(pw[j-1]-lft1[j][1])%MOD*rgt[el.ff][el.ss]%MOD*(el.ff-j-1)%MOD)%MOD;
ans=(ans+big[i-1]*tmp%MOD*lft1[j][1]%MOD*pw[n-el.ff]%MOD*(el.ff-j-1)%MOD)%MOD;
}
tmp=tmp*c[j]%MOD;
}
tmp=1;
For(j,el.ff+1,n) {
if (tmp==0) break;
if (a[j]>big[i-1]) ans=(ans+big[i-1]*tmp%MOD*pw[n-j]%MOD*lft[el.ff][el.ss]%MOD*(j-el.ff-1)%MOD)%MOD;
if (b[j]>big[i-1]) ans=(ans+big[i-1]*tmp%MOD*pw[n-j]%MOD*lft[el.ff][el.ss]%MOD*(j-el.ff-1)%MOD)%MOD;
tmp=tmp*c[j]%MOD;
}
}
}
For(i,1,n) ans=(ans-1LL*a[i]*cc%MOD)%MOD;
For(i,1,n) ans=(ans-1LL*b[i]*cc%MOD)%MOD;
ans=(ans+MOD)%MOD;
cout << ans;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 8
Accepted
time: 0ms
memory: 3712kb
input:
4 1 1 1 1 2 2 2 2
output:
6
result:
ok single line: '6'
Test #2:
score: 8
Accepted
time: 1ms
memory: 3840kb
input:
10 1 2 3 4 5 6 7 8 9 10 10 9 8 7 6 5 4 3 2 1
output:
21116
result:
ok single line: '21116'
Test #3:
score: 8
Accepted
time: 0ms
memory: 3712kb
input:
1 1 2
output:
0
result:
ok single line: '0'
Test #4:
score: 8
Accepted
time: 1ms
memory: 3584kb
input:
2 1 1 2 2
output:
0
result:
ok single line: '0'
Test #5:
score: 8
Accepted
time: 0ms
memory: 3712kb
input:
3 1 1 1 2 2 2
output:
1
result:
ok single line: '1'
Test #6:
score: 8
Accepted
time: 0ms
memory: 3712kb
input:
3 1 1 1 3 2 3
output:
3
result:
ok single line: '3'
Test #7:
score: 8
Accepted
time: 1ms
memory: 3712kb
input:
3 2 1 1 3 2 3
output:
4
result:
ok single line: '4'
Test #8:
score: 8
Accepted
time: 0ms
memory: 3840kb
input:
3 1 1 2 3 2 3
output:
4
result:
ok single line: '4'
Test #9:
score: 8
Accepted
time: 1ms
memory: 3840kb
input:
4 1 1 2 2 2 2 1 1
output:
6
result:
ok single line: '6'
Test #10:
score: 8
Accepted
time: 1ms
memory: 3712kb
input:
3 1 4 4 3 1 1
output:
2
result:
ok single line: '2'
Test #11:
score: 8
Accepted
time: 0ms
memory: 3712kb
input:
20 801072306 297281669 94099424 745640358 822582129 579751930 776707816 425952653 529794053 256263083 615445445 401366545 990263003 967996508 788867983 916880116 837997768 346996508 623409388 122077161 141734988 448434751 822901346 825591177 388082644 468025968 260356829 1164654 537396602 730502935 ...
output:
840988190
result:
ok single line: '840988190'
Test #12:
score: 8
Accepted
time: 1ms
memory: 3840kb
input:
15 792312195 195812430 401437979 703756992 502275277 612055402 556065888 470806179 556125857 640437896 240743729 861383776 500299988 911972088 161499505 167335533 694282920 379241393 144223073 973249939 83979787 962250505 359871412 130233016 655843497 928153117 542969567 974346871 369758881 962874102
output:
617169726
result:
ok single line: '617169726'
Test #13:
score: 8
Accepted
time: 0ms
memory: 3712kb
input:
20 1 1 2 1 1 1 2 2 2 2 2 1 1 1 1 2 2 1 2 1 2 2 1 2 2 2 1 1 1 1 1 2 2 2 2 1 1 2 1 2
output:
8388630
result:
ok single line: '8388630'
Test #14:
score: 8
Accepted
time: 0ms
memory: 3712kb
input:
15 2 1 1 2 1 2 2 1 2 1 2 1 2 2 1 1 2 2 1 2 1 1 2 1 2 1 2 1 1 2
output:
180241
result:
ok single line: '180241'
Test #15:
score: 0
Wrong Answer
time: 1ms
memory: 3584kb
input:
20 10 3 8 2 5 7 8 3 3 10 5 6 1 2 1 9 10 2 7 10 6 6 2 3 6 10 4 6 7 2 3 3 5 7 2 8 2 1 5 1
output:
76218368
result:
wrong answer 1st lines differ - expected: '78020608', found: '76218368'
Subtask #2:
score: 0
Wrong Answer
Test #23:
score: 17
Accepted
time: 0ms
memory: 3584kb
input:
100 948 425 211 688 416 81 356 602 712 954 117 522 797 75 281 491 662 669 78 156 939 526 929 937 916 619 166 777 48 898 449 278 298 714 668 755 679 38 389 602 195 135 672 833 655 541 473 27 596 274 351 353 598 993 837 246 950 99 179 751 481 843 550 195 964 279 806 82 330 599 124 756 649 838 513 625 ...
output:
164439470
result:
ok single line: '164439470'
Test #24:
score: 17
Accepted
time: 1ms
memory: 3712kb
input:
99 426 562 392 187 480 118 86 459 941 159 104 108 323 616 104 679 464 929 60 387 335 298 540 352 520 152 76 233 69 50 422 839 236 113 79 782 44 253 791 857 393 767 297 267 23 523 274 514 489 58 217 361 408 753 990 491 787 529 759 351 668 348 811 737 764 672 471 115 815 701 604 348 708 512 227 707 20...
output:
295469554
result:
ok single line: '295469554'
Test #25:
score: 17
Accepted
time: 1ms
memory: 3840kb
input:
100 2 1 1 2 2 2 2 1 2 2 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 1 2 1 1 2 1 2 2 1 2 2 2 1 1 1 1 2 2 2 2 2 2 2 1 2 1 1 1 1 2 2 2 2 2 2 1 2 1 2 1 2 2 1 1 2 2 1 1 2 1 2 2 2 1 1 1 1 1 1 2 2 1 2 1 1 2 1 1 1 2 1 1 2 2 1 1 2 2 1 1 1 1 2 1 1 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 2 1 2 2 1 2 1 1 2 1 1 1 2 2 2 2 1 1 1 1 1 1 1 ...
output:
865821460
result:
ok single line: '865821460'
Test #26:
score: 17
Accepted
time: 1ms
memory: 3712kb
input:
97 2 1 2 1 1 1 2 2 2 1 2 1 2 1 2 2 1 1 2 1 1 1 1 2 2 2 1 1 1 2 1 2 2 2 1 1 1 1 2 2 1 1 1 1 1 1 1 1 2 2 1 2 2 1 1 1 2 1 2 2 2 2 2 1 2 1 1 2 1 1 2 2 2 2 1 2 2 1 1 1 1 2 1 2 2 2 1 1 2 2 1 1 2 2 1 2 2 1 2 1 2 2 2 1 1 1 2 1 2 1 2 1 1 2 2 1 2 2 2 2 1 1 1 2 2 2 1 2 1 1 1 2 2 2 2 1 1 2 2 2 2 2 2 2 2 1 1 2 1...
output:
237658155
result:
ok single line: '237658155'
Test #27:
score: 0
Wrong Answer
time: 1ms
memory: 3840kb
input:
100 2 7 7 7 10 10 5 3 8 7 4 4 3 3 9 9 3 4 10 10 2 2 8 6 8 5 8 5 6 5 9 7 9 4 9 4 2 6 4 7 8 8 1 4 2 4 5 3 6 8 2 5 6 1 4 10 10 1 8 7 4 9 2 8 9 5 7 8 9 8 3 5 4 7 6 3 3 1 9 4 1 4 9 1 10 3 3 9 10 7 7 8 5 8 4 10 8 4 1 4 6 8 5 8 9 2 9 9 7 3 10 1 4 10 7 1 4 1 6 8 5 7 7 7 9 3 2 3 1 9 7 2 6 5 7 9 10 1 8 1 2 6 ...
output:
77617894
result:
wrong answer 1st lines differ - expected: '12245240', found: '77617894'
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Skipped
Dependency #1:
0%
Subtask #5:
score: 0
Runtime Error
Test #57:
score: 0
Runtime Error
input:
500000 1 1 1 1 2 2 2 1 1 1 1 1 2 2 1 1 2 2 2 2 1 2 1 1 2 1 1 1 1 2 1 2 2 1 1 2 2 2 1 1 2 2 2 2 1 2 2 2 2 1 2 2 2 1 2 2 2 1 2 2 2 2 2 2 2 2 1 1 2 1 2 1 2 2 1 2 2 2 2 2 2 2 1 1 1 2 2 2 1 1 2 1 2 2 1 2 2 1 2 2 2 1 2 2 2 2 2 2 2 2 1 1 1 2 1 2 1 1 2 1 2 1 2 2 1 1 2 1 1 1 1 1 2 2 2 2 2 2 1 2 2 1 1 2 1 2 1...
output:
result:
Subtask #6:
score: 0
Skipped
Dependency #1:
0%