이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define vll vector<ll>
typedef long long ll;
const ll inf=1e18;
const ll mxN=1e5+5;
unordered_map<ll, ll> idx;
unordered_map<ll, ll> vl;
ll ans;
ll s;
ll d;
ll n;
ll val[mxN];
struct segtree1{
vll tree;
vll lazy;
ll treelen;
void init(ll s){
tree.clear();
lazy.clear();
treelen=s+1;
while(__builtin_popcountll(treelen)!=1){
treelen++;
}
tree.resize(2*treelen);
lazy.resize(2*treelen);
}
void update(ll idx, ll low, ll high, ll qlow, ll qhigh, ll val){
if(low>high) return;
if(lazy[idx]!=0){
tree[idx]+=lazy[idx];
if(low!=high){
lazy[2*idx]+=lazy[idx];
lazy[2*idx+1]+=lazy[idx];
}
lazy[idx]=0;
}
if(low>=qlow && high<=qhigh){
tree[idx]+=val;
if(low!=high){
lazy[2*idx]+=val;
lazy[2*idx+1]+=val;
}
return;
}
if(low>qhigh || high<qlow){
return;
}
ll mid=(low+high)/2;
update(2*idx, low, mid, qlow, qhigh, val);
update(2*idx+1, mid+1, high, qlow, qhigh, val);
tree[idx]=max(tree[2*idx], tree[2*idx+1]);
}
ll qry(ll idx, ll low, ll high, ll val){
if(lazy[idx]!=0){
tree[idx]+=lazy[idx];
if(low!=high){
lazy[2*idx]+=lazy[idx];
lazy[2*idx+1]+=lazy[idx];
}
lazy[idx]=0;
}
if(tree[idx]<val){
return -inf;
}
if(low==high){
return low;
}
ll mid=(low+high)/2;
if(tree[2*idx+1]+lazy[2*idx+1]>=val){
return qry(2*idx+1, mid+1, high, val);
}
else{
return qry(2*idx, low, mid, val);
}
}
};
struct BIT{
vll tree;
ll siz;
void init(ll s){
siz=s+1;
tree.clear();
tree.resize(siz);
}
void update(ll idx, ll val){
idx++;
for(;idx<siz;idx+=(idx&-idx)){
tree[idx]+=val;
}
}
ll getpre(ll idx){
idx++;
ll re=0;
for(;idx>0;idx-=(idx&-idx)){
re+=tree[idx];
}
return re;
}
ll getsum(ll a, ll b){
if(a==0) return getpre(b);
return getpre(b)-getpre(a-1);
}
};
segtree1 seg;
BIT bit;
ll p1;
void divi(ll low, ll high, ll qlow, ll qhigh, ll flag){
if(low>high) return;
ll mid=(low+high)/2;
while(p1>mid){
p1--;
seg.update(1, 0, seg.treelen-1, 0, idx[val[p1]], 1);
bit.update(idx[val[p1]], val[p1]);
}
while(p1<mid){
seg.update(1, 0, seg.treelen-1, 0, idx[val[p1]], -1);
bit.update(idx[val[p1]], -val[p1]);
p1++;
}
if(flag==1){
ll cur=-inf;
ll lef=d-(qlow-mid)-(qlow-s);
ll id=seg.qry(1, 0, seg.treelen-1, lef);
if(id==-inf){
cur=max(cur, bit.getsum(0, n-1));
}
else{
cur=max(cur, bit.getsum(id, n-1));
}
ll op=qlow;
for(ll i=qlow+1;i<=qhigh;i++){
seg.update(1, 0, seg.treelen-1, 0, idx[val[i]], 1);
bit.update(idx[val[i]], val[i]);
lef=d-(i-mid)-(i-s);
if(lef<=0) break;
id=seg.qry(1, 0, seg.treelen-1, lef);
if(id==-inf){
if(bit.getsum(0, n-1)>cur){
cur=bit.getsum(0, n-1);
op=i;
}
}
else{
if(bit.getsum(id, n-1)>cur){
cur=bit.getsum(id, n-1);
op=i;
}
}
}
ans=max(ans, cur);
cerr<<' '<<cur<<' '<<op<<'\n';
for(ll i=qhigh;i>op;i--){
lef=d-(i-mid)-(i-s);
if(lef<=0) continue;
seg.update(1, 0, seg.treelen-1, 0, idx[val[i]], -1);
bit.update(idx[val[i]], -val[i]);
}
divi(low, mid-1, qlow, op, 0);
divi(mid+1, high, op, qhigh, 1);
for(ll i=op;i>qlow;i--){
seg.update(1, 0, seg.treelen-1, 0, idx[val[i]], -1);
bit.update(idx[val[i]], -val[i]);
}
}
else{
ll cur=-inf;
ll lef=d-(qhigh-mid)-(qlow-s);
ll id=seg.qry(1, 0, seg.treelen-1, lef);
if(id==-inf){
cur=max(cur, bit.getsum(0, n-1));
}
else{
cur=max(cur, bit.getsum(id, n-1));
}
ll op=qhigh;
for(ll i=qhigh-1;i>=qlow;i--){
seg.update(1, 0, seg.treelen-1, 0, idx[val[i+1]], -1);
bit.update(idx[val[i+1]], -val[i]);
lef=d-(i-mid)-(i-s);
if(lef<=0) continue;
id=seg.qry(1, 0, seg.treelen-1, lef);
if(id==-inf){
if(bit.getsum(0, n-1)>=cur){
cur=bit.getsum(0, n-1);
op=i;
}
}
else{
if(bit.getsum(id, n-1)>=cur){
cur=bit.getsum(id, n-1);
op=i;
}
}
}
ans=max(ans, cur);
for(ll i=qlow+1;i<=op;i++){
seg.update(1, 0, seg.treelen-1, 0, idx[val[i]], 1);
bit.update(idx[val[i]], val[i]);
}
divi(low, mid-1, qlow, op, 0);
divi(mid+1, high, op, qhigh, 1);
for(ll i=op+1;i<=qhigh;i++){
lef=d-(i-mid)-(i-s);
if(lef<=0) continue;
seg.update(1, 0, seg.treelen-1, 0, idx[val[i]], 1);
bit.update(idx[val[i]], val[i]);
}
}
}
void divi2(ll low, ll high, ll qlow, ll qhigh, ll flag){
if(low>high) return;
ll mid=(low+high)/2;
while(p1>mid){
seg.update(1, 0, seg.treelen-1, 0, idx[val[p1]], -1);
bit.update(idx[val[p1]], -val[p1]);
p1--;
}
while(p1<mid){
p1++;
seg.update(1, 0, seg.treelen-1, 0, idx[val[p1]], 1);
bit.update(idx[val[p1]], val[p1]);
}
if(flag==1){
ll cur=-inf;
ll lef=d+(qlow-mid)+(qlow-s);
ll id=seg.qry(1, 0, seg.treelen-1, lef);
if(id==-inf){
cur=max(cur, bit.getsum(0, n-1));
}
else{
cur=max(cur, bit.getsum(id, n-1));
}
ll op=qlow;
for(ll i=qlow+1;i<=qhigh;i++){
seg.update(1, 0, seg.treelen-1, 0, idx[val[i]], -1);
bit.update(idx[val[i]], -val[i]);
lef=d+(i-mid)+(i-s);
if(lef<=0) break;
id=seg.qry(1, 0, seg.treelen-1, lef);
if(id==-inf){
if(bit.getsum(0, n-1)>=cur){
cur=bit.getsum(0, n-1);
op=i;
}
}
else{
if(bit.getsum(id, n-1)>=cur){
cur=bit.getsum(id, n-1);
op=i;
}
}
}
ans=max(ans, cur);
for(ll i=qhigh;i>op;i--){
seg.update(1, 0, seg.treelen-1, 0, idx[val[i]], 1);
bit.update(idx[val[i]], val[i]);
}
divi2(low, mid-1, qlow, op, 0);
divi2(mid+1, high, op, qhigh, 1);
for(ll i=op;i>qlow;i--){
lef=d+(i-mid)+(i-s);
if(lef<=0) continue;
seg.update(1, 0, seg.treelen-1, 0, idx[val[i]], 1);
bit.update(idx[val[i]], val[i]);
}
}
else{
ll cur=-inf;
ll lef=d+(qhigh-mid)+(qhigh-s);
ll id=seg.qry(1, 0, seg.treelen-1, lef);
if(id==-inf){
cur=max(cur, bit.getsum(0, n-1));
}
else{
cur=max(cur, bit.getsum(id, n-1));
}
ll op=qhigh;
for(ll i=qhigh-1;i>=qlow;i--){
seg.update(1, 0, seg.treelen-1, 0, idx[val[i+1]], 1);
bit.update(idx[val[i+1]], val[i]);
lef=d+(i-mid)+(i-s);
if(lef<=0) continue;
id=seg.qry(1, 0, seg.treelen-1, lef);
if(id==-inf){
if(bit.getsum(0, n-1)>cur){
cur=bit.getsum(0, n-1);
op=i;
}
}
else{
if(bit.getsum(id, n-1)>cur){
cur=bit.getsum(id, n-1);
op=i;
}
}
}
ans=max(ans, cur);
for(ll i=qlow+1;i<=op;i++){
lef=d+(i-mid)+(i-s);
if(lef<=0) continue;
seg.update(1, 0, seg.treelen-1, 0, idx[val[i]], -1);
bit.update(idx[val[i]], -val[i]);
}
divi2(low, mid-1, qlow, op, 0);
divi2(mid+1, high, op, qhigh, 1);
for(ll i=op+1;i<=qhigh;i++){
seg.update(1, 0, seg.treelen-1, 0, idx[val[i]], -1);
bit.update(idx[val[i]], -val[i]);
}
}
}
ll findMaxAttraction(int tn, int st, int td, int a[]){
if(td==0) return 0;
s=st;
d=td;
n=tn;
ans=a[s];
seg.init(n);
bit.init(n);
set<ll> cor;
for(ll i=0;i<n;i++){
cor.insert(a[i]);
val[i]=a[i];
}
ll cnt=0;
for(auto it:cor){
idx[it]=cnt;
vl[cnt]=it;
cnt++;
}
p1=s;
seg.update(1, 0, seg.treelen-1, 0, idx[val[s]], 1);
bit.update(idx[val[s]], val[s]);
divi(0, s-1, s, n-1, 1);
while(p1<s){
seg.update(1, 0, seg.treelen-1, 0, idx[val[p1]], -1);
bit.update(idx[val[p1]], -val[p1]);
p1++;
}
divi2(s+1, n-1, 0, s, 0);
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... |