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>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long long, int> pli;
typedef pair<int, long long> pil;
typedef vector<pair<int, int>> vii;
typedef vector<pair<long long, long long>> vll;
typedef vector<pair<long long, int>> vli;
typedef vector<pair<int, long long>> vil;
#define bitcnt(n) __builtin_popcountll((n))
#define BIT(n, i) (((n) >> (i)) & 1ll)
#define FLIP(n, i) ((n) ^ (1ll << (i)))
#define MASK(i) (1ll << (i))
#define ON(n, i) ((n) | MASK(i))
#define OFF(n, i) ((n) & ~MASK(i))
#define Int __int128
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) a / gcd(a, b) * b
#define fi first
#define se second
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T1, class T2>
bool maximize(T1 &x, T2 y) {
if (x < y) {
x = y;
return true;
}
return false;
}
template <class T1, class T2>
bool minimize(T1 &x, T2 y) {
if (x > y) {
x = y;
return true;
}
return false;
}
const int N = 1e6 + 5;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const ll INF = 1e18;
int n, st, d;
int a[N];
ll ans = 0;
vector<int> cmp;
int get_cmp(int x) {
return lower_bound(cmp.begin(), cmp.end(), x) - cmp.begin();
}
struct SegmentTree {
vli node;
int n;
SegmentTree() {}
void resize(int _n) {
n = _n; node.assign(4 * n + 5, make_pair(0, 0));
}
pli merge(const pli A, const pli B) {
return make_pair(A.fi + B.fi, A.se + B.se);
}
void update(int id, int l, int r, int p, int val) {
if (l > p || r < p) return;
if (l == r) {
node[id] = merge(node[id], make_pair(1ll * cmp[l] * val, val));
return;
}
int mid = l + r >> 1;
update(id << 1, l, mid, p, val);
update(id << 1 | 1, mid + 1, r, p, val);
node[id] = merge(node[id << 1], node[id << 1 | 1]);
}
void update(int p, int val) {
update(1, 0, n, p, val);
}
pli walk(int id, int l, int r, int can_get) {
if (can_get <= 0) return make_pair(0, 0);
if (can_get >= node[id].se) return node[id];
if (l == r) return make_pair(1ll * cmp[l] * can_get, can_get);
int mid = l + r >> 1;
pli ri = walk(id << 1 | 1, mid + 1, r, can_get);
pli le = walk(id << 1, l, mid, can_get - ri.se);
return merge(le, ri);
}
pli walk(int can_get) {
return walk(1, 0, n, can_get);
}
} seg;
int min_dist(int x, int y) /*min dist to move from st to (x or y) to (y or x)*/ {
// cout << x << ' ' << y << ' ' << abs(x - y) + min(abs(x - st), abs(y - st)) << '\n';
// return abs(x - y) + min(abs(x - st), abs(y - st));
if (x >= st) return y - st;
if (y <= st) return st - x;
return y - x + min(y - st, st - x);
}
ll max_attraction(int can_get) /*chọn tối đa can_get vị trí i trong đoạn [l, r] có tổng lớn nhất*/ {
return seg.walk(can_get).fi;
}
void solve(int l, int r, int optL, int optR) {
if (l > r) return;
int mid = l + r >> 1;
pli best(-INF, -1);
for (int i = optL; i <= mid; ++i)
seg.update(get_cmp(a[i]), 1);
for (int i = optL; i <= min(mid, optR); ++i) {
maximize(best, make_pair(max_attraction(d - min_dist(i, mid)), i));
seg.update(get_cmp(a[i]), -1);
}
for (int i = min(mid, optR) + 1; i <= mid; ++i)
seg.update(get_cmp(a[i]), -1);
maximize(ans, best.fi);
solve(l, mid - 1, optL, best.se);
solve(mid + 1, r, best.se, optR);
}
ll findMaxAttraction(int _n, int _st, int _d, int a[]) {
n = _n, st = _st, d = _d;
for (int i = 0; i < n; ++i) cmp.emplace_back(a[i]);
sort (cmp.begin(), cmp.end());
cmp.resize(unique(cmp.begin(), cmp.end()) - cmp.begin());
seg.resize(cmp.size() + 5); solve(0, n - 1, 0, n - 1);
return ans;
}
Compilation message (stderr)
holiday.cpp: In member function 'void SegmentTree::update(int, int, int, int, int)':
holiday.cpp:87:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
87 | int mid = l + r >> 1;
| ~~^~~
holiday.cpp: In member function 'pli SegmentTree::walk(int, int, int, int)':
holiday.cpp:101:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
101 | int mid = l + r >> 1;
| ~~^~~
holiday.cpp: In function 'void solve(int, int, int, int)':
holiday.cpp:126:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
126 | int mid = l + r >> 1;
| ~~^~~
# | 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... |