HDU-Can you answer these queries?

Can you answer these queries?

题目描述

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.

输入

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 263.
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.

输出

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.

题目大意

多组输入,首先给一个数组,接下来给出三个数mark,l,r.如果mark=0,区间[l,r]所有元素均开根.如果mark=1,查询区间[l,r]的值的和.

输入

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

输出

Case #1:
19
7
6

解题思路

首先这题想到的是成段更新成段查询,但是我们用的懒惰标记这里用不了,因为懒惰标记并不是一个确定的值,因此传不下去.这里呢我们就对update做更改,让其每一次的update都能够走完整棵树,这样就用不到懒惰标记了.但是,这样会超时,于是我们就想一下有没有办法进行剪枝.当我们查询到某个全包含区间的值都是1的时候,这时开根操作就没有必要进行了.我们直接返回.这里很关键,如果不加会超时.

AC代码

//#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<string.h>
#include <iomanip>
#include<stdio.h>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const ll less_inf = 0x3f3f3f3f;
const char char_inf = 127;
#pragma GCC optimize(2)
#define accelerate cin.tie(NULL);cout.tie(NULL);ios::sync_with_stdio(false);
#define PI 3.141592653589793
#define EPS 1.0e-8
ll gcd(ll a, ll b) {
	return b ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b) {
	return a * b / gcd(a, b);
}
inline ll read() {
	ll c = getchar(), Nig = 1, x = 0;
	while (!isdigit(c) && c != '-')c = getchar();
	if (c == '-')Nig = -1, c = getchar();
	while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
	return Nig * x;
}
inline void out(ll a) {
	if (a < 0)putchar('-'), a = -a;
	if (a > 9)out(a / 10);
	putchar(a % 10 + '0');
}
ll qpow(ll x, ll n, ll mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1)res = (res * x) % mod;
		x = (x * x) % mod;
		n >>= 1;
	}
	return res;
}
#define read read()
const int N = 100000;
ll tree[N * 4 + 7];
ll tree_cnt;
ll save[N + 7];
void push_up(int rt)
{
	tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
}
void creat(int l, int r, int rt)
{
	if (l == r)
	{
		tree[rt] = save[tree_cnt++];
		return;
	}
	int mid = l + r >> 1;
	creat(l, mid, rt << 1);
	creat(mid + 1, r, rt << 1 | 1);
	push_up(rt);
}
void update(int L, int R, int l, int r, int rt)
{
	if (l == r)tree[rt] = sqrt(tree[rt]);
	else if (L <= l && r <= R && tree[rt] == r - l + 1)return;
	else
	{
		int mid = l + r >> 1;
		if (L <= mid)update(L, R, l, mid, rt << 1);
		if (R >= mid + 1)update(L, R, mid + 1, r, rt << 1 | 1);
		push_up(rt);
	}
}
ll query(int L, int R, int l, int r, int rt)
{
	if (L <= l && r <= R)return tree[rt];
	int mid = l + r >> 1;
	ll res = 0;
	if (L <= mid)res += query(L, R, l, mid, rt << 1);
	if (R >= mid + 1)res += query(L, R, mid + 1, r, rt << 1 | 1);
	return res;
}
int main()
{
	int CNT = 0;
	int n;
	while (scanf("%d", &n) != EOF)
	{
		tree_cnt = 0;
		memset(tree, 0, sizeof(tree));
		for (int i = 0; i < n; i++)save[i] = read;
		creat(1, n, 1);
		int m;
		scanf("%d", &m);
		printf("Case #%d:\n", ++CNT);
		while (m--)
		{
			int mark, l, r;
			scanf("%d%d%d", &mark, &l, &r);
			if (l > r)swap(l, r);
			if (mark)
			{
				printf("%lld\n", query(l, r, 1, n, 1));
			}
			else
				update(l, r, 1, n, 1);
		}
		printf("\n");
	}
}

By-Round Moon

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Round moon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值