标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 179 Accepted Submission(s): 65
Mean:
略
analyse:
每个询问暴力枚举区间,前缀和维护区间和,ST表维护区间最小值,然后暴力处理某一区间是否有重复的数,这样每个询问加起来是O(n)的。于是O(NM)就行了
Time complexity: O(n)
Source code:
/* * this code is made by crazyacking * Verdict: Accepted * Submission Date: 2015-05-30-22.26 * Time: 0MS * Memory: 137KB */ #include <queue> #include <cstdio> #include <set> #include <string> #include <stack> #include <cmath> #include <climits> #include <map> #include <cstdlib> #include <iostream> #include <vector> #include <algorithm> #include <cstring> #define LL long long #define ULL unsigned long long using namespace std; const int MAXN=10010; int m,n,k,Ans; int flag; long long num[MAXN]; long long minNum,sumNum; map<long long,int> Hash; long long tHash[MAXN*2]; int tot; long long sum[MAXN]; int vis[MAXN*2]; long long stTableMin[MAXN][35],preLog2[MAXN]; void StPrepare(){ preLog2[1]=0; for(int i=2;i<=n;i++){ preLog2[i]=preLog2[i-1]; if((1<<preLog2[i]+1)==i) preLog2[i]++; } for(int i=n;i>=0;--i){ stTableMin[i][0]=num[i]; for(int j=1;(i+(1<<j)-1)<=n;++j) stTableMin[i][j]=min(stTableMin[i][j-1],stTableMin[i+(1<<j-1)][j-1]); } } int queryMin(int l,int r){ int len=r-l+1,k=preLog2[len]; return min(stTableMin[l][k],stTableMin[r-(1<<k)+1][k]); } int main() { printf("Case #1:\n"); scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) scanf("%I64d",&num[i]); for (int i=1;i<=n;i++) {tHash[i*2-1]=num[i];tHash[i*2]=num[i]+1;} sort(tHash+1,tHash+n*2+1); tHash[0]=-1; for (int i=1;i<=n*2;i++) { if (tHash[i]!=tHash[i-1])Hash[tHash[i]]=++tot; } for (int i=1;i<=n;i++) num[i]=Hash[num[i]]; sum[0]=0; for (int i=1;i<=n;i++) sum[i]+=sum[i-1]+num[i]; StPrepare(); for (int i=1;i<=m;i++) { scanf("%d",&k); Ans=0;flag=0; memset(vis,0,sizeof(vis)); for (int j=1;j<=k;j++) { vis[num[j]]++; if (vis[num[j]]==2) flag++; } if (!flag) { minNum=queryMin(1,k); sumNum=sum[k]-sum[0]; if (sumNum==(long long)(minNum+minNum+k-1)*(long long)k/(long long)2) Ans++; } for (int left=2;left<=n;left++) { int right=left+k-1; if (right>n) break; vis[num[left-1]]--; if (vis[num[left-1]]==1) flag--; vis[num[right]]++; if (vis[num[right]]==2) flag++; if (!flag) { minNum=queryMin(left,right); sumNum=sum[right]-sum[left-1]; if (sumNum==(long long)(minNum+minNum+k-1)*(long long)k/(long long)2) Ans++; } } printf("%d\n",Ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/crazyacking/p/4541172.html