QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#886805 | #9973. 魔法少女网站第二部 | nullptr_qwq | TL | 2960ms | 180796kb | C++17 | 3.6kb | 2025-02-07 11:39:07 | 2025-02-07 11:39:09 |
Judging History
answer
// Problem: P8078 [WC2022] 秃子酋长
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P8078
// Memory Limit: 512 MB
// Time Limit: 5000 ms
//
// Powered by CP Editor (https://cpeditor.org)
// 私は猫です
#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mkp make_pair
#define fi first
#define se second
#define inf 1000000000
#define infll 1000000000000000000ll
#define pii pair<int,int>
#define rep(i,a,b,c) for(int i=(a);i<=(b);i+=(c))
#define per(i,a,b,c) for(int i=(a);i>=(b);i-=(c))
#define F(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define dF(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
#define cmh(sjy) while(sjy--)
#define lowbit(x) (x&(-x))
#define HH printf("\n")
#define eb emplace_back
#define poly vector<int>
#define SZ(x) ((int)x.size())
using namespace std;
template<typename T>inline void chkmax(T &x,const T &y){ x=std::max(x,y); }
template<typename T>inline void chkmin(T &x,const T &y){ x=std::min(x,y); }
const int mod=998244353,maxn=2000005;
ll ans[maxn];
int a[maxn],n,p[maxn],zsy;
namespace DS{
ll t[maxn];
inline void add(int x,ll y){ for(;x<=n;x+=lowbit(x))t[x]+=y; }
inline ll query(int x){ ll res=0;for(;x;x&=(x-1))res+=t[x]; return res; }
}
basic_string<int>que[maxn];
int pre[maxn],nxt[maxn],to[maxn],ql[maxn],qr[maxn];
inline int calc(int x,int y){
if(x<=0||x>n||y<=0||y>n)return 0;
return x>=y?x-y:y-x;
}
void Solve(int l,int r,basic_string<int>&qu){
if(l==r)return;
basic_string<int>lef,rig,vec;
const int mid=(l+r)>>1;
for(int &i:qu)
if(qr[i]<=mid)lef+=i;
else if(mid<ql[i])rig+=i;
else vec+=i;
Solve(l,mid,lef),Solve(mid+1,r,rig);
if(qu.empty())return inplace_merge(p+l,p+mid+1,p+r+1,[&](int x,int y){ return a[x]<a[y]; }),void();
auto sol_points_only_in_Left=[&](){
for(const int &i:vec)que[ql[i]]+=i;
ll sum=0;
auto upd=[&](int x,int op){
const auto &y=nxt[x];
if(!x){
if(y<=n)DS::add(to[x],-op*y);
}else if(y==n+1)DS::add(to[x],-op*x);
else sum+=abs(x-y)*op,DS::add(to[x],op*(-x-y-abs(x-y)));
};
int pos=mid;
F(i,l-1,mid){
const int x=(i==l-1)?0:p[i],y=(i==mid)?n+1:p[i+1];
nxt[x]=y,pre[y]=x,to[x]=n+1;
for(;pos<r&&a[p[pos+1]]<a[y];++pos)chkmin(to[x],p[pos+1]);
upd(x,1);
}
F(i,l,mid){
for(int &id:que[i])ans[id]+=sum+DS::query(qr[id]);
const int x=pre[i],y=nxt[i];
upd(x,-1),upd(i,-1),chkmin(to[x],to[i]),nxt[x]=y,pre[y]=x,upd(x,1);
que[i].clear();
}
};
auto sol_points_only_in_Right=[&](){
for(const int &i:vec)que[qr[i]]+=i;
ll sum=0;
auto upd=[&](int x,int op){
const auto &y=nxt[x];
if(!x){
if(y<=n)sum+=op*y,DS::add(to[x]+1,-op*y);
}else if(y==n+1) sum+=op*x,DS::add(to[x]+1,-op*x);
else sum+=op*(x+y),DS::add(to[x]+1,op*(abs(x-y)-x-y));
};
int pos=l-1;
F(i,mid,r){
const int x=(i==mid)?0:p[i],y=(i==r)?n+1:p[i+1];
nxt[x]=y,pre[y]=x,to[x]=0;
for(;pos<mid&&a[p[pos+1]]<a[y];++pos)chkmax(to[x],p[pos+1]);
upd(x,1);
}
dF(i,r,mid+1){
for(const int &id:que[i])ans[id]+=sum+DS::query(ql[id]);
const int x=pre[i],y=nxt[i];
upd(x,-1),upd(i,-1),chkmax(to[x],to[i]),nxt[x]=y,pre[y]=x,upd(x,1);
que[i].clear();
}
}; sol_points_only_in_Right(),sol_points_only_in_Left();
inplace_merge(p+l,p+mid+1,p+r+1,[&](int x,int y){ return a[x]<a[y]; });
}
void solve(){
cin>>n>>zsy,a[n+1]=n+1,a[0]=0;
F(i,1,n)cin>>a[i],p[i]=i;
basic_string<int>U;
F(i,1,zsy)cin>>ql[i]>>qr[i],U+=i;
Solve(1,n,U);
F(i,1,zsy)cout<<ans[i]<<'\n';
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int zsy=1;
F(____,1,zsy)solve();
}
詳細信息
Test #1:
score: 100
Accepted
time: 2960ms
memory: 179136kb
input:
1999999 1999998 8 9 6 4 1 7 5 13 3 16 2 20 14 10 12 17 21 11 27 29 19 23 15 18 24 26 22 31 28 37 36 25 38 41 34 43 35 30 33 45 51 32 52 42 50 44 39 40 58 56 48 49 46 59 47 63 60 57 54 55 66 53 65 62 67 61 72 75 68 69 71 79 77 64 85 82 74 73 70 87 76 81 78 88 91 90 84 97 80 89 93 99 83 98 96 86 95 10...
output:
918614 3322452 5149417 6621380 6533731 4094511 1379510 12924019 8157295 207612 6762916 1785007 4192984 2683252 8261655 6023203 9129238 1758632 6933498 3993947 4345067 11514930 5784142 3105482 8690594 6483330 5398139 146567 1726131 678045 7787022 2468410 8925300 2915763 2410040 1092694 568436 3475699...
result:
ok 1999998 numbers
Test #2:
score: 0
Accepted
time: 2922ms
memory: 180796kb
input:
2000000 2000000 1 5 10 6 7 2 16 13 3 8 15 12 4 14 17 11 9 22 20 27 30 24 21 28 18 19 25 35 33 29 23 26 43 41 44 39 32 34 31 36 40 42 45 37 48 54 38 46 59 47 52 62 63 49 50 51 58 56 64 53 60 57 61 66 55 65 77 69 75 70 67 76 80 72 74 85 68 71 89 73 79 78 83 90 86 81 96 94 82 97 93 84 98 100 99 88 87 9...
output:
313920 6698345 2961193 3760894 1903023 549714 5500986 1427297 168364 7880151 7786970 4914863 8011527 5473090 10758103 3603965 1922957 7492511 6241425 8034137 3953170 4224180 10918550 8192854 10607899 564778 493566 2426821 12903222 1008307 1113749 5046569 2226134 403996 4119770 874567 2775979 3062657...
result:
ok 2000000 numbers
Test #3:
score: 0
Accepted
time: 321ms
memory: 142480kb
input:
1997 1993006 687 310 1393 747 1050 349 1814 1842 638 1694 321 1613 1034 1724 1579 1292 1761 1387 1213 463 1884 1882 835 316 1722 8 1743 473 978 1274 403 1617 1711 156 1227 40 1858 851 1625 1787 1779 1289 472 1103 1739 1563 507 1384 1804 1079 199 347 1462 1891 198 583 1112 1399 940 1958 1830 318 1312...
output:
1240830 1253194 1124612 1178272 1124252 1207336 1222464 1067970 1182422 1294456 1081384 1134164 1028222 1303360 1176576 1333256 1217496 1184112 826586 1321466 907642 1200660 1338438 1037922 1150322 1107906 1225848 947268 763606 1312232 1092234 1048136 1276728 1282240 1320450 826070 1150624 1089414 1...
result:
ok 1993006 numbers
Test #4:
score: 0
Accepted
time: 375ms
memory: 143764kb
input:
2000 1999000 7 8 1120 3 2 6 1 5 1118 4 15 1119 14 16 18 17 19 12 10 13 11 9 1125 1126 1124 1128 653 1127 1121 1123 1122 1137 1136 620 1130 1129 598 661 599 1160 654 656 655 608 1159 1157 1158 1153 657 1154 585 1155 584 658 588 586 587 1156 609 1133 1132 621 1131 611 597 660 596 1134 595 659 610 1135...
output:
21604 8101 19995 14882 8432 877 5904 13612 1227 3406 25246 19909 10398 3746 992 6920 7365 3098 9999 12602 13469 5809 9235 5502 18869 2545 20293 9800 9162 22085 629 17750 9312 6800 3584 5285 1665 18997 8614 910 8170 3884 2539 16147 4482 6615 4589 56 14542 5125 2243 8915 11027 17562 901 13603 15113 35...
result:
ok 1999000 numbers
Test #5:
score: -100
Time Limit Exceeded
input:
1999998 1999996 1950413 1996247 1995151 1968122 1967590 1953664 1990263 1993028 1840495 1948577 1937627 1996103 1985373 1990286 1941726 1980603 1911368 1997777 1980827 1911708 1857169 1908738 1988483 1981228 1964408 1973964 1988108 1997405 1974863 1955238 1931220 1956903 1979158 1973709 1994885 1927...