結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー RhoRho
提出日時 2020-04-04 03:44:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,253 ms / 3,500 ms
コード長 3,250 bytes
コンパイル時間 2,209 ms
コンパイル使用メモリ 179,568 KB
実行使用メモリ 15,780 KB
最終ジャッジ日時 2024-04-14 06:08:53
合計ジャッジ時間 21,944 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 7 ms
6,944 KB
testcase_04 AC 6 ms
6,940 KB
testcase_05 AC 7 ms
6,944 KB
testcase_06 AC 6 ms
6,944 KB
testcase_07 AC 6 ms
6,944 KB
testcase_08 AC 7 ms
6,940 KB
testcase_09 AC 7 ms
6,944 KB
testcase_10 AC 7 ms
6,940 KB
testcase_11 AC 190 ms
14,112 KB
testcase_12 AC 184 ms
14,136 KB
testcase_13 AC 236 ms
15,636 KB
testcase_14 AC 193 ms
14,320 KB
testcase_15 AC 216 ms
15,224 KB
testcase_16 AC 181 ms
13,844 KB
testcase_17 AC 220 ms
15,124 KB
testcase_18 AC 208 ms
14,816 KB
testcase_19 AC 222 ms
15,168 KB
testcase_20 AC 233 ms
15,544 KB
testcase_21 AC 208 ms
14,956 KB
testcase_22 AC 181 ms
13,840 KB
testcase_23 AC 189 ms
14,096 KB
testcase_24 AC 223 ms
15,180 KB
testcase_25 AC 232 ms
15,480 KB
testcase_26 AC 213 ms
14,880 KB
testcase_27 AC 198 ms
14,340 KB
testcase_28 AC 235 ms
15,652 KB
testcase_29 AC 236 ms
15,776 KB
testcase_30 AC 237 ms
15,780 KB
testcase_31 AC 242 ms
15,712 KB
testcase_32 AC 238 ms
15,648 KB
testcase_33 AC 235 ms
15,648 KB
testcase_34 AC 2,253 ms
15,648 KB
testcase_35 AC 430 ms
15,648 KB
testcase_36 AC 402 ms
15,776 KB
testcase_37 AC 422 ms
15,648 KB
testcase_38 AC 402 ms
15,464 KB
testcase_39 AC 336 ms
15,344 KB
testcase_40 AC 343 ms
15,424 KB
testcase_41 AC 399 ms
15,448 KB
testcase_42 AC 384 ms
15,432 KB
testcase_43 AC 247 ms
15,500 KB
testcase_44 AC 246 ms
15,428 KB
testcase_45 AC 238 ms
15,172 KB
testcase_46 AC 635 ms
15,544 KB
testcase_47 AC 578 ms
15,376 KB
testcase_48 AC 824 ms
15,700 KB
testcase_49 AC 554 ms
15,768 KB
testcase_50 AC 583 ms
15,456 KB
testcase_51 AC 1,264 ms
15,708 KB
testcase_52 AC 1,265 ms
15,776 KB
testcase_53 AC 1,267 ms
15,648 KB
testcase_54 AC 2 ms
6,944 KB
testcase_55 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#pragma warning(disable:4996)
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
typedef pair<int, int> P;
const long long mod = 1000000007;
const long long inf = 1ll << 61;

struct RMQ {
	int n; vector<int>node2;
	void init(int N) {
		n = 1;
		while (n < N)n *= 2;
		node2.resize(2 * n, -inf);
	}

	void update(int x, int a) {
		x += n - 1;
		node2[x] = a;
		while (x > 0) {
			x = (x - 1) / 2;
			node2[x] = max(node2[x * 2 + 1], node2[x * 2 + 2]);
		}
	}
	int query2(int a, int b, int k, int l, int r) {
		if (r <= a || b <= l)return -inf;
		if (a <= l&&r <= b)return node2[k];
		int vl = query2(a, b, k * 2 + 1, l, (l + r) / 2);
		int vr = query2(a, b, k * 2 + 2, (l + r) / 2, r);
		return max(vl, vr);
	}
	int getmax(int a, int b) {
		return query2(a, b, 0, 0, n);
	}
};

struct BIT {
	int bit[100006];
	int N;

	void init(int n) {
		N = n;
		for (int i = 0; i <= N; i++)bit[i] = 0;
	}
	int sum(int i) {
		int s = 0;
		while (i > 0) {
			s += bit[i];
			i -= i&-i;
		}
		return s;
	}
	void add(int i, int x) {
		while (i <= N) {
			bit[i] += x;
			i += i&-i;
		}
	}
};

int p[100005];
int n;
RMQ seg; BIT bi;
vector<P>LS, RS, v;

void input() {
	cin >> n; rep(i, n)cin >> p[i];
	seg.init(n);
	rep(i, n)seg.update(i, p[i]);
	bi.init(n + 1);
}
int sis(int l, int r, int base) {//初項がbaseのときのISを計算
	int res = 0, mn = base;
	for (int i = l; i <= r; i++) {
		if (mn <= p[i]) {
			mn = p[i];
			res++;
		}
	}
	return res;
}
int sqrtdc(vector<P>S) {
	int sqrtN = sqrt(n);
	vector<P>iss[400]; vector<int>maxnum;
	int K = (n + sqrtN - 1) / sqrtN;

	rep(i, K) {
		int l1 = i*sqrtN, r1 = min(l1 + sqrtN - 1, n - 1);
		for (int j = l1; j <= r1; j++) {
			iss[i].push_back(P(p[j], sis(l1, r1, p[j])));
		}
		iss[i].push_back(P(inf, 0));
		sort(iss[i].begin(), iss[i].end());
	}

	int ans = 0;

	rep(i, n) {
		if (S[i].first > S[i].second)continue;
		int L = S[i].first, R = S[i].second + 1;
		int mn = 0;
		for (int k = 0; k < K; k++) {
			int l = k*sqrtN, r = (k + 1)*sqrtN;
			if (r <= L || R <= l)continue;
			if (L <= l&&r <= R) {
				auto p2 = *lower_bound(iss[k].begin(), iss[k].end(), P(mn, -1));
				if (p2.first != inf) {
					ans += p2.second;
					mn = seg.getmax(l, r);
				}
			}
			else {
				for (int j = max(l, L); j < min(R, r); j++) {
					if (mn < p[j]) {
						ans++; mn = p[j];
					}
				}
			}
		}
	}
	return ans;
}

int honsitu() {
	//区間に分ける
	rep(i, n)v.push_back(P(p[i], i));
	sort(v.begin(), v.end());
	rep(i, n) {
		int s = bi.sum(v[i].second);
		int lb = v[i].second, ub = n + 1;
		while (ub - lb > 1) {
			int mi = (ub + lb) / 2;
			if (bi.sum(mi) > s)ub = mi;
			else lb = mi;
		}
		RS.push_back(P(v[i].second + 1, lb - 1));
		bi.add(v[i].second + 1, 1);
	}

	int ans = sqrtdc(RS);

	reverse(p, p + n);
	bi.init(n + 1);
	rep(i, n)seg.update(i, p[i]);

	rep(i, n) {
		v[i].second = n - 1 - v[i].second;
		int s = bi.sum(v[i].second);
		int lb = v[i].second, ub = n + 1;
		while (ub - lb > 1) {
			int mi = (ub + lb) / 2;
			if (bi.sum(mi) > s)ub = mi;
			else lb = mi;
		}
		LS.push_back(P(v[i].second + 1, lb - 1));
		bi.add(v[i].second + 1, 1);
	}

	ans += sqrtdc(LS);
	return ans;
}


signed main() {
	input();
	cout << honsitu() << endl;
}
0