Submission #1065148

# Submission time Handle Problem Language Result Execution time Memory
1065148 2024-08-19T01:18:57 Z oscar1f Railway Trip (JOI17_railway_trip) C++17
45 / 100
2000 ms 17212 KB
#include<bits/stdc++.h>
using namespace std;

//#define int long long

const int TAILLE_MAX=100*1000+5,INFINI=1000*1000*1000,DECA=(1<<17);
int nbVal,typeMaxi,nbReq;
int val[TAILLE_MAX];
vector<int> posVal[TAILLE_MAX];
set<int> plusGrand;
set<int>::iterator it;
int supVois[TAILLE_MAX][2];
int distVois[TAILLE_MAX][2];
int numEgau[TAILLE_MAX];
int arbreSom[2*DECA];
int voisGau[TAILLE_MAX][2],voisDro[TAILLE_MAX][2];
int distGau[TAILLE_MAX][2],distDro[TAILLE_MAX][2];

void ajout(int pos) {
    pos+=DECA;
    while (pos>0) {
        arbreSom[pos]++;
        pos/=2;
    }
}

int calcInter(int deb,int fin) {
    if (deb==fin) {
        return arbreSom[deb];
    }
    if (deb%2==1) {
        return arbreSom[deb]+calcInter(deb+1,fin);
    }
    if (fin%2==0) {
        return arbreSom[fin]+calcInter(deb,fin-1);
    }
    return calcInter(deb/2,fin/2);
}

void calcSup(int deb,int tab) {
    distGau[1][tab]=0;
    distDro[1][tab]=0;
    voisGau[1][tab]=deb;
    voisDro[1][tab]=deb;
    int pos;
    for (int i=2;i<=typeMaxi;i++) {
        pos=voisGau[i-1][tab];
        if (val[pos]<i) {
            voisGau[i][tab]=supVois[pos][0];
            distGau[i][tab]=distGau[i-1][tab]+distVois[pos][0];
        }
        else {
            voisGau[i][tab]=pos;
            distGau[i][tab]=distGau[i-1][tab];
        }
        pos=voisDro[i-1][tab];
        if (val[pos]<i) {
            voisDro[i][tab]=supVois[pos][1];
            distDro[i][tab]=distDro[i-1][tab]+distVois[pos][1];
        }
        else {
            voisDro[i][tab]=pos;
            distDro[i][tab]=distDro[i-1][tab];
        }
        distGau[i][tab]=min(distGau[i][tab],distDro[i][tab]+1);
        distDro[i][tab]=min(distDro[i][tab],distGau[i][tab]+1);
    }
    /*for (int i=1;i<=typeMaxi;i++) {
        cout<<i<<"   "<<voisGau[i][tab]<<" "<<distGau[i][tab]<<"    "<<voisDro[i][tab]<<" "<<distDro[i][tab]<<endl;
    }
    cout<<endl;*/
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>nbVal>>typeMaxi>>nbReq;
    for (int i=1;i<=nbVal;i++) {
        cin>>val[i];
        numEgau[i]=posVal[val[i]].size();
        posVal[val[i]].push_back(i);
    }
    plusGrand.insert(-INFINI);
    plusGrand.insert(INFINI);
    for (int i=typeMaxi;i>=1;i--) {
        for (int j:posVal[i]) {
            it=plusGrand.lower_bound(j);
            if (abs((*it))!=INFINI) {
                supVois[j][1]=(*it);
            }
            it--;
            if (abs((*it))!=INFINI) {
                supVois[j][0]=(*it);
            }
        }
        for (int j:posVal[i]) {
            plusGrand.insert(j);
            ajout(j);
        }
        for (int j:posVal[i]) {
            if (supVois[j][0]!=0) {
                distVois[j][0]=calcInter(DECA+1,DECA+j)-calcInter(DECA+1,DECA+supVois[j][0]);
            }
            if (supVois[j][1]!=0) {
                distVois[j][1]=calcInter(DECA+1,DECA+supVois[j][1])-calcInter(DECA+1,DECA+j);
            }
        }
    }
    int deb,fin,rep,hautMax;
    for (int ireq=0;ireq<nbReq;ireq++) {
        cin>>deb>>fin;
        if (deb>fin) {
            swap(deb,fin);
        }
        calcSup(deb,0);
        calcSup(fin,1);
        hautMax=1;
        while (hautMax<=typeMaxi and voisDro[hautMax][0]<=voisGau[hautMax][1]) {
            hautMax++;
        }
        if (hautMax<=typeMaxi) {
            rep=min(distGau[hautMax][0]+distGau[hautMax][1],distDro[hautMax][0]+distDro[hautMax][1]);
        }
        else {
            rep=INFINI;
        }
        hautMax--;
        //cout<<"!"<<hautMax<<endl;
        rep=min(rep,distDro[hautMax][0]+distGau[hautMax][1]+numEgau[voisGau[hautMax][1]]-numEgau[voisDro[hautMax][0]]);
        cout<<rep-1<<"\n";
    }
    //cout<<endl;
    return 0;
}
 
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8540 KB Output is correct
6 Correct 1 ms 8540 KB Output is correct
7 Correct 1 ms 8536 KB Output is correct
8 Correct 1 ms 8536 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8796 KB Output is correct
2 Correct 50 ms 14476 KB Output is correct
3 Correct 55 ms 14416 KB Output is correct
4 Correct 64 ms 14348 KB Output is correct
5 Correct 70 ms 14364 KB Output is correct
6 Correct 76 ms 14672 KB Output is correct
7 Correct 137 ms 16468 KB Output is correct
8 Correct 91 ms 14544 KB Output is correct
9 Correct 75 ms 15444 KB Output is correct
10 Correct 54 ms 15052 KB Output is correct
11 Correct 89 ms 15700 KB Output is correct
12 Correct 68 ms 15496 KB Output is correct
13 Correct 66 ms 15120 KB Output is correct
14 Correct 89 ms 15696 KB Output is correct
15 Correct 95 ms 15440 KB Output is correct
16 Correct 67 ms 15140 KB Output is correct
17 Correct 90 ms 14840 KB Output is correct
18 Correct 80 ms 14932 KB Output is correct
19 Correct 84 ms 17212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 83 ms 14672 KB Output is correct
2 Correct 78 ms 14676 KB Output is correct
3 Correct 86 ms 15916 KB Output is correct
4 Correct 91 ms 16048 KB Output is correct
5 Correct 102 ms 15948 KB Output is correct
6 Correct 94 ms 16200 KB Output is correct
7 Correct 92 ms 16064 KB Output is correct
8 Correct 53 ms 15992 KB Output is correct
9 Correct 71 ms 16084 KB Output is correct
10 Correct 72 ms 16084 KB Output is correct
11 Correct 71 ms 16080 KB Output is correct
12 Correct 74 ms 16084 KB Output is correct
13 Correct 75 ms 16144 KB Output is correct
14 Correct 73 ms 16080 KB Output is correct
15 Correct 94 ms 15912 KB Output is correct
16 Correct 86 ms 15812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 2063 ms 15924 KB Time limit exceeded
2 Halted 0 ms 0 KB -