调了两天的BUG

来源:互联网 发布:℃-ute 知乎 编辑:程序博客网 时间:2024/03/29 17:55
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 2 63
  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:197

6

在初始化的时候,在函数中调用数组的时候,一定要确保数组已经初始化完毕或赋值完毕。

这道题还有一个很细节的地方,在update函数中,r>=mid 和r>mid 差别太大了。这和区间染色问题正好反着。

[cpp] view plain copy
在CODE上查看代码片派生到我的代码片
  1. #include<iostream>  
  2. #include<stdio.h>  
  3. #include<cmath>   
  4. #include <algorithm>  
  5. #define MAX 100010  
  6. using namespace std;  
  7. int N,M;  
  8. long long ans;  
  9. long long d[MAX];  
  10. struct node  
  11. {  
  12.     int l;  
  13.     int r;  
  14.     long long sum;  
  15. };  
  16. struct node q[4*MAX];  
  17. void build(int l,int r,int root)  
  18. {  
  19.     q[root].l=l;  
  20.     q[root].r=r;  
  21.     if(l==r)  
  22.     {  
  23.         q[root].sum=d[l];  
  24.         return ;  
  25.     }  
  26.     int mid=(l+r)/2;  
  27.     build(l,mid,root*2);  
  28.     build(mid+1,r,root*2+1);  
  29.     q[root].sum=(q[root*2].sum + q[root*2+1].sum);  
  30. }  
  31.   
  32. void updown(int root)  
  33. {  
  34.     if(q[root].l==q[root].r)  
  35.     {  
  36.         d[q[root].l]=(int)sqrt(d[q[root].l]);  
  37.         q[root].sum=d[q[root].l];  
  38.         return ;  
  39.     }  
  40.     updown(root*2);  
  41.     updown(root*2+1);  
  42.     q[root].sum=(q[root*2].sum+q[root*2+1].sum);  
  43. }  
  44.   
  45. void update(int l,int r,int root)  
  46. {  
  47.     if(q[root].l>=l && q[root].r<=r)  
  48.     {  
  49.         if((r-l+1) == q[root].sum) return ; //说明区间内全部是0,没必要再往下更新  
  50.           
  51.         updown(root);  
  52.         return ;  
  53.     }  
  54.     int mid=(q[root].l+q[root].r)/2;  
  55.       
  56.     if(r<=mid) update(l,r,root*2);  
  57.     else if(l>mid) update(l,r,root*2+1);  
  58.     else  
  59.     {  
  60.         update(l,mid,root*2);  
  61.         update(mid+1,r,root*2+1);  
  62.     }  
  63.     q[root].sum=(q[root*2].sum+q[root*2+1].sum);  
  64. }  
  65.   
  66. void find(int l,int r,int root)  
  67. {  
  68.     if(q[root].l==l && q[root].r==r)  
  69.     {  
  70.         ans+=q[root].sum;  
  71.         return ;  
  72.     }  
  73.     int mid=(q[root].l+q[root].r)/2;  
  74.       
  75.     if(r<=mid) find(l,r,root*2);  
  76.     else if(l>mid) find(l,r,root*2+1);  
  77.     else  
  78.     {  
  79.         find(l,mid,root*2);  
  80.         find(mid+1,r,root*2+1);  
  81.     }  
  82.     //return ans;  
  83. }  
  84. int main()  
  85. {  
  86.     int t=1,i,j;  
  87.     while(scanf("%d",&N)!=EOF)  
  88.     {  
  89.         printf("Case #%d:\n",t++);  
  90.          //build(1,N,1); //我之前是把这个函数写在了for循环的前边,但我build函数需要用到d[i]!!!!
  91.         for(i=1;i<=N;i++)  
  92.         scanf("%lld",&d[i]);
  93.  build(1,N,1); 
  94.         scanf("%d",&M);  
  95.         int flag;  
  96.         while(M--)  
  97.         {  
  98.             int left,right;  
  99.             scanf("%d%d%d",&flag,&left,&right);  
  100.             if(left>right) swap(left,right);  
  101.             if(!flag)  
  102.             {  
  103.                 update(left,right,1);  
  104.             }  
  105.             else  
  106.             {  
  107.                 ans=0;  
  108.                 find(left,right,1);  
  109.                 printf("%lld\n",ans);     
  110.             }  
  111.           
  112.         }  
  113.         printf("\n");  
  114.     }  
  115.     return 0;  
  116. }  
0 0
原创粉丝点击