Submission #957076

# Submission time Handle Problem Language Result Execution time Memory
957076 2024-04-02T23:06:38 Z Mher777 Sequence (APIO23_sequence) C++17
13 / 100
905 ms 47572 KB
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <array>
#include <string>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <iterator>
#include <numeric>
#include <complex>
#include <utility>
#include <random>
#include <cassert>
#include <fstream>
#include "sequence.h"
using namespace std;
mt19937 rnd(7069);
typedef int itn;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef float fl;
typedef long double ld;
using vi = vector<int>;
using vll = vector<ll>;
using mii = map<int, int>;
using mll = map<ll, ll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define ff first
#define ss second
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define mpr make_pair
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define all(x) (x).begin(), (x).end()
const int MAX = int(2e9 + 5);
const ll MAXL = ll(1e18) + 5ll;

const int N = 500005;
int a[N], f[N], lazy_pref[N * 4], lazy_suf[N * 4];
pii id[N], t_pref[N * 4], t_suf[N * 4];
int n, m;

void add_fen(int ind, int val) {
    while (ind <= n) {
        f[ind] += val;
        ind += (ind & -ind);
    }
}

int sum_fen(int ind) {
    int ret = 0;
    while (ind) {
        ret += f[ind];
        ind -= (ind & -ind);
    }
    return ret;
}

int qry_fen(int l, int r) {
    return sum_fen(r) - sum_fen(l - 1);
}

void prop(int pos) {
    if (lazy_pref[pos]) {
        t_pref[2 * pos].ff += lazy_pref[pos];
        t_pref[2 * pos].ss += lazy_pref[pos];
        t_pref[2 * pos + 1].ff += lazy_pref[pos];
        t_pref[2 * pos + 1].ss += lazy_pref[pos];
        lazy_pref[2 * pos] += lazy_pref[pos];
        lazy_pref[2 * pos + 1] += lazy_pref[pos];
        lazy_pref[pos] = 0;
    }
    if (lazy_suf[pos]) {
        t_suf[2 * pos].ff += lazy_suf[pos];
        t_suf[2 * pos].ss += lazy_suf[pos];
        t_suf[2 * pos + 1].ff += lazy_suf[pos];
        t_suf[2 * pos + 1].ss += lazy_suf[pos];
        lazy_suf[2 * pos] += lazy_suf[pos];
        lazy_suf[2 * pos + 1] += lazy_suf[pos];
        lazy_suf[pos] = 0;
    }
}

int qry_min(int l, int r, int type, int tl = 1, int tr = n, int pos = 1) {
    if (l == tl && r == tr) {
        return (type == 1 ? t_pref[pos].ff : t_suf[pos].ff);
    }
    prop(pos);
    int mid = (tl + tr) / 2;
    if (mid >= r) {
        return qry_min(l, r, type, tl, mid, 2 * pos);
    }
    if (mid < l) {
        return qry_min(l, r, type, mid + 1, tr, 2 * pos + 1);
    }
    return min(qry_min(l, mid, type, tl, mid, 2 * pos), qry_min(mid + 1, r, type, mid + 1, tr, 2 * pos + 1));
}

int qry_max(int l, int r, int type, int tl = 1, int tr = n, int pos = 1) {
    if (l == tl && r == tr) {
        return (type == 1 ? t_pref[pos].ss : t_suf[pos].ss);
    }
    prop(pos);
    int mid = (tl + tr) / 2;
    if (mid >= r) {
        return qry_max(l, r, type, tl, mid, 2 * pos);
    }
    if (mid < l) {
        return qry_max(l, r, type, mid + 1, tr, 2 * pos + 1);
    }
    return max(qry_max(l, mid, type, tl, mid, 2 * pos), qry_max(mid + 1, r, type, mid + 1, tr, 2 * pos + 1));
}

void upd(int l, int r, int val, int type, int tl = 1, int tr = n, int pos = 1) {
    if (l == tl && r == tr) {
        if (type == 1) {
            t_pref[pos].ff += val;
            t_pref[pos].ss += val;
            lazy_pref[pos] += val;
        }
        else {
            t_suf[pos].ff += val;
            t_suf[pos].ss += val;
            lazy_suf[pos] += val;
        }
        return;
    }
    prop(pos);
    int mid = (tl + tr) / 2;
    if (mid >= r) {
        upd(l, r, val, type, tl, mid, 2 * pos);
    }
    else if (mid < l) {
        upd(l, r, val, type, mid + 1, tr, 2 * pos + 1);
    }
    else {
        upd(l, mid, val, type, tl, mid, 2 * pos);
        upd(mid + 1, r, val, type, mid + 1, tr, 2 * pos + 1);
    }
    t_pref[pos].ff = min(t_pref[2 * pos].ff, t_pref[2 * pos + 1].ff);
    t_suf[pos].ff = min(t_suf[2 * pos].ff, t_suf[2 * pos + 1].ff);
    t_pref[pos].ss = max(t_pref[2 * pos].ss, t_pref[2 * pos + 1].ss);
    t_suf[pos].ss = max(t_suf[2 * pos].ss, t_suf[2 * pos + 1].ss);
}

int sequence(int N, std::vector<int> A) {
    n = N;
    for (int i = 1; i <= n; ++i) {
        a[i] = A[i - 1];
        if (!id[a[i]].ff) id[a[i]].ff = i;
        id[a[i]].ss = i;
        add_fen(i, 1);
        upd(i, n, 1, 1);
        upd(1, i, 1, 2);
    }
    for (int i = 1; i <= n; ++i) {
        int l = id[i].ff, r = id[i].ss;
        if (l) {
            add_fen(l, -1);
            upd(l, n, -1, 1);
            upd(1, l, -1, 2);
            if (l != r) {
                add_fen(r, -1);
                upd(r, n, -1, 1);
                upd(1, r, -1, 2);
            }
        }
        if (l && l != r) {
            int g = qry_fen(l, r);
            if (abs(g) <= 2) {
                return 2;
            }
            if (g > 2) {
                int add = min(0, qry_min(r, n, 1) - qry_fen(1, r)) + min(0, qry_min(1, l, 2) - qry_fen(l, n));
                if (g + add <= 2) return 2;
            }
            else {
                int add = max(0, qry_max(r, n, 1) - qry_fen(1, r)) + max(0, qry_max(1, l, 2) - qry_fen(l, n));
                if (g + add >= -1) return 2;
            }
        }
        if (l) {
            add_fen(l, -1);
            upd(l, n, -1, 1);
            upd(1, l, -1, 2);
            if (l != r) {
                add_fen(r, -1);
                upd(r, n, -1, 1);
                upd(1, r, -1, 2);
            }
        }
    }
    return 1;
}

/*
14
2 6 2 5 3 4 2 1 4 3 5 6 3 2
*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Incorrect 3 ms 8540 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Incorrect 3 ms 8540 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Incorrect 294 ms 44272 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 8540 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 839 ms 44428 KB Output is correct
2 Correct 905 ms 44556 KB Output is correct
3 Correct 363 ms 44044 KB Output is correct
4 Correct 335 ms 47572 KB Output is correct
5 Correct 535 ms 46660 KB Output is correct
6 Correct 795 ms 46416 KB Output is correct
7 Correct 745 ms 46396 KB Output is correct
8 Correct 570 ms 46092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Incorrect 3 ms 8540 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Incorrect 3 ms 8540 KB Output isn't correct
3 Halted 0 ms 0 KB -