Codevs-4919 Segment Tree Exercise 4 (interval plus a value and find the number of intervals divisible by K, segment tree + array maintenance)

Source: Internet
Author: User

Give you the number of n, there are two operations

1: Add x to all numbers within the interval [A, a]

2: Query interval [A, b] can be divisible by 7 of the number

Enter a description Input Description

The first line is a positive integer n, the next n rows n integers, and then a positive integer q, which represents the number of operations. The next Q line is a number of integers per line. If the first number is add, followed by 3 positive integer a,b,x, indicates that each number in the interval [a, a] increases by X, if it is count, indicating that the statistic interval [a, b] can be divisible by 7

Output description Output Description

One answer for each query output line

Sample input Sample Input

3 2 3 46count 1 3count 1 2add 1 3 2count 1 3add 1 3 3count 1 3

Sample output Sample Output

0

0

0

1

Data range and Tips Data Size & Hint

10%:1<n<=10,1<q<=10

30%:1<n<=10000,1<q<=10000

100%:1<n<=100000,1<q<=100000

The puzzle: and the ordinary line segment of the tree interval modification and interval summation is similar, but we use an array here to store each interval on the number of k after the sum of the number of each, the other operations and ordinary line tree is the same;

Reference code:

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <cstdlib>6#include <algorithm>7#include <queue>8#include <stack>9#include <Set>Ten#include <vector> One#include <map> A using namespacestd; -typedefLong LongLL; - Const intinf=0x3f3f3f3f; the ConstLL inf=0x3f3f3f3f3f3f3f3fll; - Const intmaxn=1e5+Ten; - intn,q,c[maxn],a,b,x,temp[7]; - stringstr; +  - structnode{ +     intl,r,tag,mod[7]; A} tree[maxn<<2]; at  - voidBuildintPosintLintR) - { -tree[pos].l=l,tree[pos].r=r,tree[pos].tag=0; -      for(intI=0;i<7; i++) tree[pos].mod[i]=0; -     if(l==R) in     { -tree[pos].mod[c[l]%7]=1; to         return ; +     } -     intMid= (TREE[POS].L+TREE[POS].R) >>1; theBuild (pos<<1, l,mid); *Build (pos<<1|1, mid+1, R); $      for(intI=0;i<7; i++) tree[pos].mod[i]=tree[pos<<1].mod[i]+tree[pos<<1|1].mod[i];Panax Notoginseng } -  the voidChangeintPosintx) + { A      for(intI=0;i<7; i++) temp[(i+x)%7]=Tree[pos].mod[i]; the      for(intI=0;i<7; i++) tree[pos].mod[i]=Temp[i]; + } -  $ voidPushup (intPOS) $ { -      for(intI=0;i<7; i++)  -tree[pos].mod[i]=tree[pos<<1].mod[i]+tree[pos<<1|1].mod[i]; the } - Wuyi voidPushdown (intPOS) the { -     if(TREE[POS].L==TREE[POS].R)return ; Wutree[pos<<1].tag+=Tree[pos].tag; -tree[pos<<1|1].tag+=Tree[pos].tag; AboutChange (pos<<1, Tree[pos].tag), Change (pos<<1|1, Tree[pos].tag); $tree[pos].tag=0; - } -  - voidUpdateintPosintLintRintx) A { +     if(Tree[pos].tag) pushdown (POS); the     if(tree[pos].l==l&&tree[pos].r==R) -     { $tree[pos].tag+=X;change (pos,x); the         return ; the     } the     intMid= (TREE[POS].L+TREE[POS].R) >>1; the     if(r<=mid) Update (pos<<1, l,r,x); -     Else if(l>=mid+1) Update (pos<<1|1, l,r,x); in     ElseUpdate (pos<<1, l,mid,x), update (pos<<1|1, mid+1, r,x); the pushup (POS); the } About  the intQueryintPosintLintR) the { the     if(Tree[pos].tag) pushdown (POS); +     if(TREE[POS].L==L&AMP;&AMP;TREE[POS].R==R)returntree[pos].mod[0]; -     intMid= (TREE[POS].L+TREE[POS].R) >>1; the     if(R<=mid)returnQuery (pos<<1, l,r);Bayi     Else if(l>=mid+1)returnQuery (pos<<1|1, l,r); the     Else returnQuery (pos<<1, L,mid) +query (pos<<1|1, mid+1, R);  the } -  - intMain () the { theIos::sync_with_stdio (false); theCin>>N; the      for(intI=1; i<=n;i++) cin>>C[i]; -Build1,1, n); theCin>>Q; the      while(q--) the     {94Cin>>str; the         if(str[0]=='a') the         { theCin>>a>>b>>x;98Update1, a,b,x); About         } -         Else if(str[0]=='C')101         {102Cin>>a>>b;103Cout<<query (1, b) <<Endl;104         } the     }106     107     return 0;108}
View Code

Codevs-4919 Segment Tree Exercise 4 (interval plus a value and find the number of intervals divisible by K, segment tree + array maintenance)

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.