Question 9 of the final of group B of the 10th Blue Bridge Cup in 2019

Two operations, C x y, change the number of X positions to y, Q x y, query the 8th largest value between [x,y], and output 0 if Y-X + 1 < = 8

Idea: the 8th largest value in the interval, the line tree is enough in time and space.. )
Each node stores the top 8 values of the range it governs. When modifying, it merge s violently. The complexity of a single modification log (n * 8)
When querying, return a list containing 8 values, and continue to merge

Code:

#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;
}

 

Posted by jernhenrik on Sun, 03 Nov 2019 11:58:24 -0800