树状数组,开O2 10分求助
查看原帖
树状数组,开O2 10分求助
283478
yhk2275580115楼主2021/8/11 00:44
#include<iostream>
#include<algorithm>
#include <vector>
#include <climits>
using namespace std;
struct {
    int maxn;
    int minn;
}tree[1000000];
int n, k;
int lowbit(int x) {
    return x & (-x);
}
inline int read(){
    int x=0,c=getchar(),f=1;
    while(c<'0'||c>'9')
        f= (c=='-')?-1:1,c=getchar();
    while(c>='0'&&c<='9')
        x=x*10+c-48,c=getchar();
    return f*x;
}
void update(int index, int val) {
    while (index <= n) {
        tree[index].maxn = val;
        tree[index].minn = val;
        int len = lowbit(index);
        for (int i = 1; i < len; i <<= 1) {
            tree[index].maxn = max(tree[index].maxn, tree[index - i].maxn);
            tree[index].minn = min(tree[index].minn, tree[index - i].minn);
        }
        index += lowbit(index);
    }
}
pair<int, int> query(int x, int y) {
    pair<int, int> ans(INT_MIN, INT_MAX);
    while(y>=x) {
        ans.first = max(ans.first,tree[y].maxn);
        ans.second = min(ans.second, tree[y].minn);
        --y;
        while(y-lowbit(y) >= x) {
            ans.first = max(ans.first,tree[y].maxn);
            ans.second = min(ans.second, tree[y].minn);
            y-=lowbit(y);
        }
    }
    return ans;
}
int main(){
    n = read();
    k = read();
    for (int i = 1; i <= n; i++) {
        int x;
        x = read();
        update(i, x);
    }
    vector<pair<int, int>> ans;
    for (int i = 1; i <= n - k + 1; i++) {
        pair<int, int> t = query(i, i+k-1);
        ans.push_back(t);
        cout << t.second << " ";
    } cout << endl;
    for (auto it : ans) {
        cout << it.first << " ";
    } cout << endl;

    return 0;
}
2021/8/11 00:44
加载中...