首页 > 代码库 > “玲珑杯”ACM比赛 Round #5 H -- Variance 简单树状数组

“玲珑杯”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 简单树状数组