Fenwick tree (update interval, a single point of inquiry)

 
N balloons in a row, from left to right are numbered 1,2,3 .... N. Given two integers each ab (a <= b), lele as they ride his "little Flying pigeon "brand electric car start from a balloon to balloon b turn to each balloon painted a color. But after N times lele I have forgotten the first balloon has been painted several times colors, and you can help him calculate each balloon is coated several colors?

Input each test case acts a first integer N, (N <= 100000) . The following N rows, each row comprising two integers ab (1 <= a <= b <= N).
When N = 0, the end of input. Output each test case output line, comprising N integers, the number of I, I represents the number of balloons is colored in total. Sample Input
3
1 1
2 2
3 3
3
1 1
1 2
1 3
0
Sample Output
1 1 1
3 2 1
#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;
typedef unsigned long long ull;
int fa[100009]  ;
long long  ans = 0 ;
int a[100009] , sum[10009];
int c[100009] ;
int n ;

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

void update(int x , int value)
{
    for(int i = x ; i <= n ; i += lowerbit(i))
    {
        c[i] += value ;
    }
}
int getsum(int x)
{
    int ans = 0 ;
    for(int i = x ; i > 0 ; i -= lowerbit(i))
    {
        ans += c[i];
    }
    return ans ;
}

int main()
{

    while(~scanf("%d" , &n) && n)
    {
        memset(c , 0 , sizeof(c));
        for(int i = 0 ; i < n ; i++)
        {
            int l , r ;
            scanf("%d%d" , &l , &r);
            update(l , 1);

            update(r+1 , -1);

        }
        for(int i = 1 ; i <= n ; i++)
        {
            if(i == 1) cout << getsum(i) ;
            else
            cout <<" " << getsum(i) ;
        }
        cout << endl ;

    }


    return 0 ;
}

 

Guess you like

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