結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー RhoRho
提出日時 2020-04-03 02:11:36
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,312 ms / 3,500 ms
コード長 3,673 bytes
コンパイル時間 2,245 ms
コンパイル使用メモリ 181,008 KB
実行使用メモリ 17,828 KB
最終ジャッジ日時 2024-04-14 06:06:18
合計ジャッジ時間 23,680 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 7 ms
6,944 KB
testcase_04 AC 7 ms
6,940 KB
testcase_05 AC 7 ms
6,940 KB
testcase_06 AC 6 ms
6,940 KB
testcase_07 AC 7 ms
6,944 KB
testcase_08 AC 7 ms
6,940 KB
testcase_09 AC 7 ms
6,940 KB
testcase_10 AC 7 ms
6,940 KB
testcase_11 AC 208 ms
16,100 KB
testcase_12 AC 207 ms
16,064 KB
testcase_13 AC 268 ms
17,684 KB
testcase_14 AC 223 ms
16,500 KB
testcase_15 AC 244 ms
17,152 KB
testcase_16 AC 206 ms
15,768 KB
testcase_17 AC 248 ms
17,304 KB
testcase_18 AC 234 ms
16,868 KB
testcase_19 AC 251 ms
17,220 KB
testcase_20 AC 268 ms
17,696 KB
testcase_21 AC 238 ms
16,880 KB
testcase_22 AC 206 ms
15,892 KB
testcase_23 AC 214 ms
16,148 KB
testcase_24 AC 249 ms
17,360 KB
testcase_25 AC 262 ms
17,528 KB
testcase_26 AC 240 ms
16,936 KB
testcase_27 AC 219 ms
16,520 KB
testcase_28 AC 270 ms
17,700 KB
testcase_29 AC 266 ms
17,696 KB
testcase_30 AC 272 ms
17,704 KB
testcase_31 AC 265 ms
17,704 KB
testcase_32 AC 266 ms
17,704 KB
testcase_33 AC 269 ms
17,828 KB
testcase_34 AC 2,312 ms
17,828 KB
testcase_35 AC 458 ms
17,828 KB
testcase_36 AC 436 ms
17,824 KB
testcase_37 AC 447 ms
17,700 KB
testcase_38 AC 430 ms
17,516 KB
testcase_39 AC 361 ms
17,268 KB
testcase_40 AC 375 ms
17,472 KB
testcase_41 AC 429 ms
17,504 KB
testcase_42 AC 416 ms
17,480 KB
testcase_43 AC 273 ms
17,660 KB
testcase_44 AC 268 ms
17,608 KB
testcase_45 AC 260 ms
17,352 KB
testcase_46 AC 674 ms
17,472 KB
testcase_47 AC 613 ms
17,528 KB
testcase_48 AC 858 ms
17,700 KB
testcase_49 AC 588 ms
17,692 KB
testcase_50 AC 620 ms
17,504 KB
testcase_51 AC 1,304 ms
17,700 KB
testcase_52 AC 1,315 ms
17,700 KB
testcase_53 AC 1,304 ms
17,696 KB
testcase_54 AC 2 ms
6,940 KB
testcase_55 AC 2 ms
6,940 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>node, node2;
	void init(int N) {
		n = 1;
		while (n < N)n *= 2;
		node.resize(2 * n, inf);
		node2.resize(2 * n, -inf);
	}

	void update(int x, int a) {
		x += n - 1;
		node[x] = a;
		node2[x] = a;
		while (x > 0) {
			x = (x - 1) / 2;
			node[x] = min(node[x * 2 + 1], node[x * 2 + 2]);
			node2[x] = max(node2[x * 2 + 1], node2[x * 2 + 2]);
		}
	}
	int query(int a, int b, int k, int l, int r) {
		if (r <= a || b <= l)return inf;
		if (a <= l&&r <= b)return node[k];
		int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
		int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
		return min(vl, vr);
	}
	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 getmin(int a, int b) {
		return query(a, b, 0, 0, n);
	}
	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;

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;
}

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 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);
	seg.init(n);
	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