zoukankan      html  css  js  c++  java
  • 数值标记问题 离线+树状数组 HDU 3938 + HDU 3333

    HDU 3938

    题目大意:给你一个长度为n的数组a,定义区间[l,r]的val为区间内所有不同的数值之和。现在有m个询问,每次询问一个区间,问区间的val是多少。

    思路:将所有的询问按照右端点排序。然后暴力枚举右区间,然后对之前出现过的val做一个标记即可,每次都更新这个标记就好了。 具体的和HDU 5869一样,只不过5869还要预处理,比较难

    //看看会不会爆int!数组会不会少了一维!
    //取物问题一定要小心先手胜利的条件
    #include <bits/stdc++.h>
    using namespace std;
    #define LL long long
    #define ALL(a) a.begin(), a.end()
    #define pb push_back
    #define mk make_pair
    #define fi first
    #define se second
    const int maxn = 50000 + 5;
    const int maxm = 200000 + 5;
    const int maxval = 1000000 + 5;
    LL tree[maxn], a[maxn], ans[maxm];
    vector<pair<int, int> > q[maxn];
    int n, m;
    int pre[maxval];
    inline int lowbit(int x) {return x & -x;}
    
    void update(int x, int val){
        for (int i = x; i <= n; i += lowbit(i))
            tree[i] += val;
    }
    
    LL sum(int x){
        LL ans = 0;
        for (int i = x; i > 0; i -= lowbit(i)){
            ans += tree[i];
        }
        return ans;
    }
    
    int main(){
        int t; cin >> t;
        while (t--){
            memset(pre, 0, sizeof(pre));
            memset(tree, 0, sizeof(tree));
            scanf("%d", &n);
            for (int i = 1; i <= n; i++) {
                scanf("%I64d", a + i);
                q[i].clear();
            }
            scanf("%d", &m);
            for (int i = 1; i <= m; i++){
                int l, r; scanf("%d%d", &l ,&r);
                q[r].pb(mk(l, i));
            }
            for (int i = 1; i <= n; i++){
                if (pre[a[i]]){
                    update(pre[a[i]], -1 * a[i]);
                }
                pre[a[i]] = i;
                update(i, a[i]);
                int len = q[i].size();
                for (int j = 0; j < len; j++){
                    pair<int, int> p = q[i][j];
                    ans[p.second] = sum(i) - sum(p.first - 1);
                }
            }
            for (int i = 1; i <= m; i++){
                printf("%I64d
    ", ans[i]);
            }
        }
        return 0;
    }
    View Code

    HDU 3333

    这道题就是a数组的范围大了一点而已,没啥的,只要离散化一下就好了,其他的思路都一样

    然后代码只需要多添加一个离散化即可

    //看看会不会爆int!数组会不会少了一维!
    //取物问题一定要小心先手胜利的条件
    #include <bits/stdc++.h>
    using namespace std;
    #define LL long long
    #define ALL(a) a.begin(), a.end()
    #define pb push_back
    #define mk make_pair
    #define fi first
    #define se second
    const int maxn = 30000 + 5;
    const int maxm = 100000 + 5;
    LL tree[maxn], a[maxn], b[maxn], ans[maxm];
    vector<pair<int, int> > q[maxn];
    int n, m;
    int pre[maxn];
    inline int lowbit(int x) {return x & -x;}
    
    void update(int x, int val){
        for (int i = x; i <= n; i += lowbit(i))
            tree[i] += val;
    }
    
    LL sum(int x){
        LL ans = 0;
        for (int i = x; i > 0; i -= lowbit(i)){
            ans += tree[i];
        }
        return ans;
    }
    
    int main(){
        int t; cin >> t;
        while (t--){
            memset(pre, 0, sizeof(pre));
            memset(tree, 0, sizeof(tree));
            scanf("%d", &n);
            for (int i = 1; i <= n; i++) {
                scanf("%I64d", a + i);
                b[i] = a[i];
                q[i].clear();
            }
            sort(b + 1, b + 1 + n);
            scanf("%d", &m);
            for (int i = 1; i <= m; i++){
                int l, r; scanf("%d%d", &l ,&r);
                q[r].pb(mk(l, i));
            }
            for (int i = 1; i <= n; i++){
                int posa = lower_bound(b + 1, b + 1 + n, a[i]) - b;
                if (pre[posa]){
                    update(pre[posa], -1 * a[i]);
                }
                pre[posa] = i;
                update(i, a[i]);
                int len = q[i].size();
                for (int j = 0; j < len; j++){
                    pair<int, int> p = q[i][j];
                    ans[p.second] = sum(i) - sum(p.first - 1);
                }
            }
            for (int i = 1; i <= m; i++){
                printf("%I64d
    ", ans[i]);
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    MySQL数据丢失讨论
    分布式系统之Quorum (NRW)算法
    阿里巴巴-OS事业群-OS手机事业部-系统服务部门招聘Java开发工程师,有意者请进来
    EQueue
    ENode 2.0
    关于MySQL的在线扩容
    我收藏的技术知识图(每张都是大图)
    关于实现一个基于文件持久化的EventStore的核心构思
    Actor的原理
    OAuth 2.0 授权原理
  • 原文地址:https://www.cnblogs.com/heimao5027/p/5873529.html
Copyright © 2011-2022 走看看