hdu 4027 - Can you answer these queries?(线段树+思维)

Can you answerthese queries?

TimeLimit: 4000/2000 MS (Java/Others)    Memory Limit:65768/65768 K (Java/Others)
Total Submission(s): 19795    Accepted Submission(s): 4683

ProblemDescription

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

Notice that the square root operation should be roundeddown 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 endurancevalue of each battleship from the beginning of the line to the end. You canassume that the sum of all endurance value is less than 263.
  The next line contains an integer M, denoting the number of actionsand queries. (1 <= M <= 100000)
  For the following M lines, each line contains three integers T, Xand Y. The T=0 denoting the action of the secret weapon, which will decreasethe 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 ofthe endurance value of the battleship between X-th and Y-th, inclusive.

 

 

Output

For each test case, print the case numberat the first line. Then print one line for each query. And remember follow ablank line after each test case.

 

 

SampleInput

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

 

 

SampleOutput

Case #1:

19

7

6

 

【题意】

   给定一长度为N的区间,对区间进行M次操作,操作分两种,一种是把区间[x,y]中的元素值变为原来的平方根,另一种是查询区间[x,y]的所有元素和,输出每次查询的结果。

 

【思路】

   很明显是线段树的题,但是区间更新很难,而且如果用直接单点更新的话一定会超时,所以这里有一个很关键的剪枝:一个很大的数进行数次若干开平方就会变成1。因为结点的value最大是2^63,而2^64也就只需开7次就可以得到1,而1开方永远是1,所以如果更新区间[x,y]的话,那么那些值为1的相应区间根本就不需要任何更新操作。而判断某区间的值是否全为1的方法也很简单,即node.right– node.left + 1 == node.value,通过这个剪枝操作就可以在限定时间之内求解了。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long ll;
const int maxn = 1e5 + 500;

#define node tree[id]
#define lson tree[id*2]
#define rson tree[id*2+1]

int n, m;
ll a[maxn];
struct Tree {
	int left, right;
	ll value;
}tree[maxn * 4];

void pushup(int id) { node.value = lson.value + rson.value; }

void build(int id, int le, int ri) {
	node.left = le;
	node.right = ri;
	if (le == ri) {
		node.value = a[le];
		return;
	}
	int mid = (le + ri) >> 1;
	build(id * 2, le, mid);
	build(id * 2 + 1, mid + 1, ri);
	pushup(id);
}

ll query(int id, int x, int y) {
	if (node.left >= x && node.right <= y) {
		return node.value;
	}
	pushup(id);
	ll ans = 0;
	int mid = (node.left + node.right) >> 1;
	if (x <= mid) ans += query(id * 2, x, y);
	if (y > mid) ans += query(id * 2 + 1, x, y);
	return ans;
}

void update(int id, int x, int y) {//单点更新,将区间[x,y]的元素值变为原来的值开根号
	if (node.left >= x && node.right <= y && node.value == (node.right - node.left + 1)) return;
	//经典剪枝操作,一定注意要判断当前节点对应的区间是否完全在所求区间中,在才做进一步判断
	//不在就向下递归了
	if (node.left == node.right) {
		node.value = (ll)sqrt(double(node.value));
		return;
	}
	int mid = (node.left + node.right) >> 1;
	if (x <= mid) update(id * 2, x, y);
	if (y > mid) update(id * 2 + 1, x, y);
	pushup(id);
}

int main() {
	int kase = 1, t, x, y;
	while (scanf("%d", &n) == 1) {
		for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
		build(1, 1, n);
		scanf("%d", &m);
		printf("Case #%d:\n", kase++);
		while (m--) {
			scanf("%d%d%d", &t, &x, &y);
			if (x > y) swap(x, y);
			if (0 == t) {
				update(1, x, y);
			}
			else {
				ll ans = query(1, x, y);
				printf("%lld\n", ans);
			}
		}
		printf("\n");
	}
	return 0;
}


posted @ 2017-11-02 23:54  不想吃WA的咸鱼  阅读(113)  评论(0编辑  收藏  举报