[NYOJ 119] attackers' competitor (3) (rmq st algorithm)

Source: Internet
Author: User

There are N soldiers in a row from left to right, numbered from 1 to N. Each of them has killed different numbers of enemies. Now we have Q queries. Each time we ask, we can find the difference between the range [L, R], which has the least number of attacks and the number of attacks. Rmq st algorithm bare question.

/*ID: wuqi9395@126.comPROG: beadsLANG: C++*/#include
 
  #include
  
   #include
   
    #include
    
     #include
     
      #include
      #include
       
        #include
        
         #include
         
          #include
          
           #include
           
            #include#include
            
             #define PI acos(-1.0)#define maxn 100010#define INF 1<<25#define mem(a, b) memset(a, b, sizeof(a))typedef long long ll;using namespace std;int N, Q, mx[20][maxn], mn[20][maxn];void RMQ(int num){ for (int i = 1; i <= log2(num) + 1; i++) for (int j = 1; j <= num; j++) if (j + (1 << i) - 1 <= num) { mx[i][j] = max(mx[i - 1][j], mx[i - 1][j + (1 << i >> 1)]); mn[i][j] = min(mn[i - 1][j], mn[i - 1][j + (1 << i >> 1)]); }}int main (){ scanf(%d%d, &N, &Q); for (int i = 1; i <= N; i++) { scanf(%d, &mx[0][i]); mn[0][i] = mx[0][i]; } RMQ(N); int st, ed; while(Q--) { scanf(%d%d, &st, &ed); int k = (int) log2(ed - st + 1.0); int mxans = max(mx[k][st], mx[k][ed - (1 << k) + 1]); int mnans = min(mn[k][st], mn[k][ed - (1 << k) + 1]); printf(%d, mxans - mnans); } return 0;}
            
           
          
         
        
       
     
    
   
  
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.