For many programming quizzes we are given a bunch of input lines and we have to process each input , do some computation and output the result.
My question is what is the best way to optimize the runtime of the solution ?
- Read all input, store it (in array or something) ,compute result for all of them, finally output it all together.
or 2. Read one input, compute the result, output the result and so on for each input given.
Since no answer was specific I would ask which approach is the best for problems like this:
Quadrant Queries (30 points)
There are N points in the plane. The ith point has coordinates (xi, yi). Perform the following queries:
1) Reflect all points between point i and j both including along the X axis. This query is represented as "X i j"
2) Reflect all points between point i and j both including along the Y axis. This query is represented as "Y i j"
3) Count how many points between point i and j both including lie in each of the 4 quadrants. This query is represented as "C i j"
The first line contains N, the number of points. N lines follow.
ith line contains xi and yi separated by a space.
The next line contains Q the number of queries. The next Q lines contain one query each, of one of the above forms.
All indices are 1 indexed.
Output one line for each query of the type "C i j". The corresponding line contains 4 integers; the number of points having indices in the range [i..j] in the 1st,2nd,3rd and 4th quadrants respectively.
1 <= N <= 100000 1 <= Q <= 1000000
You may assume that no point lies on the X or the Y axis.
All (xi,yi) will fit in a 32-bit signed integer