Bzoj 1798: [ahoi2009] seq maintenance sequence seq (hybrid operation of line tree multiplication addition)

Source: Internet
Author: User
Zookeeper

Question: Click to open the link

An array contains three operations. The first is the interval [a, B], where each number is multiplied. The second is the interval [a, B] where each number is added to C, the third is to query the sum of the [a, B] range and touch p.

The two operations cannot simply be used to pass down the mark. Each time a multiplication mark is passed, the addition mark must be multiplied at the same time. For example, an addition mark add must be provided for a certain interval, and then a multiplication mark Mul must be entered.

The result is (x + Add) * Mul = x * Mul + Add * Mul. In this way, the downward mark is relatively independent. The recursive boundary is used to update the Mul of the node before the addition mark. When the Left and Right sons are down, add the son to the Mul of the current node.

Finally, let's talk about sum. For example, the addition mark X and multiplication mark y of the current node are added first, and then multiplied by Y, then the sum of the Left son is updated to (sum + x) * Y. Since the addition mark is updated when the multiplication mark is uploaded to the current node, x = x * y, so sum [O <1] = (length of the Left interval X) + sum [O <1] * Y.


/**************************************************************    Problem: 1798    User: __ElemenT    Language: C++    Result: Accepted    Time:4676 ms    Memory:10184 kb****************************************************************/ #include <cstdio>#define lson o<<1, l, m#define rson o<<1|1, m+1, rtypedef long long LL;const int maxn = 100005; int n, a, b, c, k, q;LL sum[maxn<<2], add[maxn<<2], mul[maxn<<2], p; void up(int o) {    sum[o] = (sum[o<<1]+sum[o<<1|1]) %p;} void build(int o, int l, int r) {    add[o] = 0, mul[o] = 1;    if(l == r) {        scanf("%lld", &sum[o]);        return;    }    int m = (l+r) >> 1;    build(lson);    build(rson);    up(o);} void down(int o, int len) {   // if(add[o] != 0 && mul[o] != 1) {        add[o<<1] = (add[o<<1] * mul[o] + add[o]) %p;        add[o<<1|1] = (add[o<<1|1] * mul[o] + add[o]) %p;        mul[o<<1] = mul[o<<1] * mul[o] %p;        mul[o<<1|1] = mul[o<<1|1] * mul[o] %p;        sum[o<<1] = (sum[o<<1] * mul[o] + add[o] * (len-(len>>1))) %p;        sum[o<<1|1] = (sum[o<<1|1] * mul[o] + add[o] * (len>>1)) %p;        add[o] = 0, mul[o] = 1;   // }} void update(int o, int l, int r, int op) {    if(a <= l && r <= b) {        if(op == 1) {            add[o] = add[o]*c %p;            mul[o] = mul[o]*c %p;            sum[o] = sum[o]*c %p;        } else {            add[o] = (add[o] + c) %p;            sum[o] = (sum[o] + (LL)c*(r-l+1)) %p;        }        return;    }    down(o, r-l+1);    int m = (l+r) >> 1;    if(a <= m) update(lson, op);    if(m < b ) update(rson, op);    up(o);} LL query(int o, int l, int r) {    if(a <= l && r <= b) return sum[o] %p;    down(o, r-l+1);    int m = (l+r) >> 1;    LL ans = 0;    if(a <= m) ans = query(lson);    if(m < b ) ans += query(rson);    return ans %p;} int main(){    scanf("%d%lld", &n, &p);    build(1, 1, n);    scanf("%d", &q);    while(q--) {        scanf("%d%d%d", &k, &a, &b);        if(k != 3) {            scanf("%d", &c);            update(1, 1, n, k);        } else printf("%lld\n", query(1, 1, n));    }    return 0;}

Bzoj 1798: [ahoi2009] seq maintenance sequence seq (hybrid operation of line tree multiplication addition)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.