QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#350516#5173. 染色ANIG0 473ms183632kbC++232.4kb2024-03-10 19:45:152024-03-10 19:45:15

Judging History

This is the latest submission verdict.

  • [2024-03-10 19:45:15]
  • Judged
  • Verdict: 0
  • Time: 473ms
  • Memory: 183632kb
  • [2024-03-10 19:45:15]
  • Submitted

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[w]>r)return 0;
    int res=0,x=nxt[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){
    auto w=upper_bound(q2.begin(),q2.end(),r);
    if(w==q2.begin())return 0;
    w--;
    int res=0,x=*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');
    }
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 16000kb

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
4575
6233
8739
729
7315
183
7631
513
1140
263
2343
13226
6254
473
7507
2162
10481
1661
2675
5009
293
6253
655
6654
6445
11414
2835
3297
8169
17782
531
5096
18597
9564
6413
5207
15207
695
483
16285
12669
9255
7755
10987
6891
6673
5239
9102
8893
2273
10299
2694
13081
4958
2239
8784
1985
6749
2955...

result:

wrong answer 3rd words differ - expected: '5976', found: '6233'

Subtask #2:

score: 0
Wrong Answer

Test #7:

score: 0
Wrong Answer
time: 53ms
memory: 33156kb

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
132097
54921
,
12517
78290
93920
27166
53047
111588
90721
-
66019
37079
44640
44232
3951
146747
71676
10708
6443
82579
34092
75805
27270
10951
(
53481
4368
46276
86
10701
4893
38727
67276
58326
6553
70130
70341
46046
51864
753
18481
119885
4016
39706
85111
33972
9870
16443
33222
98475
20912
7...

result:

wrong answer 2nd words differ - expected: '133099', found: '132097'

Subtask #3:

score: 0
Wrong Answer

Test #15:

score: 0
Wrong Answer
time: 0ms
memory: 16020kb

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:

1322
2696
1781
2611
4759
4995
3027
1327
724
5187
7811
4971
935
943
808
7270
7663
5325
3901
3225
4579
1577
2519
,
925
300
4351
3975
3813
5867
4723
1658
437
1416
3623
5257
579
119
2044
7793
9112
2047
3191
3765
2953
420
741
4105
183
3231
1443
1637
7291
3075
5954
8271
1579
3807
369
4577
3300
1783
835
24...

result:

wrong answer 3rd words differ - expected: '1743', found: '1781'

Subtask #4:

score: 0
Wrong Answer

Test #23:

score: 0
Wrong Answer
time: 473ms
memory: 183632kb

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:

1270943
308608
764373
79452
160350
580121
993789
1725755
1352645
215185
615960
546263
1393711
322515
1094276
52579
277679
228859
2491
147750
144806
670861
25567
223778
184324
1453329
1675689
547639
147243
974765
1112709
237554
817297
112491
85289
1195395
318457
722015
170481
562993
767792
414545
732...

result:

wrong answer 1st words differ - expected: '1263815', found: '1270943'