“玲珑杯”ACM比赛 Round #5 H -- Variance 简单树状数组
2016-11-26 15:04
465 查看
可以把每个公式都化简,然后得到要维护的东西就是平方和,和前缀和,两个bit即可
不能cin,超时。IOS后都不行。
scanf用lld
View Code
不能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 #5 C -- Coco(递归)
- “玲珑杯”ACM比赛 Round #5 I -- I am Two【位运算判断2的幂】
- “玲珑杯”ACM比赛 Round #5 B -- Private Value【STL-map】
- “玲珑杯”ACM比赛 Round #5------I am Two
- 玲珑杯”ACM比赛 Round #5
- 玲珑杯”ACM比赛 Round #5 --2的幂次方
- “玲珑杯”acm比赛-试运行赛 部分题解
- “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)
- 玲珑杯 1066(“玲珑杯”ACM比赛 Round #6)(区间DP+四边形不等式优化)
- 玲珑学院ACM比赛 Round #5 Aplus B
- 玲珑杯”ACM比赛 Round #4 1054 - String cut 暴力。学到了扫描的另一种思想
- “玲珑杯”ACM比赛 Round #7
- “玲珑杯”ACM比赛 Round #4 G -- See car
- “玲珑杯”ACM比赛 Round #4 G -- See car【多种容器的运用】
- “玲珑杯”ACM比赛 Round #4 A -- chess play 【记忆模拟】
- “玲珑杯”ACM比赛 Round #4 A -- chess play
- 玲珑杯1052 - See car——STL集合的应用(“玲珑杯”ACM比赛 Round #4 )
- “玲珑杯”ACM比赛 Round #4 E -- array DP
- Lonlife-ACM 1005 - Spoon Devil's RP Test(同余定理)——“玲珑杯”acm比赛-试运行赛