Advertisement
Zeinab_Hamdy

Untitled

Sep 15th, 2023
996
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.31 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define nl "\n"
  4. #define fi first
  5. #define se second
  6. #define pb push_back
  7. #define ll long long
  8. #define ull unsigned ll
  9. #define RV  return void
  10. #define inf 2000000000
  11. #define sz(x) int(x.size())
  12. #define all(v) v.begin(), v.end()
  13. #define rall(v) v.rbegin(), v.rend()
  14. #define Mini(x) *min_element(all(x))
  15. #define Maxi(x) *max_element(all(x))
  16. #define fixed(n) fixed << setprecision(n)
  17. #define ceil(w, m) (((w) / (m)) + ((w) % (m) ? 1 : 0))
  18. #define cin(v) for (auto&i:v) cin >> i;
  19. #define cout(v) for (auto&i:v) cout << i << " ";
  20. #define clr(memo, x) memset(memo, x, sizeof memo)
  21. #define updmin(a, b) a = min(a, b)
  22. #define updmax(a, b) a = max(a, b)
  23. #define vi vector < int >
  24. #define vl vector < ll >
  25. #define vc vector < char >
  26. #define vs vector < string >
  27. #define v2i vector < vector < int > >
  28. #define v2l vector < vector < int > >
  29. #define seti set < int >
  30. #define setl set < ll >
  31. #define mapii map < int , int >
  32. #define mapll map < ll , ll >
  33. #define mapli map < ll , int >
  34. #define mapci map < char , int >
  35. #define mapsi map < string , int >
  36. #define pll pair < ll , ll >
  37. #define pii pair < int , int >
  38. #define range(l,r,x) for(int i=l ; i < r ; i+=x)
  39. #define FastCode ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  40. vector < string > ternary= {"NO\n" , "YES\n"};
  41.  
  42. void  Zainab(){
  43.             #ifndef ONLINE_JUDGE
  44.               freopen("input.txt", "r", stdin);
  45.               freopen("output.txt", "w", stdout);
  46.             #endif
  47. }
  48.  
  49.  
  50. /*================================  solution  ================================ */
  51.  
  52.  
  53.  
  54. struct Data{
  55.     int L , R , Idx;
  56. };
  57.  
  58. void myCode(){
  59.  
  60. int n , q;
  61. cin >> n >> q ;
  62. vector < ll > v(n) ;
  63. cin(v);
  64.  
  65. //  l , r of every subsequence (staify the condition on the problem)
  66.  
  67. vector < pair < int , int > > prs ;
  68. for(int i =0 ; i < n ; i++){
  69.     int st = i;
  70.     i++;
  71.     while(i < n and v[i] >= v[i-1]) i++;
  72.     int idx = i;
  73.     while( i < n and v[i] <=v[i-1]) i++;
  74.  
  75.     int ni=i-1;
  76.     if(idx !=i){
  77.         while(ni >=0 and v[ni]==v[i-1]){
  78.             ni--;
  79.         }
  80.     }
  81.    
  82.         if( i !=st  ){
  83.             prs.pb({st , i-1});
  84.         }
  85.      
  86.     i= ni;
  87. }
  88.  
  89.  
  90. sort(all(prs) , [&](pair < int , int > a , pair < int , int > b){
  91.     return a.se < b.se ;
  92. });
  93.  
  94.  
  95. // for(auto& [f,s] : prs){
  96. //   cout << f << " " << s << nl;
  97. // }
  98.  
  99. // l , r , index of every query
  100.  
  101. vector < Data > query ;
  102. vector < bool > ans(q);
  103.  
  104. for(int i =0 ; i < q ; i++){
  105.     int l ,r ;
  106.     cin >> l >> r;
  107.     l-- , r--;
  108.     // if(l==r or l+1 == r ){
  109.     //     //  one or two elements only
  110.     //     cout << "Yes\n";
  111.     //     continue;
  112.     // }
  113.  
  114.     query.pb({l , r , i});
  115.  
  116. }
  117.  
  118. sort(all(query) , [&](Data a , Data b){
  119.     return a.R  > b.R ;
  120. });
  121.  
  122. set<int> st;
  123.  
  124.  
  125. for(auto[L,R,Idx] : query){
  126.     while(!prs.empty() and  prs.back().second >= R)
  127.         st.insert(prs.back().first) , prs.pop_back();
  128.     auto it = st.lower_bound(L);
  129.    
  130.     ans[Idx] = (it != st.end());
  131. }
  132.  
  133. for(int i =0 ; i < q ; i++){
  134.     cout << (ans[i] ? "Yes\n" : "No\n");
  135. }
  136.  
  137.  
  138. }
  139.  
  140.  
  141. int main(){
  142.                                    FastCode ;
  143.                                      Zainab() ;
  144.  
  145.     int testCase=1;
  146.           // cin >> testCase ;
  147.       for(int i=1 ; i<= testCase ; i++)
  148.         myCode();
  149.  
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement