“玲珑杯”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;
}

时间: 2024-11-05 02:20:51

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

“玲珑杯”ACM比赛 Round #19题解&amp;源码【A,规律,B,二分,C,牛顿迭代法,D,平衡树,E,概率dp】

A -- simple math problem Time Limit:2s Memory Limit:128MByte Submissions:1599Solved:270 SAMPLE INPUT 5 20 1314 SAMPLE OUTPUT 5 21 1317 SOLUTION “玲珑杯”ACM比赛 Round #19 题目链接:http://www.ifrog.cc/acm/problem/1145 分析: 这个题解是官方写法,官方代码如下: 1 #include <iostream>

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

“玲珑杯”ACM比赛 Round #19 Start Time:2017-07-29 14:00:00 End Time:2017-07-29 16:30:00 Refresh Time:2017-07-29 16:42:55 Private B -- Buildings Time Limit:2s Memory Limit:128MByte Submissions:590Solved:151 DESCRIPTION There are nn buildings lined up, and th

“玲珑杯”ACM比赛 Round #18

"玲珑杯"ACM比赛 Round #18 Start Time:2017-07-15 12:00:00 End Time:2017-07-15 15:46:00 A -- 计算几何你瞎暴力 Time Limit:5s Memory Limit:256MByte Submissions:1764Solved:348 DESCRIPTION 今天HHHH考完了期末考试,他在教学楼里闲逛,他看着教学楼里一间间的教室,于是开始思考: 如果从一个坐标为 (x1,y1,z1)(x1,y1,z1)的

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

“玲珑杯”ACM比赛 Round #18 计算几何你瞎暴力 题意:如果从一个坐标为 (x1,y1,z1)的教室走到(x2,y2,z2)的距离为 |x1−x2|+|y1−y2|+|z1−z2|.那么有多少对教室之间的距离是不超过R的呢? tags:坐标范围很小,瞎暴力 #include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring>

“玲珑杯”ACM比赛 Round #1

Start Time:2016-08-20 13:00:00 End Time:2016-08-20 18:00:00 Refresh Time:2017-11-12 19:51:52 Public A -- Absolute Defeat Time Limit:2s Memory Limit:64MByte Submissions:394Solved:119 DESCRIPTION Eric has an array of integers a1,a2,...,ana1,a2,...,an.

Codeforces Round #261 (Div. 2) D 树状数组应用

看着题意:[1,i]中等于a[i]的个数要大于[,jn]中等于a[j]的个数 且i<j,求有多少对这样的(i,j)  ,i<j但是 i前面的合法个数 要大于j后面的 看起来很像逆序数的样子,所以很容易往树状数组想去,但是处理就看个人了,像我比赛的时候就处理得非常的麻烦,虽做出了但是花时间也多,经过杰哥的教育,其实正着塞进树状数组 反着来找就可以了,灰常的简单清晰明了,贴一发纪念我的搓比 int n; int aa[1000000 + 55]; int bb[1000000 + 55]; int

Codeforces Round #301 (Div. 2)(树状数组+离散化)

E. Infinite Inversions time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output There is an infinite sequence consisting of all positive integers in the increasing order: p?=?{1,?2,?3,?...}. We per

POJ 2481 Cows 简单树状数组区间覆盖

点击打开链接 Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13334   Accepted: 4413 Description Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line

Codeforces Gym 100114 H. Milestones 离线树状数组

H. Milestones Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description The longest road of the Fairy Kingdom has n milestones. A long-established tradition defines a specific color for milestones in each region, with a