xiaoMa
"Bye Bye Baby Blue"
线段树(HDU - 4027)

Problem:

A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.

Notice that the square root operation should be rounded down to integer.

Input:

The input contains several test cases, terminated by EOF.
  For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
  The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 2 63.
  The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
  For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.

Output:

For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.

Sample Input:

10
1 2 3 4 5 6 7 8 9 10
5
0 1 10
1 1 10
1 1 5
0 5 8
1 4 8

Sample Output:

Case #1:
19
7
6

这道题的大概意思是,有一连串的战舰,每艘战舰都有各自的耐久值,然后有两种操作 T:T=0可以让X-Y之间的耐久值开平方,T=1表示查询X-Y之间的耐久值

所以很明显如果我们直接对线性表进行操作的话,必定会超时,所以就是线段树lazy标记区间更新和求和,但是观察发现不能采取传统的lazy标记。后来我发现最大值为2 63,因此没个数最多能开6-7次方,之后1开方仍然为1。因此如果一个区间(l-r+1)中的值全为1就可以直接return。若区间不满足条件则进行单点更新,再将每个值开方。

代码也很简单,记得开四倍?

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <queue>
#include <vector>
#include <stack>
#include <algorithm>
#include <cctype>
#include <map>
#define maxx 0x3f3f3f3f
#define ll long long
#define FAST_IO std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
using namespace std;
const int maxn = 1e5 + 10;
int n, q;
ll a[maxn];
struct tree {
	int l;
	int r;
	ll sum;
} t[maxn << 2];

void pushup(int rt)
{
	t[rt].sum = t[rt << 1].sum + t[rt << 1 | 1].sum;
}

void build(int rt, int l, int r)
{
	t[rt].l = l;
	t[rt].r = r;

	if (l == r) {
		t[rt].sum = a[l];
		return;
	}

	int mid = (l + r) >> 1;

	build(rt << 1, l, mid);
	build(rt << 1 | 1, mid + 1, r);
	pushup(rt);
	return;
}

void update(int rt, int L, int R)
{
	if (t[rt].sum == t[rt].r - t[rt].l + 1) //因为长度为一就是区间的值
		return;
	if (t[rt].l == t[rt].r) {
		t[rt].sum = (ll)(sqrt(t[rt].sum));        //直接开方就行了
		return;
	}

	int mid = (t[rt].l + t[rt].r) >> 1;
	if (L <= mid) update(rt << 1, L, R);
	if (R >  mid) update(rt << 1 | 1, L, R);
	pushup(rt);
	return;
}
ll query(int rt, int L, int R)
{

	if (L <= t[rt].l &amp;&amp; t[rt].r <= R) {
		return t[rt].sum;
	}

	int mid = (t[rt].l + t[rt].r) >> 1;
	ll ans = 0;
	if (L <= mid) ans += query(rt << 1, L, R);
	if (R >  mid) ans += query(rt << 1 | 1, L, R);
	return ans;
}

int main()
{
	int i = 0;

	while (scanf("%d", &amp;n) != EOF) {
		printf("Case #%d:\n", ++i);
		for (int i = 1; i <= n; ++i)
			scanf("%lld", &amp;a[i]);

		build(1, 1, n);

		scanf("%d", &amp;q);

		while (q--) {
			int t, l, r;
			scanf("%d%d%d", &amp;t, &amp;l, &amp;r);
			if (l > r) swap(l, r);
			if (t)
				printf("%lld\n", query(1, l, r));
			else
				update(1, l, r);
		}
		printf("\n");
	}
	return 0;
}

强推Typora?,好用又好看

发表评论

textsms
account_circle
email

线段树(HDU - 4027)
Problem: A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the …
扫描二维码继续阅读
2020-02-10