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;
            }
        }
    }
  • 相关阅读:
    Leetcode 191.位1的个数 By Python
    反向传播的推导
    Leetcode 268.缺失数字 By Python
    Leetcode 326.3的幂 By Python
    Leetcode 28.实现strStr() By Python
    Leetcode 7.反转整数 By Python
    Leetcode 125.验证回文串 By Python
    Leetcode 1.两数之和 By Python
    Hdoj 1008.Elevator 题解
    TZOJ 车辆拥挤相互往里走
  • 原文地址:https://www.cnblogs.com/zengguoqiang/p/9506598.html
Copyright © 2011-2022 走看看