#P3924. 康娜的线段树

    ID: 2434 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>线段树洛谷原创O2优化前缀和期望洛谷月赛

康娜的线段树

Description

Kobayashi is a programmer, and naturally Kanna becomes very interested in this human “magic,” so Kobayashi starts teaching her OI.

Today Kanna learned a magical structure called a segment tree, which can maintain information over an interval and is very powerful. Kanna tried to write a segment tree that maintains the sum over intervals. Since she doesn’t know how to use lazy propagation, she performs each range add operation by brute-force point updates. The specific code is as follows:

struct Segment_Tree{
#define lson (o<<1)
#define rson (o<<1|1)
    int sumv[N<<2],minv[N<<2];
    inline void pushup(int o){sumv[o]=sumv[lson]+sumv[rson];}
    inline void build(int o,int l,int r){
        if(l==r){sumv[o]=a[l];return;}
        int mid=(l+r)>>1;
        build(lson,l,mid);build(rson,mid+1,r);
        pushup(o);
    }
    inline void change(int o,int l,int r,int q,int v){
        if(l==r){sumv[o]+=v;return;}
        int mid=(l+r)>>1;
        if(q<=mid)change(lson,l,mid,q,v);
        else change(rson,mid+1,r,q,v);
        pushup(o);
    }
}T; 

When modifying, she writes:

for(int i=l;i<=r;i++)T.change(1,1,n,i,addv);

Obviously, each node of this segment tree stores the sum of the interval it governs.

Kanna is thoughtful, and she suddenly came up with a question:

After each range add operation on the segment tree is completed, starting from the root, at each internal node choose one child with equal probability and continue until reaching a leaf node. Sum the values of all nodes along the path. What is the expected value of this sum?

Each time, Kanna will give you a value qwqqwq, and it is guaranteed that the expected value multiplied by qwqqwq is an integer.

This problem is so easy that clever Kanna solved it in an instant.

Now she wonders, can you solve it too?

Input Format

The first line contains integers n,m,qwqn, m, qwq, which denote the length of the original sequence, the number of operations, and the denominator used for output.

The second line contains nn integers, the original sequence.

The next mm lines each contain three integers l,r,xl, r, x, meaning add xx to the interval [l,r][l, r].

Output Format

Output mm lines. Each line is the expected path-sum value multiplied by qwqqwq.

8 2 1
1 2 3 4 5 6 7 8
1 3 4
1 8 2

90
120

Hint

Constraints:

  • For 30% of the testdata, 1n,m1001 \leq n, m \leq 100.
  • For 70% of the testdata, 1n,m1051 \leq n, m \leq 10^{5}.
  • For 100% of the testdata, 1n,m1061 \leq n, m \leq 10^{6}.
  • 1000ai,x1000-1000 \leq a_i, x \leq 1000.

Translated by ChatGPT 5