---restore content starts---
These days to Zhejiang province to choose the bottom (Orz I was Konjac konjac), and then by the way to review the tree-like array
The tree-like array, in fact very well understood, mainly is the lowbit () operation of the ingenious
A tree array is a very elegant data structure. You might consider using a tree array when you want to make frequent changes to the array elements while also frequently querying the sum of any interval elements in the arrays.
In other words, the most basic application of a tree-like array:
For an array, if there are multiple operations, each operation has two kinds: 1, modify the value of an element in the array, 2, sum, to find the sum of the array element A[1]+a[2]+...a[num].
The--km of the
In fact, the tree-like array to write a few of their own code is easy to understand, but also see here (km of the big)
1 //tree-array Templates2#include <bits/stdc++.h>3 using namespacestd;4 Const intmax=100000;5 inta[max+Ten],c[max+Ten],n,s[max+Ten];6 intLowbit (intx) {returnx& (-x);}7 //Modify: Modify the X element to change the x element to X+delta8 //O (LOGN)9 //support for single-point modification, interval update complexity O (NLOGN)Ten voidModifyintXintDelta) { One while(x<=N) { Ac[x]+=Delta; -x+=lowbit (x); - } the } - //Query : query [1,x] interval and, to query [x, y], you need to call [1,y]-[1,x] - //O (LOGN) - intCalintx) { + intret=0; - while(x!=0) { +ret+=C[x]; Ax-=lowbit (x); at } - returnret; - } - - intMain () { -scanf"%d",&n); in for(intI=1; i<=n;++i) { -scanf"%d",&a[i]); tosum[i]=sum[i-1]+A[i]; +c[i]=sum[i]-sum[i-lowbit (i)]; - } the intT; *scanf"%d",&T); $ while(t--) {Panax Notoginseng //Modify Query Online - //The code here is self-filled the } + return 0; A}
View Code
The application of tree-like array
HDU 1166 Enemy Soldiers
Pure template title, very simple. Here to say, my code may be indented in the main section is a bit messy, because HDU often for multiple sets of data, I was to write a good treearray before adding a multi-data test.
1# include <iostream>2# include <stdio.h>3# include <string.h>4 using namespacestd;5 inta[50010], c[50010], N, sum[50010]={};6 intLowbit (intx) {returnx& (-x);}7 voidMdfintPintdel) {8 while(p<=N) {9c[p]+=del;Tenp+=lowbit (p); One } A } - intCaintDG) { - intR=0; the while(dg!=0) { -r+=C[DG]; -dg-=Lowbit (DG); - } + returnR; - } + intMain () {intt,ft; Ascanf"%d", &t); ft=T; at while(t--) { -printf"Case %d:\n", ft-T); -scanf"%d", &n); - for(intI=1; i<=n;++i) { -scanf"%d", &a[i]); -sum[i]=sum[i-1]+A[i]; inc[i]=sum[i]-sum[i-lowbit (i)]; - } to //for (int i=1;i<=n;++i) printf ("%d", C[i]); + while(1) { - Charst[Ten];intTA,TB; thescanf"%s", ST); * //printf ("%s%d%d", ST,TA,TB); $ if(STRCMP (St,"End")==0) Break;Panax Notoginsengscanf"%d%d",&ta,&TB); - if(STRCMP (St,"ADD")==0) { the MDF (TA,TB); + } A Else if(STRCMP (St,"Sub")==0) { theMDF (ta,-1*TB); + } - Else if(STRCMP (St,"Query")==0) { $ if(TA>TB) {intp=ta;ta=tb;tb=p;} $printf"%d\n", CA (TB)-ca (ta-1)); - } - }} the return 0; -}
View Code
All right, I'm going to write this tree-like array for the time being, and I'm still too weak, Orz. Ben
---restore content ends---
Tree-like array template trees array