Страница 2 Can you suggest what is causing this error. N = int(input()) s = [0] for i in range(N): k = int(input()) s.append(int(s[i] + k)) Q = int(input()) while Q: l, r = map(int, input().split()) print(s[r] - s[l-1]) Q -= 1 Time limit test 20 failed on python. Rewrited on C# - everything OK. Edited by author 04.10.2019 02:08 Edited by author 04.10.2019 02:08 1. Segment tree. 2. Fenwick tree. 3. Partial sums (b[i] = SUM(a[0], ..., a[i])). 4. Sqrt decomposition. Hope, it helps to understand how to solve this task. Edited by author 28.02.2017 15:25 Use scanf and printf instead of cin,cout. I had a really hard time to figure it out)) if you have TLE #20. Use BufferedReader and Printwriter in Java. Good luck... Thanks a lot! I don't really understand why PrintWriter works faster than System.out... Hello My python code is as follows: [code deleted] I get TLE in # 20 Where can I save time? Edited by moderator 01.12.2019 21:35 I'm not sure, but maybe a lot of .append()'s could work too slow... Maybe you could try a = [0] * n and then a[i] = ... Hello When I change my solution with a=[0]*n it takes 0.546 sec Before the change it took 0.515 sec. The first hint brings no AC Edited by author 20.09.2013 22:41 Edited by author 20.09.2013 22:41 Tough to AC, but possible (finally I managed to do it). Since there can be up to 100000 queries, bottleneck is output as usual with Python. Don't use print - use sys.stdout.write() instead (interestingly, why you used sys.stdin for input and print for output?) Edited by author 18.09.2013 04:30 Using sys.stdout.write ('\n'.join ([str (x) for x in b]) My solution takes .515 sec No AC Looking at TLE times in testing queue is a misleading way to analyze your code effectiveness (actually, it shows time after which your code was halted with TLE verdict rather than acrtual running time). Test it locally on large tests and measure time efficiency of each alternative. Also, I see you submit on Python 3. That may be the problem - I ACed on Python 2. I used python 2.7.5 this time. My code was unchanged AC in .468 Thanks for all the advise Edited by author 21.09.2013 03:57 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class N1330 {
public static void main(String[] args) {
try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(in.readLine()); int list[] = new int[n]; for (int i = 0; i < n; i++) list[i] = Integer.parseInt(in.readLine()); int q = Integer.parseInt(in.readLine()); for (int i = 0; i < q; i++) { int sum = 0; StringTokenizer tok = new StringTokenizer(in.readLine()); int start = Integer.parseInt(tok.nextToken())-1; int end = Integer.parseInt(tok.nextToken())-1; for (int j = start; j <= end; j++) sum += list[j]; System.out.println(sum); }
} catch(IOException e) {
}
} } Same algorithm (sum[b]-sum[a-1]) got AC using C/C++, but got time exceeded using Java I'm using StreamTokenizer and have TLE on 21 test :((( Who wants solution, e-mail me: nevertheless_94@hotmail.com :D OMG!!! OMG!!! OMG!!! OMG!!! OMG!!! But, 0.125, 144 kb I use Vector and have 0.125s, 196kb When i use just array: 0.109s, 150kb :) Edited by author 04.12.2011 22:13 [code deleted] Edited by moderator 01.12.2019 21:45 I dont know java, therefore I may be mistake. Try to use binary search. I also using Java, but have AC with 0.25 s ans ~4.5 Mb Try to use array of array of partial sums. P.S. sorry for my bad English Use Fenwick Tree :) No Fenwick Tree, just summ[y] - summ[x-1]. I recommend you to use the simplest input you have. 1. For a line with one number just use int.Parse(Console.ReadLine()) 2. For a line with two numbers use the simplest parser string query = Console.ReadLine(); int separator = query.IndexOf(' '); int a = int.Parse(query.Substring(0, separator)) - 1, b = int.Parse(query.Substring(separator + 1)) - 1; Otherwise you 'll get TL. P.S. Does anybody know a faster input? 5 1 2 3 -1 4 2 1 4 4 1 Resul will be: 5 5 ??? My solve O(N), but output too slow. How fast print answer? printf() I use printf. Also slow. Edited by author 11.08.2010 14:00 I think time 0.031 was on old server, now as I can see there is no 0.031 solutions. my solution has 0.062 Edited by author 11.08.2010 14:02 06:43:27 1 окт 2009, 0.031s. 14:54:16 23 июл 2009 Andrew Hoffmann aka SKYDOS [Vladimir SU] C++ 0.062 Edited by author 11.08.2010 14:09 Yep! old server I think. my solution uses printf(), scanf()... so faster - I dont know how to do) 06:43:27 1 окт 2009, 0.031s. I do not think that the old server worked faster new or incorrectly measured time. I think that the reason not in it. Yes, you are right. Now the output works very slowly. Can be there are even ways quickly to print symbols, except cout and printf? I dont know, I am using C# and there is no way to print faster and using less memory. Possible to read faster and with less memory...thats all. I suppose there's not such a way, as long as you wish to use Microsoft's stdio. Otherwise you'll have to print chars directly to output, using sth like asm, about which I know nothing. Страница 1 [code deleted] Edited by moderator 01.12.2019 21:31 thanks bro i didn't use your program to get ac but i learned some tricks from it that helped me solve some other problems Actually u dont need __int64 and btw ure solution get TLE now, use printf, scanf instead of cin. cout Edited by author 18.03.2015 23:50 Navchin Crash barib duribdi? here is my C code #include<stdio.h> #include<stdlib.h> int main(void) { int n,x,i,l,m; long *a=0,*b=0; scanf("%d",&n); a=(long*)malloc((n+1)*n); a[0]=0; for(i=1;i<n+1;++i) { scanf("%d",&x); a[i]=a[i-1]+ x; } scanf("%d",&x); b=(long*)malloc(x*sizeof(long)); for(i=0;i<x;++i) { scanf("%d %d",&l,&m); b[i]=a[m]-a[l-1]; } for(i=0;i<x-1;++i) printf("%ld\n",b[i]); printf("%ld",b[i]); return 0; } I got WA on test 18 using binary tree! does somebody know the test? [code deleted] Edited by moderator 01.12.2019 21:49 Don't use new Integer(), use Integer.parseInt() instead Thank you. I solved my problem, but diffrent way. I used : in.readLine().trim() instead of in.readLine() Edited by author 12.10.2008 21:20 AC this problem is very simple! Try to solve the problem with O(n). Try to solve it in O(log n) //disregarding the input, of course ;) the fastest algorithm is O(N+Q). Faster is impossible! Building RMQ doesn't have a sense, because you don't using the operation update. My C++ solution was successful with 0.14 sec and 284k. Now i wrote the same algorithm in C#, and on 20 test i have ML on char[] separator = { ' ', '\n', '\r', '\t' }; string[] input = Console.In.ReadToEnd().Split(separator, StringSplitOptions.RemoveEmptyEntries); int k = 0; int n = int.Parse(input[k++]); ... And TL on int n = int.Parse(Console.ReadLine()); ... So, how I should read input data in C# to have AC? I got AC! In TL version i wrote in cycle string[] input = Console.ReadLine().Split(' '); int l = int.Parse(input[0]), r = int.Parse(input[1]); in AC version string input = Console.ReadLine(); int pos = input.IndexOf(' '); int l = int.Parse(input.Substring(0, pos)), r = int.Parse(input.Substring(pos + 1)); Whith this optimization program works 0.047 sec less, and i got AC :) |
|