“玲珑杯”ACM比赛 Round #5 H -- Variance 简单树状数组

Posted stupid_one

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了“玲珑杯”ACM比赛 Round #5 H -- Variance 简单树状数组相关的知识,希望对你有一定的参考价值。

可以把每个公式都化简,然后得到要维护的东西就是平方和,和前缀和,两个bit即可

不能cin,超时。ios后都不行。

scanf用lld

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
#define MY "H:/CodeBlocks/project/CompareTwoFile/DataMy.txt", "w", stdout
#define ANS "H:/CodeBlocks/project/CompareTwoFile/DataAns.txt", "w", stdout


#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e6 + 20;
int n, m;
LL c_pow[maxn];
LL c[maxn];
LL a[maxn];
int lowbit(int x) {
    return x & (-x);
}
void upDate_pow(int pos, LL val) {
    while (pos <= n) {
        c_pow[pos] += val;
        pos += lowbit(pos);
    }
}
void upDate(int pos, LL val) {
    while (pos <= n) {
        c[pos] += val;
        pos += lowbit(pos);
    }
}
LL query_pow(int pos) {
    LL ans = 0;
    while (pos > 0) {
        ans += c_pow[pos];
        pos -= lowbit(pos);
    }
    return ans;
}
LL query(int pos) {
    LL ans = 0;
//    if (pos < 0) arrest
    while (pos > 0) {
        ans += c[pos];
        pos -= lowbit(pos);
    }
    return ans;
}
void work() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; ++i) {
        scanf("%lld", &a[i]);
        upDate(i, a[i]);
        upDate_pow(i, a[i] * a[i]);
    }
    bool flag1 = false;
    for (int i = 1; i <= m; ++i) {
        int flag;
        scanf("%d", &flag);
        if (flag == 1) {
            int x, y;
            scanf("%d%d", &x, &y);
            LL now = query(x) - query(x - 1);
            upDate(x, -now);
            upDate(x, y);

            now = query_pow(x) - query_pow(x - 1);
            upDate_pow(x, -now);
            upDate_pow(x, y * y);
        } else {
            int L, R;
            scanf("%d%d", &L, &R);
            LL t1 = (query_pow(R) - query_pow(L - 1)) * (R - L + 1);
            LL haha = query(R) - query(L - 1);
            LL t2 = -2 * haha * haha;
            LL t3 = t2 / -2;
            printf("%lld\\n", t1 + t2 + t3);
        }
    }
}
int main() {
#ifdef local
    freopen("data.txt","r",stdin);
#endif
    work();
    return 0;
}
View Code

 

以上是关于“玲珑杯”ACM比赛 Round #5 H -- Variance 简单树状数组的主要内容,如果未能解决你的问题,请参考以下文章

“玲珑杯”ACM比赛 Round #19

“玲珑杯”ACM比赛 Round #1

“玲珑杯”ACM比赛 Round #19 B -- Buildings (RMQ + 二分)

“玲珑杯”ACM比赛 Round #23

玲珑杯”ACM比赛 Round #19 B 维护单调栈

“玲珑杯”ACM比赛 Round #18 A 暴力水 C dp