Code first, then talk
// Type: se_tr
#include<bits/stdc++.h>
using namespace std;
struct node{
node *ls, *rs;
int lb, rb, mi;
node () {
mi = 0x3f3f3f3f;
ls = rs = NULL;
}
};
void build(node *cur, int lb, int rb){
cur -> lb = lb;
cur -> rb = rb;
cur -> mi = 0x3f3f3f3f;
if (lb != rb){
cur -> ls = new node;
cur -> rs = new node;
int mid = (lb + rb) >> 1;
build(cur -> ls, lb, mid);
build(cur -> rs, mid+1, rb);
}
}
void sett(node *cur, int x, int y){
if (cur -> lb == x && cur -> rb == x){
cur -> mi = y;
}
else {
int mid = (cur -> lb + cur -> rb) >> 1;
if (x <= mid) sett(cur -> ls, x, y);
else sett(cur -> rs, x, y);
cur -> mi = min(cur -> ls -> mi, cur -> rs -> mi);
}
}
int getm(node *cur, int lb, int rb){
if (cur -> lb == lb && cur -> rb == rb) return cur -> mi;
int ans = 0x3f3f3f3f;
int mid = (cur -> lb + cur -> rb) >> 1;
if (lb <= mid) ans = min(ans, getm(cur -> ls, lb, min(mid, rb)));
if (rb > mid) ans = min(ans, getm(cur -> rs, max(mid+1, lb), rb));
return ans;
}
node *root;
int solve(node *cur, int lb, int rb, int x){
if (cur -> lb == cur -> rb) {
if (cur -> mi <= x) return cur -> lb;
else return -1;
}
int mid = (cur -> lb + cur -> rb) >> 1;
if (rb <= mid) return solve(cur -> ls, lb, rb, x);
else if (lb > mid) return solve(cur -> rs, lb, rb, x);
else {
if (getm(cur -> ls, lb, mid) <= x) return solve(cur -> ls, lb, mid, x);
else return solve(cur -> rs, mid+1, rb, x);
}
}
int n, q;
int main(){
scanf("%d%d", &n, &q);
root = new node;
build(root, 1, n);
int x, y;
int lc, rc;
lc = rc = 0;
char c;
for (int i = 1;i <= q;i++){
c = getchar();
while (!isalpha(c)) c = getchar();
scanf("%d%d", &x, &y);
if (c == 'M'){
sett(root, y, x);
}
else{
int anss = solve(root, y, n, x);
if (anss == 0x3f3f3f3f) printf("-1\n");
else printf("%d\n", anss);
}
}
return 0;
}
题目思路
这道题一看容易觉得是 mod hist se_tr ,但其实不能这样做。
理清 BF 思路,然后发现其实可以用 se_tr 维护 当前年龄的下车时间。
设年龄为 \(i\) 的人下车时间为 \(i\),对于输入 \(x, y\) 要求出 \(min_{y\le i\le n, d_i\ge x}(d_i)\) 。
于是线段树上二分就对了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。