QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#554288 | #8546. Min or Max 2 | ucup-team1231# | TL | 4953ms | 26852kb | C++17 | 4.5kb | 2024-09-09 10:01:46 | 2024-09-09 10:01:46 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define SZ 1234567
int T,n,a[SZ],b[SZ];
typedef long long ll;
typedef pair<int,int> pii;
#define fi first
#define se second
#define pb push_back
struct func {
bool ct=0;
int cmx=-1e9,cmi=1e9,c;
func() {}
func(int a,int b) {cmx=a; cmi=b;}
func(int s) {ct=1; c=s;}
int f(int x) {
if(ct) return c;
return max(min(x,cmi),cmx);
}
};
func operator + (func a,func b) {
if(a.ct) return func(b.f(a.c));
if(b.ct) return b;
if(max(a.cmx,b.cmx)<=min(a.cmi,b.cmi))
return func(max(a.cmx,b.cmx),min(a.cmi,b.cmi));
if(a.cmi<b.cmx) return func(b.cmx);
if(a.cmx>b.cmi) return func(b.cmi);
assert(0);
}
pii operator + (pii a,pii b) {
return pii(min(a.fi,b.fi),max(a.se,b.se));
}
bool ne(pii s) {
return s.fi<=s.se;
}
pii em(1e9,-1e9);
pii apply(pii p,func f) {
if(!ne(p)) return em;
return pii(f.f(p.fi),f.f(p.se));
}
func MX(int x) {return func(x,1e9);}
func MI(int x) {return func(-1e9,x);}
pii rst[SZ];
bool ceq[SZ];
int M;
vector<func> tg,seg2;
vector<pii> seg;
void pd(int x) {
seg[x]=apply(seg[x],tg[x]);
if(x<M)
tg[x+x]=tg[x+x]+tg[x],
tg[x+x+1]=tg[x+x+1]+tg[x];
tg[x]=func();
}
void pu(int x) {
seg[x]=seg[x+x]+seg[x+x+1];
}
pii qry(int x,int l,int r,int ql,int qr) {
if(r<ql||qr<l) return em;
pd(x);
if(ql<=l&&r<=qr) return seg[x];
int m=(l+r)>>1;
return qry(x+x,l,m,ql,qr)+qry(x+x+1,m+1,r,ql,qr);
}
void tagg(int x,int l,int r,int ql,int qr,func s) {
if(r<ql||qr<l) return;
pd(x);
if(ql<=l&&r<=qr) {
tg[x]=s;
pd(x);
return;
}
int m=(l+r)>>1;
tagg(x+x,l,m,ql,qr,s);
tagg(x+x+1,m+1,r,ql,qr,s);
pu(x);
}
void edt(int x,int l,int r,int ql,int qr,pii s) {
if(r<ql||qr<l) return;
pd(x);
if(ql<=l&&r<=qr) {
seg[x]=s;
assert(l==r);
return;
}
int m=(l+r)>>1;
edt(x+x,l,m,ql,qr,s);
edt(x+x+1,m+1,r,ql,qr,s);
pu(x);
}
pii qry(int l,int r) {
if(l>r) return em;
return qry(1,0,M-1,l,r);
}
void upd(int x,pii u) {
edt(1,0,M-1,x,x,u);
}
void tagg(int l,int r,func s) {
tagg(1,0,M-1,l,r,s);
}
void edtb(int x,func s) {
x+=M; seg2[x]=s;
while(x>>=1) seg2[x]=seg2[x+x]+seg2[x+x+1];
}
func qryb(int l,int r) {
func L,R;
for(l+=M-1,r+=M+1;l^r^1;l>>=1,r>>=1) {
if(~l&1) L=L+seg2[l^1];
if(r&1) R=seg2[r^1]+R;
}
return L+R;
}
ll ans[SZ];
void work(bool eq) {
for(int i=1;i<=n;++i) upd(i,em);
M=1;
while(M<n+2) M<<=1;
tg.resize(M+M+2);
for(auto&s:tg) s=func();
seg2=tg;
seg.resize(M+M+2);
for(auto&s:seg) s=em;
upd(a[1],pii(b[1],b[1]));
for(int i=1;i<=n;++i) {
if(i!=1) {
pii L=qry(1,a[i]-1);
// cerr<<L.fi<<"~"<<L.se<<" "<<b[i]<<"\n";
ceq[i]=L.fi<b[i]&&ne(L);
L=apply(L,MX(b[i]));
pii R=qry(a[i]+1,n);
ceq[i]|=R.se>b[i]&&ne(R);
R=apply(R,MI(b[i]));
tagg(1,a[i]-1,MI(b[i]));
tagg(a[i]+1,n,MX(b[i]));
upd(a[i],L+R);
}
else ceq[i]=1;
rst[i]=qry(a[i],a[i]);
// cerr<<i<<":"<<ceq[i]<<" "<<rst[i].fi<<"~"<<rst[i].se<<"\n";
}
for(int i=1;i<=n;++i) edtb(i,MI(b[i]));//s2[i+M]=set_lg(i);
vector<int> U;
for(int i=1;i<=n;++i) U.pb(i);
sort(U.begin(),U.end(),[&](int p,int q) {return a[p]<a[q];});
for(auto i:U) {
func r = qryb(i+1,n);
set<int> B;
if(eq&&ceq[i]) {
if(r.f(b[i])==b[i]) B.insert(b[i]);
}
for(auto g:vector<int>{rst[i].fi,rst[i].se}) {
int gg=r.f(g);
bool good=0;
if(r.ct) good=(gg==r.c);
else good=(gg==r.cmi||gg==r.cmx);
if(good) B.insert(gg);
}
// cout<<a[i]<<"->";
// for(auto t:B) cout<<t<<" ";
// cout<<" ("<<eq<<")\n";
for(auto t:B) {
int diff = abs(t - a[i]);
++ans[diff];
}
edtb(i,MX(b[i]));
}
}
void sol() {
scanf("%d",&n);
for(int i=0;i<=n;++i) ans[i]=0;
for(int i=1;i<=n;++i) scanf("%d",a+i);
for(int i=1;i<=n;++i) scanf("%d",b+i);
work(1);
for(int i=1;i<=n;++i) swap(a[i],b[i]);
work(0);
for(int i=0;i<n;++i)
printf("%lld ",ans[i]);
printf("\n");
}
int main() {
scanf("%d",&T);
while(T--) sol();
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 9944kb
input:
4 2 1 2 2 1 5 2 4 1 5 3 2 4 1 5 3 5 1 2 3 4 5 5 4 3 2 1 8 5 8 3 4 2 7 1 6 4 6 3 8 5 1 2 7
output:
2 0 5 0 0 0 0 2 2 2 2 0 5 5 2 2 1 0 0 0
result:
ok 20 numbers
Test #2:
score: 0
Accepted
time: 840ms
memory: 9924kb
input:
66664 7 4 2 6 5 7 1 3 6 5 3 1 4 7 2 10 6 8 10 7 5 1 4 3 9 2 5 10 3 8 6 7 2 9 1 4 9 3 2 4 8 7 6 9 1 5 8 1 2 9 6 7 4 3 5 10 4 3 9 6 7 2 10 1 8 5 3 5 4 1 2 7 10 9 6 8 5 3 4 1 2 5 5 1 3 2 4 5 2 4 3 5 1 2 3 1 4 5 6 2 6 1 3 4 5 6 4 5 1 3 2 10 10 1 2 7 5 8 4 3 9 6 9 4 2 3 6 1 7 8 5 10 5 1 2 4 5 3 4 1 2 5 3...
output:
4 4 2 2 1 0 0 5 6 3 2 2 1 0 0 0 0 5 6 3 2 1 0 0 0 0 4 4 4 3 2 1 0 0 0 0 5 3 0 0 0 2 2 2 2 0 3 3 3 1 0 0 5 7 4 2 1 0 0 0 0 0 5 2 0 0 0 6 3 0 0 0 0 3 3 2 0 0 5 4 2 1 0 0 0 3 2 3 1 0 0 4 6 3 0 0 0 0 3 4 3 2 1 0 0 3 2 2 2 2 2 2 1 0 4 5 3 1 0 0 0 3 4 3 2 3 3 1 0 0 0 8 5 0 0 0 0 0 0 7 8...
result:
ok 499999 numbers
Test #3:
score: 0
Accepted
time: 1778ms
memory: 11980kb
input:
6690 72 31 50 47 60 24 33 72 49 5 26 17 65 40 64 8 2 19 51 30 58 71 16 66 56 9 48 21 61 44 59 22 11 15 28 68 29 1 27 37 41 23 6 20 62 43 34 18 4 70 54 13 12 36 35 25 67 45 38 69 53 42 63 55 3 14 7 57 32 52 39 10 46 31 9 7 56 32 64 39 33 62 24 49 54 18 53 43 40 4 28 37 2 61 47 10 26 23 16 22 30 11 60...
output:
7 11 7 5 6 6 5 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2 2 2 3 4 4 4 4 4 3 1 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 3 4 6 6 4 4 3 2 2 2 2 2 2 2 2 2 3 4 4 4 4 4 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 6 6 7 8 7 6 7 8 8 7 5 4 4 3 2 2 2...
result:
ok 499981 numbers
Test #4:
score: 0
Accepted
time: 2651ms
memory: 12060kb
input:
666 775 98 357 198 407 409 200 454 585 319 622 366 264 710 91 765 78 32 528 335 101 469 204 312 382 276 613 231 342 327 324 441 544 413 299 494 393 349 611 211 702 165 297 320 284 401 530 317 567 142 742 447 482 662 126 506 273 362 328 555 416 206 604 589 305 99 114 291 131 386 75 670 280 704 189 43...
output:
11 20 20 20 19 18 18 18 18 18 18 18 18 18 18 18 18 17 16 16 16 16 16 16 16 16 16 16 16 16 16 15 14 14 14 14 13 12 12 12 12 12 12 12 12 11 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 9 8 8 8 8 7 6 6 6 6 6 6 6 6 6 6 6 6 ...
result:
ok 500000 numbers
Test #5:
score: 0
Accepted
time: 3627ms
memory: 10992kb
input:
65 9836 5216 2035 5946 2744 9708 9116 1184 2000 4650 569 2428 585 3406 8146 6809 875 9131 9092 5998 2088 8393 9447 7766 4990 3903 7730 3426 6726 2029 4208 1546 4639 997 1428 2357 8630 7552 3531 7241 3530 4548 7310 3205 3508 9764 8929 4781 5702 3777 670 7384 1049 1707 4544 1637 9349 2427 3338 634 596...
output:
8 13 12 11 10 10 10 10 10 11 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 10 8 8 8 8 8 8 8 8 8 7 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6...
result:
ok 500000 numbers
Test #6:
score: 0
Accepted
time: 4550ms
memory: 17524kb
input:
10 50000 41908 17741 2708 24703 42556 45152 20315 17143 16957 48829 30280 7534 9806 2455 27752 28698 34180 30641 1976 19099 8271 18233 1745 46600 1241 19569 3867 840 10336 49514 49491 43521 26857 2277 9054 3016 41237 15468 48237 37950 17442 44525 46971 40928 34005 15252 14887 48465 26039 13079 40736...
output:
14 26 24 23 22 20 19 18 18 18 18 18 18 18 18 17 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 ...
result:
ok 500000 numbers
Test #7:
score: 0
Accepted
time: 4953ms
memory: 26852kb
input:
5 100000 70435 82297 64478 96762 51480 50092 4607 731 51337 24967 98580 69079 26300 41515 57826 4744 92883 9657 3287 12443 17262 15832 41138 53280 53542 76263 69136 64644 83006 75161 76406 36134 88895 42403 28621 24815 35829 8899 38986 16475 15367 82413 56456 17355 21826 58017 2762 62100 53594 63010...
output:
18 35 35 34 32 30 29 28 28 28 27 26 25 24 24 24 24 24 23 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 ...
result:
ok 500000 numbers
Test #8:
score: -100
Time Limit Exceeded
input:
1 500000 288678 283027 462112 250066 400523 251310 197889 338696 246896 186409 257754 49529 168322 411341 237155 355566 411724 493312 55378 447538 212152 404975 126509 127651 69151 318513 199941 486793 15909 363053 288273 492124 313983 118718 165238 332535 415290 337782 365660 206063 123709 273038 4...