退役后学java...
裸线段树
//By SiriusRen import java.util.*; import java.math.*; public class Main{ public static void build(int l,int r,int pos,int []tree,int []score){ if(l==r){tree[pos]=score[l];return;} int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1; build(l,mid,lson,tree,score);build(mid+1,r,rson,tree,score); tree[pos]=Math.max(tree[lson],tree[rson]); } public static void insert(int l,int r,int pos, int num,int wei,int []tree){ if(l==r){tree[pos]=wei;return;} int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1; if(mid<num)insert(mid+1,r,rson,num,wei,tree); else if(mid>=num)insert(l,mid,lson,num,wei,tree); tree[pos]=Math.max(tree[lson],tree[rson]); } public static int query(int l,int r,int pos,int L,int R,int []tree){ if(l>=L&&r<=R)return tree[pos]; int mid=(l+r)>>1,lson=pos<<1,rson=pos<<1|1; if(mid<L)return query(mid+1,r,rson,L,R,tree); else if(mid>=R)return query(l,mid,lson,L,R,tree); else return Math.max(query(l,mid,lson,L,R,tree),query(mid+1,r,rson,L,R,tree)); } public static void main(String args[]){ int []score=new int[200005]; int []Tree=new int[3500000]; Scanner in=new Scanner(System.in); while(in.hasNextInt()){ int n=in.nextInt(),m=in.nextInt(); for(int i=1;i<=n;i++)score[i]=in.nextInt(); build(1,n,1,Tree,score); for(int i=1;i<=m;i++){ String p=in.next(); if(p.equals("Q")){ int jyx=in.nextInt(),jyy=in.nextInt(); int jy=query(1,n,1,jyx,jyy,Tree); System.out.println(jy); } else{ int jyx=in.nextInt(),jyy=in.nextInt(); insert(1,n,1,jyx,jyy,Tree); } } } } }
时间: 2024-10-04 21:40:28