Fenwick tree (single-point update, query interval)

A rival country C country's ongoing military exercises this time, the country's spy chief C Derek and his men began to busy Tidy up. A country coastline arranged along a line of the N engineering camps, Derek and Tidy task is to monitor the activities of these engineers camps. As a result of some kind of advanced monitoring tools, so the number of engineers in each camp C crystal clear grasp States, the number of engineers in each camp are likely to occur change, may increase or decrease the number of staff, but they can not escape C monitor the country.
CIA to study enemy tactics exactly what exercises, so Derek Tidy to keep the report a certain period of continuous engineering camp a total of how many people, such as Derek asked: "Tidy, immediately report the first three camps to many people of a total of 10 camps ! "Tidy will immediately begin to calculate the total number of this paragraph and report. But the number of enemy camps often change, and Derek asked each segment is different, so every time a Tidy had to go to a number of camps, soon exhausted, Derek to calculate the speed of the Tidy increasingly dissatisfied: "! you a dead fat boy, considered so slow, I fire you" Tidy thought: "you do the math yourself, which is really a tiring job I wish you fire me too!! "In desperation, Tidy had to call a computer expert to help Windbreaker, Windbreaker, said:"! dead fat boy, do you usually called multi-point acm title and see multi-point arithmetic book, now tasted the bitter fruit of it "Tidy said:" I admitted the mistake ... "but Windbreaker has hung up the phone. Tidy very upset, so he really will count crash, the intelligent reader, you can write a program to help him finish the job? But if your program efficiency is not high enough, Tidy will still be scolded Derek's.

Input a first line integer T, T set of data expressed.
Data of the first line of each a positive integer N (N <= 50000), there are N represents the enemy camps engineer, the next positive integers with N, has the i ai positive integer representing the i-th ai start engineering camp personal (1 <= ai <= 50 ).
Next, each row having a command, command has four forms:
(. 1) ij of the Add, i, and j is a positive integer, denotes the i th camp increase personal j (j is not more than 30)
(2) Sub ij of, i and j It is a positive integer, denotes the i th camps reduce j individuals (j does not exceed 30);
(. 3) Query ij of, i and j are positive integers, i <= j, presents to ask the total number of i-th to j-th camps;
(4) end indicates the end, the last command that appears in each set of data;
up to 40,000 commands each data
output of the i-th group of data, first outputs "Case i:" and enter,
for each query Query output the total number of integer and a carriage return, indicating an inquiry segment, this number remains within int.
Sample Input
1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End 
Sample Output
1 Case: 
6 
33 
59 

Excellent blog https://blog.csdn.net/qq_39553725/article/details/76696168

#include <iostream>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include <stdio.h>
#include <string.h>
#define rep(i , n) for(int i = 0 ; i < (n) ; i++)
using namespace std;
const int N = 1000010 ;
long long ans = 0 , flag = 1;
int a[50009] ,  c[50009];
int m ;
char b[10];

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

int sum(int l , int r)
{
    int suml = 0 , sumr = 0 ;
    for(int i = l ; i > 0 ; i -= lowerbit(i))
    {
        suml += c[i];
    }
    for(int i = r ; i > 0 ; i-= lowerbit(i))
    {
        sumr += c[i];
    }
    return sumr - suml ;
}


void update(int x , int v)
{
    for(int i = x ; i <= m ; i += lowerbit(i))
    {
        c[i] += v ;
    }
}

int main()
{
    int n ;
    scanf("%d" , &n);
    int ans = 0 ;
    while(n--)
    {
        ans++ ;
        printf("Case %d:\n" , ans);
        scanf("%d" , &m);
        memset(a , 0 , sizeof(a));
        memset(c , 0 , sizeof(c));
        for(int i = 1 ; i <= m ; i++)
        {
            scanf("%d" , &a[i]);
            update(i , a[i]);
        }
        while(~scanf("%s" , b))
        {
            if(strcmp(b , "End") == 0)
                break ;
            else if(strcmp(b , "Query") == 0)
            {
                int l , r ;
                scanf("%d%d" , &l , &r);
                cout << sum(l - 1, r) << endl;
            }
            else if(strcmp(b , "Add") == 0)
            {
                int x , value ;
                scanf("%d%d" , &x , &value);
                update(x , value);
            }
            else
            {
                int x , value ;
                scanf("%d%d" , &x ,&value);
                update(x , -value);
            }
        }

    }


    return 0;
}

 

Guess you like

Origin www.cnblogs.com/nonames/p/11274483.html