HDU 4027 Can you answer these queries(线段树 成段更新)

Problem Description

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

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

题意:给一个区间跟新,不过不同的是把这一个区间的数都开根号,

思路:因为一个数开一定的根号后是1 ,那么就不会再变了,一段区间这样都是一就没必要改变了,所以这样可以节约时间,还有这份代码用G++交超时,c++交过

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>

#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef __int64 ll;

#define fre(i,a,b)  for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;

#define INF 0x3f3f3f3f
#define N 1000005

struct stud{
int le,ri;
ll len,sum;
}f[N*4];

ll a[N];

inline void pushup(int pos)
{
	f[pos].sum=f[L(pos)].sum+f[R(pos)].sum;
}

void build(int pos,int le,int ri)
{
	f[pos].le=le;
	f[pos].ri=ri;
	f[pos].len=ri-le+1;
	if(le==ri)
	{
		f[pos].sum=a[le];
		return ;
	}

    int mid=MID(le,ri);
    build(L(pos),le,mid);
    build(R(pos),mid+1,ri);

    pushup(pos);
}

void hello(int pos)       //把这一个区间的数都开根号
{
	if(f[pos].len==f[pos].sum)
		return;

	if(f[pos].le==f[pos].ri)
	{
		f[pos].sum=(ll)(sqrt(f[pos].sum+0.0));
		return ;
	}
    hello(L(pos));
    hello(R(pos));

    pushup(pos);
}

void update(int pos,int le,int ri)
{
    if(f[pos].len==f[pos].sum)
		return ;

	if(f[pos].le==le&&f[pos].ri==ri)
	{
		hello(pos);
		return ;
	}

	int mid=MID(f[pos].le,f[pos].ri);

	if(mid>=ri)
		update(L(pos),le,ri);
	else
		if(mid<le)
		 update(R(pos),le,ri);
	else
		{
			update(L(pos),le,mid);
			update(R(pos),mid+1,ri);
		}
   pushup(pos);
}

ll query(int pos,int le,int ri)
{
	if(f[pos].le==le&&f[pos].ri==ri)
		return f[pos].sum;

	int mid=MID(f[pos].le,f[pos].ri);

	if(mid>=ri)
		return query(L(pos),le,ri);
	else
		if(mid<le)
		return query(R(pos),le,ri);
	else
		return (ll)(query(L(pos),le,mid)+query(R(pos),mid+1,ri));
}

int main()
{
   int i,j,n,m,ca=0;
   while(~sf(n))
   {
   	   fre(i,1,n+1)
   	     scanf("%I64d",&a[i]);

		build(1,1,n);
		sf(m);
		pf("Case #%d:\n",++ca);

		int op,le,ri;

		while(m--)
		{
			sfff(op,le,ri);
			if(le>ri) swap(le,ri);
			if(op)
			   pf("%I64d\n",query(1,le,ri));
			else
			   update(1,le,ri);
		}
		pf("\n");
   }
   return 0;
}
时间: 2024-12-20 23:58:33

HDU 4027 Can you answer these queries(线段树 成段更新)的相关文章

HDU 4027 Can you answer these queries?(线段树,区间更新,区间查询)

题目 线段树 简单题意: 区间(单点?)更新,区间求和 更新是区间内的数开根号并向下取整 这道题不用延迟操作 //注意: //1:查询时的区间端点可能前面的比后面的大: //2:优化:因为每次更新都是开平方,同一个数更新有限次数就一直是1了,所以可以这样优化 #include <stdio.h> #include<math.h> #define N 100010 #define LL __int64 #define lson l,m,rt<<1 #define rson

HDU 4027 Can you answer these queries?(线段树 区间不等更新)

题意  输入n个数  然后有两种操作   输入0时将给定区间所有数都变为自己的开方   输入1输出给定区间所有数的和 虽然是区间更新  但每个点更新的不一样  因此只能对单点进行更新  其实一个点最多被更新7次  2^64开平方7次后就变为1了  如果某个区间的数都变为了1  那么对这个区间的开方就不用考虑了   另外要注意给你的区间可能是反的 #include <bits/stdc++.h> #define lc p<<1,s,mid #define rc p<<1|

HDU 4027 Can you answer these queries? (线段树+区间点修改)

题意:给你 n 个数,m个询问(c,x,y) c==0 把x,y区间的值变为原来的平方根(向下取整) c==1 计算x,y区间的和. 利用1的开方永远为1剪枝.. #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> //#include<map> #include<cmath> #include<iostream> #include

HDU 4027 Can you answer these queries? 线段树裸题

题意: 给定2个操作 0.把区间的每个数sqrt 2.求和 因为每个数的sqrt次数很少,所以直接更新到底,用个标记表示是否更新完全(即区间内的数字只有0,1就不用再更新了) #include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #incl

hdu 4027 Can you answer these queries? 线段树区间开根号,区间求和

Can you answer these queries? Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5195 Description A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapo

HDU 4027 Can you answer these queries?(线段树)

Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 9216    Accepted Submission(s): 2106 Problem Description A lot of battleships of evil are arranged in a line before

HDU 4027 Can you answer these queries? 线段树,区间修改

Can you answer these queries? Problem Description 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 end

HDU - 4027 Can you answer these queries? (线段树区间更新+思维)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 题意:给定两种操作,查询(求出区间内的和),更新(区间内每个值都开根号,取整数) 题目给出所有数字之和小于263,所以最大的数最多7次也就变成1.所以在更新的时候加个判断条件,提前结束. 然后这道题目还有个坑,L有可能比R大,需要交换一下,(╬▔皿▔) . 1 #include <cstdio> 2 #include <algorithm> 3 #include <cmat

hdu 4027 2011上海赛区网络赛 线段树 成段平方根 ***

不能直接使用成段增减的那种,因为一段和的平方根不等于平方根的和,直接记录是否为1,是1就不需要更新了 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespace std; 9 #de

HDU4027——线段树成段更新——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