2019第十届蓝桥杯B组决赛题解第九题

题意: 两种操作,C x y,将x位置的数修改为y,Q x y,查询[x,y]之间的第8大值,y-x+1<= 8的话输出0

思路: 区间第8大值,线段树在时间、空间都够了(蓝桥怎么会让手写主席树。。)
每个节点存储它管辖的这个区间的前8大值,修改的时候暴力merge,单次修改复杂度log(n)*8
查询的时候返回含有8个值得list,并不断merge

代码:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5+55555;
const ll mod = 998244353;
const double eps = 1e-7;

struct tree {
    int l,r;
    int p[10];
} t[maxn<<2];

int l,n;

void build(int i,int l,int r) {
    t[i].l = l;
    t[i].r = r;
    mem(t[i].p,0);

    if(l == r) return ;
    int mid = (l+r)>>1;
    build(i<<1,l,mid);
    build(i<<1|1,mid+1,r);
    return ;
}

void update(int i) {
    int cnt1 = 0,cnt2 = 0;
    for(int j = 0;j< 8;j++) {
        if(t[i<<1].p[cnt1]> t[i<<1|1].p[cnt2]) {
            t[i].p[j] = t[i<<1].p[cnt1];
            cnt1++;
        } else {
            t[i].p[j] = t[i<<1|1].p[cnt2];
            cnt2++;
        }
    }
    return ;
}

void modify(int i,int pos,int val) {
    if(t[i].l> pos || t[i].r< pos) return ;
    if(t[i].l == t[i].r) {
        t[i].p[0] = val;
        return ;
    }
    modify(i<<1,pos,val);
    modify(i<<1|1,pos,val);
    update(i);
    return ;
}

vector<int> merge(vector<int> ans1,vector<int> ans2) {
    vector<int> ans;
    int cnt1 = 0,cnt2 = 0;
    for(int j = 0;j< 8;j++) {
        if(ans1[cnt1]> ans2[cnt2]) {
            ans.push_back(ans1[cnt1]);
            cnt1++;
        } else {
            ans.push_back(ans2[cnt2]);
            cnt2++;
        }
    }
    return ans;
}

vector<int> query(int i,int l,int r) {
    vector<int> ans;
    if(t[i].l> r||t[i].r< l) {
        for(int j = 0;j< 8;j++) ans.push_back(0);
        return ans;
    }

    if(t[i].l>= l&&t[i].r<= r) {
        for(int j = 0;j< 8;j++) ans.push_back(t[i].p[j]);
        return ans;
    }

    return merge(query(i<<1,l,r),query(i<<1|1,l,r));
}

int main() {
    cin>>l>>n;

    build(1,1,l);
    char c;
    int x,y;
    while(n--) {
        scanf(" %c %d %d",&c,&x,&y);
        if(c == 'C') {
            modify(1,x,y);
        } else {
            if(y-x+1< 8) {
                printf("0\n");
                continue;
            }
            vector<int> ans = query(1,x,y);
            printf("%d\n",ans[7]);
        }
    }

    return 0;
}

 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值