#include "fish.h"
#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const ll maxn = 2*1e5+5, INF = 4e18+9;
struct fish{
ll y, w, p;
};
bool cmp(fish a, fish b){
return a.y < b.y;
}
template<class Info, class Tag>
struct LazySegmentTree {
int n;
vector<Info> info;
vector<Tag> tag;
LazySegmentTree() : n(0) {}
LazySegmentTree(int n_, Info v_ = Info()) {
init(n_, v_);
}
template<class T>
LazySegmentTree(vector<T> init_) {
init(init_);
}
void init(int n_, Info v_ = Info()) {
init(vector<Info>(n_, v_));
}
template<class T>
void init(vector<T> init_) {
n = init_.size();
info.assign(4 << __lg(n), Info());
tag.assign(4 << __lg(n), Tag());
function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
info[p] = init_[l];
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
pull(p);
};
build(1, 0, n);
}
void pull(int p) {
info[p] = info[2 * p] + info[2 * p + 1];
}
void apply(int p, const Tag &v) {
info[p].apply(v);
tag[p].apply(v);
}
void push(int p) {
apply(2 * p, tag[p]);
apply(2 * p + 1, tag[p]);
tag[p] = Tag();
}
void modify(int p, int l, int r, int x, const Info &v) {
if (r - l == 1) {
info[p] = v;
return;
}
int m = (l + r) / 2;
push(p);
if (x < m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v) {
modify(1, 0, n, p, v);
}
Info rangeQuery(int p, int l, int r, int x, int y) {
if (l >= y || r <= x) {
return Info();
}
if (l >= x && r <= y) {
return info[p];
}
int m = (l + r) / 2;
push(p);
return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
}
Info rangeQuery(int l, int r) {
return rangeQuery(1, 0, n, l, r);
}
void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
if (l >= y || r <= x) {
return;
}
if (l >= x && r <= y) {
apply(p, v);
return;
}
int m = (l + r) / 2;
push(p);
rangeApply(2 * p, l, m, x, y, v);
rangeApply(2 * p + 1, m, r, x, y, v);
pull(p);
}
void rangeApply(int l, int r, const Tag &v) {
return rangeApply(1, 0, n, l, r, v);
}
template<class F>
int findFirst(int p, int l, int r, int x, int y, F &&pred) {
if (l >= y || r <= x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findFirst(2 * p, l, m, x, y, pred);
if (res == -1) {
res = findFirst(2 * p + 1, m, r, x, y, pred);
}
return res;
}
template<class F>
int findFirst(int l, int r, F &&pred) {
return findFirst(1, 0, n, l, r, pred);
}
template<class F>
int findLast(int p, int l, int r, int x, int y, F &&pred) {
if (l >= y || r <= x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findLast(2 * p + 1, m, r, x, y, pred);
if (res == -1) {
res = findLast(2 * p, l, m, x, y, pred);
}
return res;
}
template<class F>
int findLast(int l, int r, F &&pred) {
return findLast(1, 0, n, l, r, pred);
}
};
struct Tag {
ll add = 0;
void apply(const Tag &t) & {
add += t.add;
}
};
struct Info {
ll mx = 0;
void apply(const Tag &t) & {
mx += t.add;
}
Info operator+(const Info &b) {
return {max(mx, b.mx)};
}
};
struct update{
ll l, r, w;
};
long long max_weights(int n, int m, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
for(int i = 0; i < m; i++){
X[i]++;
Y[i]++;
}
vector<vector<fish>> pond(n+2), pond2(n+2);
for(int i = 1; i <= m; i++){
pond[X[i-1]].push_back({Y[i-1], W[i-1], 0});
pond2[X[i-1]-1].push_back({Y[i-1], W[i-1], 2});
pond2[X[i-1]+1].push_back({Y[i-1], W[i-1], 1});
}
for(int i = 0; i <= n+1; i++){
pond2[i].push_back({0, 0, 0});
sort(pond[i].begin(), pond[i].end(), cmp);
sort(pond2[i].begin(), pond2[i].end(), cmp);
}
vector<vector<ll>> f(n+2), g(n+2);
LazySegmentTree<Info, Tag> T(n+1);
for(int i = 1; i <= n; i++){
vector<update> rollback;
for(int j = 0; j < (int)f[i-1].size(); j++){
if(T.rangeQuery(pond2[i-1][j].y, pond2[i-1][j].y+1).mx == 0){
T.rangeApply(pond2[i-1][j].y, pond2[i-1][j].y+1, {f[i-1][j]});
rollback.push_back({pond2[i-1][j].y, pond2[i-1][j].y, f[i-1][j]});
}
}
for(auto it : pond2[i]){
if(it.p == 1) {
T.rangeApply(0, it.y-1+1, {it.w});
rollback.push_back({0, it.y-1, it.w});
}
ll mx = T.info[1].mx;
f[i].push_back(mx);
}
for(auto it : rollback){
T.rangeApply(it.l, it.r+1, {-it.w});
}
}
for(int i = n; i >= 1; i--){
vector<update> rollback;
for(int j = 0; j < (int)g[i+1].size(); j++){
if(T.rangeQuery(pond2[i+1][j].y, pond2[i+1][j].y+1).mx == 0){
T.rangeApply(pond2[i+1][j].y, pond2[i+1][j].y+1, {g[i+1][j]});
rollback.push_back({pond2[i+1][j].y, pond2[i+1][j].y, g[i+1][j]});
}
}
for(auto it : pond2[i]){
if(it.p == 2) {
T.rangeApply(0, it.y-1+1, {it.w});
rollback.push_back({0, it.y-1, it.w});
}
ll mx = T.info[1].mx;
g[i].push_back(mx);
}
for(auto it : rollback){
T.rangeApply(it.l, it.r+1, {-it.w});
}
}
ll ans = 0;
for(int i = 1; i <= n; i++){
for(int j = 0; j < (int)pond2[i].size(); j++){
ans = max(ans, f[i][j]+g[i][j]);
}
}
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |