This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "holiday.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
struct FenwickTree{
int n;
vector<ll> a;
FenwickTree(int _n){
n = _n;
a.resize(n+1);
}
void update(int i, ll v){
while(i <= n){
a[i] += v;
i += LASTBIT(i);
}
}
ll get(int i){
ll ans = 0;
while(i > 0){
ans += a[i];
i -= LASTBIT(i);
}
return ans;
}
int lower_bound(ll v){
if (v == 0) return 0;
int p = MASK(logOf(n)), idx = 0;
while(p > 0){
if (idx + p < n && v > a[idx + p]){
idx += p;
v -= a[idx];
}
p >>= 1;
}
return idx + 1;
}
};
int _s;
vector<int> _a;
void dnc1(int l, int r, int u, int v, FenwickTree &bit, FenwickTree &sum, vector<int> &pos, vector<int> &f, vector<ll> &supersayan){
if (l > r || u > v) return;
int mid = (l + r) >> 1;
int opt = -1;
ll cur = -1;
for(int i = u; i <= v; ++i){
bit.update(pos[i], 1); sum.update(pos[i], _a[i]);
int dis = i - _s;
dis = max(0, mid - dis);
if (maximize(cur, sum.get(bit.lower_bound(dis))))
opt = i;
}
supersayan[mid] = cur;
f[mid] = opt;
for(int i = v; i >= opt; --i){
bit.update(pos[i], -1); sum.update(pos[i], -_a[i]);
}
dnc1(mid + 1, r, opt, v, bit, sum, pos, f, supersayan);
for(int i = opt - 1; i >= u; --i){
bit.update(pos[i], -1); sum.update(pos[i], -_a[i]);
}
dnc1(l, mid - 1, u, opt, bit, sum, pos, f, supersayan);
}
void dnc2(int l, int r, int u, int v, FenwickTree &bit, FenwickTree &sum, vector<int> &pos, vector<int> &f, vector<ll> &supersayan){
if (l > r || u > v) return;
int mid = (l + r) >> 1;
int opt = -1;
ll cur = -1;
for(int i = v; i >= u; --i){
bit.update(pos[i], 1); sum.update(pos[i], _a[i]);
int dis = _s - i;
dis = max(0, mid - dis * 2);
if (maximize(cur, sum.get(bit.lower_bound(dis))))
opt = i;
}
supersayan[mid] = cur;
f[mid] = opt;
for(int i = u; i <= opt; ++i){
bit.update(pos[i], -1); sum.update(pos[i], -_a[i]);
}
dnc2(mid + 1, r, u, opt, bit, sum, pos, f, supersayan);
for(int i = opt + 1; i <= v; ++i){
bit.update(pos[i], -1); sum.update(pos[i], -_a[i]);
}
dnc2(l, mid - 1, opt, v, bit, sum, pos, f, supersayan);
}
ll findMaxAttraction_Chiral(int n, int s, int d, vector<int> a){
_s = s;
_a = a;
// find max attraction, where distance to the left get doubled, while distance to the right doesn't
ll ans = 0;
vector<pair<int, int>> b(n);
for(int i = 0; i<n; ++i) b[i] = {a[i], i};
sort(ALL(b)); reverse(ALL(b));
vector<int> pos(n);
for(int i = 0; i<n; ++i) pos[b[i].second] = i + 1;
// first, find the distance to the right
vector<int> f1(d + 1), f2(d+1);
vector<ll> supersayan1(d + 1), supersayan2(d+1);
FenwickTree bit(n), sum(n);
dnc1(1, d, s, n-1, bit, sum, pos, f1, supersayan1);
dnc2(1, d, 0, s-1, bit, sum, pos, f2, supersayan2);
// printArr(supersayan1);
// printArr(supersayan2);
for(int i = 1; i<=d; ++i) maximize(supersayan2[i], supersayan2[i-1]);
for(int i = 0; i<=d; ++i) maximize(ans, supersayan1[i] + supersayan2[d - i]);
return ans;
}
ll findMaxAttraction(int n, int s, int d, int attraction[]){
ll ans = 0;
for(int x = 0; x <= 1; ++x){
vector<int> a(n);
for(int i = 0; i<n; ++i) a[i] = attraction[i];
if (x) {
reverse(ALL(a));
s = n - s - 1;
}
maximize(ans, findMaxAttraction_Chiral(n, s, d, a));
}
return ans;
}
// int main(void){
// ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
// clock_t start = clock();
// int n, s, d;
// cin >> n >> s >> d;
// int attraction[n];
// for(int i = 0; i<n; ++i) cin >> attraction[i];
// cout << findMaxAttraction(n, s, d, attraction) << "\n";
// cerr << "Time elapsed: " << clock() - start << " ms\n";
// return 0;
// }
# | 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... |