QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#350520 | #5173. 染色 | ANIG | 0 | 455ms | 183560kb | C++23 | 2.5kb | 2024-03-10 19:47:37 | 2024-03-10 19:47:38 |
Judging History
answer
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast","inline")
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+5,T=17;
int n,m,w[N],lst[N],nxt[N],wz[N],to[N],f1[N][20],f2[N][20],mk[N];
vector<int>q1,q2;
inline int read(){
int res=0;char c;
do{
c=getchar();
}while(!isdigit(c));
while(isdigit(c)){
res=res*10+c-'0';
c=getchar();
}
return res;
}
int solve1(int l,int r){
int L=0,R=q1.size();
while(L<R){
int mid=L+R>>1;
if(q1[mid]>=l)R=mid;
else L=mid+1;
}
int w=L;
if(q1[w]<l||nxt[q1[w]]>r)return 0;
int res=0,x=nxt[q1[w]];
for(int i=T;i>=0;i--){
if(f1[x][i]<=r)x=f1[x][i],res|=1<<i;
}
return res+1;
}
int solve2(int l,int r){
int L=0,R=q2.size();
while(L<R){
int mid=L+R>>1;
if(q2[mid]>=r-1)R=mid;
else L=mid+1;
}
int w=L;
if(q2[w]>r)return 0;
int res=0,x=q2[w];
for(int i=T;i>=0;i--){
if(f2[x][i]>=l)x=f2[x][i],res|=1<<i;
}
return res+(lst[x]>=l);
}
int solve(int l,int r){
if(l<=r)return 2*(r-l)-solve1(l,r);
return 2*(l-r)-solve2(r,l);
}
void print(int x){
if(x>9)print(x/10);
putchar(x%10+'0');
}
signed main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
cin>>n>>m;
for(int i=1;i<=n;i++){
w[i]=read();
lst[i]=wz[w[i]];
wz[w[i]]=i;
}
for(int i=1;i<=n;i++)wz[i]=n+1;
for(int i=n;i>=1;i--){
nxt[i]=wz[w[i]];
wz[w[i]]=i;
}
for(int i=0;i<=T;i++)for(int j=0;j<=n+1;j++)f1[j][i]=n+1;
for(int i=1,j=0,nw=0;i<=n;i++){
if(lst[i]<lst[j]){
mk[i]=1;
continue;
}
to[j]=i;
while(to[nw]<=lst[i])nw=to[nw];
f2[i][0]=nw;
j=i;
q1.push_back(lst[i]);
q2.push_back(i);
}
nxt[n+1]=n+1;
for(int i=n,j=n+1,nw=n+1;i>=1;i--){
if(mk[nxt[i]]||nxt[i]>n)continue;
to[j]=i;
while(to[nw]>=nxt[i])nw=to[nw];
f1[nxt[i]][0]=nxt[nw];
// cout<<i<<" "<<nxt[i]<<" "<<nxt[nw]<<endl;
j=i;
}
for(int i=1;i<=T;i++){
for(int j=1;j<=n;j++){
f1[j][i]=f1[f1[j][i-1]][i-1];
f2[j][i]=f2[f2[j][i-1]][i-1];
}
}
for(int i=1;i<=m;i++){
int l=read(),r=read();
print(solve(l,r));
putchar('\n');
}
}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Wrong Answer
Test #1:
score: 0
Wrong Answer
time: 4ms
memory: 18080kb
input:
10000 100 84 85 52 2 78 53 20 21 23 76 37 44 18 5 37 8 81 65 46 58 69 1 69 37 53 46 37 35 35 89 1 77 35 6 46 59 89 46 25 55 50 38 61 67 44 23 29 24 46 4 42 15 34 77 20 34 83 79 12 50 69 26 38 14 9 66 80 72 22 26 9 68 35 38 19 84 92 30 83 62 100 71 81 60 7 37 64 50 33 60 86 75 45 78 32 53 3 48 87 60 ...
output:
3668 4776 5976 8382 760 7013 183 7320 492 1190 253 2249 13226 6254 456 7507 2162 10481 1791 2784 4801 304 5997 627 6946 6179 11900 2712 3157 7836 18542 511 5486 17835 9980 6147 4996 14577 670 463 15613 13216 8872 7833 11456 6603 6399 5023 9496 8529 2173 10740 2806 12537 5170 2144 8809 1907 6476 2828...
result:
wrong answer 2nd words differ - expected: '4575', found: '4776'
Subtask #2:
score: 0
Wrong Answer
Test #7:
score: 0
Wrong Answer
time: 52ms
memory: 33204kb
input:
100000 100000 3 2 3 3 3 3 2 3 2 1 3 1 1 1 3 2 1 3 1 2 2 1 3 1 2 2 1 1 1 3 2 1 3 3 3 3 1 1 1 2 3 3 2 1 1 1 3 1 3 1 3 2 1 3 2 3 3 2 3 3 2 3 3 3 3 3 2 3 2 3 1 3 3 3 3 3 3 3 1 2 3 3 1 3 1 1 2 2 3 1 1 2 3 2 3 1 3 2 1 3 2 3 2 1 1 3 3 1 3 1 2 2 2 3 2 3 2 3 2 1 1 3 1 3 2 2 3 3 3 1 2 2 3 3 2 1 3 1 2 2 2 3 2 ...
output:
113194 133099 54922 6483 9199 78290 93921 29192 38941 111588 66619 7226 66019 42776 44640 49274 11102 107722 71676 10708 4735 87161 46860 75805 27270 14916 12237 59513 18641 55105 11626 7850 5521 38727 67276 58326 16941 70131 70341 53673 51864 551 18481 163246 5456 43296 62515 33973 9871 24982 33223...
result:
wrong answer 3rd words differ - expected: '54921', found: '54922'
Subtask #3:
score: 0
Wrong Answer
Test #15:
score: 0
Wrong Answer
time: 3ms
memory: 15888kb
input:
5000 5000 256 63 197 36 75 66 33 72 27 75 66 248 29 166 209 252 141 95 84 226 147 249 116 94 192 256 199 273 182 166 116 274 27 211 154 144 283 23 53 110 215 11 164 284 161 221 251 96 43 47 18 115 12 51 156 61 116 209 93 98 47 165 174 106 83 67 184 75 12 290 183 197 112 240 67 56 215 148 104 5 141 2...
output:
1354 2696 1743 2672 4874 4995 2950 1360 880 5076 8000 4860 958 925 808 7270 7844 5458 3813 3310 4478 1612 2540 40 901 310 4460 3882 3898 5739 4610 1694 446 1450 3706 5378 565 122 2090 7615 9330 2098 3121 3683 2883 432 727 4006 179 3308 1474 1602 7127 3008 6096 8083 1539 3892 363 4476 3374 1741 818 2...
result:
wrong answer 1st words differ - expected: '1322', found: '1354'
Subtask #4:
score: 0
Wrong Answer
Test #23:
score: 0
Wrong Answer
time: 455ms
memory: 183560kb
input:
1000000 1000000 1105 3246 1880 3554 818 2331 2576 4140 149 4562 3498 3536 3400 4788 4363 4742 1216 4218 4032 1701 1489 4889 1761 3022 3145 4945 3067 4304 5016 4624 1612 13 1335 3613 1086 2210 386 3464 1156 3352 4341 5006 3465 3900 622 654 1826 2983 1250 4164 3335 4308 2995 1982 1347 4335 2535 5054 4...
output:
1263815 310352 760115 79906 161252 576908 988222 1716103 1345118 216404 619442 546263 1385912 320711 1100470 52291 276158 227555 2476 148584 145596 667128 25418 225036 185386 1445226 1666310 550730 146406 969316 1106498 238878 821898 113132 84810 1188701 316694 717959 169528 559867 772116 412202 736...
result:
wrong answer 2nd words differ - expected: '308608', found: '310352'