結果

問題 No.878 Range High-Element Query
ユーザー definedefine
提出日時 2019-09-08 21:23:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 328 ms / 2,000 ms
コード長 3,624 bytes
コンパイル時間 2,033 ms
コンパイル使用メモリ 179,684 KB
実行使用メモリ 16,028 KB
最終ジャッジ日時 2023-09-09 22:44:12
合計ジャッジ時間 5,300 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,588 KB
testcase_01 AC 5 ms
6,604 KB
testcase_02 AC 5 ms
6,916 KB
testcase_03 AC 3 ms
6,868 KB
testcase_04 AC 4 ms
6,640 KB
testcase_05 AC 5 ms
6,592 KB
testcase_06 AC 3 ms
6,704 KB
testcase_07 AC 3 ms
6,628 KB
testcase_08 AC 5 ms
6,812 KB
testcase_09 AC 5 ms
6,664 KB
testcase_10 AC 4 ms
6,748 KB
testcase_11 AC 305 ms
15,932 KB
testcase_12 AC 214 ms
14,600 KB
testcase_13 AC 250 ms
11,912 KB
testcase_14 AC 185 ms
11,484 KB
testcase_15 AC 221 ms
14,548 KB
testcase_16 AC 305 ms
15,608 KB
testcase_17 AC 323 ms
15,872 KB
testcase_18 AC 328 ms
16,028 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define int long long
#define mod (int)(1e9+7)
#define inf (int)(3e18)
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define PP pair<P,P>
#define all(v) v.begin(),v.end()
#define mkp make_pair
#define mkt make_tuple
#define prique(T) priority_queue<T,vector<T>,greater<T>>
#define vecunique(vec) sort(vec.begin(), vec.end());decltype(vec)::iterator result = std::unique(vec.begin(), vec.end());vec.erase(result, vec.end())
using namespace std;
template<class T> inline void chmax(T& a, T b) { a = max(a, b); }
template<class T> inline void chmin(T& a, T b) { a = min(a, b); }

bool prime(int x) {
	for (int i = 2; i * i <= x; i++) {
		if (x % i == 0)return false;
	}
	return x != 1;
}
int gcd(int x, int y) {
	if (y == 0)return x;
	return gcd(y, x % y);
}
int lcm(int x, int y) {
	return x / gcd(x, y) * y;
}
int kai(int x, int y) {
	int res = 1;
	for (int i = x - y + 1; i <= x; i++) {
		res *= i; res %= mod;
	}
	return res;
}
int mod_pow(int x, int y, int m) {
	int res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x % m;
		}
		x = x * x % m;
		y >>= 1;
	}
	return res;
}
int comb(int x, int y) {
	if (y > x)return 0;
	return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}
int get_rand(int MIN, int MAX) {
	random_device rnd;
	mt19937_64 mt64(rnd());
	uniform_int_distribution<int>engine(MIN, MAX);
	return engine(mt64);
}
/*--------Library Zone!--------*/



struct RangeMaxQuery {
	int size = 1;
	vector<int>dat;
	RangeMaxQuery(int x) {
		while (size < x)size *= 2;
		dat.resize(2 * size - 1, -1);
	}
	void update(int a, int b, int k = 0, int l = 0, int r = -1) {
		if (r == -1)r = size;
		if (r - l == 1) {
			dat[k] = b; return;
		}
		if (a < (l + r) / 2) {
			update(a, b, k * 2 + 1, l, (l + r) / 2);
		}
		else {
			update(a, b, k * 2 + 2, (l + r) / 2, r);
		}
		dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
	}
	int query(int a, int k = 0, int l = 0, int r = -1) {
		if (r == -1)r = size;
		if (r <= a)return -1;
		if (l >= a)return dat[k];
		int lval = query(a, k * 2 + 1, l, (l + r) / 2);
		int rval = query(a, k * 2 + 2, (l + r) / 2, r);
		return max(lval, rval);
	}
};
struct RangeUpdateQuery {
	int size = 1;
	vector<int>dat, lazy;
	RangeUpdateQuery(int x) {
		while (size < x)size *= 2;
		dat.resize(2 * size - 1, 0);
		lazy.resize(2 * size - 1, 0);
	}
	void eval(int k, int l, int r) {
		dat[k] += lazy[k];
		if (r - l > 1) {
			lazy[k * 2 + 1] += lazy[k];
			lazy[k * 2 + 2] += lazy[k];
		}
		lazy[k] = 0;
	}
	void update(int a, int b, int k = 0, int l = 0, int r = -1) {
		if (r == -1)r = size;
		eval(k, l, r);
		if (r <= a || b <= l)return;
		if (a <= l && r <= b) {
			lazy[k] ++;
			eval(k, l, r);
			return;
		}
		update(a, b, k * 2 + 1, l, (l + r) / 2);
		update(a, b, k * 2 + 2, (l + r) / 2, r);
		dat[k] = dat[k * 2 + 1] + dat[k * 2 + 2];
	}
	int query(int a, int k = 0, int l = 0, int r = -1) {
		if (r == -1)r = size;
		eval(k, l, r);
		if (r - l == 1)return dat[k];
		if (a < (l + r) / 2) {
			return query(a, k * 2 + 1, l, (l + r) / 2);
		}
		return query(a, k * 2 + 2, (l + r) / 2, r);
	}
};
int N, Q;
int A[114514];
vector<P>Qu[114514];
int ans[114514];
signed main() {
	cin >> N >> Q;
	RangeMaxQuery RMQ(N);
	RangeUpdateQuery RUQ(N);
	rep(i, N) {
		cin >> A[i]; A[i]--;
	}
	rep(i, Q) {
		int a, b; cin >> a >> a >> b; a--; b--;
		Qu[b].push_back({ a,i });
	}
	rep(i, N) {
		int b = RMQ.query(A[i]);
		RUQ.update(b + 1, i + 1);
		RMQ.update(A[i], i);
		for (P j : Qu[i]) {
			ans[j.second] = RUQ.query(j.first);
		}
	}
	rep(i, Q) {
		cout << ans[i] << endl;
	}
}
0