• Gym-10071A-Queries(树状数组)


    链接:

    https://vjudge.net/problem/Gym-100741A

    题意:

    Mathematicians are interesting (sometimes, I would say, even crazy) people. For example, my friend, a mathematician, thinks that it is very fun to play with a sequence of integer numbers. He writes the sequence in a row. If he wants he increases one number of the sequence, sometimes it is more interesting to decrease it (do you know why?..) And he likes to add the numbers in the interval [l;r]. But showing that he is really cool he adds only numbers which are equal some mod (modulo m).

    Guess what he asked me, when he knew that I am a programmer? Yep, indeed, he asked me to write a program which could process these queries (n is the length of the sequence):

    • p r It increases the number with index p by r. (, )
      You have to output the number after the increase.
    • p r It decreases the number with index p by r. (, ) You must not decrease the number if it would become negative.
      You have to output the number after the decrease.

    s l r mod You have to output the sum of numbers in the interval which are equal mod (modulo m). () ()

    思路:

    看半天没看懂题.
    建10个树状数组即可, 对模m的每种情况分别统计.

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <vector>
    //#include <memory.h>
    #include <queue>
    #include <set>
    #include <map>
    #include <algorithm>
    #include <math.h>
    #include <stack>
    #include <string>
    #include <assert.h>
    #include <iomanip>
    #define MINF 0x3f3f3f3f
    using namespace std;
    typedef long long LL;
    const int INF = 1e9;
    
    const int MAXN = 1e4+10;
    LL A[MAXN], C[15][MAXN];
    int n, m, q;
    
    int Lowbit(int x)
    {
        return x&(-x);
    }
    
    void Add(int pos, int mod, LL val)
    {
        while (pos <= n)
        {
            C[mod][pos] += val;
            pos += Lowbit(pos);
        }
    }
    
    LL Query(int pos, int mod)
    {
        LL ans = 0;
        while (pos > 0)
        {
            ans += C[mod][pos];
            pos -= Lowbit(pos);
        }
        return ans;
    }
    
    int main()
    {
        scanf("%d%d", &n, &m);
        for (int i = 1;i <= n;i++)
        {
            scanf("%lld", &A[i]);
            Add(i, A[i]%m, A[i]);
        }
        scanf("%d", &q);
        char opt[5];
        int l, r, mod;
        while (q--)
        {
            scanf("%s", opt);
            if (opt[0] == 's')
            {
                scanf("%d%d%d", &l, &r, &mod);
                printf("%lld
    ", Query(r, mod)-Query(l-1, mod));
            }
            else if (opt[0] == '+')
            {
                scanf("%d%d", &l, &r);
                Add(l, A[l]%m, -A[l]);
                A[l] += r;
                Add(l, A[l]%m, A[l]);
                printf("%lld
    ", A[l]);
            }
            else
            {
                scanf("%d%d", &l, &r);
                Add(l, A[l]%m, -A[l]);
                if (r <= A[l])
                    A[l] -= r;
                Add(l, A[l]%m, A[l]);
                printf("%lld
    ", A[l]);
            }
        }
    
        return 0;
    }
    
  • 相关阅读:
    【LOJ6041】「雅礼集训 2017 Day7」事情的相似度(用LCT维护SAM的parent树)
    【BZOJ1171】大sz的游戏(线段树+单调队列)
    2019年4月训练记录(4.07~4.22)
    【BZOJ4766】文艺计算姬(prufer序列)
    【BZOJ4573】[ZJOI2016] 大森林(LCT)
    2019.03.19 ZJOI2019模拟赛 解题报告
    【牛客挑战赛30D】小A的昆特牌(组合问题抽象到二维平面)
    【洛谷2624】[HNOI2008] 明明的烦恼(Python+利用prufer序列结论求解)
    【洛谷2290】[HNOI2004] 树的计数(Python+利用prufer序列结论求解)
    初识prufer序列
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11527724.html
Copyright © 2020-2023  润新知