记录编号 58232 评测结果 AAAAAAAAAA
题目名称 最终得分 100
用户昵称 Gravatardigital-T 是否通过 通过
代码语言 C++ 运行时间 2.815 s
提交时间 2013-04-18 18:24:57 内存使用 7.83 MiB
显示代码纯文本
  1. #include<fstream>
  2. #include<iomanip>
  3. using namespace std;
  4. ifstream fi("treed.in");
  5. ofstream fo("treed.out");
  6. class bb
  7. {
  8. public:
  9. int l,r,Lchild,Rchild,s;//左界右界左儿子右儿子区间高度总值
  10. }tree[400000];
  11. int n,m,tot,d[200000];
  12. double ans;
  13. void maketree(int root,int x,int y)
  14. {
  15. tree[root].l=x;
  16. tree[root].r=y;
  17. if(x!=y)//非叶子节点
  18. {
  19. int mid=(x+y)/2;
  20. tot++;
  21. tree[root].Lchild=tot;
  22. maketree(tot,x,mid);
  23. tot++;
  24. tree[root].Rchild=tot;
  25. maketree(tot,mid+1,y);
  26. tree[root].s=tree[tree[root].Lchild].s+tree[tree[root].Rchild].s;
  27. }
  28. else//叶子节点
  29. {
  30. tree[root].s=d[x];
  31. tree[root].Lchild=-1;
  32. tree[root].Rchild=-1;
  33. }
  34. }
  35. int quest(int root,int x,int y)//查询[x,y]
  36. {
  37. if(root==-1)return 0;
  38. if(y<tree[root].l||tree[root].r<x)return 0;//不相交区间
  39. if(tree[root].Lchild==-1)return tree[root].s;//叶子节点
  40. if(x<=tree[root].l&&tree[root].r<=y)return tree[root].s;//全覆盖区间
  41. //相交区间
  42. int sum=0;
  43. if(x<=tree[tree[root].Lchild].r)//左区间
  44. sum+=quest(tree[root].Lchild,x,y);
  45. if(tree[tree[root].Rchild].l<=y)//右区间
  46. sum+=quest(tree[root].Rchild,x,y);
  47. return sum;
  48. }
  49. void cutdown(int root,int x)
  50. {
  51. if(tree[root].l>x||tree[root].r<x)return;//不相交区间
  52. if(tree[root].Lchild==-1)//叶子节点
  53. {
  54. tree[root].s=0;
  55. d[x]=0;
  56. return;
  57. }
  58. tree[root].s-=d[x];
  59. cutdown(tree[root].Lchild,x);
  60. cutdown(tree[root].Rchild,x);
  61. }
  62. int main()
  63. {
  64. fi>>n;
  65. for(int i=1;i<=n;i++)fi>>d[i];
  66. tot=0;
  67. maketree(0,1,n);
  68. fi>>m;
  69. /*for(int i=0;i<n*2;i++)
  70. fo<<tree[i].l<<' '<<tree[i].r<<' '<<tree[i].s<<endl;*/
  71. int a,b;
  72. for(int i=1;i<=m;i++)
  73. {
  74. fi>>a>>b;
  75. ans=quest(0,a,b);//fo<<a<<' '<<b<<' '<<ans<<endl;
  76. ans*=3.14;
  77. fo<<setprecision(2)<<setiosflags(ios::fixed)<<ans<<endl;
  78. cutdown(0,(a+b)/2);
  79. }
  80. return 0;
  81. }