Java线段树答案,总是TLE卡在第2和第9个示例,不知为啥,求大佬指点

P3374 【模板】树状数组 1

java 太慢了
by Celestial_Intertwine @ 2022-05-19 21:12:14


很有可能是java被卡常了
by happybob @ 2022-05-19 21:46:36


@[Eternal蒟蒻](/user/398190) 那该咋改一下呢?
by v7fgg @ 2022-05-20 19:32:25


@[happybob](/user/332914) 那该咋改一下呢?
by v7fgg @ 2022-05-20 19:32:50


我知道了,不是算法的原因,是输出没用快写的原因 ```java import java.util.*; import java.io.*; import java.math.*; public class Main{ public static void main(String args[]) throws IOException{ Read sc=new Read(); int n=sc.nextInt(),m=sc.nextInt(); long preSum[]=new long[n+1]; for(int i=0;i<n;i++){ preSum[i+1]=sc.nextInt()+preSum[i]; } SegTree sg=new SegTree(preSum,0,n-1); buildTree(sg); for(int i=0;i<m;i++){ int a=sc.nextInt(); if(a==1){ int x=sc.nextInt(),k=sc.nextInt(); addK(sg,x-1,k); } else{ int x=sc.nextInt(),y=sc.nextInt(); sc.println(findSum(sg,x-1,y-1)); } } sc.bw.flush(); sc.bw.close(); } public static long findSum(SegTree sg,int l,int r){ if(l==sg.l&&r==sg.r){ return sg.sum; } int mid=(sg.l+sg.r)/2; if(r<=mid){ return findSum(sg.left,l,r); } else if(l>=mid+1){ return findSum(sg.right,l,r); } return findSum(sg.left,l,mid)+findSum(sg.right,mid+1,r); } public static void addK(SegTree sg,int idx,int k){ sg.sum+=k; if(sg.l==sg.r){ return; } int mid=(sg.l+sg.r)/2; if(idx<=mid){ addK(sg.left,idx,k); } else{ addK(sg.right,idx,k); } } public static void buildTree(SegTree t){ if(t.l==t.r){ return; } int mid=(t.l+t.r)/2; long preSum[]=t.preSum; t.left=new SegTree(preSum,t.l,mid); t.right=new SegTree(preSum,mid+1,t.r); buildTree(t.left); buildTree(t.right); } } class SegTree{ SegTree left,right; int l,r; long sum; long preSum[]; public SegTree(long preSum[],int l,int r){ this.preSum=preSum; this.l=l; this.r=r; sum=preSum[r+1]-preSum[l]; } } class Read{ BufferedReader bf; StringTokenizer st; BufferedWriter bw; public Read(){ bf=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""); bw=new BufferedWriter(new OutputStreamWriter(System.out)); } public String nextLine() throws IOException{ return bf.readLine(); } public String next() throws IOException{ while(!st.hasMoreTokens()){ st=new StringTokenizer(bf.readLine()); } return st.nextToken(); } public char nextChar() throws IOException{ return next().charAt(0); } public int nextInt() throws IOException{ return Integer.parseInt(next()); } public long nextLong() throws IOException{ return Long.parseLong(next()); } public double nextDouble() throws IOException{ return Double.parseDouble(next()); } public float nextFloat() throws IOException{ return Float.parseFloat(next()); } public byte nextByte() throws IOException{ return Byte.parseByte(next()); } public short nextShort() throws IOException{ return Short.parseShort(next()); } public BigInteger nextBigInteger() throws IOException{ return new BigInteger(next()); } public void println(int a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } public void print(int a) throws IOException{ bw.write(String.valueOf(a)); return; } public void println(String a) throws IOException{ bw.write(a); bw.newLine(); return; } public void print(String a) throws IOException{ bw.write(a); return; } public void println(long a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } public void print(long a) throws IOException{ bw.write(String.valueOf(a)); return; } public void println(double a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } public void print(double a) throws IOException{ bw.write(String.valueOf(a)); return; } } ```
by v7fgg @ 2022-05-25 23:06:53


|