标签:struct node com std 模板 win clu stream pre
题目链接:
https://www.acwing.com/problem/content/1272/
题解:
线段树模板题,单点求和、区间查询都可
AC代码:
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <climits> using namespace std; const int N = 1e5+10; int a[N]; struct Node{ int l,r; int maxv; }tr[N*4]; int n,m; void pushup(int i){ tr[i].maxv = max(tr[2*i].maxv , tr[2*i+1].maxv); } void build(int g,int l,int r){ if(l >= r) tr[g] = {l,r,a[l]}; else{ int mid = (l+r) / 2; tr[g] = {l,r}; build(g*2,l,mid); build(g*2+1,mid+1,r); pushup(g); } } int query(int g,int l,int r){ int maxv = INT_MIN; int mid = (tr[g].l + tr[g].r) / 2; if(l <= tr[g].l && tr[g].r <= r) return tr[g].maxv; else{ if(l <= mid) maxv = max(maxv,query(g*2,l,r)); if(mid < r) maxv = max(maxv,query(g*2+1,l,r)); } return maxv; } int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]); build(1,1,n); int l,r; while(m--){ scanf("%d%d",&l,&r); // printf("%d %d\n",l,r); printf("%d\n",query(1,l,r)); // exit(0); } return 0; }
1270. 数列区间最大值(climits用法+线段树模板题)
标签:struct node com std 模板 win clu stream pre
原文地址:https://www.cnblogs.com/doubest/p/12319023.html