이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
const int A = 1e9;
struct ST{
struct node{
int s;
node *l, *r;
node(){
s = 0;
l = r = 0;
}
};
node *rt;
void init(){
rt = new node();
}
void upd(node *v, int tl, int tr, int& p, int& x){
if (tl == tr){
v -> s += x;
return;
}
int tm = (tl + tr) / 2;
if (p <= tm){
if (!(v -> l)) v -> l = new node();
upd(v -> l, tl, tm, p, x);
}
else {
if (!(v -> r)) v -> r = new node();
upd(v -> r, tm + 1, tr, p, x);
}
v -> s = 0;
if (v -> l) v -> s += v -> l -> s;
if (v -> r) v -> s += v -> r -> s;
}
void upd(int p, int x){
if (p > A) return;
upd(rt, 1, A, p, x);
}
void upd(int l, int r, int x){
upd(l, x);
upd(r + 1, -x);
}
int get(node *v, int tl, int tr, int l, int r){
if (l > tr || r < tl) return 0;
if (l <= tl && tr <= r) return v -> s;
int tm = (tl + tr) / 2, out = 0;
if (v -> l) out += get(v -> l, tl, tm, l, r);
if (v -> r) out += get(v -> r, tm + 1, tr, l, r);
return out;
}
int get(int v){
return get(rt, 1, A, 1, v);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, q; cin>>n>>q;
vector<int> a(n + 2), x(n + 2);
for (int i = 1; i <= n; i++){
cin>>a[i];
}
ST T; T.init();
auto add = [&](int v, int t){
int k = max(x[v - 1], x[v + 1]);
if (k < x[v]){
T.upd(k + 1, x[v], t);
}
k = min({x[v - 1], x[v], x[v + 1]});
if (1 <= k){
T.upd(1, k, -t);
}
};
for (int i = 1; i <= n; i++){
x[i] = a[i];
add(i, 1);
}
while (q--){
int type; cin>>type;
if (type == 1){
int f; cin>>f;
cout<<T.get(f)<<"\n";
}
else {
int f, y; cin>>f>>y;
add(f, -1);
x[f] = y;
add(f, 1);
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |