Tree-shaped Array template

Source: Internet
Author: User
Tags arrays min

Turn to God: http://blog.csdn.net/acmmmm/article/details/9850687

One-dimensional array form

int c[n], MAXN;  
inline int lowbit (int x) {return x& (-X);}  
void change (int i, int x)//i point increment is x  
{while  
    (I <= maxn)  
    {  
        C[i] + = x;  
        i + = Lowbit (i);  
    }  
}  
int sum (int x) {//Interval sum [1,x]  
    int ans = 0;  
    for (int i = x; I >= 1; I-= Lowbit (i))  
        ans + = c[i];  
    return ans;  
}  

Find the interval maximum

struct tree{//manual initialization of num array int lowbit (int x) {return x& (-X);}  
    int a[100005], num[100005], MAXN;  
        void init (int len) {maxn = Len;  
            for (int i = 1; I <= maxn; i++) {a[i] = Num[i];  
        for (int j = 1; J < Lowbit (i); J <<= 1) a[i] = min (A[i], a[i-j]);  
        }} void Updata (int pos, int val) {Num[pos] = val;  
            for (int i = pos; I <= maxn; i + = Lowbit (i)) {a[i] = val;  
        for (int j = 1; J < Lowbit (i); J <<= 1) a[i] = min (A[i], a[i-j]);  
    } return;  
        } int query (int l, int r) {int ans = num[r];  
            while (true) {ans = min (ans, num[r]);  
            if (r = = L) break;  
        for (R-= 1; r-l >= lowbit (R); R-= Lowbit (r)) ans = min (ans, a[r]);  
    } return ans;   }}tree;

Change paragraph to section Template:

const int N = 4e5 + 100;  
    Template<class t> struct tree{T c[2][n];  
    int MAXN;  
    void init (int x) {maxn = x+10; memset (c, 0, sizeof C);  
    } inline int lowbit (int x) {return x&-x;}  
        T sum (t *b, int x) {T ans = 0;  
        if (x = = 0) ans = b[0];  
        while (x) ans + = b[x], X-= Lowbit (x);  
    return ans;  
        } void Change (t *b, int x, t value) {if (x = = 0) b[x] + = value, x + +;  
    while (x <= maxn) b[x] + = value, x + = Lowbit (x);  
    } T get_pre (int r) {return sum (c[0], R) * r + sum (c[1], R);  
        } void Add (int l, int r, T value) {//Interval weighted change (c[0], L, value);  
        Change (C[0], R + 1,-value);  
        Change (c[1], L, Value * (-L + 1));  
    Change (C[1], R + 1, value * r);  
    } T get (int l, int r) {//Interval sum return Get_pre (R)-Get_pre (L-1);  
}  
};  
Tree<ll> x, y; 

When used in reverse:

int c[n], MAXN;  
inline int lowbit (int x) {return x& (-X);}  
int sum (int i)//single point update I point to x  
{  
    int ans = 0;  
    while (i <= maxn)  
    {  
        ans + = c[i];  
        i + = Lowbit (i);  
    }  
    return ans;  
}  

void change (int x, int value) {//[1,x] increment is value for  
    (int i = x; I >= 1; I-= Lowbit (i))  
        c[i] + = value;  
}  

To reverse the number of tree-like arrays:
4 3 1 2
i = 1: Insert 4:update (4,1), SUM (4) returns 1, then current is greater than 4 for i-1 = 0;
i = 2: Insert 3:update (3,1), SUM (3) returns 1, then current is greater than 3 for i-1 = 1;
i = 3: Insert 1:update, SUM (1) returns 1, then the current is greater than 1 for i-1 = 2;
i = 4: Insert 2:update (2,1), SUM (2) returns 2, then current is greater than 2 for i-2 = 2;

Number of reverse #include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> using  
namespace Std;  
const int n=1000005;  
#define LL __int64 ll Cal[n],res[n],ans,ros[n];  
LL N,MAXN,MINM;  
int lowbit (int x) {return x&-x;}  
    ll getsum (int x) {ll s=0;  
    for (; x>0;s+=cal[x],x-=lowbit (x));  
return s;  
} void Update (int x,int value) {for (; X<=maxn;cal[x]+=value,x+=lowbit (x));  
    } int find (int value) {int l=1,r=maxn,mid;  
        while (l<=r) {mid= (l+r) >>1;  
        if (Ros[mid]==value) return mid;  
        else if (ros[mid]>value) r=mid-1;  
    else l=mid+1;  
} return-1;  
    } int main () {int i,j,k;  
    LL Total;  
            while (~SCANF ("%d", &n)) {for (i=1;i<=n;i++) {scanf ("%i64d", &res[i]);  
        Ros[i]=res[i];  
        } memset (Cal,0,sizeof (CAL));  
        Sort (ros+1,ros+n+1); for (j=1,i=2;i<=n;i++) {if (ros[i]!=ros[j]) ros[++j]=ros[i];  
        } maxn=j;  
            for (i=1,total=0;i<=n;i++) {k=find (res[i]);  
            Update (k,1);  
        Total+=i-getsum (k);  
    } printf ("%i64d\n", total);  
} return 0; }/* 3 3 1 2 ans=2 */

Two-dimensional tree arrays support sub-block operations on matrices, single-point operations, and sum of sub-blocks

const int max=1200;  
int C[max][max];  
int n;  
int lowbit (int t) {return t& (-T);}  
    int Sum (int endx,int Endy)//summation matrix [1,1]-[endx,endy] and {int sum=0;  
    int Temp=endy;  
            while (endx>0) {endy=temp;//while (endy>0) {Sum+=c[endx][endy];  
        Endy-=lowbit (Endy);  
    } endx-=lowbit (EndX);  
} return sum;  
    } void Add (int addx,int addy,int num)//add num {int temp=addy to [x, y] position;  
        while (Addx <=n) {addy=temp;  
            while (addy<=n) {c[addx][addy]+=num;  
        Addy+=lowbit (Addy);  
    } addx+=lowbit (ADDX); }} int getsum (int l,int b,int r,int t)//ask for [l,b]-[r,t] and if Point is Getsum (x,y,x,y) {return Sum (r,t)-sum (r,b-1)-S  
Um (l-1,t) +sum (l-1,b-1); } void Updat (int x,int y,int val) {//Update [1,1]-[x,y] interval points all +val for (; x<=n;x+=lowbit (x)) fo R (int i=y;i<=N;i+=lowbit (i)) c[x][i]+=val;  
            } scanf ("%d%d%d%d", &x1,&y1,&x2,&y2);
            x2++,y2++;  
            Updat (x1, y1, 1);  
            Updat (x2, Y2,-1);  
            Updat (x1, y2,-1);   Updat (x2, Y1,-1);
Related Article

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.