QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#56447 | #4239. MST | KING_UT# | TL | 2ms | 3640kb | C++20 | 2.4kb | 2022-10-19 16:56:26 | 2022-10-19 16:56:28 |
Judging History
answer
#ifndef LOCAL
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#endif
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define pb push_back
#define eb emplace_back
#define bg begin()
#define ed end()
#define all(x) x.bg,x.ed
#define si(x) int(x.size())
template<class t>using vc=vector<t>;
template<class t>using vvc=vc<vc<t>>;
using vi=vc<int>;
template<class t,class u>bool chmin(t&a,u b){
if(a>b){a=b;return true;}
else return false;
}
template<class t,class u>bool chmax(t&a,u b){
if(a<b){a=b;return true;}
else return false;
}
#define mp make_pair
#define a first
#define b second
using pi=pair<int,int>;
const int inf=INT_MAX/2-100;
struct segtree{
struct N{
pi mn=pi(inf,-1),rw=pi(inf,-1);
int lz=inf;
void init(int v,int i){
mn=pi(inf,-1);
rw=pi(v,i);
lz=inf;
}
void add(int v){
chmin(mn,pi(rw.a+v,rw.b));
chmin(lz,v);
}
void push(N&x,N&y){
x.add(lz);
y.add(lz);
lz=inf;
}
static N merge(const N&x,const N&y){
N res;
res.mn=min(x.mn,y.mn);
res.rw=min(x.rw,y.rw);
res.lz=inf;
return res;
}
};
vc<N> x;
int s;
void upd(int i){
x[i]=N::merge(x[i*2],x[i*2+1]);
}
void push(int i){
x[i].push(x[i*2],x[i*2+1]);
}
void init(vi a){
int n=si(a);
s=1;while(s<n)s*=2;
x.resize(s*2);
rep(i,n)x[s+i].init(a[i],i);
gnr(i,1,s)upd(i);
}
segtree(vi a){init(a);}
void add(int i,int l,int r,int b,int e,int v){
if(e<=l||r<=b)return;
if(b<=l&&r<=e)return x[i].add(v);
push(i);
add(i*2,l,(l+r)/2,b,e,v);
add(i*2+1,(l+r)/2,r,b,e,v);
upd(i);
}
void add(int b,int e,int v){
add(1,0,s,b,e,v);
}
void del(int i){
i+=s;
x[i].init(inf,-1);
while(i>>=1)upd(i);
}
/*pi get(int i,int l,int r,int b,int e){
if(e<=l||r<=b)return pi(inf,-1);
if(b<=l&&r<=e)return x[i].mn;
return min(
get(i*2,l,(l+r)/2,b,e),
get(i*2+1,(l+r)/2,r,b,e));
}
pi get(int b,int e){
return get(1,0,s,b,e);
}*/
};
void slv(){
int n;cin>>n;
vi x(n);rep(i,n)cin>>x[i];
vi neg=x;for(auto&v:neg)v=-v;
segtree a(x),b(neg);
ll ans=0;
auto work=[&](int i){
a.add(i+1,n,-x[i]);
b.add(0,i,x[i]);
a.del(i);
b.del(i);
};
work(0);
rep(step,n-1){
pi cur=min(a.x[1].mn,b.x[1].mn);
ans+=cur.a;
work(cur.b);
}
cout<<ans<<endl;
}
signed main(){
cin.tie(0);
ios::sync_with_stdio(0);
int t;cin>>t;rep(_,t)
slv();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3640kb
input:
2 5 1 2 3 4 5 3 10 45 10
output:
4 -35
result:
ok 2 number(s): "4 -35"
Test #2:
score: -100
Time Limit Exceeded
input:
300000 1 -167586 1 45048 1 -218274 1 -39107 1 -15880 1 33014 1 217559 1 -208936 1 -260570 1 -83353 1 -39868 1 -253159 1 -26640 1 -114610 1 -244464 1 -7217 1 -196817 1 168691 1 146930 1 284612 1 -93130 1 -186071 1 -31746 1 37800 1 -255791 1 -237603 1 81359 1 201796 1 106965 1 -8371 1 -85871 1 -270622...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...