Submission #2171410
Source Code Expand
// #include {{{
#include <iostream>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <bitset>
#include <vector>
#include <complex>
#include <algorithm>
using namespace std;
// }}}
// #define {{{
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << (x) << endl
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
// }}}
const int N = 1e5 + 10;
const ll inf = 1e18;
struct Segment {
int n;
pair<ll,int> mn[N << 2];
ll add[N << 2];
void Add(int c,int l,int r,int L,int R,ll x) {
if(L <= l && r <= R) {
add[c] += x;
mn[c].fi += x;
} else {
int m = (l + r) >> 1;
if(L <= m) Add(c << 1 , l , m , L , R , x);
if(R > m) Add(c << 1 | 1 , m + 1 , r , L , R , x);
mn[c] = min(mn[c << 1] , mn[c << 1 | 1]);
mn[c].fi += add[c];
}
}
pair<ll,int> Query(int c,int l,int r,int L,int R) {
if(L <= l && r <= R) {
return mn[c];
} else {
int m = (l + r) >> 1;
pair<ll,int> res(inf,-1);
if(L <= m) res = min(res , Query(c << 1 , l , m , L , R));
if(R > m) res = min(res , Query(c << 1 | 1 , m + 1 , r , L , R));
res.fi += add[c];
return res;
}
}
void Build(int c,int l,int r) {
add[c] = 0;
if(l == r) {
mn[c] = mp(0 , l);
} else {
int m = (l + r) >> 1;
Build(c << 1 , l , m);
Build(c << 1 | 1 , m + 1 , r);
mn[c] = min(mn[c << 1] , mn[c << 1 | 1]);
}
}
void Add(int L,int R,ll x) {
Add(1 , 0 , n - 1 , L , R , x);
}
pair<ll,int> Query(int L,int R) {
return Query(1 , 0 , n - 1 , L , R);
}
void init(int _n) {
n = _n;
Build(1 , 0 , n - 1);
}
};
Segment A , B;
int n;
ll a[N] , b[N];
int main(){
scanf("%d",&n);
rep(i,0,n) scanf("%lld",a+i);
rep(i,0,n) scanf("%lld",b+i);
A.init(n);
B.init(n);
ll del = 0;
ll ans = 0;
rep(i,0,n) {
del += a[i] - b[i];
ans += abs(del);
A.Add(0 , i , del > 0 ? -1 : 1);
B.Add(i , i , del > 0 ? del : inf);
}
while(del) {
pair<ll,int> x = A.Query(0 , n - 1);
pair<ll,int> y = B.Query(x.se , n - 1);
ll way = min(del , y.fi);
ans += x.fi * way;
del -= way;
B.Add(x.se , n - 1 , -way);
for(int i = x.se;i < n;) {
pair<ll,int> z = B.Query(i , n - 1);
if(z.fi) break;
A.Add(0 , z.se , 2);
B.Add(z.se , z.se , inf);
i = z.se + 1;
}
}
printf("%lld\n",ans);
return 0;
}
Submission Info
Submission Time |
|
Task |
H - WAAAAAAAAAAAAALL |
User |
Y_UME |
Language |
C++14 (GCC 5.4.1) |
Score |
200 |
Code Size |
2923 Byte |
Status |
AC |
Exec Time |
125 ms |
Memory |
19456 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:94:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
^
./Main.cpp:95:31: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
rep(i,0,n) scanf("%lld",a+i);
^
./Main.cpp:96:31: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
rep(i,0,n) scanf("%lld",b+i);
^
Judge Result
Set Name |
Subtask1 |
Subtask2 |
All |
Score / Max Score |
30 / 30 |
30 / 30 |
140 / 140 |
Status |
|
|
|
Set Name |
Test Cases |
Subtask1 |
00_00_sample.txt, 00_01_sample.txt, 00_02_sample.txt, 00_03_random.txt, 00_04_random.txt, 00_05_random.txt, 00_06_random.txt, 00_07_random.txt, 00_08_random.txt, 00_09_random.txt, 00_10_random.txt, 00_11_random.txt, 00_12_random.txt, 00_13_random.txt, 00_14_random.txt, 00_15_random.txt, 00_16_random.txt, 00_17_random.txt, 00_18_random.txt, 00_19_random.txt, 00_20_random.txt, 00_21_random.txt, 00_22_random.txt, 00_23_freedom.txt, 00_24_freedom.txt, 00_25_freedom.txt, 00_26_full.txt, 00_27_full.txt, 00_28_full.txt, 00_29_min.txt, 00_30_min.txt, 00_31_min.txt, 00_32_max.txt, 00_33_max.txt, 00_34_max.txt, 00_35_max.txt, 00_36_max.txt |
Subtask2 |
01_37_sample.txt, 01_38_sample.txt, 01_39_random.txt, 01_40_random.txt, 01_41_random.txt, 01_42_random.txt, 01_43_random.txt, 01_44_random.txt, 01_45_random.txt, 01_46_random.txt, 01_47_random.txt, 01_48_random.txt, 01_49_random.txt, 01_50_random.txt, 01_51_random.txt, 01_52_random.txt, 01_53_random.txt, 01_54_random.txt, 01_55_random.txt, 01_56_random.txt, 01_57_random.txt, 01_58_random.txt, 01_59_freedom.txt, 01_60_freedom.txt, 01_61_freedom.txt, 01_62_full.txt, 01_63_full.txt, 01_64_full.txt, 01_65_min.txt, 01_66_min.txt, 01_67_min.txt, 01_68_max.txt, 01_69_max.txt, 01_70_max.txt, 01_71_max.txt, 01_72_max.txt |
All |
00_00_sample.txt, 00_01_sample.txt, 00_02_sample.txt, 00_03_random.txt, 00_04_random.txt, 00_05_random.txt, 00_06_random.txt, 00_07_random.txt, 00_08_random.txt, 00_09_random.txt, 00_10_random.txt, 00_11_random.txt, 00_12_random.txt, 00_13_random.txt, 00_14_random.txt, 00_15_random.txt, 00_16_random.txt, 00_17_random.txt, 00_18_random.txt, 00_19_random.txt, 00_20_random.txt, 00_21_random.txt, 00_22_random.txt, 00_23_freedom.txt, 00_24_freedom.txt, 00_25_freedom.txt, 00_26_full.txt, 00_27_full.txt, 00_28_full.txt, 00_29_min.txt, 00_30_min.txt, 00_31_min.txt, 00_32_max.txt, 00_33_max.txt, 00_34_max.txt, 00_35_max.txt, 00_36_max.txt, 01_37_sample.txt, 01_38_sample.txt, 01_39_random.txt, 01_40_random.txt, 01_41_random.txt, 01_42_random.txt, 01_43_random.txt, 01_44_random.txt, 01_45_random.txt, 01_46_random.txt, 01_47_random.txt, 01_48_random.txt, 01_49_random.txt, 01_50_random.txt, 01_51_random.txt, 01_52_random.txt, 01_53_random.txt, 01_54_random.txt, 01_55_random.txt, 01_56_random.txt, 01_57_random.txt, 01_58_random.txt, 01_59_freedom.txt, 01_60_freedom.txt, 01_61_freedom.txt, 01_62_full.txt, 01_63_full.txt, 01_64_full.txt, 01_65_min.txt, 01_66_min.txt, 01_67_min.txt, 01_68_max.txt, 01_69_max.txt, 01_70_max.txt, 01_71_max.txt, 01_72_max.txt, 02_100_min.txt, 02_101_min.txt, 02_102_max.txt, 02_103_max.txt, 02_104_max.txt, 02_105_max.txt, 02_106_max.txt, 02_73_random.txt, 02_74_random.txt, 02_75_random.txt, 02_76_random.txt, 02_77_random.txt, 02_78_random.txt, 02_79_random.txt, 02_80_random.txt, 02_81_random.txt, 02_82_random.txt, 02_83_random.txt, 02_84_random.txt, 02_85_random.txt, 02_86_random.txt, 02_87_random.txt, 02_88_random.txt, 02_89_random.txt, 02_90_random.txt, 02_91_random.txt, 02_92_random.txt, 02_93_freedom.txt, 02_94_freedom.txt, 02_95_freedom.txt, 02_96_full.txt, 02_97_full.txt, 02_98_full.txt, 02_99_min.txt |
Case Name |
Status |
Exec Time |
Memory |
00_00_sample.txt |
AC |
5 ms |
15104 KB |
00_01_sample.txt |
AC |
5 ms |
15104 KB |
00_02_sample.txt |
AC |
5 ms |
15104 KB |
00_03_random.txt |
AC |
5 ms |
15104 KB |
00_04_random.txt |
AC |
5 ms |
15104 KB |
00_05_random.txt |
AC |
5 ms |
15104 KB |
00_06_random.txt |
AC |
5 ms |
15104 KB |
00_07_random.txt |
AC |
5 ms |
15104 KB |
00_08_random.txt |
AC |
5 ms |
15104 KB |
00_09_random.txt |
AC |
5 ms |
15104 KB |
00_10_random.txt |
AC |
5 ms |
15104 KB |
00_11_random.txt |
AC |
5 ms |
15104 KB |
00_12_random.txt |
AC |
5 ms |
15104 KB |
00_13_random.txt |
AC |
5 ms |
15104 KB |
00_14_random.txt |
AC |
5 ms |
15104 KB |
00_15_random.txt |
AC |
5 ms |
15104 KB |
00_16_random.txt |
AC |
5 ms |
15104 KB |
00_17_random.txt |
AC |
5 ms |
15104 KB |
00_18_random.txt |
AC |
5 ms |
15104 KB |
00_19_random.txt |
AC |
5 ms |
15104 KB |
00_20_random.txt |
AC |
5 ms |
15104 KB |
00_21_random.txt |
AC |
5 ms |
15104 KB |
00_22_random.txt |
AC |
5 ms |
15104 KB |
00_23_freedom.txt |
AC |
5 ms |
15104 KB |
00_24_freedom.txt |
AC |
5 ms |
15104 KB |
00_25_freedom.txt |
AC |
5 ms |
15104 KB |
00_26_full.txt |
AC |
5 ms |
15104 KB |
00_27_full.txt |
AC |
5 ms |
15104 KB |
00_28_full.txt |
AC |
5 ms |
15104 KB |
00_29_min.txt |
AC |
5 ms |
15104 KB |
00_30_min.txt |
AC |
5 ms |
15104 KB |
00_31_min.txt |
AC |
5 ms |
15104 KB |
00_32_max.txt |
AC |
5 ms |
15104 KB |
00_33_max.txt |
AC |
5 ms |
15104 KB |
00_34_max.txt |
AC |
5 ms |
15104 KB |
00_35_max.txt |
AC |
5 ms |
15104 KB |
00_36_max.txt |
AC |
5 ms |
15104 KB |
01_37_sample.txt |
AC |
5 ms |
15104 KB |
01_38_sample.txt |
AC |
5 ms |
15104 KB |
01_39_random.txt |
AC |
5 ms |
15104 KB |
01_40_random.txt |
AC |
5 ms |
15104 KB |
01_41_random.txt |
AC |
6 ms |
15104 KB |
01_42_random.txt |
AC |
5 ms |
15104 KB |
01_43_random.txt |
AC |
5 ms |
15104 KB |
01_44_random.txt |
AC |
5 ms |
15104 KB |
01_45_random.txt |
AC |
5 ms |
15104 KB |
01_46_random.txt |
AC |
6 ms |
15104 KB |
01_47_random.txt |
AC |
5 ms |
15104 KB |
01_48_random.txt |
AC |
5 ms |
15104 KB |
01_49_random.txt |
AC |
5 ms |
15104 KB |
01_50_random.txt |
AC |
5 ms |
15104 KB |
01_51_random.txt |
AC |
5 ms |
15104 KB |
01_52_random.txt |
AC |
5 ms |
15104 KB |
01_53_random.txt |
AC |
5 ms |
15104 KB |
01_54_random.txt |
AC |
5 ms |
15104 KB |
01_55_random.txt |
AC |
5 ms |
15104 KB |
01_56_random.txt |
AC |
5 ms |
15104 KB |
01_57_random.txt |
AC |
6 ms |
15104 KB |
01_58_random.txt |
AC |
5 ms |
15104 KB |
01_59_freedom.txt |
AC |
5 ms |
15104 KB |
01_60_freedom.txt |
AC |
6 ms |
15104 KB |
01_61_freedom.txt |
AC |
6 ms |
15104 KB |
01_62_full.txt |
AC |
5 ms |
15104 KB |
01_63_full.txt |
AC |
5 ms |
15104 KB |
01_64_full.txt |
AC |
5 ms |
15104 KB |
01_65_min.txt |
AC |
5 ms |
15104 KB |
01_66_min.txt |
AC |
5 ms |
15104 KB |
01_67_min.txt |
AC |
5 ms |
15104 KB |
01_68_max.txt |
AC |
6 ms |
15104 KB |
01_69_max.txt |
AC |
6 ms |
15104 KB |
01_70_max.txt |
AC |
6 ms |
15104 KB |
01_71_max.txt |
AC |
6 ms |
15104 KB |
01_72_max.txt |
AC |
6 ms |
15104 KB |
02_100_min.txt |
AC |
5 ms |
15104 KB |
02_101_min.txt |
AC |
5 ms |
15104 KB |
02_102_max.txt |
AC |
110 ms |
19456 KB |
02_103_max.txt |
AC |
104 ms |
19456 KB |
02_104_max.txt |
AC |
101 ms |
19456 KB |
02_105_max.txt |
AC |
125 ms |
19456 KB |
02_106_max.txt |
AC |
116 ms |
19456 KB |
02_73_random.txt |
AC |
93 ms |
19072 KB |
02_74_random.txt |
AC |
106 ms |
19456 KB |
02_75_random.txt |
AC |
69 ms |
17920 KB |
02_76_random.txt |
AC |
104 ms |
19328 KB |
02_77_random.txt |
AC |
48 ms |
17664 KB |
02_78_random.txt |
AC |
76 ms |
19072 KB |
02_79_random.txt |
AC |
25 ms |
17408 KB |
02_80_random.txt |
AC |
98 ms |
19456 KB |
02_81_random.txt |
AC |
114 ms |
19328 KB |
02_82_random.txt |
AC |
94 ms |
19072 KB |
02_83_random.txt |
AC |
57 ms |
17792 KB |
02_84_random.txt |
AC |
58 ms |
17792 KB |
02_85_random.txt |
AC |
117 ms |
19328 KB |
02_86_random.txt |
AC |
80 ms |
19072 KB |
02_87_random.txt |
AC |
39 ms |
17536 KB |
02_88_random.txt |
AC |
93 ms |
19328 KB |
02_89_random.txt |
AC |
98 ms |
19456 KB |
02_90_random.txt |
AC |
33 ms |
17536 KB |
02_91_random.txt |
AC |
19 ms |
15232 KB |
02_92_random.txt |
AC |
48 ms |
17792 KB |
02_93_freedom.txt |
AC |
99 ms |
19200 KB |
02_94_freedom.txt |
AC |
52 ms |
17792 KB |
02_95_freedom.txt |
AC |
70 ms |
17920 KB |
02_96_full.txt |
AC |
10 ms |
15232 KB |
02_97_full.txt |
AC |
48 ms |
19328 KB |
02_98_full.txt |
AC |
44 ms |
19200 KB |
02_99_min.txt |
AC |
5 ms |
15104 KB |