CodeForces 52C Circular RMQ (线段树的区间更新+lazy tag)

忘是亡心i 2022-06-12 02:44 245阅读 0赞

You are given circular array a0, a1, …, a**n - 1. There are two types of operations with it:

  • inc(lf, rg, v) — this operation increases each element on the segment [lf, rg](inclusively) by v;
  • rmq(lf, rg) — this operation returns minimal value on the segment [lf, rg](inclusively).

Assume segments to be circular, so if n = 5 and lf = 3, rg = 1, it means the index sequence: 3, 4, 0, 1.

Write program to process given sequence of operations.

Input

The first line contains integer n (1 ≤ n ≤ 200000). The next line contains initial state of the array: a0, a1, …, a**n - 1 ( - 106 ≤ a**i ≤ 106), a**i are integer. The third line contains integer m (0 ≤ m ≤ 200000), m — the number of operartons. Next mlines contain one operation each. If line contains two integer lf, rg (0 ≤ lf, rg ≤ n - 1) it means rmq operation, it contains three integers lf, rg, v (0 ≤ lf, rg ≤ n - 1; - 106 ≤ v ≤ 106) — inc operation.

Output

For each rmq operation write result for it. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).

Example

Input

  1. 4
  2. 1 2 3 4
  3. 4
  4. 3 0
  5. 3 0 -1
  6. 0 1
  7. 2 1

Output

  1. 1
  2. 0
  3. 0

题解:

现在还在比赛。。但我已经写不出来题了,就来写博客了,好高兴线段树的题没看题解就写出来了,这几天线段树和树状数组的题没白刷,这题就是如果输入区间不是从小到大就分成两个区间做,然后要注意一下区间更新要lazy tag,延迟更新,然后输入的时候做点判断处理这题就ac了

代码:

  1. #include<algorithm>
  2. #include<iostream>
  3. #include<cstring>
  4. #include<stdio.h>
  5. #include<math.h>
  6. #include<string>
  7. #include<stdio.h>
  8. #include<queue>
  9. #include<stack>
  10. #include<map>
  11. #include<deque>
  12. using namespace std;
  13. struct node
  14. {
  15. int l,r;
  16. long long minn;
  17. long long tag;
  18. };
  19. node t[200005*4];
  20. int a[200005];
  21. void Build(int l,int r,int num)//日常建树
  22. {
  23. t[num].l=l;
  24. t[num].r=r;
  25. t[num].tag=0;
  26. if(l==r)
  27. {
  28. t[num].minn=a[l];
  29. return;
  30. }
  31. int mid=(l+r)/2;
  32. Build(l,mid,num*2);
  33. Build(mid+1,r,num*2+1);
  34. t[num].minn=min(t[num*2].minn,t[num*2+1].minn);//更新下最小值
  35. }
  36. void update(int l,int r,int num,int v)
  37. {
  38. if(t[num].l==l&&t[num].r==r)
  39. {
  40. t[num].minn+=v;
  41. t[num].tag+=v;
  42. return;
  43. }
  44. if(t[num].tag!=0)//除了这个都是日常更新,这个可以写成一个函数lazy tag,将上次没有更新的东西向下子节点更新
  45. {
  46. t[num*2].minn+=t[num].tag;
  47. t[num*2+1].minn+=t[num].tag;
  48. t[num*2].tag+=t[num].tag;
  49. t[num*2+1].tag+=t[num].tag;
  50. t[num].tag=0;//清除状态
  51. }
  52. int mid=(t[num].l+t[num].r)/2;
  53. if(r<=mid)
  54. update(l,r,num*2,v);
  55. else if(l>mid)
  56. update(l,r,num*2+1,v);
  57. else
  58. {
  59. update(l,mid,num*2,v);
  60. update(mid+1,r,num*2+1,v);
  61. }
  62. t[num].minn=min(t[num*2].minn,t[num*2+1].minn);//更新最小值
  63. }
  64. long long query(int l,int r,int num)//访问
  65. {
  66. if(t[num].l==l&&t[num].r==r)
  67. {
  68. return t[num].minn;
  69. }
  70. if(t[num].tag!=0)//和上面一样,pushdown
  71. {
  72. t[num*2].minn+=t[num].tag;
  73. t[num*2+1].minn+=t[num].tag;
  74. t[num*2].tag+=t[num].tag;
  75. t[num*2+1].tag+=t[num].tag;
  76. t[num].tag=0;
  77. }
  78. int mid=(t[num].l+t[num].r)/2;
  79. if(r<=mid)
  80. return query(l,r,num*2);
  81. else if(l>mid)
  82. {
  83. return query(l,r,num*2+1);
  84. }
  85. else
  86. return min(query(l,mid,num*2),query(mid+1,r,num*2+1));
  87. }
  88. int main()
  89. {
  90. int i,j,n,m,x,y,z,tag,flag;
  91. char s[100];
  92. scanf("%d",&n);
  93. for(i=0;i<n;i++)
  94. scanf("%d",&a[i]);
  95. Build(0,n-1,1);
  96. scanf("%d",&m);
  97. getchar();//吸收回车
  98. for(i=0;i<m;i++)
  99. {
  100. tag=0;
  101. gets(s);
  102. x=0,y=0,z=0;
  103. flag=0;
  104. for(j=0;j<strlen(s);j++)
  105. {
  106. if(s[j]==' ')//根据空格数目判断情况,储存数据
  107. {
  108. tag++;
  109. continue;
  110. }
  111. if(tag==0)
  112. {
  113. x=x*10+s[j]-'0';
  114. }
  115. else if(tag==1)
  116. {
  117. y=y*10+s[j]-'0';
  118. }
  119. else
  120. {
  121. if(s[j]=='-')
  122. flag=1;
  123. else
  124. {
  125. z=z*10+s[j]-'0';
  126. }
  127. }
  128. }
  129. if(flag)//z处有负号
  130. z=-z;
  131. if(tag==1)
  132. {
  133. if(x<=y)
  134. printf("%I64d\n",query(x,y,1));//codeforce要用i64d
  135. else
  136. printf("%I64d\n",min(query(0,y,1),query(x,n-1,1)));//如果输入的y大于x,就分成两个部分
  137. }
  138. else
  139. {
  140. if(x<=y)
  141. update(x,y,1,z);
  142. else
  143. {
  144. update(0,y,1,z);//同理分成两个部分
  145. update(x,n-1,1,z);
  146. }
  147. }
  148. }
  149. return 0;
  150. }

发表评论

表情:
评论列表 (有 0 条评论,245人围观)

还没有评论,来说两句吧...

相关阅读