1080 line tree exercises
time limit: 1 sspace limit: 128000 KBtitle level: Diamonds DiamondTitle Description
Description
A row of n squares, beginning with an integer in each lattice. Some questions and modifications are presented dynamically: the form of questioning is the summation of all elements in a particular sub-interval [a, b]; the modified rule is to specify a lattice x, plus or minus a specific value, a. You are now asked to make the right answer to each question. 1≤n<100000, the total number of questions and modifications m<10000.
Enter a description
Input Description
The input file first behaves as an integer n, followed by n rows n integers, representing the original integers in the lattice. followed by a positive integer m, followed by M-line, the M-query, the first integer to indicate the query code, the number 1 indicates an increase, the following two numbers x and a for the value on position x to increase a, ask the code 2 for the interval sum, followed by two integers for A and B, representing the interval between [A, a] and
Output description
Output Description
A total of M lines, each integer
Sample input
Sample Input
6
4
5
6
2
1
3
4
1 3 5
2 1 4
1 1 9
2 2 6
Sample output
Sample Output
22
22
Data range and Tips
Data Size & Hint
1≤n≤100000, m≤10000.
Code:
#include <iostream>using namespacestd; #include<cstdio>#defineINF 100001Long Long intC[inf];intn,m,a,b,x;intLowbit (intk) { returnk& (-k);}voidAddintKintnum) { while(k<=N) {c[k]+=num; K+=lowbit (k);/*To add num to himself, add num to the father that contains it.*/ }}voidinput () {scanf ("%d",&N); intp; for(intI=1; i<=n;++i) {scanf ("%d",&p); Add (i,p);/*The tree array does not have to separate space to store the original array, and when the data is entered, it is added to the tree .*/ }}Long Long intSum (intK/*to sum the 1--k intervals,*/{ Long Long intsum=0; while(k) {sum+=C[K];/*start adding from that node, and then find his child node*/k-=Lowbit (k); } returnsum;}intMain () {input (); scanf ("%d",&m); while(m--) {scanf ("%d",&x); if(x==1) {scanf ("%d%d",&a,&b); Add (A, b); } Else{scanf ("%d%d",&a,&b); printf ("%lld\n", Sum (b)-sum (A-1));/*This is the method of finding any interval and*/ } } return 0;}
View Code
Codevs 1080 Line tree exercises--with a tree-like array