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 "meetings.h"
#include <bits/stdc++.h>
#define sz(x) (int)x.size()
#define ALL(x) x.begin(), x.end()
#define chmax(a, b) a=max(a,b)
#define chmin(a, b) a=min(a,b)
#define pb push_back
#define ff first
#define ss second
using namespace std;
using ll = long long;
using VI = vector <int>;
using PII = pair <int, int>;
int n, m, q;
const int N = 1e5+1;
const int M = 3*N;
int nums[N];
struct Node {
int l, r, L, R, mx;
} node[M];
Node zero = {0, 0, 0};
Node merge(int L, int R, Node a, Node b) {
Node res = {
(a.mx == a.R-a.L+1 ? a.mx : 0) + b.l,
a.r + (b.mx == b.R - b.L + 1 ? b.mx : 0),
L,
R,
max(a.mx, b.mx)
};
res.mx = max({res.mx, res.l, res.r, a.r+b.l});
return res;
}
void build(int l, int r, int head) {
if (l == r) {
int x = (nums[l] == 1);
node[head] = {x, x, l, r, x};
return;
}
int mid = (l+r)>>1;
build(l, mid, head*2+1);
build(mid+1, r, head*2+2);
node[head] = merge(l, r, node[head*2+1], node[head*2+2]);
}
Node query(int l, int r, int L, int R, int head) {
if (l > R || L > r) return zero;
if (L <= l && r <= R) return node[head];
int mid = (l+r)>>1;
return merge(
l, r,
query(l, mid, L, R, head*2+1),
query(mid+1, r, L, R, head*2+2)
);
}
vector<long long> minimum_costs(vector<int> h, vector<int> L, vector<int> R) {
n = sz(h);
q = sz(L);
vector<long long> ans(q, 1e18);
for (int i = 0; i < n; i++) nums[i] = h[i];
build(0, n-1, 0);
for (int i = 0; i < q; i++) {
int len = R[i] - L[i] + 1;
ans[i] = len * 2 - query(0, n-1, L[i], R[i], 0).mx;
}
return ans;
}
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |