답안 #818066

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
818066 2023-08-10T01:15:33 Z oscar1f 식물 비교 (IOI20_plants) C++17
100 / 100
770 ms 60728 KB
#include<bits/stdc++.h>
#include "plants.h"

using namespace std;

struct noeud{
    int deb,fin,somme=0;
    pair<int,int> maxi;
};

const int MAX_VAL=200*1000+5,INFINI=1000*1000*1000,DECA=(1<<18),MAX_LOG=20;
int nbVal,nbSuiv,valCour,nouv,idNouv;
vector<int> val,ordre;
int rep[MAX_VAL];
noeud lazy[2*DECA];
pair<int,int> arbreMax[2*DECA];
int fils[MAX_VAL][2][MAX_LOG];

void afficher() {
    for (int i=1;i<8;i++) {
        cout<<i<<" : "<<lazy[i].deb<<" "<<lazy[i].fin<<" "<<lazy[i].somme<<" "<<lazy[i].maxi.first<<" "<<lazy[i].maxi.second<<endl;
    }
    cout<<endl;
}

void creer(int pos,int debInter,int finInter) {
    lazy[pos].deb=debInter;
    lazy[pos].fin=finInter;
    if (debInter==finInter) {
        lazy[pos].maxi={val[debInter],debInter};
    }
    else {
        int midInter=(debInter+finInter)/2;
        creer(2*pos,debInter,midInter);
        creer(2*pos+1,midInter+1,finInter);
        lazy[pos].maxi=max(lazy[2*pos].maxi,lazy[2*pos+1].maxi);
    }
}

void pushFlag(int pos) {
    lazy[pos].maxi.first+=lazy[pos].somme;
    if (lazy[pos].deb!=lazy[pos].fin) {
        lazy[2*pos].somme+=lazy[pos].somme;
        lazy[2*pos+1].somme+=lazy[pos].somme;
    }
    lazy[pos].somme=0;
}

int find(int pos,int debInter,int finInter) {
    pushFlag(pos);
    if (lazy[pos].deb>finInter or lazy[pos].fin<debInter) {
        return -1;
    }
    if (lazy[pos].deb>=debInter and lazy[pos].fin<=finInter) {
        if (lazy[pos].maxi.first!=nbSuiv-1) {
            return -1;
        }
        return lazy[pos].maxi.second;
    }
    return max(find(2*pos,debInter,finInter),find(2*pos+1,debInter,finInter));
}

void supp(int pos,int posSup) {
    pushFlag(pos);
    if (lazy[pos].deb>posSup or lazy[pos].fin<posSup) {
        return;
    }
    if (lazy[pos].deb==lazy[pos].fin) {
        lazy[pos].maxi.first=-INFINI;
    }
    else {
        supp(2*pos,posSup);
        supp(2*pos+1,posSup);
        lazy[pos].maxi=max(lazy[2*pos].maxi,lazy[2*pos+1].maxi);
    }
}

void ajout(int pos,int debInter,int finInter) {
    if (lazy[pos].deb>finInter or lazy[pos].fin<debInter) {
        pushFlag(pos);
        return;
    }
    if (lazy[pos].deb>=debInter and lazy[pos].fin<=finInter) {
        lazy[pos].somme++;
        pushFlag(pos);
        return;
    }
    pushFlag(pos);
    ajout(2*pos,debInter,finInter);
    ajout(2*pos+1,debInter,finInter);
    lazy[pos].maxi=max(lazy[2*pos].maxi,lazy[2*pos+1].maxi);
}

int verif(int pos) {
    if (pos>=nbSuiv-1) {
        return find(1,pos-nbSuiv+1,pos-1);
    }
    else {
        if (pos!=0) {
            return max(find(1,0,pos-1),find(1,pos-nbSuiv+1+nbVal,nbVal-1));
        }
        return find(1,pos-nbSuiv+1+nbVal,nbVal-1);
    }
}

void extraire(int pos) {
    while (verif(pos)!=-1) {
        extraire(verif(pos));
    }
    idNouv++;
    rep[pos]=idNouv;
    ordre.push_back(pos);
    supp(1,pos);
    if (pos>=nbSuiv-1) {
        ajout(1,pos-nbSuiv+1,pos);
    }
    else {
        ajout(1,0,pos);
        ajout(1,pos-nbSuiv+1+nbVal,nbVal-1);
    }
    //afficher();
}

pair<int,int> calcMax(int debInter,int finInter) {
    if (debInter==finInter) {
        return arbreMax[debInter];
    }
    if (debInter%2==1) {
        return max(arbreMax[debInter],calcMax(debInter+1,finInter));
    }
    if (finInter%2==0) {
        return max(arbreMax[finInter],calcMax(debInter,finInter-1));
    }
    return calcMax(debInter/2,finInter/2);
}

void maj(int pos) {
    arbreMax[DECA+pos]={rep[pos],pos};
    pos+=DECA;
    while (pos>1) {
        pos/=2;
        arbreMax[pos]=max(arbreMax[2*pos],arbreMax[2*pos+1]);
    }
}

int dist(int a,int b,int tab) {
    if (a==b) {
        return 0;
    }
    if (tab==1) {
        if (b>a) {
            return b-a;
        }
        else {
            return nbVal-a+b;
        }
    }
    else {
        return nbVal-dist(a,b,1);
    }
}

bool estEntre(int a,int b,int c) {
    if (b==nbVal) {
        return false;
    }
    if (b<a) {
        b+=nbVal;
    }
    if (c<a) {
        c+=nbVal;
    }
    if (b>=a and c>=b) {
        return true;
    }
    return false;
}

void init(int k,vector<int> r) {
	nbSuiv=k;
	val=r;
	nbVal=val.size();
	creer(1,0,nbVal-1);
    //afficher();
    while (find(1,0,nbVal-1)!=-1) {
        extraire(find(1,0,nbVal-1));
    }
    pair<int,int> meil;
    for (int nouv:ordre) {
        if (nouv>=nbSuiv-1) {
            meil=calcMax(DECA+nouv-nbSuiv+1,DECA+nouv-1);
        }
        else {
            if (nouv>0) {
                meil=max(calcMax(DECA+0,DECA+nouv-1),calcMax(DECA+nouv-nbSuiv+1+nbVal,DECA+nbVal-1));
            }
            else {
                meil=calcMax(DECA+nouv-nbSuiv+1+nbVal,DECA+nbVal-1);
            }
        }
        if (meil.first>0) {
            fils[nouv][0][0]=meil.second;
        }
        else {
            fils[nouv][0][0]=nbVal;
        }
        if (nouv+nbSuiv-1<=nbVal-1) {
            meil=calcMax(DECA+nouv+1,DECA+nouv+nbSuiv-1);
        }
        else {
            if (nouv<nbVal-1) {
                meil=max(calcMax(DECA+nouv+1,DECA+nbVal-1),calcMax(DECA+0,DECA+nouv+nbSuiv-1-nbVal));
            }
            else {
                meil=calcMax(DECA+0,DECA+nouv+nbSuiv-1-nbVal);
            }
        }
        if (meil.first>0) {
            fils[nouv][1][0]=meil.second;
        }
        else {
            fils[nouv][1][0]=nbVal;
        }
        maj(nouv);
    }
    for (int j=0;j<=1;j++) {
        fils[nbVal][j][0]=nbVal;
        for (int k=1;k<MAX_LOG;k++) {
            for (int i=0;i<=nbVal;i++) {
                if (fils[i][j][k-1]==nbVal or fils[fils[i][j][k-1]][j][k-1]==nbVal or dist(i,fils[i][j][k-1],j)+dist(fils[i][j][k-1],fils[fils[i][j][k-1]][j][k-1],j)>=nbVal) {
                    fils[i][j][k]=nbVal;
                }
                else {
                    fils[i][j][k]=fils[fils[i][j][k-1]][j][k-1];
                }
            }
        }
    }
    /*for (int i=0;i<=nbVal;i++) {
        cout<<i<<" : ";
        for (int k=0;k<MAX_LOG;k++) {
            cout<<fils[i][0][k]<<" ";
        }
        cout<<"\t";
        for (int k=0;k<MAX_LOG;k++) {
            cout<<fils[i][1][k]<<" ";
        }
        cout<<endl;
    }
    for (int i=0;i<nbVal;i++) {
        cout<<i<<" : "<<fils[i][0][0]<<" "<<fils[i][1][0]<<endl;
    }
    for (int i=0;i<nbVal;i++) {
        cout<<rep[i]<<" ";
    }
    cout<<endl;*/
}

