Fenwick tree of understanding (prefix and and differential)

Two more -

There are gods to reflect the number of stars that explode outside the chain of title, I decided to stick to you about the map, if there is translation in one piece through increase of Fenwick tree that chapter, but also modified some Chinese grammar errors

 

This time to learn the tree line group, when the gods are learning kmp and hash, and my konjac Reach the ,,,,, So after I finished up string algorithms decided to fix up a data structure

This summary is mainly to see for himself, so the principle of the venue and this Fenwick tree

He high Juan guys blogs

This example mainly to

The first is a board question

P3374 [template] Fenwick tree 1

The problem is the board

Let's look at some of the operations of Fenwick tree

1. Add a value to a certain point

void update(int x,int y)
{
    while(x <= n){
        t[x] += y;
        x += lowbit(x);
    }
}

Consider Fenwick tree t [x] denotes the interval [x-lowbit (x) + 1, x] and among all the numbers, and x is a value t as all integer multiples of the array 2 are an integer from 2 to front contribution multiples, so we take every lowbit (x) will be added the value of x

If you can not read, you can look at this map

2. There is a range of query values

We certainly know the value in the prefix and [x, y] of this section is to sum (y) -sum (x-1) right, then we just need to write out the sum function on it

int sum(int x)
{
    int res = 0;
    while(x>0){
        res += t[x];
        x -= lowbit(x);
    }
    return res;
}

There is a code value quickly find lowbit (x) of

int lowbit(int x)
{
    return x & (-x);
}

This road board and we almost get away with title

Put the code

#include <iostream>
#include <cstdio>
using namespace std;
int n, m, t[500010];
int lowbit(int x)
{
    return x & (-x);
}
void update(int x,int y)
{
    while(x <= n){
        t[x] += y;
        x += lowbit(x);
    }
}
int sum(int x)
{
    int res = 0;
    while(x>0){
        res += t[x];
        x -= lowbit(x);
    }
    return res;
}
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1, v; i <= n; ++i){
        scanf("%d", &v);
        update(i,v);
    }
    for (int i = 1, temp, u, v; i <= m; ++i){
        scanf("%d%d%d", &temp, &u, &v);
        if(temp == 1)    update(u,v);
        else    printf("%d\n",sum(v) - sum(u - 1));
    }
    return 0;
}

Let's see the second board

P3368 [template] Fenwick tree 2

The title change of a question is asked, is the difference between asking a few numbers, so we add this element difference

The original meaning of p [x] represents becomes [, x x-lowbiut (x) + 1] The difference between the right end and the left end of the interval, as or in accordance lowbit (x) to the multiplication, so we can through this sum function to obtain the differential value of an arbitrary number two, then subtract able to get the required numbers, do not understand the words that I speak slowly come.

First sum and update method is the same, but because all of us for a number of intervals are added z, so the differential value within the range is unchanged, we are only left point of difference scores plus z, the right end point -z +1 differential value of the position on the line

update(l,z);
update(r + 1,-z);

To request a certain number, then this number directly to an all sum to obtain a difference value, i.e. sum (x)

The question on the bin it

#include <iostream>
#include <cstdio>
using namespace std;    
long long n, m,t[500010];
inline int lowbit(int x)
{
    return x & (-x);
}  
inline void update(int x,int y)
{
    while(x<=n){
        t[x] += y;
        x += lowbit(x);
    }
}
inline int sum(int x)
{
    int res = 0;
    while(x){
        res += t[x];
        x -= lowbit(x);
    }
    return res;
}
int main()
{
    scanf("%d%d", &n, &m);
    int now,past = 0;
    for (int  i = 1; i <= n; ++i){
        scanf("%d", &now);
        update(i,now - past);
        past = now;
    }
    for (int i = 1,k; i <= m;++i){
        scanf("%d", &k);
        if(k == 1){
            int x, y, z;
            scanf("%d%d%d", &x, &y, &z);
            update(x,z);
            update(y + 1,-z);
        }
        else{
            int x;
            scanf("%d", &x);
            printf("%d\n", sum(x));
        }
    }
    return 0;
}

There are a topic more fun

ural 1028 stars

 

The question of the general meaning is to give you the coordinates of the stars, the stars have k pieces at the bottom left of the stars (including being left and being down), then this is the k-class stars, ask each level the number of stars, according to the coordinates of the stars y-axis entered in ascending order, y-axis by the same x-axis entered in ascending

 

How should I say, I see a two-dimensional array coordinate certainly thought of it first, but the range of data in this place, and you certainly can not open p [15000] [15000], we look at this question, his input is great ah to ensure that the input must be entered before the stars included, so we can use a [x] represents the horizontal axis is the number of the stars of x, and then run again look at the statistics can be spicy

The Code

This place has a pit, that is, the coordinates of the stars may be (0,0) but when we ran the prefix and because there lowbit (x) operation, the subscript must start at 1, so we each read all incoming x + +, so not only does not affect the final result, but it does not pan

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
using namespace std;
 
int c[32010];
int level[32010];
 
//求2的K次幂
int lowbit(int t)
{
    return t&(-t);
}
//更新树状数组
void update(int t)
{
    while(t<32010)
    {
        ++c[t];
        t+=lowbit(t);
    }
}
//获取前N项和
int getSum(int t)
{
    int sum = 0;
    while(t>0)
    {
        sum+=c[t];
        t-=lowbit(t);
    }
    return sum;
}
int main()
{
    int n;
    int x;
    int y;
    int i;
    int sum;
 
    scanf("%d",&n);
 
    memset(c,0,sizeof(c));
    memset(level,0,sizeof(c));
 
    for(i = 0;i<n;i++)
    {
        scanf("%d%d",&x,&y);
        ++x;//星星的左边可以从0开始,但是update函数的参数却不能是0,所有向后移一位
        update(x);
        sum = getSum(x);
        ++level[sum];
    }
    for(i = 0;i<n;i++)
    {
        printf("%d\n",level[i+1]);
    }
    return 0;
}

ok 完事~

Guess you like

Origin www.cnblogs.com/this-is-M/p/11082874.html