Original title Address
Spare Table Exercises
Remember to enter the output with scanf and printf, otherwise it will be tle
Code:
1#include <iostream>2#include <cstring>3#include <cmath>4 5 using namespacestd;6 7 #defineMax_size 10000108 9 intN, Q;Ten intW[max_size]; One intq[max_size][ +]; A - voidbuild () { - for(inti =1; (1<< i) <= N; i++) { the for(intj =0; J + (1<< i) <= N; J + +) { -Q[j][i] = min (Q[j][i-1], Q[j + (1<< (I-1))][i-1]); - } - } + } - + intQueryintLintr) { A intLen = r-l +1; at inti =0; - - while((1<< (i +1)) <=len) -i++; - returnMin (Q[l][i], Q[r-(1<< i) +1][i]); - } in - intMain () { toscanf"%d", &N); + for(inti =0; i < N; i++) { -scanf"%d", &(W[i])); theq[i][0] =W[i]; * } $ Panax Notoginseng build (); - thescanf"%d", &Q); + while(q--) { A intL, R; thescanf"%d%d", &l, &R); +printf"%d\n", Query (L-1, R-1)); - } $ $ return 0;
hihocoder#1068 rmq-st algorithm