zoukankan      html  css  js  c++  java
  • “玲珑杯”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
    ", t1 + t2 + t3);
            }
        }
    }
    int main() {
    #ifdef local
        freopen("data.txt","r",stdin);
    #endif
        work();
        return 0;
    }
    View Code
  • 相关阅读:
    Template(模板)模式
    Android活动(Activity)创建及生命周期
    Android--SharedPreferences数据存储方案
    Adapter适配器模式--图解设计模式
    准时制生产(Just in Time,JIT)
    术语
    制造资源计划(Manufacturing Resource Planning,Mrp II)
    Angualr6表单提交验证并跳转
    Android PDA扫描枪广播接搜条码并使用
    Java统计代码行数
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6104240.html
Copyright © 2011-2022 走看看