摘要

题目链接 难度等级 完成状态 完成分数 最后编辑时间 失误原因(初次提交分数)
线段树练习 2 ★☆☆☆☆ 答案正确 100 2015/02/05 14:09:26 算法错误(10)

题意

线段树操作。

题解

代码

注意
本段代码不适用于成段查询!


1081.cpp代码已折叠
展开折叠内容
显示/移除行号
  1. #include<cstdio>
  2. template<size_t N>
  3. class SegmentTree
  4. {
  5. //模板:线段树//
  6. public:
  7. int tree[N];
  8. int lazy[N];
  9. #define cm int m=(l+r)>>1
  10. #define lp p<<1
  11. #define rp p<<1|1
  12. #define lc lp,l,m
  13. #define rc rp,m+1,r
  14. #define stdp int p,int l,int r
  15. int build(stdp)
  16. {
  17. if(l==r)
  18. {
  19. scanf("%d",&tree[p]);
  20. return tree[p];
  21. }
  22. cm;
  23. return tree[p]=build(lc)+build(rc);
  24. }
  25. void update(int L,int R,int delta,stdp)
  26. {
  27. if(L<=l&&r<=R)
  28. {
  29. lazy[p]+=delta;
  30. return;
  31. }
  32. cm;
  33. if(L<=m)update(L,R,delta,lc);
  34. if(R>m)update(L,R,delta,rc);
  35. }
  36. int query(int L,int R,stdp)
  37. {
  38. if(L<=l&&r<=R) return tree[p]+lazy[p];
  39. int ret=lazy[p];
  40. cm;
  41. if(L<=m)ret+=query(L,R,lc);
  42. if(R>m)ret+=query(L,R,rc);
  43. return ret;
  44. }
  45. };
  46. SegmentTree<2000000> st;
  47. int main()
  48. {
  49. int n;
  50. scanf("%d",&n);
  51. st.build(1,1,n);
  52. int Q,f,a,b,d;
  53. scanf("%d",&Q);
  54. while(Q--)
  55. {
  56. int f;
  57. scanf("%d",&f);
  58. if(f==1)
  59. {
  60. scanf("%d%d%d",&a,&b,&d);
  61. st.update(a,b,d,1,1,n);
  62. }
  63. else
  64. {
  65. int a;
  66. scanf("%d",&a);
  67. printf("%d\n",st.query(a,a,1,1,n));
  68. }
  69. }
  70. return 0;
  71. }

著作权声明[编辑]

关于[编辑]