[TEMPLATE] basic operations of zkw line segment tree and zkw line segment renewal

Source: Internet
Author: User

[TEMPLATE] basic operations of zkw line segment tree and zkw line segment renewal

I sorted out the basic operations of the zkw line segment tree:

1. Modify the element value: for x and y, change the value of a [x] to y;

2. query element value: returns the value of a [x] given x;

3. increase or decrease in the interval: given l, r, x, will be from a [l] to a [r] (including al and ar), that is, the interval [l, add x to each value in r;

4. interval summation: given l and r, the output ranges from a [l] to a [r] (including al and ar), that is, the sum of each element in the range [l, r.

#include<cstdio>#include<cstdlib>#include<cctype>#include<cstring>#define LL long longLL x,a,b,c,n,m,pre=1;LL num[10000000],numm[10000000];inline int read(){    int x=0,f=1;    char ch=getchar();    while(ch<'0'||ch>'9')    {        if(ch=='-')            f=-1;        ch=getchar();            }    while(ch>='0'&&ch<='9')    {        x=x*10+ch-'0';        ch=getchar();    }    return x*f;}inline void add(int a,int b){num[a+=pre]+=b;numm[a]=(a-pre)*num[a];a>>=1;while(a){num[a]=num[a<<1]+num[(a<<1)+1];numm[a]=numm[a<<1]+numm[(a<<1)+1];a>>=1;}}inline LL query(int a,int b){LL ans=0;a+=pre-1;b+=pre+1;while(a^b^1){if(~a&1)ans+=num[a^1];if(b&1)ans+=num[b^1];a>>=1;b>>=1;}return ans;}inline LL nquery(int a,int b){LL ans=0;a+=pre-1;b+=pre+1;while(a^b^1){if(~a&1)ans+=numm[a^1];if(b&1)ans+=numm[b^1];a>>=1;b>>=1;}return ans;}int main(){int i;n=read();m=read();n=read();while(pre<=n)pre<<=1;for(i=pre+1;i<=pre+n;++i)num[i]=read();for(i=pre+n;i>pre;--i){num[i]-=num[i-1];numm[i]=(i-pre)*num[i];}for(i=pre-1;i>=1;--i){num[i]=num[i<<1]+num[(i<<1)+1];numm[i]=numm[i<<1]+numm[(i<<1)+1];}for(i=1;i<=m;++i){x=read();if(x==1){a=read(),b=read();add(a,b),add(a+1,-b);printf("-1\n");}if(x==2) { a=read();printf("%lld\n",query(1,a));}if(x==3){a=read(),b=read(),c=read();add(a,c),add(b+1,-c);printf("-1\n");}if(x==4){a=read(),b=read();printf("%lld\n",(query(1,b)*(b+1)-nquery(1,b))-query(1,a-1)*a+nquery(1,a-1));}}printf("\n");return 0;}
Therefore, CODEVS 1954 line segment tree was created.


Saffah's predecessors abused and cried 0.0

Please help me ..................

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.