标签:nice show sync cli png interval names display www
一开始入c++的时候成天跑cin,cout
直到有一天用cin,cout超时
才知道scanf比cin快的多
但是后来又听说加了ios::sync_with_stdio(false);的cin跟飞一样
那么到底哪个快呢?
咱们来做个小测试
题目选择:https://www.luogu.org/problem/show?pid=3368#sub
树状数组模板2(因为这题数据比较大)
首先是龟速的cin与cout
成功的T掉三个点
=.=
那么scanf呢??
完美的完成任务!!
身负众望的ios::sync_with_stdio(false);呢??
见证奇迹的时刻。。。
速度虽然不及scanf
但是也是可以AC的
另外不得不提一下超神的读入优化
快于scanf
综上所述:
如果实在懒得敲读入优化
还是乖乖的用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)// 增加 40 { 41 int x,y,v; 42 cin>>x>>y>>v; 43 interval_increase(y,v); 44 interval_increase(x-1,-v); 45 } 46 else 47 { 48 int p; 49 cin>>p; 50 cout<<point_ask(p)<<endl; 51 } 52 } 53 return 0; 54 }
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%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)// 加 39 { 40 int x,y; 41 scanf("%d%d",&x,&y); 42 point_increase(x,y); 43 } 44 else// 求和 45 { 46 int x,y; 47 scanf("%d%d",&x,&y); 48 printf("%d\n",interval_ask(y)-interval_ask(x-1)); 49 } 50 } 51 return 0; 52 }
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)// 增加 40 { 41 int x,y,v; 42 cin>>x>>y>>v; 43 interval_increase(y,v); 44 interval_increase(x-1,-v); 45 } 46 else 47 { 48 int p; 49 cin>>p; 50 cout<<point_ask(p)<<endl; 51 } 52 } 53 return 0; 54 }
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)// 增加 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 else 59 { 60 int p; 61 read(p); 62 printf("%d",point_ask(p)); 63 putchar(‘\n‘); 64 } 65 } 66 return 0; 67 }
标签:nice show sync cli png interval names display www
原文地址:http://www.cnblogs.com/zwfymqz/p/6827529.html