Poj 3468 a simple problem with Integers

Source: Internet
Author: User

I have to say that the line segment tree is too scum. I found a piece of great god's code, and I will study it carefully later.

The reprinted link cannot be specified because the source cannot be found. Please forgive me.

 1 #include <cstdio> 2 #include <algorithm> 3 using namespace std; 4  5 #define lson l , m , rt << 1 6 #define rson m + 1 , r , rt << 1 | 1 7 #define LL long long 8 const int maxn = 111111; 9 LL add[maxn<<2];10 LL sum[maxn<<2];11 void PushUp(int rt) {12     sum[rt] = sum[rt<<1] + sum[rt<<1|1];13 }14 void PushDown(int rt,int m) {15     if (add[rt]) {16         add[rt<<1] += add[rt];17         add[rt<<1|1] += add[rt];18         sum[rt<<1] += add[rt] * (m - (m >> 1));19         sum[rt<<1|1] += add[rt] * (m >> 1);20         add[rt] = 0;21     }22 }23 void build(int l,int r,int rt) {24     add[rt] = 0;25     if (l == r) {26         scanf("%lld",&sum[rt]);27         return ;28     }29     int m = (l + r) >> 1;30     build(lson);31     build(rson);32     PushUp(rt);33 }34 void update(int L,int R,int c,int l,int r,int rt) {35     if (L <= l && r <= R) {36         add[rt] += c;37         sum[rt] += (LL)c * (r - l + 1);38         return ;39     }40     PushDown(rt , r - l + 1);41     int m = (l + r) >> 1;42     if (L <= m) update(L , R , c , lson);43     if (m < R) update(L , R , c , rson);44     PushUp(rt);45 }46 LL query(int L,int R,int l,int r,int rt) {47     if (L <= l && r <= R) {48         return sum[rt];49     }50     PushDown(rt , r - l + 1);51     int m = (l + r) >> 1;52     LL ret = 0;53     if (L <= m) ret += query(L , R , lson);54     if (m < R) ret += query(L , R , rson);55     return ret;56 }57 int main() {58     int N , Q;59     //freopen("D:acm.txt","r",stdin);60     scanf("%d%d",&N,&Q);61     build(1 , N , 1);62     while (Q --) {63         char op[2];64         int a , b , c;65         scanf("%s",op);66         if (op[0] == ‘Q‘) {67             scanf("%d%d",&a,&b);68             printf("%lld\n",query(a , b , 1 , N , 1));69         } else {70             scanf("%d%d%d",&a,&b,&c);71             update(a , b , c , 1 , N , 1);72         }73     }74     return 0;75 }

 

Poj 3468 a simple problem with Integers

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.