rgoewedky's blog

By rgoewedky, history, 4 years ago, In English

https://cses.fi/problemset/task/1091

I am getting TLE in two cases.

Here is my code:

import java.io.*;
import java.util.*;
public class Solution {
    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }
        String next() { // reads in the next string
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public int nextInt() { return Integer.parseInt(next()); } // reads in the next int
        public long nextLong() { return Long.parseLong(next()); } // reads in the next long
        public double nextDouble() { return Double.parseDouble(next()); } // reads in the next double
    }
     
    static InputReader f= new InputReader(System.in);
    static PrintWriter out= new PrintWriter(System.out);
    public static void main(String[] args) {
        // YOUR CODE HERE //! @ % & * () _ {} # ~ : < > ? "" | ^

        int n=f.nextInt();
        int m=f.nextInt();
        int x=0,ind=0;
    
        TreeMap<Integer,Integer> map=new TreeMap<>();
        for (int i=0;i<n;i++) {
            x=f.nextInt();
            if (map.containsKey(x)) {
                map.put(x,map.get(x)+1);
            }
            else map.put(x,1);
        }

        //sort(a);

        for (int i=0;i<m;i++) {
            x=f.nextInt();
            //ind=lower_bound(a,x);

            if (map.containsKey(x)) {
                
                if (map.get(x)>0) {
                    out.println(x);
                   map.put(x,map.get(x)-1);

                   if (map.get(x)==0) {
                       map.remove(x);
                   }
                }

                else {
                    out.println(-1);
                   // map.put(x,map.get(x)-1);
                }
                
            }

            else if (map.lowerKey(x)!=null) {
                if (map.get(map.lowerKey(x))>0) {
                    out.println(map.lowerKey(x));
                   map.put(map.lowerKey(x),map.get(map.lowerKey(x))-1);

                   if (map.get(map.lowerKey(x))==0) {
                       map.remove(map.lowerKey(x));
                   }
                }
                else out.println(-1);
            }
            else out.println(-1);
        }



        out.close(); // flushes the output once printing is done
    }
}

Full text and comments »

  • Vote: I like it
  • -1
  • Vote: I do not like it

By rgoewedky, history, 4 years ago, In English

I am getting TLE in Range Sum Query 1 of CSES problem set. Although, I pre-compute prefix sum and time complexity is also under constraints. Here is my code . Please Help. THANKS in ADVANCE

import java.io.*;
import java.util.*;

public class Main{      
	public static void main(String[] args)throws Exception{
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
         
          StringTokenizer st;
         
        st=new StringTokenizer(br.readLine()); 
        int n=Integer.parseInt(st.nextToken());
        int q=Integer.parseInt(st.nextToken());

        long prefix[]=new long[n+1];
        prefix[0]=0;

        st=new StringTokenizer(br.readLine()); 
        for (int i=1;i<=n;i++) {
          int x=Integer.parseInt(st.nextToken());
          prefix[i]=prefix[i-1]+x;
        }

    while(q-->0){
      
       st=new StringTokenizer(br.readLine()); 
      int l=Integer.parseInt(st.nextToken());
      int r=Integer.parseInt(st.nextToken());
       System.out.println(prefix[r]-prefix[l-1]);
	}
}
}


Full text and comments »

  • Vote: I like it
  • -1
  • Vote: I do not like it

By rgoewedky, 4 years ago, In English

Recently, I learned prefix sum concept . Please give me some Basic to Advanced problems to apply this concept in practice . THANKS :-)

Full text and comments »

  • Vote: I like it
  • -6
  • Vote: I do not like it