Submission #870163

# Submission time Handle Problem Language Result Execution time Memory
870163 2023-11-07T06:42:52 Z salmon Sequence (APIO23_sequence) C++17
20 / 100
1752 ms 50116 KB
#include "sequence.h"

#include <bits/stdc++.h>
using namespace std;
int N;
int miin[500100 * 4];
int maax[500100 * 4];
int lazy[500100 * 4];
vector<int> in[500100];
vector<pair<int,int>> sat;

void build(int i, int s, int e){
    miin[i] = - e - 1;
    maax[i] = - s - 1;
    lazy[i] = 0;

    if(s != e){
        int m = (s + e + 2)/2 - 1;

        build(i * 2, s, m);
        build(i * 2 + 1, m + 1, e);
    }
}

void update(int i, int s, int e, int S){
    if(S <= s){
        miin[i] += 2;
        maax[i] += 2;
        lazy[i] += 2;
        return;
    }

    int m = (s + e + 2)/2 - 1;

    update(i * 2 + 1, m + 1, e, S);
    if(S <= m){
        update(i * 2, s, m, S);
    }

    miin[i] = min(miin[i * 2], miin[i * 2 + 1]) + lazy[i];
    maax[i] = max(maax[i * 2], maax[i * 2 + 1]) + lazy[i];
}

pair<int,int> query(int i, int s, int e, int S, int E){
    if(S <= s && e <= E){
        return make_pair(miin[i],maax[i]);
    }

    int m = (s + e + 2)/2 - 1;

    pair<int,int> ii,ii1;

    if(S <= m && m < E){
        ii = query(i * 2, s, m, S, E);
        ii1 = query(i * 2 + 1, m + 1, e, S, E);

        ii.first = min(ii.first, ii1.first) + lazy[i];
        ii.second = max(ii.second, ii1.second) + lazy[i];
    }
    else if(S <= m){
        ii = query(i * 2, s, m, S, E);

        ii.first = ii.first + lazy[i];
        ii.second = ii.second + lazy[i];
    }
    else{
        ii = query(i * 2 + 1, m + 1, e, S, E);

        ii.first = ii.first + lazy[i];
        ii.second = ii.second + lazy[i];
    }

    return ii;
}

int sequence(int N, std::vector<int> lst) {
    for(int i = 0; i < N; i++){
        in[lst[i]].push_back(i);
    }

    build(1,-1,N-1);

    int ans = 0;

    for(int i = 1; i <= N; i++){
        sat.clear();

        for(int j = 0; j < in[i].size() ; j++){
            sat.push_back(make_pair(in[i][j],j));

            update(1,-1,N-1,in[i][j]);
        }
        sat.push_back(make_pair(N + 5, N + 5));

        for(int j = 0; j < in[i].size() ; j++){
            pair<int,int> ii = query(1,-1,N - 1,-1,in[i][j] - 1);

            int s = in[i][j];
            int e = N - 1;

            ii.second = ii.second + in[i].size() - j;

            pair<int,int> ii1 = query(1, -1, N - 1, s, e);

            //printf("\n%d %d\n",ii.first,ii.second);
            //printf("%d %d\n",ii1.first,ii1.second);

            if( (ii1.second < ii.first) || (ii.second < ii1.first)){
                continue;
            }

            while(s != e){
                int m = (s + e)/2;

                //printf("s %d %d\n",m+1,e);

                ii1 = query(1, -1, N - 1, m + 1, e);

                //printf("%d %d\n",ii1.first,ii1.second);

                if( (ii1.second < ii.first) || (ii.second < ii1.first)){
                    e = m;
                }
                else{
                    s = m + 1;
                }
            }

            auto it = lower_bound(sat.begin(),sat.end(),make_pair(s,1100100100));
            it--;

            //printf("%d\n",(it -> second) - j  + 1);
            ans = max(ans,(it -> second) - j  + 1);
        }
    }

    return ans;

}

Compilation message

sequence.cpp: In function 'int sequence(int, std::vector<int>)':
sequence.cpp:88:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   88 |         for(int j = 0; j < in[i].size() ; j++){
      |                        ~~^~~~~~~~~~~~~~
sequence.cpp:95:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |         for(int j = 0; j < in[i].size() ; j++){
      |                        ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 4 ms 16988 KB Output is correct
3 Correct 3 ms 17024 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Incorrect 3 ms 16988 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 4 ms 16988 KB Output is correct
3 Correct 3 ms 17024 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Incorrect 3 ms 16988 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 1235 ms 44964 KB Output is correct
3 Correct 1335 ms 45140 KB Output is correct
4 Correct 1232 ms 37216 KB Output is correct
5 Correct 1309 ms 45460 KB Output is correct
6 Correct 1267 ms 45464 KB Output is correct
7 Correct 1245 ms 37252 KB Output is correct
8 Correct 1299 ms 37204 KB Output is correct
9 Correct 1262 ms 38380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 16988 KB Output is correct
2 Correct 1280 ms 42236 KB Output is correct
3 Correct 1420 ms 37180 KB Output is correct
4 Correct 1415 ms 37180 KB Output is correct
5 Incorrect 1571 ms 41984 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1752 ms 48900 KB Output is correct
2 Correct 1680 ms 48976 KB Output is correct
3 Correct 1698 ms 48328 KB Output is correct
4 Correct 1752 ms 50116 KB Output is correct
5 Correct 1546 ms 46780 KB Output is correct
6 Correct 1560 ms 46856 KB Output is correct
7 Correct 1358 ms 47028 KB Output is correct
8 Correct 1328 ms 46724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 4 ms 16988 KB Output is correct
3 Correct 3 ms 17024 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Incorrect 3 ms 16988 KB Output isn't correct
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 16988 KB Output is correct
2 Correct 4 ms 16988 KB Output is correct
3 Correct 3 ms 17024 KB Output is correct
4 Correct 3 ms 16988 KB Output is correct
5 Incorrect 3 ms 16988 KB Output isn't correct
6 Halted 0 ms 0 KB -