树状数组错题总结

题目链接

HDU - 1166 敌兵布阵(模板题)

      C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。
中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.

Input

第一行一个整数T,表示有T组数据。
每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
接下来每行有一条命令,命令有4种形式:
(1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
(2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
(3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
(4)End 表示结束,这条命令在每组数据最后出现;
每组数据最多有40000条命令

Output

对第i组数据,首先输出“Case i:”和回车,
对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在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

Case 1:
6
33
59

    这道题纯板子,没啥好说的

AC代码

#include<iostream>
#include<cmath>
#include<string>
#include<string.h>
#define maxn 50005
using namespace std;
int a[maxn];
int c[maxn];
int n,x,y;
string s;
void init() {
	memset(a,0,sizeof(a));
	memset(c,0,sizeof(c));
}
int lowbit(int x) {
	return x&(-x);
}
// 区间查询 
int getsum(int x) {
	int ans = 0;
	for(int i=x; i>0;i-=lowbit(i))
		ans += c[i];
	return ans;
}
// 单点更新 
void add(int x, int y) {
	for(int i=x; i<=n;i+=lowbit(i))
		c[i] += y;
} 
int main() {
	int T;
	int cnt=1;
	cin >> T;
	while(T--) {
		init();
		cin >> n;
		for(int i=1;i<=n;i++) {
			cin >> a[i];
			add(i,a[i]);
		}
		cout << "Case " << cnt << ":" << endl;
		while(1) {
			cin >> s;
			if(s[0] == 'E')
				break;
			cin >> x >> y;
			if(s[0] == 'Q')
				cout << getsum(y)-getsum(x-1) << endl;
			else if(s[0] == 'A')
				add(x,y);
			else
				add(x,-y);  
		}
		cnt++;
	}
	return 0;
}

HDU - 1754  I Hate It

    很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
这让很多学生很反感。
    不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。

Input

本题目包含多组测试,请处理到文件结束。
    在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。
学生ID编号分别从1编到N。
    第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。
当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。

Output

对于每一次询问操作,在一行里面输出最高成绩。

Sample Input

5 6
1 2 3 4 5
Q 1 5
U 3 6
Q 3 4
Q 4 5
U 2 9
Q 1 5

Sample Output

5
6
5
9         

Hint

Huge input,the C function scanf() will work better than cin

    这道题需要注意每输入一次就要更新一次树状数组中存的最大值,当修改的时候只用将需要修改的值修改,然后再次传入修改的位置进行更新

AC代码

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<string.h>
#define maxn 400010
using namespace std;
int n,m,x,y;
int a[maxn],c[maxn];
char ch;
void init() {
	memset(a,0,sizeof(a));
	memset(c,0,sizeof(c));
}
int lowbit(int x) {
	return x&(-x);
}
//void update(int i,int v) { //更新 
//	while(i<=n) {
//		a[i] = max(a[i],v);
//		i += lowbit(i);
//	}
//}
void updata(int x)
{
	int lx, i;
	while (x <= n)
	{
		c[x] = a[x];
		lx = lowbit(x);
		for (i=1; i<lx; i<<=1)
			c[x] = max(c[x], c[x-i]);
		x += lowbit(x);
	}		
}
int query(int l, int r) { //询问 
	int ans = 0;
	while(r >= l) {
		ans = max(a[r],ans);
		r--;
		for(; r-lowbit(r)>=l; r-=lowbit(r))
			ans = max(c[r],ans);
	}
	return ans;
}
int main() {
	while(scanf("%d%d",&n,&m) != EOF) {
		ios::sync_with_stdio(false);
		cin.tie(0);
		cout.tie(0);
		init();
		for(int i=1; i<=n; i++) {
			scanf("%d",&a[i]);
			updata(i);
		}
			
		for(int i=1; i<=m; i++) {
			getchar();
			scanf("%c",&ch);
			getchar();
			if(ch == 'Q') { //询问 
				scanf("%d%d",&x,&y);
				printf("%d\n",query(x,y));
			}
			if(ch == 'U') { //更新
				scanf("%d%d",&x,&y);
				a[x] = y;
				updata(x);
			}
		}		
	} 
	return 0;
}

HDU - 5443 The Water Problem

In Land waterless, water is a very limited resource. People always fight for the biggest source of water. Given a sequence of water sources with a1,a2,a3,...,ana1,a2,a3,...,an representing the size of the water source. Given a set of queries each containing 22 integers ll and rr , please find out the biggest water source between alal and arar .

Input

First you are given an integer T(T≤10)T(T≤10) indicating the number of test cases. For each test case, there is a number n(0≤n≤1000)n(0≤n≤1000) on a line representing the number of water sources. nn integers follow, respectively a1,a2,a3,...,ana1,a2,a3,...,an , and each integer is in {1,...,106}{1,...,106} . On the next line, there is a number q(0≤q≤1000)q(0≤q≤1000) representing the number of queries. After that, there will be qq lines with two integers ll and r(1≤l≤r≤n)r(1≤l≤r≤n) indicating the range of which you should find out the biggest water source.

Output

For each query, output an integer representing the size of the biggest water source.

Sample Input

3
1
100
1
1 1
5
1 2 3 4 5
5
1 2
1 3
2 4
3 4
3 5
3
1 999999 1
4
1 1
1 2
2 3
3 3

Sample Output

100
2
3
4
4
5
1
999999
999999
1

跟上一道题一样,输入输出格式改一下即可

AC代码

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<string.h>
#define maxn 4010
using namespace std;
int n,m,x,y;
int a[maxn],c[maxn];
char ch;
void init() {
	memset(a,0,sizeof(a));
	memset(c,0,sizeof(c));
}
int lowbit(int x) {
	return x&(-x);
}
void updata(int x) //更新 
{
	int lx, i;
	while (x <= n)
	{
		c[x] = a[x];
		lx = lowbit(x);
		for (i=1; i<lx; i<<=1)
			c[x] = max(c[x], c[x-i]);
		x += lowbit(x);
	}		
}
int query(int l, int r) { //询问 
	int ans = 0;
	while(r >= l) {
		ans = max(a[r],ans);
		r--;
		for(; r-lowbit(r)>=l; r-=lowbit(r))
			ans = max(c[r],ans);
	}
	return ans;
}
int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		init();
		scanf("%d",&n);
		for(int i=1; i<=n; i++) {
			scanf("%d",&a[i]);
			updata(i);
		}
		cin >> m;	
		for(int i=1; i<=m; i++) {
			scanf("%d%d",&x,&y);
			printf("%d\n",query(x,y));
		}
		
	} 
	return 0;
}

 

POJ - 2299 Ultra-QuickSort(树状数组求逆序对) 

 

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 
9 1 0 5 4 , 

Ultra-QuickSort produces the output 
0 1 4 5 9 . 

Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence. 
Input
The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed. 
Output
For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence. 
Sample Input
5
9
1
0
5
4
3
1
2
3
0
Sample Output
6
0

  这道题真的太?了,数据范围一定要开到ll,不然就会一直WA...编译器出来的结果没问题,但提交上去就是一直WA。我找了好久的错误,贼纳闷。后来想起来是不是因为数据范围的问题,就一股脑地把所有数据类型换成ll,也是个狠人...

#include<algorithm> 
#include<cstdio>
#include<string.h>
#define maxn 510000
#define ll long long 
using namespace std;
ll n,result;
ll b[maxn];
ll t[maxn];
struct unt {
	ll val; //输入的数 
	ll id; //输入的顺序 
}a[maxn];
bool cmp(unt a,unt b) {
	if(a.val == b.val)
		return a.id < b.id;
	return a.val < b.val;
}
ll lowbit(ll x) {
	return x&(-x); 
}
void add(ll x)
{
    while(x <= maxn)
    {
        t[x]++; //这个数出现了几次 
        x += lowbit(x);
    }
}
ll sum(ll x)
{
    ll ans=0;
    for ( ; x; x-=lowbit(x))
        ans += t[x];
    return ans;
}
void init() { //初始化 
	for(ll i=0;i<maxn;i++) {
		a[i].val=0;
		a[i].id=0;
	}
	memset(b,0,sizeof(b));
	memset(t,0,sizeof(t));
}
int main() {
	while(scanf("%lld",&n)&&n!=0) {
		result = 0;
		init();
		for(ll i=1; i<=n; i++) {
			scanf("%lld",&a[i].val);
			a[i].id = i;
		}
		sort(a+1,a+n+1,cmp);  //反过来排序,即离散化 避免数据过大内存超限 
		
    	for(ll i=1; i<=n; i++) 
			b[i] = a[i].id; //离散化 将a的数值存储到b中,为了add()接受 
			
		for(ll i=1;i<=n;i++) {
	        add(b[i]);
	        result += i-sum(b[i]);
    	}
   		printf("%lld\n",result);		
	}
	return 0;
}

 

 POJ - 3067  Japan 

Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 1000, N <= 1000). K superhighways will be build. Cities on each coast are numbered 1, 2, ... from North to South. Each superhighway is straight line and connects city on the East coast with city of the West coast. The funding for the construction is guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. At most two superhighways cross at one location. Write a program that calculates the number of the crossings between superhighways.

Input

The input file starts with T - the number of test cases. Each test case starts with three numbers – N, M, K. Each of the next K lines contains two numbers – the numbers of cities connected by the superhighway. The first one is the number of the city on the East coast and second one is the number of the city of the West coast.

Output

For each test case write one line on the standard output:
Test case (case number): (number of crossings)

Sample Input

1
3 4 4
1 4
2 3
3 2
3 1

Sample Output

Test case 1: 5

 

题意:日本岛东海岸与西海岸分别有N和M个城市,现在修高速公路连接东西海岸的城市,求公路交点个数。

这道题还是一道逆序排列问题
思路参见其他博客,记每条告诉公路为(x,y), 即东岸的第x个城市与西岸的第y个城市修一条路。当两条路有交点时,满足(x1-x2)*(y1-y2) < 0。所以,将每条路按x从小到达排序,若x相同,按y从小到大排序。 然后按排序后的公路用树状数组在线更新,y的逆序数之和即为交点个数。

AC代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;
const int maxn=1003;
ll C[maxn];
int n,m,k;
struct unt {
	int x; //输入的数 
	int y; //输入的顺序 
}edge[maxn*maxn];
bool cmp(unt a,unt b) {
    if(a.x==b.x)
		return a.y<=b.y;
    else return a.x<b.x;
}
int lowbit(int x) {
    return x&(-x);
}
void add(int p) {
    for(int i=p;i<=m;i+=lowbit(i))
    	C[i]++;
}
int getsum(int p) {
    ll ans=0;
    for(int i=p;i>0;i-=lowbit(i)) {
      ans+=C[i];
    }
    return ans;
}
int main() {
    int T=0;
    int num;
    scanf("%d",&num);
    while(num--) {
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0;i<k;i++)
       scanf("%d%d",&edge[i].x,&edge[i].y);
    memset(C,0,sizeof(C));
    sort(edge,edge+k,cmp);
    ll ans=0;

     for(int i=0;i<k;i++) {
        add(edge[i].y);
        ans+=(getsum(m)-getsum(edge[i].y));
    }
    T++;
    printf("Test case %d: %lld\n",T,ans);
    }
    return 0;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值