Tag: vector calculates date using log for CTO return span
1. For interval modification:
Directly modify the array c[], that is, n times add, will definitely tle;
A new array is introduced here: Addv[],addv[i] refers to all elements of the tree with node I as the root plus addv[i].
Set each number in the interval [A, a,] plus x,
Then just from B to the left, the corresponding addv[] plus x, and then from A-1 to the left, the multi-modified node addv[] minus X.
2. For single point queries:
Set the query node I, the original value is:
(The and of all elements of the tree with node I as root)-(and all nodes except node I in this tree);
Then, from node I to the root of the tree, the traversed node's addv[] value is added to the previous computed nodes I, the end result is the value of node I now.
The code is as follows:
1#include <cstdio>2#include <iostream>3#include <cstring>4#include <ctime>5#include <cstdlib>6#include <algorithm>7#include <string>8#include <stack>9#include <queue>Ten#include <vector> One using namespacestd; A voidAddint,int); - voidUpdateint,int,int); - intQueryint); the intc[500010]; - intaddv[500010]; - intn,m; - inti,t; + intF; - intx,y,k; + intMain () { Ascanf"%d%d",&n,&m); at for(i=1; i<=n;i++){ -scanf"%d",&t); - Add (i,t); - } - for(i=1; i<=m;i++){ -scanf"%d",&f); in if(f==1){ -scanf"%d%d%d",&x,&y,&k); to Update (X,Y,K); + } - Else{ thescanf"%d",&x); *printf"%d\n", query (x)); $ }Panax Notoginseng } - return 0; the } + voidAddintPintx) { A while(p<=N) { thec[p]+=x; +p+=p&-p; - } $ } $ voidUpdateintAintBintx) { - while(b>=a) {//interval plus -addv[b]+=x; theb-=b&-b; - }Wuyia--; the while(A>B) {//interval minus -addv[a]-=x; Wua-=a&-A; - } About } $ intQueryintp) { - intp1=p-(p&-p); - intp2=p-1; - intt=C[p]; A while(p2>p1) { +t-=C[P2]; thep2-=p2&-P2; -}//Calculate node Original value $ while(p<=N) { thet+=Addv[p]; thep+=p&-p; the}//Append addv[] the returnT; -}
[Template] Valley T3368 Tree Array template 2