結果

問題 No.284 門松と魔法(2)
ユーザー yosupotyosupot
提出日時 2015-09-23 12:40:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 568 ms / 5,000 ms
コード長 3,193 bytes
コンパイル時間 1,177 ms
コンパイル使用メモリ 125,700 KB
実行使用メモリ 22,648 KB
最終ジャッジ日時 2023-08-25 02:18:25
合計ジャッジ時間 6,209 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
21,804 KB
testcase_01 AC 8 ms
21,812 KB
testcase_02 AC 8 ms
21,804 KB
testcase_03 AC 8 ms
21,872 KB
testcase_04 AC 7 ms
21,864 KB
testcase_05 AC 8 ms
21,872 KB
testcase_06 AC 7 ms
21,736 KB
testcase_07 AC 7 ms
21,940 KB
testcase_08 AC 7 ms
21,928 KB
testcase_09 AC 7 ms
21,888 KB
testcase_10 AC 8 ms
21,924 KB
testcase_11 AC 8 ms
21,808 KB
testcase_12 AC 8 ms
21,876 KB
testcase_13 AC 7 ms
21,896 KB
testcase_14 AC 7 ms
21,804 KB
testcase_15 AC 8 ms
21,788 KB
testcase_16 AC 8 ms
21,828 KB
testcase_17 AC 7 ms
21,808 KB
testcase_18 AC 8 ms
21,752 KB
testcase_19 AC 8 ms
22,056 KB
testcase_20 AC 8 ms
21,880 KB
testcase_21 AC 8 ms
21,784 KB
testcase_22 AC 8 ms
21,788 KB
testcase_23 AC 33 ms
21,812 KB
testcase_24 AC 339 ms
22,384 KB
testcase_25 AC 124 ms
22,312 KB
testcase_26 AC 8 ms
21,844 KB
testcase_27 AC 7 ms
21,808 KB
testcase_28 AC 8 ms
22,068 KB
testcase_29 AC 521 ms
22,440 KB
testcase_30 AC 329 ms
22,564 KB
testcase_31 AC 123 ms
22,584 KB
testcase_32 AC 568 ms
22,512 KB
testcase_33 AC 189 ms
22,648 KB
testcase_34 AC 171 ms
22,448 KB
testcase_35 AC 8 ms
21,840 KB
testcase_36 AC 7 ms
21,780 KB
testcase_37 AC 135 ms
22,648 KB
testcase_38 AC 104 ms
22,340 KB
testcase_39 AC 9 ms
21,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <vector>
#include <valarray>
#include <array>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <complex>
#include <random>

using namespace std;
typedef long long ll;


/**
 * 遅延評価で書かれているStarry Sky Tree
 */
template <int S>
struct StarrySkyTree {
    typedef pair<int, int> D;
    static const int N = 1<<S;
    D seg[2*N][2], lz[2*N][2];
    int sz[2*N];
    StarrySkyTree() {
        for (int i = 2*N-1; i >= N; i--) {
            sz[i] = 1;
        }
        for (int i = N-1; i >= 1; i--) {
            sz[i] = sz[i*2]+sz[i*2+1];
        }
    }
    int base;
    void init(int b) {
    	base = b;
        for (int i = 1; i < 2*N; i++) {
        	seg[i][0] = seg[i][1] = D(0, base);
        }
    }

    void lzdata(int k, D x) {
    	if (x > seg[k][0]) {
    		swap(x, seg[k][0]);
    	}
    	if (x.second == seg[k][0].second) return;
    	if (x > seg[k][1]) {
    		swap(x, seg[k][1]);
    	}
    }
    void push(int k) {
        assert(1 <= k && k < N);
        if (seg[k][0].first >= 1) {
            lzdata(k*2, seg[k][0]);
            lzdata(k*2+1, seg[k][0]);
            seg[k][0] = D(0, base);
        }
        if (seg[k][1].first >= 1) {
            lzdata(k*2, seg[k][1]);
            lzdata(k*2+1, seg[k][1]);
            seg[k][1] = D(0, base);
        }
    }
    void update(int k) {
        assert(1 <= k && k < N);
    }

    pair<D, D> get(int k) {
        k += N;
        for (int i = S; i > 0; i--) {
            push(k>>i);
        }
        return make_pair(seg[k][0], seg[k][1]);
    }

    void set(int a, int b, D x, int k = 1) {
        if (sz[k] <= a || b <= 0) return;
        if (a <= 0 && sz[k] <= b) {
            lzdata(k, x);
            return;
        }
        push(k);
        set(a, b, x, k*2);
        set(a-sz[k]/2, b-sz[k]/2, x, k*2+1);
        update(k);
    }
};

const int MN = 100100;
int a[MN];

StarrySkyTree<17> st1, st2;

int main() {
	int n;
	cin >> n;
	vector<int> v;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
		v.push_back(a[i]);
	}
	sort(v.begin(), v.end());
	v.erase(unique(v.begin(), v.end()), v.end());
	for (int i = 0; i < n; i++) {
		a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
	}
	st1.init(-1); st2.init(n);
	int res = 0;
	for (int i = 0; i < n; i++) {
		//up
		auto x = st1.get(a[i]);
		{	
			auto y = x.first;
			y.first++;
			res = max(res, y.first);
			auto u = y.second; y.second = a[i];
			st2.set(0, u, y);
			st2.set(u+1, a[i], y);
		}
		{	
			auto y = x.second;
			y.first++;
			res = max(res, y.first);
			auto u = y.second; y.second = a[i];
			st2.set(0, u, y);
			st2.set(u+1, a[i], y);
		}
		x = st2.get(a[i]);
		{	
			auto y = x.first;
			y.first++;
			res = max(res, y.first);
			auto u = y.second; y.second = a[i];
			st1.set(a[i]+1, u, y);
			st1.set(u+1, n, y);
		}
		{	
			auto y = x.second;
			y.first++;
			res = max(res, y.first);
			auto u = y.second; y.second = a[i];
			st1.set(a[i]+1, u, y);
			st1.set(u+1, n, y);
		}
	}
	if (res <= 2) res = 0;
	cout << res << endl;
    return 0;
}
0