int compare_plants(int x, int y) {
    int pos;
    if (rep[x]>rep[y]) {
        pos=x;
        for (int k=MAX_LOG-1;k>=0;k--) {
            //cout<<pos<<" ";
            if (estEntre(pos,fils[pos][1][k],y)) {
                pos=fils[pos][1][k];
            }
        }
        //cout<<pos<<"\t";
        if (dist(pos,y,1)<nbSuiv and rep[pos]>=rep[y]) {
            return 1;
        }
        pos=x;
        for (int k=MAX_LOG-1;k>=0;k--) {
            if (estEntre(y,fils[pos][0][k],pos)) {
                pos=fils[pos][0][k];
            }
        }
        //cout<<pos<<endl;
        if (dist(pos,y,0)<nbSuiv and rep[pos]>=rep[y]) {
            return 1;
        }
        return 0;
    }
    else {
        pos=y;
        for (int k=MAX_LOG-1;k>=0;k--) {
            if (estEntre(x,fils[pos][0][k],pos)) {
                pos=fils[pos][0][k];
            }
        }
        //cout<<pos<<" ";
        if (dist(pos,x,0)<nbSuiv and rep[pos]>=rep[x]) {
            return -1;
        }
        pos=y;
        for (int k=MAX_LOG-1;k>=0;k--) {
            if (estEntre(pos,fils[pos][1][k],x)) {
                pos=fils[pos][1][k];
            }
        }
        //cout<<pos<<endl;
        if (dist(pos,x,1)<nbSuiv and rep[pos]>=rep[x]) {
            return -1;
        }
        return 0;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 10580 KB Output is correct
2 Correct 4 ms 10580 KB Output is correct
3 Correct 4 ms 10580 KB Output is correct
4 Correct 4 ms 10552 KB Output is correct
5 Correct 6 ms 10580 KB Output is correct
6 Correct 53 ms 13308 KB Output is correct
7 Correct 88 ms 17260 KB Output is correct
8 Correct 362 ms 51356 KB Output is correct
9 Correct 377 ms 51656 KB Output is correct
10 Correct 404 ms 51448 KB Output is correct
11 Correct 423 ms 52276 KB Output is correct
12 Correct 494 ms 56024 KB Output is correct
13 Correct 408 ms 54280 KB Output is correct
14 Correct 497 ms 60636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 10580 KB Output is correct
2 Correct 5 ms 10580 KB Output is correct
3 Correct 4 ms 10612 KB Output is correct
4 Correct 4 ms 10580 KB Output is correct
5 Correct 4 ms 10580 KB Output is correct
6 Correct 7 ms 10836 KB Output is correct
7 Correct 62 ms 14328 KB Output is correct
8 Correct 5 ms 10708 KB Output is correct
9 Correct 7 ms 10836 KB Output is correct
10 Correct 68 ms 14400 KB Output is correct
11 Correct 68 ms 14228 KB Output is correct
12 Correct 79 ms 14532 KB Output is correct
13 Correct 61 ms 14360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 10580 KB Output is correct
2 Correct 5 ms 10580 KB Output is correct
3 Correct 4 ms 10612 KB Output is correct
4 Correct 4 ms 10580 KB Output is correct
5 Correct 4 ms 10580 KB Output is correct
6 Correct 7 ms 10836 KB Output is correct
7 Correct 62 ms 14328 KB Output is correct
8 Correct 5 ms 10708 KB Output is correct
9 Correct 7 ms 10836 KB Output is correct
10 Correct 68 ms 14400 KB Output is correct
11 Correct 68 ms 14228 KB Output is correct
12 Correct 79 ms 14532 KB Output is correct
13 Correct 61 ms 14360 KB Output is correct
14 Correct 101 ms 17356 KB Output is correct
15 Correct 731 ms 51428 KB Output is correct
16 Correct 101 ms 17304 KB Output is correct
17 Correct 690 ms 51332 KB Output is correct
18 Correct 565 ms 51424 KB Output is correct
19 Correct 734 ms 54472 KB Output is correct
20 Correct 627 ms 51428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 10580 KB Output is correct
2 Correct 4 ms 10580 KB Output is correct
3 Correct 68 ms 13760 KB Output is correct
4 Correct 537 ms 52992 KB Output is correct
5 Correct 642 ms 54860 KB Output is correct
6 Correct 680 ms 54784 KB Output is correct
7 Correct 736 ms 54968 KB Output is correct
8 Correct 749 ms 55092 KB Output is correct
9 Correct 516 ms 54616 KB Output is correct
10 Correct 529 ms 55072 KB Output is correct
11 Correct 429 ms 54336 KB Output is correct
12 Correct 577 ms 60728 KB Output is correct
13 Correct 568 ms 54560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 10580 KB Output is correct
2 Correct 5 ms 10580 KB Output is correct
3 Correct 4 ms 10580 KB Output is correct
4 Correct 4 ms 10580 KB Output is correct
5 Correct 4 ms 10580 KB Output is correct
6 Correct 6 ms 10580 KB Output is correct
7 Correct 17 ms 11212 KB Output is correct
8 Correct 16 ms 11220 KB Output is correct
9 Correct 17 ms 11216 KB Output is correct
10 Correct 15 ms 11220 KB Output is correct
11 Correct 17 ms 11220 KB Output is correct
12 Correct 17 ms 11236 KB Output is correct
13 Correct 15 ms 11212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 10580 KB Output is correct
2 Correct 5 ms 10580 KB Output is correct
3 Correct 4 ms 10608 KB Output is correct
4 Correct 4 ms 10580 KB Output is correct
5 Correct 6 ms 10708 KB Output is correct
6 Correct 513 ms 51424 KB Output is correct
7 Correct 697 ms 51680 KB Output is correct
8 Correct 770 ms 51408 KB Output is correct
9 Correct 738 ms 51552 KB Output is correct
10 Correct 455 ms 51392 KB Output is correct
11 Correct 616 ms 51416 KB Output is correct
12 Correct 494 ms 55528 KB Output is correct
13 Correct 538 ms 53956 KB Output is correct
14 Correct 661 ms 53888 KB Output is correct
15 Correct 733 ms 54040 KB Output is correct
16 Correct 508 ms 54632 KB Output is correct
17 Correct 539 ms 53736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 10580 KB Output is correct
2 Correct 4 ms 10580 KB Output is correct
3 Correct 4 ms 10580 KB Output is correct
4 Correct 4 ms 10552 KB Output is correct
5 Correct 6 ms 10580 KB Output is correct
6 Correct 53 ms 13308 KB Output is correct
7 Correct 88 ms 17260 KB Output is correct
8 Correct 362 ms 51356 KB Output is correct
9 Correct 377 ms 51656 KB Output is correct
10 Correct 404 ms 51448 KB Output is correct
11 Correct 423 ms 52276 KB Output is correct
12 Correct 494 ms 56024 KB Output is correct
13 Correct 408 ms 54280 KB Output is correct
14 Correct 497 ms 60636 KB Output is correct
15 Correct 4 ms 10580 KB Output is correct
16 Correct 5 ms 10580 KB Output is correct
17 Correct 4 ms 10612 KB Output is correct
18 Correct 4 ms 10580 KB Output is correct
19 Correct 4 ms 10580 KB Output is correct
20 Correct 7 ms 10836 KB Output is correct
21 Correct 62 ms 14328 KB Output is correct
22 Correct 5 ms 10708 KB Output is correct
23 Correct 7 ms 10836 KB Output is correct
24 Correct 68 ms 14400 KB Output is correct
25 Correct 68 ms 14228 KB Output is correct
26 Correct 79 ms 14532 KB Output is correct
27 Correct 61 ms 14360 KB Output is correct
28 Correct 101 ms 17356 KB Output is correct
29 Correct 731 ms 51428 KB Output is correct
30 Correct 101 ms 17304 KB Output is correct
31 Correct 690 ms 51332 KB Output is correct
32 Correct 565 ms 51424 KB Output is correct
33 Correct 734 ms 54472 KB Output is correct
34 Correct 627 ms 51428 KB Output is correct
35 Correct 4 ms 10580 KB Output is correct
36 Correct 4 ms 10580 KB Output is correct
37 Correct 68 ms 13760 KB Output is correct
38 Correct 537 ms 52992 KB Output is correct
39 Correct 642 ms 54860 KB Output is correct
40 Correct 680 ms 54784 KB Output is correct
41 Correct 736 ms 54968 KB Output is correct
42 Correct 749 ms 55092 KB Output is correct
43 Correct 516 ms 54616 KB Output is correct
44 Correct 529 ms 55072 KB Output is correct
45 Correct 429 ms 54336 KB Output is correct
46 Correct 577 ms 60728 KB Output is correct
47 Correct 568 ms 54560 KB Output is correct
48 Correct 6 ms 10580 KB Output is correct
49 Correct 5 ms 10580 KB Output is correct
50 Correct 4 ms 10580 KB Output is correct
51 Correct 4 ms 10580 KB Output is correct
52 Correct 4 ms 10580 KB Output is correct
53 Correct 6 ms 10580 KB Output is correct
54 Correct 17 ms 11212 KB Output is correct
55 Correct 16 ms 11220 KB Output is correct
56 Correct 17 ms 11216 KB Output is correct
57 Correct 15 ms 11220 KB Output is correct
58 Correct 17 ms 11220 KB Output is correct
59 Correct 17 ms 11236 KB Output is correct
60 Correct 15 ms 11212 KB Output is correct
61 Correct 63 ms 15464 KB Output is correct
62 Correct 94 ms 19392 KB Output is correct
63 Correct 453 ms 54296 KB Output is correct
64 Correct 604 ms 54448 KB Output is correct
65 Correct 679 ms 54716 KB Output is correct
66 Correct 750 ms 54876 KB Output is correct
67 Correct 740 ms 55024 KB Output is correct
68 Correct 587 ms 54440 KB Output is correct
69 Correct 640 ms 54996 KB Output is correct
70 Correct 502 ms 55464 KB Output is correct
71 Correct 606 ms 54852 KB Output is correct
72 Correct 678 ms 54712 KB Output is correct
73 Correct 742 ms 54908 KB Output is correct
74 Correct 472 ms 54276 KB Output is correct
75 Correct 549 ms 54632 KB Output is correct