If you have "Time limit exceeded", just use sys.stdin.read()... and it will put load on memory, as I suppose reading huge amount of data via input() takes forever. 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 I got AC in 0.125s and 284KB!!! If you want my AC code,post me:happyzeyu@163.com Edited by author 25.12.2006 17:30 Edited by author 25.12.2006 17:30 I got AC in 0.109s abd 306KB!! Can I see your code? Post me:happyzeyu@163.com I got AC in 0.093 and 175KB :)) in russian that mean : "Пиписькомерство" *THUMBS UP* 0.031 - 189 Kb Just "Пиписькомерство"! :) Pascal - 0.093s 154 КБ PS "Пиписькомерство" - +1 =)) pascal 0.093s 122k d**kcomparing)))) My Java solution got TLE#20, but my dick is still bigger no matter what. haha, I am spend 0.078s and 197 k If you want my AC code, send mail to k421668239@gmail.com "ПИписькомерство")))+1 but for what it???for me more important the result!!!!! not how it is!!!!!! sorry for my english) 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 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... 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 How to do my program more simply? Who khow? When I send this program - Time limit Exceed on 20 test. Help, who can! Program zadacha; var a:array[1..10000] of integer; b,c,i,j,n:integer; d:array[0..100000] of longint; m:longint; begin readln(n); for i:=1 to n do read(a[i]); readln(m); for i:=1 to m do begin read(b,c); for j:=b to c do d[i]:=d[i]+a[j]; end; for i:=1 to m do writeln(d[i]); end. 100.000 * 10.000 = 1.000.000.000 change algoritm (he easy) 100.000 * 10.000 = 1.000.000.000 change algoritm (he easy) So, what algoritm must I use there? Can you tell me? I don't understand. Please, write once again. 6 4 2 3 1 5 7 2 2 4 3 5 {k,n} answer: (4+2+3+1)-(4)=6 (4+2+3+1+5)-(4+2)=9 Clearly: a[n]-a[k-1] Thank you very much. I don't see if it works faster than summing from i to j directly. Summing from 1 to j requires more work and then we yet have to compute another sum and subtract. Use scanf and printf instead of cin,cout. I had a really hard time to figure it out)) [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 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 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 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 :((( [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. 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. 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; } |
|