Advertisement
Morass

RMQ

Sep 23rd, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #define LG (17)
  2. #define MX (1<<LG)
  3. #define P2(v) (!(v&(v-1)))
  4. int dp[MX][LG+2],G[MX],XX,O=-1;
  5. void ini(int *A,int n){
  6.     if(!XX++)FT(1,MX)G[k]=O+=P2(k);
  7.     F(n)dp[i][0]=i;
  8.     FT(1,k-(1<<k)+n+1)F(n+1-(1<<k))
  9.         if(A[dp[i][k-1]]<A[dp[i+(1<<(k-1))][k-1]])
  10.             dp[i][k]=dp[i][k-1];
  11.         else dp[i][k]=dp[i+(1<<(k-1))][k-1];
  12. }
  13. int qy(int *A,int L,int R){
  14.     int j(G[R-L+1]);
  15.     if(A[dp[L][j]]<=A[dp[R-(1<<j)+1][j]])
  16.         return A[dp[L][j]];
  17.     return A[dp[R-(1<<j)+1][j]];
  18. }
  19. \end{lstlisting}
  20. Obdobně maxium
  21. \begin{lstlisting}[language=C++]
  22. int dp[MX][LG+2],G[MX],XX,O=-1;
  23. void ini(int *A,int n){
  24.     if(!XX++)FT(1,MX)G[k]=O+=P2(k);
  25.     F(n)dp[i][0]=i;
  26.     FT(1,k-(1<<k)+n+1)F(n+1-(1<<k))
  27.         if(A[dp[i][k-1]]>A[dp[i+(1<<(k-1))][k-1]])
  28.             dp[i][k]=dp[i][k-1];
  29.         else dp[i][k]=dp[i+(1<<(k-1))][k-1];      
  30. }
  31. int qy(int *A,int L,int R){
  32.     int j(G[R-L+1]);
  33.     if(A[dp[L][j]]>=A[dp[R-(1<<j)+1][j]])
  34.         return A[dp[L][j]];
  35.     return A[dp[R-(1<<j)+1][j]];
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement