zoukankan      html  css  js  c++  java
  • 多校1010 Taotao Picks Apples

    》》点击进入原题《《

     思路:题解很有意思,适合线段树进阶

    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    #define lson l,m,o<<1
    #define rson m+1,r,o<<1|1
    
    const int maxn = 1e5 + 10;
    int a[maxn], d1[maxn], d2[maxn];
    int tree[maxn << 2], vis[maxn << 2];
    int ans, cur;
    
    void build(int l, int r, int o){
        if (l == r){
            tree[o] = a[l];
            vis[o] = l;
            return;
        }
        int m = (l + r) >> 1;
        build(lson);
        build(rson);
        tree[o] = max(tree[o << 1], tree[o << 1 | 1]);
        if (tree[o << 1] >= tree[o << 1 | 1])
            vis[o] = vis[o << 1];
        else vis[o] = vis[o << 1 | 1];
    
    }
    void query(int l, int r, int o, int ql, int qr, int k){
        if (l == r){
            if (tree[o]>k)
                cur = min(cur, l);
            return;
        }
        int m = (l + r) >> 1;
        if (l >= ql&&r <= qr){
            if (tree[o << 1] > k)
                query(lson, ql, qr, k);
            else if (tree[o << 1 | 1] > k)
                query(rson, ql, qr, k);
            return;
        }
        if (ql <= m)
            query(lson, ql, qr, k);
        if (qr > m)query(rson, ql, qr, k);
    }
    void query1(int l, int r, int o, int ql, int qr){
        if (l >= ql&&r <= qr){
            if (tree[o] > a[cur])
                cur = vis[o];
            return;
        }
        int m = (l + r) >> 1;
        if (ql <= m)query1(lson, ql, qr);
        if (qr > m)query1(rson, ql, qr);
    }
    int main(){
        ios::sync_with_stdio(false);
    
        int t; cin >> t;
        while (t--){
            int n, m, p, q, Max = 0;
            cin >> n >> m;
            for (int i = 1; i <= n; i++){
                cin >> a[i];
                if (a[i] > Max)
                    d1[i] = d1[i - 1] + 1, Max = a[i];
                else d1[i] = d1[i - 1];
            }
            build(1, n, 1);
            for (int i = n; i; i--){
                cur = n + 1;
                query(1, n, 1, i, n, a[i]);
                if (cur > n)cur = 0;
                d2[i] = d2[cur] + 1;
            }
            while (m--){
                cin >> p >> q;
                ans = cur = 0;
                if (p != 1){
                    query1(1, n, 1, 1, p - 1);
                }
                ans += d1[cur];
                if (q > a[cur])ans++;
                else q = a[cur];
                cur = n + 1;
    
                if (p != n)query(1, n, 1, p + 1, n, q);
                if (cur <= n)ans += d2[cur];
                cout << ans << endl;
            }
        }
    }
  • 相关阅读:
    AT2172 Shik and Travel
    bzoj5138 [Usaco2017 Dec]Push a Box
    bzoj3545 [ONTAK2010]Peaks、bzoj3551 [ONTAK2010]Peaks加强版
    bzoj5183 [Baltic2016]Park
    bzoj4423 [AMPPZ2013]Bytehattan
    bzoj2125 最短路
    斐波那契数列小结
    记一场模拟赛
    洛谷2387 BZOJ3669魔法森林题解
    COGS-2638 区间与,异或,询问max
  • 原文地址:https://www.cnblogs.com/zengguoqiang/p/9506598.html
Copyright © 2011-2022 走看看