程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 4027 Can you answer these queries(線段樹 成段更新)

HDU 4027 Can you answer these queries(線段樹 成段更新)

編輯:C++入門知識

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
#include
#include
#include
#include
#include
#include
#include
#include
#include


#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 = 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=ri)
		return query(L(pos),le,ri);
	else
		if(midri) swap(le,ri);
			if(op)
			   pf("%I64d\n",query(1,le,ri));
			else
			   update(1,le,ri);
		}
		pf("\n");
   }
   return 0;
}






 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved