Advertisement
Saleh127

UVA 11235

Aug 1st, 2021
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define test int t; cin>>t; for(int cs=1;cs<=t;cs++)
  5.  
  6. ll a[100005];
  7. ll tree[400005];
  8. ll lf[100005],rf[100005];
  9.  
  10.  
  11. void treeconstruct(ll node,ll b,ll e)
  12. {
  13.  
  14. if(b==e)
  15. {
  16. tree[node]=lf[b];
  17. return;
  18. }
  19.  
  20. ll left = node*2;
  21. ll right = node*2 + 1ll;
  22.  
  23. ll mid=(b+e)/2;
  24.  
  25. treeconstruct(left,b,mid);
  26. treeconstruct(right,mid+1,e);
  27.  
  28. tree[node]=max(tree[left],tree[right]);
  29.  
  30. }
  31.  
  32. ll queries(ll node,ll b,ll e,ll i,ll j)
  33. {
  34. if(i>e || j<b) return INT_MIN;
  35.  
  36. if(b>=i && e<=j) return tree[node];
  37.  
  38.  
  39. ll left = node*2;
  40. ll right = node*2 + 1ll;
  41.  
  42. ll mid=(b+e)/2;
  43.  
  44. ll x=queries(left,b,mid,i,j);
  45.  
  46. ll y=queries(right,mid+1,e,i,j);
  47.  
  48. return max(x,y);
  49.  
  50. }
  51.  
  52.  
  53.  
  54. int main()
  55. {
  56. ios_base::sync_with_stdio(0);
  57. cin.tie(0);
  58. cout.tie(0);
  59.  
  60.  
  61. ll n,q,i,j,k,l,r;
  62.  
  63. while(cin>>n && n)
  64. {
  65. cin>>q;
  66.  
  67. for(i=1;i<=n;i++)
  68. {
  69. cin>>a[i];
  70.  
  71. if(i>1 && a[i]==a[i-1])
  72. {
  73. lf[i]=lf[i-1]+1;
  74. }
  75. else lf[i]=1;
  76. }
  77.  
  78. for(i=n;i>=1;i--)
  79. {
  80. if(i<n && a[i]==a[i+1])
  81. {
  82. rf[i]=rf[i+1]+1;
  83. }
  84. else rf[i]=1;
  85. }
  86.  
  87.  
  88. treeconstruct(1ll,1ll,n);
  89.  
  90. while(q--)
  91. {
  92. cin>>l>>r;
  93.  
  94. if(a[l]==a[r]) cout<<(r-l+1)<<endl;
  95. else
  96. {
  97. k=queries(1,1,n,l+rf[l],r);
  98.  
  99. cout<<max(k,rf[l])<<endl;
  100. }
  101. }
  102.  
  103. }
  104.  
  105.  
  106. return 0;
  107. }
  108.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement