Back Junアルゴリズム14427:数列とクエリー15
20789 ワード
リンク
https://www.acmicpc.net/problem/14427
質問する
長さNの数列A 1、A 2、…、ANが与えられた.次のクエリを実行するプログラムを作成します.
数列のインデックスは1から始まります.
入力
最初の行は、数列のサイズNを与える.(1 ≤ N ≤ 100,000)
2行目はA 1,A 2,...,ANが与えられた.(1 ≤ Ai ≤ 109)
3行目には、クエリの個数Mが与えられる.(1 ≤ M ≤ 100,000)
4行目から、M行にクエリーが与えられます.
しゅつりょく
2回のクエリの場合、正解は行単位で出力されます.
入力と出力の例
プールコード(C++)
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define sz(v) (int)(v).size()
#define all(v) v.begin(), v.end()
#define rall(v) (v).rbegin(), (v).rend()
#define compress(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define OOB(x, y) ((x) < 0 || (x) >= n || (y) < 0 || (y) >= m)
#define debug(x) cout << (#x) << ": " << (x) << '\n'
using namespace std;
using ll = long long;
using ull = unsigned long long;
using dbl = double;
using ldb = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using tii = tuple<int,int,int>;
template<typename T> using wector = vector<vector<T>>;
const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const ll LNF = 1e18 + 7;
template<typename T = int64_t, size_t sz = 17, typename F = plus<T>>
struct SegTree {
vector<T> tree; T t; F f{};
SegTree(T t = T()) : tree(1 << sz + 1, t), t(t) {}
explicit SegTree(T t, const F& f) : tree(1 << sz + 1, t), t(t), f(f) {}
void Init() {
for (int i = (1 << sz) - 1; i; i--) {
tree[i] = f(tree[i << 1], tree[i << 1 | 1]);
}
}
void Update(int i, T val) {
--i |= 1 << sz; tree[i] = val;
while (i >>= 1) tree[i] = f(tree[i << 1], tree[i << 1 | 1]);
}
T Query(int l, int r) {
T L = t, R = t; --l |= 1 << sz, --r |= 1 << sz;
while (l <= r) {
if (l & 1) L = f(L, tree[l++]);
if (~r & 1) R = f(tree[r--], R);
l >>= 1, r >>= 1;
}
return f(L, R);
}
};
struct F{
pii operator()(pii a,pii b){
if(a.second == b.second){
return a.first < b.first ? a : b;
}
return a.second < b.second ? a : b;
}
} f;
SegTree<pll, 20, F> ST({INF,INF}, f);
int main() {
fastio;
int n,m; cin >> n;
for(ll i = 1; i <= n; i++){
int t; cin >> t;
ST.Update(i, {i, t});
}
cin >> m;
while(m--){
int a,b,c; cin >> a;
if(a & 1){
cin >> b >> c;
ST.Update(b, {b, c});
}
else cout << ST.tree[1].first << "\n";
}
return 0;
}
最高値の値を持つインデックスの出力が要求されるため、pairを使用してセグメントツリーを整理します.なお、値が同じであれば、処理を行わなければならない.
Reference
この問題について(Back Junアルゴリズム14427:数列とクエリー15), 我々は、より多くの情報をここで見つけました https://velog.io/@inwooleeme/백준-알고리즘-14427번-수열과-쿼리-15テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol