标签:include groups http sel stream using tput mode ret
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2483 Accepted Submission(s): 1272
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<map> 7 #include<queue> 8 #include<stack> 9 #include<vector> 10 #include<set> 11 #define ll __int64 12 using namespace std; 13 int n,m; 14 struct node 15 { 16 int l,r,id; 17 } N[100005]; 18 int p[100005]; 19 int block; 20 int a[100005]; 21 int x[100005]; 22 int mp[100005]; 23 ll ans=0; 24 ll re[100005]; 25 int cmp(struct node aa,struct node bb) 26 { 27 if(p[aa.l]==p[bb.l]) 28 return aa.r<bb.r; 29 else 30 return p[aa.l]<p[bb.l]; 31 } 32 void update(int w,int h) 33 { 34 if(h==1) 35 { 36 mp[a[w]]=1; 37 if(mp[a[w]-1]==0&&mp[a[w]+1]==0) 38 ans++; 39 if(mp[a[w]-1]==1&&mp[a[w]+1]==1) 40 ans--; 41 } 42 else 43 { 44 mp[a[w]]=0; 45 if(mp[a[w]-1]==1&&mp[a[w]+1]==1) 46 ans++; 47 if(mp[a[w]-1]==0&&mp[a[w]+1]==0) 48 ans--; 49 } 50 } 51 int t; 52 int main() 53 { 54 scanf("%d",&t); 55 for(int o=1;o<=t;o++) 56 { 57 for(int i=0;i<100004;i++) 58 mp[i]=0; 59 scanf("%d %d",&n,&m); 60 for(int i=1; i<=n; i++) 61 scanf("%d",&a[i]); 62 for(int i=1; i<=m; i++) 63 { 64 scanf("%d %d",&N[i].l,&N[i].r); 65 N[i].id=i; 66 } 67 block=(int)sqrt((double)n); 68 for(int i=1; i<=n; i++) 69 p[i]=(i-1)/block+1; 70 sort(N+1,N+1+m,cmp); 71 ans=0; 72 for(int i=1,l=1,r=0; i<=m; i++) 73 { 74 for(; r<N[i].r; r++) update(r+1,1); 75 for(; l>N[i].l; l--) update(l-1,1); 76 for(; r>N[i].r; r--) update(r,-1); 77 for(; l<N[i].l; l++) update(l,-1); 78 re[N[i].id]=ans; 79 } 80 for(int i=1; i<=m; i++) 81 printf("%I64d\n",re[i]); 82 } 83 return 0; 84 }
标签:include groups http sel stream using tput mode ret
原文地址:http://www.cnblogs.com/hsd-/p/7050493.html