제출 #954702

#제출 시각아이디문제언어결과실행 시간메모리
954702Mher777서열 (APIO23_sequence)C++17
0 / 100
72 ms53208 KiB
#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], pref[N], suf[N], lazy_pref[N * 4], lazy_suf[N * 4], ran[N * 4];
pii id[N], t_pref[N * 4], t_suf[N * 4];
int n;

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) {
    t_pref[2 * pos].ff += lazy_pref[pos] * ran[2 * pos];
    t_pref[2 * pos + 1].ff += lazy_pref[pos] * ran[2 * pos + 1];
    t_pref[2 * pos].ss += lazy_pref[pos] * ran[2 * pos];
    t_pref[2 * pos + 1].ss += lazy_pref[pos] * ran[2 * pos + 1];
    lazy_pref[2 * pos] += lazy_pref[pos];
    lazy_pref[2 * pos + 1] += lazy_pref[pos];
    lazy_pref[pos] = 0;
    t_suf[2 * pos].ff += lazy_suf[pos] * ran[2 * pos];
    t_suf[2 * pos + 1].ff += lazy_suf[pos] * ran[2 * pos + 1];
    t_suf[2 * pos].ss += lazy_suf[pos] * ran[2 * pos];
    t_suf[2 * pos + 1].ss += lazy_suf[pos] * ran[2 * pos + 1];
    lazy_suf[2 * pos] += lazy_suf[pos];
    lazy_suf[2 * pos + 1] += lazy_suf[pos];
    lazy_suf[pos] = 0;
}

void bld(int l = 1, int r = n, int pos = 1) {
    ran[pos] = r - l + 1;
    if (l == r) {
        t_pref[pos].ff = t_pref[pos].ss = pref[l];
        t_suf[pos].ff = t_suf[pos].ss = suf[l];
        return;
    }
    int mid = (l + r) / 2;
    bld(l, mid, 2 * pos);
    bld(mid + 1, r, 2 * pos + 1);
    t_pref[pos].ff = min(t_pref[2 * pos].ff, t_pref[2 * pos + 1].ff);
    t_pref[pos].ss = max(t_pref[2 * pos].ss, t_pref[2 * pos + 1].ss);
    t_suf[pos].ff = min(t_suf[2 * pos].ff, t_suf[2 * pos + 1].ff);
    t_suf[pos].ss = max(t_suf[2 * pos].ss, t_suf[2 * pos + 1].ss);
}

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 add(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 * ran[pos];
            t_pref[pos].ss += val * ran[pos];
            lazy_pref[pos] += val;
        }
        else {
            t_suf[pos].ff += val * ran[pos];
            t_suf[pos].ss += val * ran[pos];
            lazy_suf[pos] += val;
        }
        return;
    }
    prop(pos);
    int mid = (tl + tr) / 2;
    if (mid >= r) {
        add(l, r, val, type, tl, mid, 2 * pos);
    }
    else if (mid < l) {
        add(l, r, val, type, mid + 1, tr, 2 * pos + 1);
    }
    else {
        add(l, mid, val, type, tl, mid, 2 * pos);
        add(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_pref[pos].ss = max(t_pref[2 * pos].ss, t_pref[2 * pos + 1].ss);
    t_suf[pos].ff = min(t_suf[2 * pos].ff, t_suf[2 * pos + 1].ff);
    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;
    int ans = 1;
    for (int i = 1; i <= n; ++i) {
        a[i] = A[i - 1];
        if (a[i] != 1) {
            add_fen(i, 1);
            pref[i] = suf[i] = 1;
        }
        pref[i] += pref[i - 1];
        if (!id[a[i]].ff) id[a[i]].ff = i;
        id[a[i]].ss = i;
    }
    for (int i = n; i >= 1; --i) {
        suf[i] += suf[i + 1];
    }
    bld();
    int l, r;
    for (int i = 1; i <= n; ++i) {
        l = id[i].ff, r = id[i].ss;
        if (i != 1 && l) {
            add_fen(l, -1);
            if (l != r) add_fen(r, -1);
            add(l, n, -1, 1);
            add(1, l, -1, 2);
            if (l != r) {
                add(r, n, -1, 1);
                add(1, r, -1, 2);
            }
        }
        if (r && l != r) {
            int g = qry_fen(l, r);
            if (g >= -2 && g <= 2) {
                ans = 2;
                break;
            }
            if (g < -2) {
                int mx1 = qry_max(1, l, 2) - qry_max(l, l, 2);
                int mx2 = qry_max(r, n, 1) - qry_max(r, r, 1);
                if (mx1 + mx2 + g >= -2) {
                    ans = 2;
                    break;
                }
            }
            if (g > 2) {
                int mn1 = qry_min(1, l, 2) - qry_min(l, l, 2);
                int mn2 = qry_min(r, n, 1) - qry_min(r, r, 1);
                if (mn1 + mn2 + g <= 2) {
                    ans = 2;
                    break;
                }
            }
        }
        if (l) {
            add_fen(l, -1);
            if (l != r) add_fen(r, -1);
            add(l, n, -1, 1);
            add(1, l, -1, 2);
            if (l != r) {
                add(r, n, -1, 1);
                add(1, r, -1, 2);
            }
        }
    }
    return ans;
}

/*
6
1 2 3 1 2 3
3

9
1 1 2 3 4 3 2 1 1
2

14
2 6 2 5 3 4 2 1 4 3 5 6 3 2
3
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...