HDU-4027-Can you answer these queries?

来源:互联网 发布:日系rpg手游 知乎 编辑:程序博客网 时间:2024/05/29 01:54

Can you answer these queries?
Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 15008    Accepted Submission(s): 3521


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
68
 

Source
The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest 
 题意就是个线段树  输入一段区间数据 然后在对区间数据进行修改  修改方式是对区间内的每个数据开方,然后查询


这里无法成段更新 只能够单点更新,不需要LAZY!因为 对数据无论开方多少次 最大也就7~8次左右

所以判断的时候如果区间内的数据的大小等于区间的长度 那么就说明这个区间内数据都已经是1了,直接返回

不过坑点很多 - -

1 区间起点可能大于区间终点

2 输出注意空行和Case后的:号

3 还有中间的变量 中转变量都要用LL



#include<cstdio>#include<iostream>#include<cmath>#include<set>#include<cstring>#include<algorithm>#define MAX 100019#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1using namespace std;typedef long long ll;struct tree{    ll d;  //  bool flag;}tre[MAX<<2];void build(int l,int r,int rt){    if(l==r)    {        scanf("%lld",&tre[rt].d);        return;    }    int mid = (l+r)>>1;    build(lson);    build(rson);    tre[rt].d = tre[rt<<1].d+tre[rt<<1|1].d;}void update(int s,int e,int l,int r,int rt){    if(tre[rt].d==r-l+1)return;    if(l==r)    {        tre[rt].d=(ll)(sqrt(tre[rt].d));   //     if(tre[rt].d==1)tre[rt].flag=1;//        return ;    }    int mid = (l+r)>>1;    if(s<=mid)update(s,e,lson);    if(e>mid)update(s,e,rson);    tre[rt].d=tre[rt<<1].d+tre[rt<<1|1].d;  //  if(tre[rt<<1].flag&&tre[rt<<1|1].flag)tre[rt].flag=1;}ll query(int s,int e,int l,int r,int rt){    if(s<=l&&r<=e)return tre[rt].d;    int mid = (l+r)>>1;    ll a = 0;//一开始这里没用ll - -......导致WR    if(s<=mid)a+=query(s,e,lson);    if(e>mid)a+=query(s,e,rson);    return a;}int main(){    int n;    int m;    int c=0;    while(~scanf("%d",&n))    {    //    memset(tre,0,sizeof(tre));        build(1,n,1);        scanf("%d",&m);        printf("Case #%d:\n",++c);        int flag,a,b;        for(int i=1;i<=m;i++)        {            scanf("%d%d%d",&flag,&a,&b);            if(a>b)            {                a=a+b;                b=a-b;                a=a-b;       //         cout<<a<<" "<<b<<endl;            }            if(flag)printf("%lld\n",query(a,b,1,n,1));            else update(a,b,1,n,1);        }        printf("\n");    }    return 0;}




0 0
原创粉丝点击