1 条题解
-
0
C :
int main(int argc, char* argv[]) {char f[2]; int a,b,i,score[30001],N,M,max; while(~scanf("%d%d",&N,&M)) { for(i=1;i<=N;i++) scanf("%d",&score[i]); while(M--) {scanf("%s%d%d",&f,&a,&b); max=0; if(f[0]=='Q'){ max=score[a]; for(i=a+1;i<=b;i++) if(score[i]>max)max=score[i]; printf("%d\n",max); } else if(f[0]=='U') { score[a]=b; } } } return 0; }C++ :
#include<cstdio> #include<algorithm> using namespace std; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 const int maxn=222222; int MAX[maxn<<2]; void PushUP(int rt) { MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]); } void build(int l,int r,int rt) { if(l==r) { scanf("%d",&MAX[rt]); return ; } int m=(l+r)>>1; build(lson); build(rson); PushUP(rt); } void update(int p,int sc,int l,int r,int rt) { if(l==r) { MAX[rt]=sc; return ; } int m=(l+r)>>1; if(p<=m) update(p,sc,lson); else update(p,sc,rson); PushUP(rt); } int query(int L,int R,int l,int r,int rt) { if(L<=l&&r<=R) { return MAX[rt]; } int m=(l+r)>>1; int ret=0; if(L<=m) ret=max(ret,query(L,R,lson)); if(R>m) ret=max(ret,query(L,R,rson)); return ret; } int main() { int n,m; while(scanf("%d%d",&n,&m)!=EOF) { build(1,n,1); while(m--) { char op[2]; int a,b; scanf("%s%d%d",op,&a,&b); if(op[0]=='Q') printf("%d\n",query(a,b,1,n,1)); else update(a,b,1,n,1); } } return 0; }Java :
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in=new Scanner(System.in); while(in.hasNextInt()){ int n=in.nextInt(); int m=in.nextInt(); int mark[]=new int[n]; int a,b; for(int i=0;i<n;i++) mark[i]=in.nextInt(); for(int j=1;j<=m;j++){ String c=in.next(); char s=c.charAt(0); if(s=='Q') { a =in.nextInt()-1; b =in.nextInt()-1; int max=mark[a]; for(int k=a+1;k<=b;k++) if(mark[k]>max) max=mark[k]; System.out.println(max); } if(s=='U') { a =in.nextInt(); b =in.nextInt(); mark[a-1]=b; } } } } }
- 1
信息
- ID
- 1421
- 时间
- 1000ms
- 内存
- 32MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者