// https://oj.uz/problem/view/JOI21_ho_t2
// OI\JOI21\JOI21-Snowball.pdf
#include <bits/stdc++.h>
using namespace std;
// Macros
#define pb push_back
#define pf push_front
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define up(v) upper_bound(v)
#define low(v) lower_bound(v)
// #define rep(i, x, n) for(int i = x; i < n; ++i)
// #define rrep(i, x, n) for(int i = x; i >= n; --i)
// Read and Print
#define read(a, n) for(ll i = 0; i < n; ++i) cin >> a[i];
#define print(a, n) for(ll i = 0; i < n; ++i){ cout << a[i] << " ";} cout << "\n";
#define endl "\n"
#define sp " "
// Typedefs
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pii;
typedef vector <ll> vi;
typedef vector <vector <ll>> vvi;
using vec = vector<int>;
// Constants
const ll mxn = 1e6 + 5;
const ll mod = 1e9 + 7;
// Solve
const ll N = 1e18;
void solve() {
ll n, q; cin>>n>>q;
vi a(n+2);
a[0] = -N;
a[n+1] = N;
for(int i = 1; i<=n; i++) cin>>a[i];
vi m(q+1), pref(q+1);
map<ll, ll> mp;
mp[0] = 0;
for(ll i = 1; i<=q; i++){
cin>>m[i];
pref[i] += pref[i-1] + m[i];
if(mp.find(pref[i])!=mp.end()) continue;
if(pref[i]>0){
auto it = mp.up(pref[i]);
if(it==mp.end()) mp[pref[i]] = i;
}
else{
auto it = mp.low(pref[i]);
if(it==mp.begin()) mp[pref[i]] = i;
}
}
for(ll i = 1; i<=n; i++){
ll left = a[i], right = a[i];
ll l = a[i-1], r = a[i];
while(r-l>1){
ll mid = (l+r)/2;
ll x = mid - a[i];
ll y = mid - a[i-1];
auto it1 = mp.up(x);
if(it1 == mp.begin()){
l = mid;
continue;
}
it1--;
auto it2 = mp.low(y);
if(it2 == mp.end()){
r = mid;
continue;
}
if((it1->ss)<(it2->ss)) r=mid;
else l = mid;
}
auto it1 = mp.up(r-1-a[i]);
auto it2 = mp.low(r-a[i-1]);
if(it1!=mp.begin()&&(it2==mp.end()||((--it1)->ss < it2->ss))) r--;
left = r;
l = a[i];
r = a[i+1];
while(r-l>1){
ll mid = (l+r)/2;
ll x = mid - a[i];
ll y = mid - a[i+1];
it1 = mp.low(x);
if(it1 == mp.end()){
r = mid;
continue;
}
it2 = mp.up(y);
if(it2 == mp.begin()){
l = mid;
continue;
}
it2--;
if((it1->ss)<(it2->ss)) l=mid;
else r = mid;
}
it1 = mp.low(l+1-a[i]);
it2 = mp.up(l-a[i+1]);
if(it1!=mp.end()&&(it2==mp.begin()||(it1->ss < (--it2)->ss))) l++;
right = l;it1 = mp.low(l+1-a[i]);
cout<<right - left <<endl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
8 ms |
592 KB |
Output is correct |
5 |
Correct |
7 ms |
592 KB |
Output is correct |
6 |
Correct |
6 ms |
592 KB |
Output is correct |
7 |
Correct |
6 ms |
592 KB |
Output is correct |
8 |
Correct |
6 ms |
592 KB |
Output is correct |
9 |
Correct |
4 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
548 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
9 ms |
592 KB |
Output is correct |
16 |
Correct |
7 ms |
688 KB |
Output is correct |
17 |
Correct |
4 ms |
760 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
592 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
8 ms |
592 KB |
Output is correct |
5 |
Correct |
7 ms |
592 KB |
Output is correct |
6 |
Correct |
6 ms |
592 KB |
Output is correct |
7 |
Correct |
6 ms |
592 KB |
Output is correct |
8 |
Correct |
6 ms |
592 KB |
Output is correct |
9 |
Correct |
4 ms |
592 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
548 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
9 ms |
592 KB |
Output is correct |
16 |
Correct |
7 ms |
688 KB |
Output is correct |
17 |
Correct |
4 ms |
760 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
55 ms |
18000 KB |
Output is correct |
21 |
Correct |
55 ms |
17992 KB |
Output is correct |
22 |
Correct |
58 ms |
17736 KB |
Output is correct |
23 |
Correct |
73 ms |
16108 KB |
Output is correct |
24 |
Correct |
253 ms |
17880 KB |
Output is correct |
25 |
Correct |
2105 ms |
22088 KB |
Output is correct |
26 |
Correct |
2130 ms |
22436 KB |
Output is correct |
27 |
Correct |
2050 ms |
20808 KB |
Output is correct |
28 |
Correct |
1835 ms |
18884 KB |
Output is correct |
29 |
Correct |
1418 ms |
14824 KB |
Output is correct |
30 |
Correct |
358 ms |
9032 KB |
Output is correct |
31 |
Correct |
50 ms |
8008 KB |
Output is correct |
32 |
Correct |
45 ms |
7180 KB |
Output is correct |
33 |
Correct |
109 ms |
2288 KB |
Output is correct |
34 |
Correct |
829 ms |
7132 KB |
Output is correct |
35 |
Correct |
1051 ms |
11080 KB |
Output is correct |
36 |
Correct |
2051 ms |
21776 KB |
Output is correct |
37 |
Correct |
1853 ms |
18324 KB |
Output is correct |
38 |
Correct |
2008 ms |
17732 KB |
Output is correct |
39 |
Correct |
1771 ms |
20852 KB |
Output is correct |
40 |
Correct |
471 ms |
21320 KB |
Output is correct |
41 |
Correct |
91 ms |
18760 KB |
Output is correct |
42 |
Correct |
50 ms |
7244 KB |
Output is correct |
43 |
Correct |
952 ms |
22088 KB |
Output is correct |
44 |
Correct |
106 ms |
18504 KB |
Output is correct |
45 |
Correct |
820 ms |
17736 KB |
Output is correct |
46 |
Correct |
1431 ms |
23624 KB |
Output is correct |
47 |
Correct |
504 ms |
15488 KB |
Output is correct |
48 |
Correct |
572 ms |
19312 KB |
Output is correct |