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 <bits/stdc++.h>
#include "holiday.h"
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int pos[N], p[N];
struct Node {
ll sum; int cnt;
Node(ll sum = 0, int cnt = 0) :
sum(sum), cnt(cnt) {}
} t[N << 2];
Node merge(Node a, Node b) {
return Node(a.sum + b.sum, a.cnt + b.cnt);
}
void update(int node, int l, int r, int i, int x) {
if(l == r) { t[node] = Node(x, 1); return; }
int m = l + r >> 1;
if(i <= m) update(node << 1, l, m, i, x);
else update(node << 1 | 1, m + 1, r, i, x);
t[node] = merge(t[node << 1], t[node << 1 | 1]);
}
ll query(int node, int l, int r, int k) {
if(l == r) return k ? t[node].sum : 0;
int m = l + r >> 1;
if(t[node << 1].cnt >= k) return query(node << 1, l, m, k);
else return query(node << 1 | 1, m + 1, r, k - t[node << 1].cnt) + t[node << 1].sum;
}
ll findMaxAttraction(int n, int start, int d, int a[]) {
iota(p, p + n, 0);
sort(p, p + n, [&](int i, int j) { return a[i] > a[j]; });
for(int i = 0; i < n; ++i) pos[p[i]] = i;
ll ans = 0;
for(int i = 0; i < n; ++i) {
if(i > d) break;
update(1, 0, n - 1, pos[i], a[i]);
ans = max(ans, query(1, 0, n - 1, d - i));
}
return ans;
}
Compilation message (stderr)
holiday.cpp: In function 'void update(int, int, int, int, int)':
holiday.cpp:22:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = l + r >> 1;
~~^~~
holiday.cpp: In function 'll query(int, int, int, int)':
holiday.cpp:30:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = l + r >> 1;
~~^~~
grader.cpp: In function 'int main()':
grader.cpp:7:12: warning: variable 'n_s' set but not used [-Wunused-but-set-variable]
int i, n_s;
^~~
# | 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... |