Title Description Description
Give you the number of n, there are two operations:
1: Add x for all numbers of interval [a, b]
2: Ask for the number of intervals [A, b] and.
Enter a description input Description
The first line is a positive integer n, and the next n rows n integers,
Then the next positive integer q, each line represents the number of operations,
If the first number is 1, followed by 3 positive integers,
Indicates that each number in the interval [a, b] is increased by x, if it is 2,
Represents the number of the and of the action 2 ask interval [A, b].
Pascal player please do not use READLN to read in
outputs description output Description
One answer for each query output line
sample input to sample
3
1
2
3
2
1 2 3 2
2 2 3
sample output Sample outputs
9
Data Size & Hint
Data range
1<=n<=200000
1<=q<=200000
The problem: The three problems with the same code to change the main function can be AC (this is clearly a problem ok ╭ (╯^╰) ╮), the simplest segment tree template.
The code is as follows:
#include <cstdio> #include <cstring> #include <iostream> using namespace std;
typedef long Long LL;
const LL maxn=233333;
int XL[MAXN];
struct JGT {//int l,r;
ll Sum,add;
}HAH[MAXN*4];
void build (int p,int l,int R)//build a line segment tree 233 {hah[p].l=l;
Hah[p].r=r;
if (l==r) {hah[p].sum=xl[l];
return;
} int mid= (L+R)/2;
Build (P*2,l,mid);
Build (p*2+1,mid+1,r);//Line tree is a binary tree 233 hah[p].sum=hah[p*2].sum+hah[p*2+1].sum;
} void spread (int p)//to devolve the tag {if (Hah[p].add) {hah[p*2].sum+= (hah[p*2].r-hah[p*2].l+1) *hah[p].add;
hah[p*2+1].sum+= (hah[p*2+1].r-hah[p*2+1].l+1) *hah[p].add;
Hah[p*2].add+=hah[p].add;
Hah[p*2+1].add+=hah[p].add;
Hah[p].add=0;
}} ll ask (int p,int l,int R)//interval query {if (l<=hah[p].l&&hah[p].r<=r) {return hah[p].sum;
} spread (P);//decentralization marks ll ans=0;
int mid= (HAH[P].R+HAH[P].L)/2;
if (l<=mid) ans+=ask (p*2,l,r); if (mId<r) Ans+=ask (p*2+1,l,r);
return ans;
} void Change (int p,int l,int r,ll x)//For each modification we make a tag that is evaluated only when needed (L<=HAH[P].L&&HAH[P].R<=R)
{hah[p].sum+= (LL) (hah[p].r-hah[p].l+1) *x;
Hah[p].add+=x;
return;
} spread (P);//decentralization Mark int mid= (HAH[P].R+HAH[P].L)/2;
if (l<=mid) change (p*2,l,r,x);
if (mid<r) change (p*2+1,l,r,x);//Continue to modify child nodes hah[p].sum=hah[p*2].sum+hah[p*2+1].sum;
} int main () {ll n,q;
scanf ("%lld", &n);
for (int i=1;i<=n;i++) scanf ("%d", &xl[i]);
Build (1,1,n);//Achievements scanf ("%lld", &q);
for (int i=1;i<=q;i++) {int ss;
scanf ("%d", &SS);
if (ss==1) {int A, B;
ll X;
scanf ("%d%d%lld", &a,&b,&x);
Change (1,a,b,x);//Modify} if (ss==2) {int A, B;
scanf ("%d%d", &a,&b);
printf ("%lld\n", Ask (1,a,b));//Query}} return 0; }