Which of the following is a quick question about scanf and cin? scanf and cin
Start with c ++ and run cin and cout all day long.
Cout times out until one day when cin is used.
Scanf is much faster than cin.
However, I later heard that the cin with ios: sync_with_stdio (false); is the same as that with Fei.
So which one is faster?
Let's do a small test.
Question: https://www.luogu.org/problem/show? Pid = 3368 # sub
Tree array template 2 (because the data is big)
First, the cin and cout of the turtle speed
The successful T drops three points.
=. =
What about scanf ??
Complete the task perfectly !!
Ios: sync_with_stdio (false); what about it ??
Witness a miraculous moment...
Not as fast as scanf
But it can also be AC
In addition, I have to mention the reading Optimization of supergods.
Faster than scanf
To sum up:
If you are too reluctant to read and optimize
Use scanf ..
Code:
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 using namespace std; 5 const int MAXN = 500001; 6 int n, m; 7 int a [MAXN]; 8 int tree [MAXN]; 9 int lowbit (int p) 10 {return p & (-p);} 11 12 void interval_increase (int x, int v) 13 {14 for (int I = x; I> 0; I = I-lowbit (I) 15 {16 tree [I] + = v; 17} 18} 19 20 int point_ask (int p) 21 {22 int ans = a [p]; 23 for (int I = p; I <= n; I = I + lowbit (I) 24 {25 ans = ans + tree [I]; 26} 27 return ans; 28} 29 int main () 30 {31 // ios: sync_with_stdio (false); 32 cin> n> m; 33 for (int I = 1; I <= n; I ++) 34 cin> a [I]; 35 for (int I = 1; I <= m; I ++) 36 {37 int how; 38 cin> how; 39 if (how = 1) // increase by 40 {41 int x, y, v; 42 cin> x> y> v; 43 interval_increase (y, v); 44 interval_increase (x-1,-v); 45} 46 else47 {48 int p; 49 cin> p; 50 cout <point_ask (p) <endl; 51} 52} 53 return 0; 54}Cin 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 using namespace std; 5 const int MAXN = 500001; 6 int n, m, p; 7 int tree [MAXN]; // 8 int lowbit (int p) 9 {10 return p & (-p); 11} 12 void point_increase (int w, int v) 13 {14 for (int I = w; I <= n; I = I + lowbit (I) 15 tree [I] = tree [I] + v; 16 return; 17} 18 int interval_ask (int x) 19 {20 int ans = 0; 21 for (int I = x; I! = 0; I = I-lowbit (I) 22 {23 ans = ans + tree [I]; 24} 25 return ans; 26} 27 int main () 28 {29 scanf ("% d", & n, & m); 30 for (int I = 1; I <= n; I ++) 31 {32 scanf ("% d", & p); 33 point_increase (I, p); 34} 35 for (int I = 1; I <= m; I ++) 36 {37 scanf ("% d", & p); 38 if (p = 1) // Add 39 {40 int x, y; 41 scanf ("% d", & x, & y); 42 point_increase (x, y); 43} 44 else // sum 45 {46 int x, y; 47 scanf ("% d", & x, & y); 48 printf ("% d \ n", interval_ask (y)-interval_ask (x-1 )); 49} 50} 51 return 0; 52}Nice scanf 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 using namespace std; 5 const int MAXN = 500001; 6 int n, m; 7 int a [MAXN]; 8 int tree [MAXN]; 9 int lowbit (int p) 10 {return p & (-p);} 11 12 void interval_increase (int x, int v) 13 {14 for (int I = x; I> 0; I = I-lowbit (I) 15 {16 tree [I] + = v; 17} 18} 19 20 int point_ask (int p) 21 {22 int ans = a [p]; 23 for (int I = p; I <= n; I = I + lowbit (I) 24 {25 ans = ans + tree [I]; 26} 27 return ans; 28} 29 int main () 30 {31 ios :: sync_with_stdio (false); 32 cin> n> m; 33 for (int I = 1; I <= n; I ++) 34 cin> a [I]; 35 for (int I = 1; I <= m; I ++) 36 {37 int how; 38 cin> how; 39 if (how = 1) // increase 40 {41 int x, y, v; 42 cin> x> y> v; 43 interval_increase (y, v); 44 interval_increase (x-1, -v); 45} 46 else47 {48 int p; 49 cin> p; 50 cout <point_ask (p) <endl; 51} 52} 53 return 0; 54}Good cin optimization 1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 using namespace std; 5 const int MAXN = 500001; 6 int n, m; 7 int a [MAXN]; 8 int tree [MAXN]; 9 int lowbit (int p) 10 {return p & (-p );} 11 12 int read (int & n) 13 {14 char ch = ''; int q = 0, w = 1; 15 for (; (ch! = '-') & (Ch <'0') | (ch> '9'); ch = getchar ()); 16 if (ch = '-') w =-1, ch = getchar (); 17 for (; ch> = '0' & ch <= '9 '; ch = getchar () q = q * 10 + ch-48; 18 n = q * w; return n; 19} 20 21 void interval_increase (int x, int v) 22 {23 for (int I = x; I> 0; I = I-lowbit (I) 24 {25 tree [I] + = v; 26} 27} 28 29 int point_ask (int p) 30 {31 int ans = a [p]; 32 for (int I = p; I <= n; I = I + lowbit (I) 33 {34 ans = ans + tree [I]; 35} 36 return ans; 37} 38 int main () 39 {40 ios :: sync_with_stdio (false); 41 read (n); 42 read (m); 43 for (int I = 1; I <= n; I ++) 44 read (a [I]); 45 for (int I = 1; I <= m; I ++) 46 {47 int how; 48 read (how ); 49 if (how = 1) // increase by 50 {51 int x, y, v; 52 read (x); 53 read (y); 54 read (v ); 55 interval_increase (y, v); 56 interval_increase (x-1,-v); 57} 58 else59 {60 int p; 61 read (p); 62 printf ("% d ", point_ask (p); 63 putchar ('\ n'); 64} 65} 66 return 0; 67}Fast Read Optimization