QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#592362 | #9222. Price Combo | Displace_ | TL | 19ms | 324508kb | C++14 | 3.7kb | 2024-09-26 22:04:42 | 2024-09-26 22:04:43 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double dou;
typedef pair<int,int> pii;
#define fi first
#define se second
#define mapa make_pair
typedef long double ld;
typedef unsigned long long ull;
template <typename T>inline void read(T &x){
x=0;char c=getchar();bool f=0;
for(;c<'0'||c>'9';c=getchar()) f|=(c=='-');
for(;c>='0'&&c<='9';c=getchar())
x=(x<<1)+(x<<3)+(c^48);
x=(f?-x:x);
}
const int N=2e5+5;
const ll inf=1e18;
int n;
int a[N], b[N];
int rka[N], rkb[N], px[N], py[N];
inline bool cmp1(int x, int y){return a[x]<a[y];}
inline bool cmp2(int x, int y){return b[x]<b[y];}
struct mat{
ll a[4][4];
int n1, n2;
mat(){
for(int i=0; i<4; ++i) for(int j=0; j<4; ++j) a[i][j]=inf;
n1=4; n2=4;
}
inline friend mat operator *(mat x, mat y) {
mat ret;
for(int i=0; i<x.n1; ++i){
for(int j=0; j<y.n2; ++j){
for(int k=0; k<x.n2; ++k){
ret.a[i][j]=min(ret.a[i][j], x.a[i][k]+y.a[k][j]);
}
}
}
ret.n1=x.n1; ret.n2=y.n2;
return ret;
}
inline friend mat operator +(mat x, mat y){
mat ret;
for(int i=0; i<4; ++i) for(int j=0; j<4; ++j) ret.a[i][j]=min(x.a[i][j], y.a[i][j]);
return ret;
}
inline friend bool operator !=(mat x, mat y){
for(int i=0; i<4; ++i) for(int j=0; j<4; ++j) if(x.a[i][j]!=y.a[i][j]) return true;
return false;
}
}tr[N<<2], tt[N<<2], tag[N<<2], I, tem, bs;
int idx;
inline void push_up(int p){
tt[p]=tt[p<<1|1]*tt[p<<1];
tr[p]=(tr[p<<1|1]*tt[p<<1])+tr[p<<1];
}
inline void build(int p, int l, int r){
tag[p]=I; tt[p]=I;
if(l==r){
tr[p].n1=1; tr[p].n2=4;
if(l==n+1){
tr[p].a[0][0]=0;
}
return ;
}
int mid=(l+r)>>1;
build(p<<1, l, mid); build(p<<1|1, mid+1, r);
push_up(p);
}
inline void push_down(int p){
if(tag[p]!=I){
tr[p<<1]=tr[p<<1]*tag[p]; tag[p<<1]=tag[p<<1]*tag[p];
tr[p<<1|1]=tr[p<<1|1]*tag[p]; tag[p<<1|1]=tag[p<<1|1]*tag[p];
tag[p]=I;
}
}
inline void qry(int p, int l, int r, int L, int R){
if(L<=l&&r<=R) {
tem=(tem*tt[p])+tr[p];
return ;
}
int mid=(l+r)>>1;
push_down(p);
if(R>mid) qry(p<<1|1, mid+1, r, L, R);
if(L<=mid) qry(p<<1, l, mid, L, R);
}
inline void mdf(int p, int l, int r, int L, int R){
if(L>R) return ;
if(L<=l&&r<=R){
tr[p]=tr[p]*tem; tag[p]=tag[p]*tem;
return ;
}
int mid=(l+r)>>1;
push_down(p);
if(L<=mid) mdf(p<<1, l, mid, L, R);
if(R>mid) mdf(p<<1|1, mid+1, r, L, R);
push_up(p);
}
inline void mdf2(int p, int l, int r, int x){
if(l==r){
tt[p]=tem;
return ;
}
int mid=(l+r)>>1;
push_down(p);
if(x<=mid) mdf2(p<<1, l, mid, x);
else mdf2(p<<1|1, mid+1, r, x);
push_up(p);
}
inline void st(int p, int l, int r, int x){
if(l==r){
tr[p]=tem;
return ;
}
int mid=(l+r)>>1;
push_down(p);
if(x<=mid) st(p<<1, l, mid, x);
else st(p<<1|1, mid+1, r, x);
push_up(p);
}
int main(){
// freopen("D:\\nya\\acm\\C\\test.in","r",stdin);
// freopen("D:\\nya\\acm\\C\\test.out","w",stdout);
for(int i=0; i<4; ++i) I.a[i][i]=0;
read(n);
for(int i=1; i<=n; ++i) read(a[i]);
for(int i=1; i<=n; ++i) read(b[i]);
for(int i=1; i<=n; ++i) rka[i]=rkb[i]=i;
sort(rka+1, rka+n+1, cmp1);
sort(rkb+1, rkb+n+1, cmp2);
for(int i=1; i<=n; ++i) px[rka[i]]=i, py[rkb[i]]=i;
build(1, 0, n+1);
for(int _=n; _>=0; --_){
int i=rka[_];
tem=bs; tem.n1=1; tem.n2=4;
qry(1, 0, n+1, py[i]+1, n+1);
st(1, 0, n+1, py[i]);
mat v1=bs;
v1.a[1][0]=v1.a[3][2]=0;
v1.a[0][1]=v1.a[2][3]=b[rkb[py[i]]];
tem=v1;
mdf2(1, 0, n+1, py[i]);
mat v2=bs;
v2.a[2][0]=v2.a[3][1]=0;
v2.a[0][2]=v2.a[1][3]=a[i];
tem=v2;
mdf(1, 0, n+1, 0, py[i]);
}
tem=bs; tem.n1=1; tem.n2=4;
qry(1, 0, n+1, 0, n+1);
ll ans=inf;
for(int i=0; i<4; ++i) ans=min(ans, tem.a[0][i]);
printf("%lld\n", ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 16ms
memory: 324508kb
input:
7 10 12 19 99 10 8 49 9 14 15 199 11 7 19
output:
131
result:
ok single line: '131'
Test #2:
score: 0
Accepted
time: 19ms
memory: 323980kb
input:
7 10 12 19 99 10 8 49 9 14 15 199 11 7 19
output:
131
result:
ok single line: '131'
Test #3:
score: -100
Time Limit Exceeded
input:
199913 1212731 2525164 3210261 2457211 1013738 1931420 923123 867112 762069 2108660 108920 2491869 867107 387025 2278045 574027 1661570 820133 1274150 2001346 779766 3305537 3000211 2418643 2108660 2001343 1074820 2754411 826712 3117447 1661569 338161 1849064 3007920 3057426 468078 3252798 1274146 4...