hkshakib

Untitled

Mar 21st, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int k = 16;
  4. const int N = 1e5;
  5. const int ZERO = 1e9 + 1;
  6. int table[N][k + 1];
  7. int Arr[N];
  8.  
  9. int main()
  10. {
  11. int t,cnt=1;
  12. cin>>t;
  13. while(t--)
  14. {
  15. int n, L, R, q;
  16. scanf("%d%d",&n,&q);
  17. for(int i = 0; i < n; i++)
  18. scanf("%d",&Arr[i]);
  19.  
  20. for(int i = 0; i < n; i++)
  21. table[i][0] = Arr[i];
  22. for(int j = 1; j <= k; j++)
  23. {
  24. for(int i = 0; i <= n - (1 << j); i++)
  25. table[i][j] = min(table[i][j - 1], table[i + (1 << (j - 1))][j - 1]);
  26. }
  27. printf("Case %d:\n",cnt++);
  28. for(int i = 0; i < q; i++)
  29. {
  30. scanf("%d%d",&L,&R);
  31. L--,R--;
  32. int answer = ZERO;
  33. for(int j = k; j >= 0; j--)
  34. {
  35. if(L + (1 << j) - 1 <= R)
  36. {
  37. answer = min(answer, table[L][j]);
  38. L += 1 << j;
  39. }
  40. }
  41. cout << answer << endl;
  42. }
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment