HDU

来源:互联网 发布:美国劳工部就业数据 编辑:程序博客网 时间:2024/06/03 17:24

Can you answer these queries?

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 19065    Accepted Submission(s): 4500


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
101 2 3 4 5 6 7 8 9 1050 1 101 1 101 1 50 5 81 4 8
 

Sample Output
Case #1:1976


题意:给定区间 操作是把某个区间的数都开根号 问某个区间的和


思路:用lazy标签记录这个区间是不是都是1 是1就不用管 否则会超时


#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <queue>#include <stack>#include <cmath>#include <vector>#define max_ 100010#define inf 0x3f3f3f3f#define ll __int64using namespace std;struct node{int l,r,lazy;ll sum;};struct node tree[max_*4];int n,m,casenum=1;void build(int i,int l,int r){tree[i].l=l;tree[i].r=r;if(l==r){scanf("%I64d",&tree[i].sum);if(tree[i].sum==1)tree[i].lazy=1;elsetree[i].lazy=0;return;}int mid=(l+r)>>1;build(i<<1,l,mid);build(i<<1|1,mid+1,r);tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;if(tree[i<<1].lazy&&tree[i<<1|1].lazy)tree[i].lazy=1;elsetree[i].lazy=0;}void update(int i,int l,int r){if(tree[i].lazy)return;if(tree[i].l==tree[i].r){tree[i].sum=sqrt(tree[i].sum);if(tree[i].sum==1)tree[i].lazy=1;return;}int mid=(tree[i].l+tree[i].r)>>1;if(l>mid)update(i<<1|1,l,r);else if(r<=mid)update(i<<1,l,r);else{update(i<<1,l,mid);update(i<<1|1,mid+1,r);}tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;if(tree[i<<1].lazy&&tree[i<<1|1].lazy)tree[i].lazy=1;}ll query(int i,int l,int r){if(tree[i].l==l&&tree[i].r==r)return tree[i].sum;int mid=(tree[i].l+tree[i].r)>>1;if(l>mid)return query(i<<1|1,l,r);else if(r<=mid)return query(i<<1,l,r);elsereturn query(i<<1,l,mid)+query(i<<1|1,mid+1,r);}int main(int argc, char const *argv[]){while(scanf("%d",&n)!=EOF){memset(tree,0,sizeof(tree));build(1,1,n);scanf("%d",&m);printf("Case #%d:\n",casenum++);while(m--){int t,x,y;scanf("%d%d%d",&t,&x,&y);if(x>y)swap(x,y);if(t==0)update(1,x,y);else if(t==1)printf("%I64d\n",query(1,x,y));}printf("\n");}return 0;}


原创粉丝点击