吐个槽,我现在线段树敲得贼熟练,但是树状数组和st表这么强大的东西竟然基本不会!!!啊啊啊,我太菜了!
代码:
#include<iostream> #include<cstdio> #include<cmath> #include<ctime> #include<queue> #include<algorithm> #include<cstring> using namespace std; #define duke(i,a,n) for(int i = a;i <= n;i++) #define lv(i,a,n) for(int i = a;i >= n;i--) #define clean(a) memset(a,0,sizeof(a)) const int INF = 1 << 30; const int N = 300005; typedef long long ll; typedef double db; template <class T> void read(T &x) { char c; bool op = 0; while(c = getchar(), c < '0' || c > '9') if(c == '-') op = 1; x = c - '0'; while(c = getchar(), c >= '0' && c <= '9') x = x * 10 + c - '0'; if(op) x = -x; } template <class T> void write(T x) { if(x < 0) putchar('-'), x = -x; if(x >= 10) write(x / 10); putchar('0' + x % 10); } int st[1000005][23],a[1000005],n,m; void build() { duke(i,1,n) st[i][0] = a[i]; duke(i,1,21) { for(int j = 1;j + (1 << i) - 1 <= n;j++) { st[j][i] = max(st[j][i - 1],st[j + (1 << (i - 1))][i - 1]); } } } void query(int l,int r) { int y = log2(r - l + 1); int maxn = 0; maxn = max(st[l][y],st[r - (1 << y) + 1][y]); printf("%d ",maxn); } int main() { read(n);read(m); duke(i,1,n) read(a[i]); build(); while(m--) { int l,r; read(l);read(r); query(l,r); } return 0; }