This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma optimize("Bismillahirrahmanirrahim")
//█▀█─█──█──█▀█─█─█
//█▄█─█──█──█▄█─█■█
//█─█─█▄─█▄─█─█─█─█
//Allahuekber
//ahmet23 orz...
//FatihSultanMehmedHan
//YavuzSultanSelimHan
//AbdulhamidHan
//Sani buyuk Osman Pasa Plevneden cikmam diyor
#define author tolbi
#include <bits/stdc++.h>
using namespace std;
template<typename X, typename Y> ostream& operator<<(ostream& os, pair<X,Y> pr) {return os<<pr.first<<" "<<pr.second;}
template<typename X> ostream& operator<<(ostream& os, vector<X> v){for (auto &it : v) os<<it<<" ";return os;}
template<typename X, size_t Y> ostream& operator<<(ostream& os, array<X,Y> v){for (auto &it : v) os<<it<<" ";return os;}
ostream& operator<<(ostream& os, bool bl){return os<<(int32_t)bl;}
#define endl '\n'
#define deci(x) int x;cin>>x;
#define decstr(x) string x;cin>>x;
#define cinarr(x) for(auto &it : x) cin>>it;
#define coutarr(x) for(auto &it : x) cout<<it<<" ";cout<<endl;
#define sortarr(x) sort(x.begin(), x.end())
#define sortrarr(x) sort(x.rbegin(), x.rend())
#define rev(x) reverse(x.begin(), x.end())
#define tol(bi) (1ll<<((int)(bi)))
typedef long long ll;
const ll INF = LONG_LONG_MAX;
const ll MOD = 1e9+9;
mt19937 ayahya(chrono::high_resolution_clock().now().time_since_epoch().count());
#include"holiday.h"
struct SegTree{
vector<long long> segtree;
vector<int> say;
vector<int> pos;
vector<int> val;
SegTree(vector<int> arr){
int n = arr.size();
segtree.resize(tol(ceil(log2(n)+1))-1,0ll);
say.resize(segtree.size(), 0ll);
pos.resize(n);
val=arr;
vector<pair<int,int>> hsh(n);
for (int i = 0; i < n; i++){
hsh[i]={arr[i],i};
}
sortrarr(hsh);
for (int i = 0; i < n; i++){
pos[hsh[i].second]=i;
}
}
void activate(int node){
int vval = val[node];
node=pos[node];
node+=segtree.size()/2;
say[node]=1;
segtree[node]=vval;
while (node){
node=(node-1)/2;
segtree[node]=segtree[node*2+1]+segtree[node*2+2];
say[node]=say[node*2+1]+say[node*2+2];
}
}
void deactivate(int node){
node=pos[node];
node+=segtree.size()/2;
segtree[node]=0;
say[node]=0;
while (node){
node=(node-1)/2;
segtree[node]=segtree[node*2+1]+segtree[node*2+2];
say[node]=say[node*2+1]+say[node*2+2];
}
}
long long query(int x){
if (x<0) x = 0;
if (x>say[0]) x = say[0];
if (!x) return 0;
int node = 0;
long long rval = 0ll;
while (node*2+1<segtree.size()){
if (say[node*2+1]>=x){
node=node*2+1;
}
else {
rval+=segtree[node*2+1];
x-=say[node*2+1];
node=node*2+2;
}
}
rval+=segtree[node];
return rval;
}
};
pair<vector<int>,vector<ll>> f(vector<int> attraction){
int n = attraction.size();
vector<int> res(n*2);
vector<ll> val(n*2,-1);
if (n==0){
return {res,val};
}
SegTree segtree(attraction);
function<void(int,int,int)> calc;
calc = [&](int x, int l, int r){
for (int i = l; i <= r; i++){
segtree.activate(i);
int j = x-i;
long long crr = segtree.query(j);
if (crr>val[x]){
val[x]=crr;
res[x]=i;
}
}
for (int i = l; i <= r; i++){
segtree.deactivate(i);
}
};
function<void(int,int,int,int)> f;
f = [&](int l, int r, int tl, int tr)->void{
if (l>r) return;
int mid = l+(r-l)/2;
calc(mid,tl,tr);
int pos = res[mid];
f(l,mid-1,tl,pos);
for (int i = tl; i < pos; i++){
segtree.activate(i);
}
f(mid+1,r,pos,tr);
for (int i = tl; i < pos; i++){
segtree.deactivate(i);
}
};
f(0,res.size()-1,0,n-1);
return {res,val};
}
long long int findMaxAttraction(int n, int start, int d, int attraction[]) {
bool tekrar = true;
if (attraction[0]<0) attraction[0]*=-1,tekrar=false;
vector<int> _left, _right;
for (int i = start; i >= 0; i--){
_left.push_back(attraction[i]);
}
for (int i = start+1; i < n; i++){
_right.push_back(attraction[i]);
}
auto [left, valleft] = f(_left);
auto [right, valright] = f(_right);
ll ans = 0;
if (right.size()==0){
return valleft[min(d,(int)valleft.size()-1)];
}
for (int i = 0; i < min(d+1,(int)left.size()); i++){
int pos = left[i];
int kalan = d-i-pos-1;
if (kalan<0 || valright.size()==0) {ans=max(ans,valleft[i]);continue;}
ans=max(ans,valleft[i]+valright[min(kalan,(int)valright.size()-1)]);
}
if (tekrar){
for (int i = 0; i < n-i-1; i++){
swap(attraction[i],attraction[n-i-1]);
}
attraction[0]*=-1;
ans=max(ans,findMaxAttraction(n, n-start-1, d, attraction));
}
return ans;
}
Compilation message (stderr)
holiday.cpp:1: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
1 | #pragma optimize("Bismillahirrahmanirrahim")
|
holiday.cpp: In member function 'long long int SegTree::query(int)':
holiday.cpp:81:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | while (node*2+1<segtree.size()){
| ~~~~~~~~^~~~~~~~~~~~~~~
# | 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... |