poj 2352 hdu 2642 hdu 1556 poj 2155 树状数组

一维树状数组模板:

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

void add(int x,int val)//下标为x值增加val
{
    while(x<=n)//n是数组的规模,容易出错
    {
        c[x]+=val;
        x+=lowbit(x);
    }
}

int sum(int x)//求下标1~x的数组之和
{
    int ans=0;
    while(x>0)
    {
        ans+=c[x];
        x-=lowbit(x);
    }
    return ans;
}


主函数:
模板一:修改某个点,求某段区间的和
	int c[200010]//树状数组,a[200010]//普通数组;	memset(c,0,sizeof(c));//建立树状数组
        for(int i=1;i<=n;i++)//必须从1开始存入
        {
            scanf("%d",&a[i]);
            add(i,a[i]);
        }
	 cout<<sum(r)-sum(l-1)<<endl;//输出[l,r]之和
	
	add(z,b-a[z]);//修改a[z]=b
        a[z]=b;
模板2:修改某个区间,求某个点的值
memset(c,0,sizeof(c));
        for(int t=0;t<=n-1;t++)
        {
            int a,b;
            scanf("%d%d",&a,&b);//区间[a,b]增加1
            add(a,1);
            add(b+1,-1);
        }
cout<<sum(i)<<endl;//求i的值



模板1:

修改某个点,求某段区间的和


poj 2352

Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars. 

For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3. 

You are to write a program that will count the amounts of the stars of each level on a given map.

Input

The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate. 

Output

The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.

Sample Input

5
1 1
5 1
7 1
3 3
5 5

Sample Output

1
2
1
1
0

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
int n;
int c[32010];
int result[32010];
int lowbit(int x)
{
    return x&(-x);
}
void add(int x,int val)
{
    while(x<=32010)//注意数组的规模
    {
        c[x]+=val;
        x+=lowbit(x);
    }
}
int sum(int x)
{
    int ans=0;
    while(x>0)
    {
        ans+=c[x];
        x-=lowbit(x);
    }
    return ans;
}
int main()
{
    while(cin>>n)
    {
        memset(c,0,sizeof(c));
        memset(result,0,sizeof(result));
        for(int i=0;i<=n-1;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            int t=sum(++x);
            result[t]++;
            add(x,1);
        }
        for(int i=0;i<=n-1;i++)
            cout<<result[i]<<endl;
    }
    return 0;
}




模板2:

修改某个区间,求某个点的值


Problem Description
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
 

Input
每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
当N = 0,输入结束。
 

Output
每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。
 

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<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<string>
#include<algorithm>
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
int c[100010];
int n;
int lowbit(int x)
{
    return x&(-x);
}
void add(int x,int val)
{
    while(x<=n)
    {
        c[x]+=val;
        x+=lowbit(x);
    }
}

int getsum(int x)
{
    LL ans=0;
    while(x>0)
    {
        ans+=c[x];
        x-=lowbit(x);
    }
    return ans;
}
int main()
{
    while(cin>>n)
    {
        if(n==0)
            return 0;
        memset(c,0,sizeof(c));
        for(int t=0;t<=n-1;t++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            add(a,1);
            add(b+1,-1);
        }
        for(int i=1;i<=n-1;i++)
            cout<<getsum(i)<<" ";
        cout<<getsum(n)<<endl;
    }
    return 0;
}


二维树状数组模板

int lowbit(int x)
{
    return x&(-x);
}
void add(int x,int y,int val)//a[x][y]增加val
{
	for(int i=x;i<=n;i+=lowbit(i))
		for(int j=y;j<=n;j+=lowbit(j))
			c[i][j]+=val;
}

int getsum(int x,int y)//1~x,1~y之和
{
	int ans=0;
	for(int i=x;i>=1;i-=lowbit(i))
		for(int j=y;j>=1;j-=lowbit(j))
			ans+=c[i][j];
	return ans;
}

主函数:
模板1:修改某个点的值,求某个区域的和
int c[1010][1010]//树状数组,a[1010][1010]//普通数组;	memset(c,0,sizeof(c));//建立树状数组
        for(int i=1;i<=n;i++)表示个数
        {
            scanf("%d%d%d",&m,&n,&x);//a[m][n]=x
            add(m,n,x);
        }
区间[xmax,ymax]和:
getsum(xmax,ymax)-getsum(xmin-1,ymax)-getsum(xmax,ymin-1)+getsum(xmin-1,ymin-1);//查询[xmin,xmax],[ymin,ymax]之和

模板2:改变某个区域的值,求某个点的值

memset(c,0,sizeof(c));
改变左上角为(x1,y1),右上角(x2,y2)区域增加x
scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&x);
                add(x1,y1,1);
                add(x1,y2+1,1);
                add(x2+1,y1,1);
                add(x2+1,y2+1,1);
求点的值:cout<<getsum(x,y)<<endl;

模板1:
Problem Description
Yifenfei is a romantic guy and he likes to count the stars in the sky.
To make the problem easier,we considerate the sky is a two-dimension plane.Sometimes the star will be bright and sometimes the star will be dim.At first,there is no bright star in the sky,then some information will be given as "B x y" where 'B' represent bright and x represent the X coordinate and y represent the Y coordinate means the star at (x,y) is bright,And the 'D' in "D x y" mean the star at(x,y) is dim.When get a query as "Q X1 X2 Y1 Y2",you should tell Yifenfei how many bright stars there are in the region correspond X1,X2,Y1,Y2.

There is only one case.
 

Input
The first line contain a M(M <= 100000), then M line followed.
each line start with a operational character.
if the character is B or D,then two integer X,Y (0 <=X,Y<= 1000)followed.
if the character is Q then four integer X1,X2,Y1,Y2(0 <=X1,X2,Y1,Y2<= 1000) followed.
 

Output
For each query,output the number of bright stars in one line.
 

Sample Input
  
  
5 B 581 145 B 581 145 Q 0 600 0 200 D 581 145 Q 0 600 0 200
 

Sample Output
  
  
1 0

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#define LL long long
#define inf 0x3f3f3f3f
#define n 1006
using namespace std;
int c[1010][1010];
int visited[1010][1010];
int lowbit(int x)
{
    return x&(-x);
}
void add(int x,int y,int val)
{
	for(int i=x;i<=n;i+=lowbit(i))
		for(int j=y;j<=n;j+=lowbit(j))
			c[i][j]+=val;
}

int getsum(int x,int y)
{
	int ans=0;
	for(int i=x;i>=1;i-=lowbit(i))
		for(int j=y;j>=1;j-=lowbit(j))
			ans+=c[i][j];
	return ans;
}
int main()
{
    int m;
    cin>>m;
    memset(visited,0,sizeof(visited));
    memset(c,0,sizeof(c));
    while(m--)
    {
        char ch;
        cin>>ch;
        if(ch=='B')
        {
            int a,b;
            cin>>a>>b;
            a++;b++;
            if(!visited[a][b])
            {
                add(a,b,1);
                visited[a][b]=1;
            }
        }
        if(ch=='D')
        {
            int a,b;
            cin>>a>>b;
            a++;b++;
            if(visited[a][b])
            {
                add(a,b,-1);
                visited[a][b]=0;
            }
        }
        if(ch=='Q')
        {
            int x1,y1,x2,y2;
            cin>>x1>>x2>>y1>>y2;
            x1++;x2++;y1++;y2++;
            int xmax=max(x1,x2);
            int xmin=min(x1,x2);
            int ymax=max(y1,y2);
            int ymin=min(y1,y2);
            cout<<getsum(xmax,ymax)-getsum(xmin-1,ymax)-getsum(xmax,ymin-1)+getsum(xmin-1,ymin-1)<<endl;
        }
    }
    return 0;
}

模板2:poj 2155

Description

Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1 <= i, j <= N). 

We can change the matrix in the following way. Given a rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2), we change all the elements in the rectangle by using "not" operation (if it is a '0' then change it into '1' otherwise change it into '0'). To maintain the information of the matrix, you are asked to write a program to receive and execute two kinds of instructions. 

1. C x1 y1 x2 y2 (1 <= x1 <= x2 <= n, 1 <= y1 <= y2 <= n) changes the matrix by using the rectangle whose upper-left corner is (x1, y1) and lower-right corner is (x2, y2). 
2. Q x y (1 <= x, y <= n) querys A[x, y]. 

Input

The first line of the input is an integer X (X <= 10) representing the number of test cases. The following X blocks each represents a test case. 

The first line of each block contains two numbers N and T (2 <= N <= 1000, 1 <= T <= 50000) representing the size of the matrix and the number of the instructions. The following T lines each represents an instruction having the format "Q x y" or "C x1 y1 x2 y2", which has been described above. 

Output

For each querying output one line, which has an integer representing A[x, y]. 

There is a blank line between every two continuous test cases. 

Sample Input

1
2 10
C 2 1 2 2
Q 2 2
C 2 1 2 1
Q 1 1
C 1 1 2 1
C 1 2 1 2
C 1 1 2 2
Q 1 1
C 1 1 2 1
Q 2 1

Sample Output

1
0
0
1


#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<string>
#include<algorithm>
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
int c[1010][1010];
int n;
int lowbit(int x)
{
    return x&(-x);
}
void add(int x,int y,int val)//a[x][y]增加val
{
	for(int i=x;i<=n;i+=lowbit(i))
		for(int j=y;j<=n;j+=lowbit(j))
			c[i][j]=(c[i][j]+val)%2;
}

int getsum(int x,int y)//1~x,1~y之和
{
	int ans=0;
	for(int i=x;i>=1;i-=lowbit(i))
		for(int j=y;j>=1;j-=lowbit(j))
			ans=(ans+c[i][j])%2;
	return ans;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int m;
        cin>>n>>m;
        memset(c,0,sizeof(c));
        while(m--)
        {
            char ch[5];
            scanf("%s",ch);
            if(ch[0]=='C')
            {
                int x1,y1,x2,y2;
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                add(x1,y1,1);
                add(x1,y2+1,1);
                add(x2+1,y1,1);
                add(x2+1,y2+1,1);
            }
            else
            {
                int x,y;
                scanf("%d%d",&x,&y);
                cout<<getsum(x,y)<<endl;
            }
        }
        if(T)
            cout<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值