zoukankan      html  css  js  c++  java
  • codeforces 1217E E. Sum Queries? (线段树

    codeforces 1217E E. Sum Queries? (线段树

    传送门:https://codeforces.com/contest/1217/problem/E

    题意:

    n个数,m次询问

    单点修改

    询问区间内最小的unbalanced number

    balanced number定义是,区间内选取数字的和sum

    sum上的每一位都对应着选取的数上的一位

    否则就是unbalanced number

    题解:

    根据题意

    如果区间存在unbalance number,那么一定存在两个数就可以组成unbalance number

    为什么?

    因为根据balanced number的定义来说,只要在和的一位上 有两个数字在这个位置上的数都不为0的话那么这两个数字一定可以组成unbalanced number,所以我们需要对每一位非0的数进行操作

    值域最多为2e9,所以我们建立十颗线段树,保存每一位上的 如果当前位不为0,保存这个位置上所在数的最小值

    记录一个保存答案的线段树,答案就为 当前位的两个数都不为0的两数之和的最小值

    代码:

    /**
     *        ┏┓    ┏┓
     *        ┏┛┗━━━━━━━┛┗━━━┓
     *        ┃       ┃  
     *        ┃   ━    ┃
     *        ┃ >   < ┃
     *        ┃       ┃
     *        ┃... ⌒ ...  ┃
     *        ┃       ┃
     *        ┗━┓   ┏━┛
     *          ┃   ┃ Code is far away from bug with the animal protecting          
     *          ┃   ┃   神兽保佑,代码无bug
     *          ┃   ┃           
     *          ┃   ┃        
     *          ┃   ┃
     *          ┃   ┃           
     *          ┃   ┗━━━┓
     *          ┃       ┣┓
     *          ┃       ┏┛
     *          ┗┓┓┏━┳┓┏┛
     *           ┃┫┫ ┃┫┫
     *           ┗┻┛ ┗┻┛
     */
    // warm heart, wagging tail,and a smile just for you!
    //
    //                            _ooOoo_
    //                           o8888888o
    //                           88" . "88
    //                           (| -_- |)
    //                           O  =  /O
    //                        ____/`---'\____
    //                      .'  |     |//  `.
    //                     /  |||  :  |||//  
    //                    /  _||||| -:- |||||-  
    //                    |   |   -  /// |   |
    //                    | \_|  ''---/''  |   |
    //                      .-\__  `-`  ___/-. /
    //                  ___`. .'  /--.--  `. . __
    //               ."" '<  `.___\_<|>_/___.'  >'"".
    //              | | :  `- \`.;` _ /`;.`/ - ` : | |
    //                 `-.   \_ __ /__ _/   .-` /  /
    //         ======`-.____`-.___\_____/___.-`____.-'======
    //                            `=---='
    //        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    //                     佛祖保佑      永无BUG
    #include <set>
    #include <map>
    #include <stack>
    #include <cmath>
    #include <queue>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    typedef long long LL;
    typedef pair<int, int> pii;
    typedef unsigned long long uLL;
    #define ls rt<<1
    #define rs rt<<1|1
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    #define bug printf("*********
    ")
    #define FIN freopen("input.txt","r",stdin);
    #define FON freopen("output.txt","w+",stdout);
    #define IO ios::sync_with_stdio(false),cin.tie(0)
    #define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]
    "
    #define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]
    "
    #define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]
    "
    const int maxn = 3e5 + 5;
    const int INF = 2e9+7;
    const int mod = 1e9 + 7;
    const double Pi = acos(-1);
    LL gcd(LL a, LL b) {
        return b ? gcd(b, a % b) : a;
    }
    LL lcm(LL a, LL b) {
        return a / gcd(a, b) * b;
    }
    double dpow(double a, LL b) {
        double ans = 1.0;
        while(b) {
            if(b % 2)ans = ans * a;
            a = a * a;
            b /= 2;
        } return ans;
    }
    LL quick_pow(LL x, LL y) {
        LL ans = 1;
        while(y) {
            if(y & 1) {
                ans = ans * x % mod;
            } x = x * x % mod;
            y >>= 1;
        } return ans;
    }
    LL a[maxn];
    LL Min[maxn << 2][15];
    LL ANS[maxn << 2];
    void push_up(int rt) {
        ANS[rt] = INF;
        for(int i = 0; i <= 12; i++) {
            if(Min[ls][i] != INF && Min[rs][i] != INF) {
                ANS[rt] = min(ANS[rt], Min[ls][i] + Min[rs][i]);
            }
            Min[rt][i] = min(Min[ls][i], Min[rs][i]);
        }
        ANS[rt] = min(ANS[rt], min(ANS[ls], ANS[rs]));
    }
    void build(int l, int r, int rt) {
        ANS[rt] = INF;
        if(l == r) {
            int tmp = a[l];
            // debug1(tmp);
            for(int i = 0; i <= 12; i++) {
                int val = tmp % 10;
                if(val == 0) Min[rt][i] = INF;
                else Min[rt][i] = a[l];
                tmp /= 10;
            }
            return;
        }
        int mid = (l + r) >> 1;
        build(lson);
        build(rson);
        push_up(rt);
    }
    void update(int pos, int x, int l, int r, int rt) {
        if(l == r) {
            int tmp = x;
            ANS[rt] = INF;
            // debug1(tmp);
            for(int i = 0; i <= 12; i++) {
                int val = tmp % 10;
                if(val == 0)   Min[rt][i] = INF;
                else Min[rt][i] = x;
                // Min[rt][i] = val;
                // debug2(val,Min[rt][i]);
                tmp /= 10;
            }
            return;
        }
        int mid = (l + r) >> 1;
        if(pos <= mid) update(pos, x, lson);
        else update(pos, x, rson);
        push_up(rt);
    }
    LL res[15];
    LL ans = INF;
    void query(int L, int R, LL &ans, int l, int r, int rt) {
        // if(l > R || r < L)  return;
        if(L <= l && r <= R) {
            for(int i = 0; i <= 12; i++) {
                if(res[i] != INF && Min[rt][i] != INF) {
                    ans = min(ans, res[i] + Min[rt][i]);
                }
            }
            for(int i = 0; i <= 10; i++) {
                res[i] = min(res[i], Min[rt][i]);
            }
            ans = min(ANS[rt], ans);
            return;
        }
        int mid = (l + r) >> 1;
        if(L <= mid) query(L, R, ans, lson);
        if(R > mid)  query(L, R, ans, rson);
    }
    int main() {
    #ifndef ONLINE_JUDGE
        FIN
    #endif
        int n, m;
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
        }
        build(1, n, 1);
        while(m--) {
            int op, l, r, pos, x;
            scanf("%d", &op);
            if(op == 1) {
                scanf("%d%d", &pos, &x);
                update(pos, x, 1, n, 1);
            } else {
                scanf("%d%d", &l, &r);
                ans = INF;
                for(int i = 0; i <= 12; i++) res[i] = INF;
                query(l, r, ans, 1, n, 1);
                if(ans == INF) ans = -1;
                printf("%lld
    ", ans);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    利用Java脚本实现弹出窗口后,按确定实现跳转
    客服利用QQ实现即时聊天
    获取页面可见区域,屏幕区域的尺寸
    圆角模板百度知道
    利用javascript实现web页面刷新的方法
    论:命名空间,程序集和类
    我从少年时候就非常喜欢的诗歌:雨巷
    魔兽世界 圣骑士唯一的远程武器任务
    又想起我年少时候熟记的抒情诗致海伦
    System.Text.Encoding.UTF8 字符串和字节数组的互相转换
  • 原文地址:https://www.cnblogs.com/buerdepepeqi/p/11496022.html
Copyright © 2011-2022 走看看