Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. /*
  6.  
  7. Bismillahir Rahmanir Rahim
  8. Problem :
  9. Problem Link :
  10. Topics :
  11. Solver : Masud Parves
  12. I Love Code More than Sharks Love Blood <3
  13. */
  14.  
  15.  
  16. #define ff first
  17. #define ss second
  18. #define pb push_back
  19. #define mp make_pair
  20. #define all(a) a.begin(), a.end()
  21.  
  22.  
  23. #define sf(a) scanf("%d",&a)
  24. #define sff(a,b) scanf("%d%d",&a,&b)
  25. #define sfff(a,b,c) scanf("%d%d%d",&a,&b,&c)
  26.  
  27. #define f0(i,b) for(int i=0;i<(b);i++)
  28. #define f1(i,b) for(int i=1;i<=(b);i++)
  29. #define f2(i,a,b) for(int i=(a);i<=(b);i++)
  30. #define fr(i,b,a) for(int i=(b);i>=(a);i--)
  31.  
  32. #define CIN ios_base::sync_with_stdio(0)
  33. #define mx 100005
  34. #define TEST_CASE(t) for(int z=1 ; z<=t ; z++)
  35. #define PRINT_CASE printf("Case %d: ",z)
  36. #define NOT_VISITED 0
  37. #define IS_VISITED 1
  38.  
  39.  
  40.  
  41. int fx[4]= {1,-1,0,0};
  42. int fy[4]= {0,0,1,-1};
  43.  
  44.  
  45. const double PI = acos(-1.0);
  46. const double EPS = 1e-6;
  47. const int MOD = (int)1e9+7;
  48. const int maxn = (int)2e5+5;
  49.  
  50. typedef long long ll;
  51. typedef unsigned long long ull;
  52. typedef pair<int, int> pii;
  53. typedef pair<ll, int> plli;
  54. typedef pair<int, ll> pill;
  55.  
  56. int arr[mx];
  57.  
  58. struct segment
  59. {
  60. int ans,preValue,suffValue,preCnt,SuffCnt;
  61.  
  62. void marge(segment left, segment right)
  63. {
  64. preValue=left.preValue;
  65. preCnt=left.preCnt; /// if value a[i] , a[i+1] are not same
  66. suffValue=right.suffValue;
  67. SuffCnt=right.SuffCnt; /// if value a[i] , a[i+1] are not same
  68.  
  69. if(left.preValue==right.preValue) ///if value a[i] , a[i+1] are not same
  70. {
  71. preCnt=left.preCnt+right.preCnt;
  72. }
  73. if(left.suffValue==right.suffValue)
  74. {
  75. SuffCnt=left.SuffCnt+right.SuffCnt;
  76. }
  77.  
  78. ans=max(left.ans, right.ans);
  79.  
  80. if(left.suffValue==right.preValue)
  81. {
  82. ans=max(ans, left.SuffCnt+right.preCnt);
  83. }
  84.  
  85. }
  86. } tree[4*mx];
  87.  
  88. void BuildTree(int node, int b, int e )
  89. {
  90. if(b>e)
  91. return;
  92. if(b==e)
  93. {
  94. tree[node].preValue=tree[node].suffValue=arr[b];
  95. tree[node].preCnt=tree[node].SuffCnt=1;
  96. tree[node].ans=1;
  97. return;
  98. }
  99.  
  100. int mid=(b+e)/2,left=node*2,right=left+1;
  101.  
  102. BuildTree(left, b, mid);
  103. BuildTree(right, mid+1, e);
  104.  
  105. tree[node].marge(tree[left], tree[right]);
  106.  
  107. }
  108.  
  109.  
  110. segment Query(int node, int b, int e, int l, int r)
  111. {
  112. if(b>e || b>r || e<l)
  113. {
  114. segment res;
  115. res.preValue=0;
  116. res.suffValue=0;
  117. res.preCnt=0;
  118. res.SuffCnt=0;
  119. res.ans=0;
  120. return res;
  121. }
  122.  
  123. if(b>=l && e<=r)
  124. {
  125. return tree[node];
  126. }
  127.  
  128. int mid=(b+e)/2,left=node*2,right=left+1;
  129.  
  130. segment lft,rgt,result;
  131. lft=Query(left, b, mid, l, r);
  132. rgt=Query(right, mid+1, e, l, r);
  133.  
  134. result.marge(lft,rgt);
  135.  
  136. return result;
  137. }
  138.  
  139. int n,q,l,r;
  140. int main()
  141. {
  142. CIN;
  143. cin.tie(NULL);
  144. cout.tie(NULL);
  145. //freopen("input.txt", "r", stdin);
  146. //freopen("output.txt", "w", stdout);
  147.  
  148. /*
  149. 10 3
  150. -1 -1 1 1 1 1 3 10 10 10
  151. 2 3
  152. 1 10
  153. 5 10
  154. 0
  155. */
  156. sf(n);
  157.  
  158.  
  159. while(n)
  160. {
  161. sf(q);
  162.  
  163. for(int i=1 ; i<=n ; i++)
  164. {
  165. sf(arr[i]);
  166. }
  167. BuildTree(1, 1, n);
  168.  
  169. while(q--)
  170. {
  171. sff(l,r);
  172.  
  173. printf("%d\n", Query(1,1,n,l,r).ans);
  174. }
  175. sf(n);
  176. }
  177. return 0;
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement