当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 洛谷P3924 康娜的线段树(期望 前缀和)

洛谷P3924 康娜的线段树(期望 前缀和)

2019年01月26日  | 移动技术网IT编程  | 我要评论

小班开学寄语,长沙教育学院教师资格证,铁观音属于红茶吗

题意

题目链接

sol

思路就是根据期望的线性性直接拿前缀和算贡献。。

这题输出的时候是不需要约分的qwq

如果你和我一样为了ac不追求效率的话直接#define int __int128就行了。。

代码十分清新

#include<bits/stdc++.h>
#define int __int128
using namespace std;
const int maxn = 1e6 + 10;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
void print(int x) {
    if(x < 0) putchar('-'), x = -x;
    if (x > 9) print(x / 10);
    putchar('0' + x % 10);
}
int n, m, qwq, s[maxn], a[maxn], ans, lim;
int get(int dep) {
    return 1 << (lim - (dep - 1));
}
void build(int l, int r, int dep, int sum) {
    ans += (a[r] - a[l - 1]) * get(dep);
    if(l == r) {s[l] = sum + get(dep); return ;}
    int mid = l + r >> 1;
    build(l, mid, dep + 1, sum + get(dep)); 
    build(mid + 1, r,dep + 1, sum + get(dep));
}
signed main() {
    n = read(); m = read(); qwq = read();
    for(int cur = 1; cur <= n; lim ++, cur <<= 1);
    for(int i = 1; i <= n; i++) a[i] = read(), a[i] += a[i - 1];
    build(1, n, 1, 0);
    for(int i = 1; i <= n; i++) s[i] += s[i - 1];
    while(m--) {
        int l = read(), r = read(), v = read();
        ans += ((s[r] - s[l - 1]) * v);
        print(((ans * qwq) >> lim)); putchar('\n');
    }
    return 0;
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网