fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int main() {
  4. int n,m;
  5. cin>>n>>m;
  6. long long a[n],left[n],right[n];
  7. for(int i=0;i<n;i++)
  8. cin>>a[i];
  9. for(int i=0;i<m;i++){
  10. int l,r,k;
  11. cin>>l>>r>>k;
  12.  
  13. left[0]=0;
  14. for(int i=1;i<n;i++)
  15. {
  16. if(a[i]==a[i-1])
  17. left[i]=left[i-1]; // bug was here....
  18. else
  19. left[i]=i;
  20. }
  21. right[n-1]=n-1;
  22. for(int i=n-2;i>=0;i--)
  23. {
  24. if(a[i]==a[i+1])
  25. right[i]=right[i+1]; // bug was here
  26. else
  27. right[i]=i;
  28. }
  29. for(int i=0;i<n;i++)
  30. cout<<left[i]<<" "; //left array
  31. cout<<endl;
  32. for(int i=0;i<n;i++)
  33. cout<<right[i]<<" "; //right array
  34. int mid=(l+r)/2;
  35. // Your code is incomplete... You must include some condition to check if right[mid]>R or left[mid]<L or not.
  36. if((right[mid]-left[mid]+1)>=k) // you must include the middle number. that's why add 1 to count
  37. cout<<a[mid]<<endl;
  38. else
  39. cout<<-1<<endl;
  40. }
  41. }
  42.  
Success #stdin #stdout 0s 15240KB
stdin
5 1
1 2 2 2 2
1 5 3
stdout
0 1 1 1 1 
0 4 4 4 4 2