树状数组练习题

一、 敌兵布阵(模板裸题)

1.题目描述

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

2.解题思路

直接套模板。

3.AC代码

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int n, x, y, m, a[50010], c[50010];
string op;
int lowbit(int x) {
	return x & (-x);
}
void update1(int i, int k) {
	while (i <= n) {
		c[i] += k;
		i += lowbit(i);
	}
}

void update2(int i, int k) {
	while (i <= n) {
		c[i] -= k;
		i += lowbit(i);
	}
}
int query(int i) {
	int res = 0;
	while (i > 0) {
		res += c[i];
		i -= lowbit(i);
	}
	return res;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	int t;
	cin >> t;
	int f1 = 0;
	while (t--) {
		memset(c, 0, sizeof c);
		memset(a, 0, sizeof a);
		f1++;
		cout << "Case " << f1 << ":" << endl;
		cin >> n;
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
			update1(i, a[i]);
		}
		
		while (true) {
			cin >> op;
			if (op == "Add") {
				cin >> x >> y;
				update1(x, y);
			}
			if (op == "Sub") {
				cin >> x >> y;
				update2(x, y);
			}
			if (op == "Query") {
				cin >> x >> y;
				cout << query(y) - query(x-1) << endl;
			}
			if (op == "End") {
				break;
			}
	    }
	}
	return 0;
}

二、Ultra-QuickSort

1.题目描述

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

2.解题思路

啊这,这题以前做过类似的,就是求逆序对,但是那个时候不会树状数组,用归并排序,也看不懂树状数组,这个时候用树状数组,这个算法牛逼多了。(蒟蒻本人),这里还有一个序列化,序列化后变成了求正序对,那个累加的边界没减一,wa了好几次

3.AC代码

#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
const int MX = 5e5 + 10;
ll a[MX], d[MX], c[MX], n;
bool cmp(int x, int y) {
	if (a[x] == a[y]) return x > y;
	else              return a[x] > a[y];
}
int lowbit(int x) {
	return x & (-x);
}

void update(int i, int k) {
	while (i <= n) {
		c[i] += k;
		i += lowbit(i);
	}
}

int query(int i) {
	int res = 0;
	while (i > 0) {
		res += c[i];
		i -= lowbit(i);
	}
	return res;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);

	ll res;
	while (cin >> n && n != 0) {
		memset(a, 0, sizeof a);
		memset(c, 0, sizeof c);
		res = 0;
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
			d[i] = i;
		}
		sort(d + 1, d + n + 1, cmp);
		for (int i = 1; i <= n; i++) {
			update(d[i],1);
			res += query(d[i]-1);
		}
		cout << res << endl;
	}
	return 0;
}

三、stars(测评姬卡我十多次)

1.题目描述

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.

2.解题思路

注意c[0]就行,x+1

3.AC代码

#include<iostream>
#include<cctype>
using namespace std;
const int MX = 32010;
int  n, x, y;
int c[MX], ans[MX];
int lowbit(int x) { return x & (-x); }
void update(int i, int k) {
    while (i < MX) {
        c[i] += k;
        i += lowbit(i);
    }
}

int query(int i) {
    int res = 0;
    while (i > 0) {
        res += c[i];
        i -= lowbit(i);
    }
    return res;
}

int main()
{
    scanf("%d", &n);
    int tmp = n;
    while (tmp--) {
        scanf("%d%d", &x, &y);
        ans[query(x+1)]++;
        update(x+1, 1);
    }
    for (int i = 0; i < n; i++) {
        printf("%d\n", ans[i]);
    }

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值