[Template] Valley T3368 Tree Array template 2

Source: Internet
Author: User

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&GT;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

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.