P3373 [TEMPLATE] adds the summation interval of the Line Segment tree 2 and the p3373 Line Segment

Source: Internet
Author: User

P3373 [TEMPLATE] adds the summation interval of the Line Segment tree 2 and the p3373 Line Segment
Description

For example, if you know a sequence, you need to perform the following two operations:

1. Add x to each number in a certain range

2. Multiply each number in a certain range by x

3. Obtain the sum of each number in a certain range.

Input/Output Format Input Format:

The first row contains three integers, N, M, and P, indicating the number of numbers in the sequence, the total number of operations, and the modulus.

The second row contains N integers separated by spaces. the I-th digit indicates the initial value of the I-th Column.

Each row in the next M row contains 3 or 4 integers, indicating an operation, as shown below:

Operation 1: Format: 1 x y k meaning: multiply each number in the range [x, y] by k

Operation 2: Format: 2 x y k meaning: Add k to each number in the range [x, y]

Operation 3: Format: 3 x y Meaning: Result of P modulo operation for each number in the output range [x, y]

Output Format:

The output contains several line integers, that is, the result of all operations 3.

Input and Output sample Input example #1:
5 5 381 5 4 2 32 1 4 13 2 51 2 4 22 3 5 53 1 4
Output sample #1:
172
Description

Time-Space limit: 1000 ms, 128 M

Data scale:

For 30% of data: N <= 8, M <= 10

For 70% of data: N <= 1000, M <= 10000

For 100% of data: N <= 100000, M <= 100000

(Data has been enhanced ^_^)

Example:

Therefore, the output should be 17, 2 (40 mod 38 = 2)

 

According to the addition and subtraction principle ,,

It seems that this can only be explained,

Put the multiplication mark first

Add a tag

Note that ll and rr remain unchanged during query.

  1 #include<iostream>  2 #include<cstdio>  3 #include<cstring>  4 #include<cmath>  5 #define LLI long long   6 using namespace std;  7 const LLI MAXN=400001;  8 LLI read(LLI & n)  9 { 10     char p='+';LLI x=0; 11     while(p<'0'||p>'9') 12         p=getchar(); 13     while(p>='0'&&p<='9') 14     x=x*10+p-48,p=getchar(); 15     n=x; 16 } 17 LLI n,m,mod,wl,wr,wv,ans; 18 struct node 19 { 20     LLI l,r,w,fc,fj; 21 }a[MAXN]; 22 void update(LLI k) 23 { 24     a[k].w=(a[k<<1].w+a[k<<1|1].w)%mod; 25 } 26 void build_tree(LLI k,LLI ll,LLI rr) 27 { 28     a[k].l=ll;a[k].r=rr; 29     a[k].fc=1; 30     a[k].fj=0; 31     if(a[k].l==a[k].r) 32     { 33         read(a[k].w); 34         return ; 35     } 36     LLI mid=(ll+rr)/2; 37     build_tree(k<<1,ll,mid); 38     build_tree(k<<1|1,mid+1,rr); 39     update(k); 40 } 41 void pushdown(LLI k,LLI ll,LLI rr,LLI mid) 42 { 43     a[k<<1].w*=a[k].fc;a[k<<1|1].w*=a[k].fc; 44     a[k<<1].w+=a[k].fj*(mid-ll+1);a[k<<1|1].w+=a[k].fj*(rr-mid); 45     a[k<<1].fc*=a[k].fc;a[k<<1|1].fc*=a[k].fc; 46     a[k<<1].fj*=a[k].fc;a[k<<1|1].fj*=a[k].fc; 47     a[k<<1].fj+=a[k].fj;a[k<<1|1].fj+=a[k].fj; 48     a[k].fc=1;a[k].fj=0; 49     a[k<<1].w%=mod;a[k<<1].fj%=mod;a[k<<1].fc%=mod; 50     a[k<<1|1].w%=mod;a[k<<1|1].fj%=mod;a[k<<1|1].fc%=mod; 51 } 52 void interval_add(LLI k,LLI ll,LLI rr,LLI v) 53 { 54     if(a[k].l>rr||a[k].r<ll) 55         return ; 56     if(ll<=a[k].l&&rr>=a[k].r) 57     { 58         a[k].w=(a[k].w+v*(a[k].r-a[k].l+1))%mod; 59         a[k].fj=(a[k].fj+v)%mod; 60         return ; 61     } 62     LLI mid=(a[k].l+a[k].r)/2; 63     pushdown(k,a[k].l,a[k].r,mid); 64     //if(ll<=mid) 65     interval_add(k<<1,ll,rr,v); 66     //if(rr>mid) 67     interval_add(k<<1|1,ll,rr,v); 68     update(k); 69 } 70 void interval_mul(LLI k,LLI ll,LLI rr,LLI v) 71 { 72     if(a[k].l>rr||a[k].r<ll) 73         return ; 74     if(ll<=a[k].l&&rr>=a[k].r) 75     { 76         a[k].w*=v%mod; 77         a[k].fc*=v%mod; 78         a[k].fj*=v%mod; 79         return ; 80     } 81     LLI mid=(a[k].l+a[k].r)/2; 82     pushdown(k,a[k].l,a[k].r,mid); 83     //if(ll<=mid) 84     interval_mul(k<<1,ll,rr,v); 85     //if(rr>mid) 86     interval_mul(k<<1|1,ll,rr,v); 87     update(k); 88 } 89 void interval_sum(LLI k,LLI ll,LLI rr) 90 { 91     if(a[k].l>rr||a[k].r<ll) 92         return ; 93     if(ll<=a[k].l&&rr>=a[k].r) 94     { 95         ans=(ans+a[k].w)%mod; 96         return ; 97     } 98     LLI mid=(a[k].l+a[k].r)/2; 99     pushdown(k,a[k].l,a[k].r,mid);100     //if(ll<=mid)101         interval_sum(k<<1,ll,rr);102     //if(rr>mid)103         interval_sum(k<<1|1,ll,rr);104 }105 int main()106 {107     read(n);read(m);read(mod);108     build_tree(1,1,n);109     for(LLI i=1;i<=m;i++)110     {111         LLI p;112         read(p);113         if(p==1)114         {115             read(wl);read(wr);read(wv);116             interval_mul(1,wl,wr,wv);117         }118         else if(p==2)119         {120             read(wl);read(wr);read(wv);121             interval_add(1,wl,wr,wv);122         }123         else if(p==3)124         {125             ans=0;126             read(wl);read(wr);127             interval_sum(1,wl,wr);128             //cout<<ans%mod<<endl;129             printf("%lld\n",ans%mod);130         }131     }132     return 0;133 }

 

 

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.