QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#845291 | #9971. 新本格魔法少女りすか | pwx2009 | 10 | 3794ms | 20844kb | C++14 | 2.9kb | 2025-01-06 15:48:09 | 2025-01-06 15:48:11 |
Judging History
answer
#include<cstdio>
#define ll long long
#define lowbit(x) (x&(-x))
static const int BUF = 1<<20;
inline char gc() {
static char buf[BUF],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,BUF,stdin),p1==p2)?-1:*p1++;
}
int read(){
int num=0;
char ch;ch=gc();
while(ch<48||ch>57)ch=gc();
while(ch>47&&ch<58){
num=(num<<1)+(num<<3)+(ch^48);
ch=gc();
}return num;
}
const int N=5e5+15;
const int B=360;
int n,m,K;
int pos[N],l[N],r[N],a[N];
int ql[N],qr[N],qid[N];
int s[N],ss[N],S[N];
void put(int x,int val){
while(x<=n){
s[x]+=val;
x+=lowbit(x);
}
}
int ask(int x){
int val=0;
while(x){
val+=s[x];
x-=lowbit(x);
}return val;
}
ll ans[N];
int main(){
n=read(),m=read();
for(int i=1;i<=n;i++){
a[i]=read();
if(i>B)pos[i]=pos[i-B]+1;
if(!l[pos[i]])l[pos[i]]=i;
r[pos[i]]=i;
}
if(n+1>B)pos[n+1]=pos[n+1-B]+1;
if(!l[pos[n+1]])l[pos[n+1]]=n+1;
r[pos[n+1]]=n+1;
for(int i=1;i<=m;i++){
int p=read();
while(p--){
K++;
ql[K]=read(),qr[K]=read(),qid[K]=i;
}
}
for(int L=1,R=B;L<=n;L+=B,R+=B){
for(int i=L;i<=R;i++)s[a[i]]++;
for(int i=1;i<=n;i++)ss[i]=s[i]+ss[i-1];
for(int i=1;i<=n;i++){
S[i]=S[i-1];
if(i<L)S[i]+=R-L+1-ss[a[i]];
if(i>R)S[i]+=ss[a[i]];
}
for(int i=1,j=1;i<=K;i=j){
bool flag=0;
ll sum=0;
while(j<=K&&qid[j]==qid[i]){
if(ql[j]<=L&&qr[j]>=R)flag=1;
else{
if(ql[j]>R&&l[pos[qr[j]+1]]>r[pos[ql[j]-1]])
sum-=S[l[pos[qr[j]+1]]-1]-S[r[pos[ql[j]-1]]];
sum+=S[qr[j]]-S[ql[j]-1];
}j++;
}ans[qid[i]]+=sum*flag;
}for(int i=L;i<=R;i++)s[a[i]]--;
}
for(int i=1,j=1;i<=K;){
while(j<=K&&qid[j]==qid[i]){
if(r[pos[ql[j]-1]]<l[pos[qr[j]+1]]){
for(int k=ql[j];k<=r[pos[ql[j]-1]];k++)ans[qid[i]]+=ask(a[k]);
for(int k=l[pos[qr[j]+1]];k<=qr[j];k++)ans[qid[i]]+=ask(a[k]);
for(int k=ql[j];k<=r[pos[ql[j]-1]];k++)put(a[k],1);
for(int k=l[pos[qr[j]+1]];k<=qr[j];k++)put(a[k],1);
}else{
for(int k=ql[j];k<=qr[j];k++)ans[qid[i]]+=ask(a[k]);
for(int k=ql[j];k<=qr[j];k++)put(a[k],1);
}j++;
}
while(i<j){
if(r[pos[ql[i]-1]]<l[pos[qr[i]+1]]){
for(int k=ql[i];k<=r[pos[ql[i]-1]];k++)put(a[k],-1);
for(int k=l[pos[qr[i]+1]];k<=qr[i];k++)put(a[k],-1);
}else for(int k=ql[i];k<=qr[i];k++)put(a[k],-1);
i++;
}
}
for(int i=1;i<=m;i++)printf("%lld\n",ans[i]);
}
詳細信息
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 0
Time Limit Exceeded
input:
500000 50174 453952 363686 491616 32825 57465 422945 471561 73291 421205 416554 23295 133266 242225 330448 25039 340064 28713 465664 162059 323880 28978 273728 101338 161413 294941 214275 63951 267981 294251 202822 253124 272510 3268 37918 139343 385983 111577 311901 487665 482827 347449 416029 3065...
output:
result:
Subtask #2:
score: 10
Accepted
Test #6:
score: 10
Accepted
time: 3784ms
memory: 18296kb
input:
500000 5 157360 289139 98034 293691 150262 268366 36782 147093 365410 444658 343224 375392 278298 357620 389673 167019 7747 119244 102126 83512 3649 459230 197365 245259 38071 249539 34617 213697 292553 389625 395778 280152 280038 239519 301475 232272 145919 370004 422791 271143 488283 185166 351026...
output:
50666226791 50440151159 50719090228 50631079493 50747050985
result:
ok 5 number(s): "50666226791 50440151159 50719090228 50631079493 50747050985"
Test #7:
score: 10
Accepted
time: 3561ms
memory: 20844kb
input:
500000 5 70752 248917 41910 13653 177839 45858 54229 174152 10090 332231 437916 391622 432270 53875 446421 91921 461870 243336 363086 249844 338371 495447 423857 350363 365324 255747 170681 435791 177298 199582 240768 449011 302158 378518 233809 267343 362784 187864 114604 322031 255693 54447 406202...
output:
48041849427 47573923544 48149375837 46484835228 62373267184
result:
ok 5 number(s): "48041849427 47573923544 48149375837 46484835228 62373267184"
Test #8:
score: 10
Accepted
time: 3794ms
memory: 18328kb
input:
500000 5 241608 419462 82981 424356 17797 426283 105137 396042 362939 472171 51823 373467 302031 234957 103628 345691 280071 391667 370350 226645 393628 99397 111403 124617 289298 487366 430929 445895 146724 165853 250446 134811 333218 232414 263197 300887 194261 147235 493474 305082 109715 82765 16...
output:
50381279295 50436234418 50754046820 50457636384 51060372228
result:
ok 5 number(s): "50381279295 50436234418 50754046820 50457636384 51060372228"
Test #9:
score: 10
Accepted
time: 3553ms
memory: 19564kb
input:
500000 5 384662 360905 151827 77657 463152 472106 142072 464841 224605 243338 468024 153983 367505 347441 107735 63958 144215 118222 294699 106900 236160 308499 164080 9056 373110 343094 108210 164120 416292 77276 327997 344605 375908 60401 331692 169348 431340 174229 301328 62849 130369 272892 1802...
output:
47987223119 47114724607 47699758559 47424207451 62476342231
result:
ok 5 number(s): "47987223119 47114724607 47699758559 47424207451 62476342231"
Test #10:
score: 10
Accepted
time: 3775ms
memory: 18880kb
input:
500000 5 454113 396339 270960 34848 390907 416021 317393 9900 333318 50842 198100 115 286821 210759 79669 257489 94065 308047 220510 343737 11392 410497 58962 488921 315855 115533 11194 298501 112352 81722 415920 175976 463840 491310 29020 479390 494454 375738 37344 278711 347144 351121 38809 194902...
output:
50554118536 50349176037 50371935305 50817175419 50798953918
result:
ok 5 number(s): "50554118536 50349176037 50371935305 50817175419 50798953918"
Subtask #3:
score: 0
Skipped
Dependency #1:
0%