转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map> #include <string> #define LL long long using namespace std; const int N = 100005; int n,m,a[N],r[N]; int one[N],two[N]; LL sum_one[N],sum_two[N]; char str[10]; map<string,int>mymap; bool cmp(char a,char b){ return a>b; } int main(){ while(scanf("%d%d",&n,&m)!=EOF){ mymap.clear(); for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=n;i>=1;i--){ string s=""; int t=a[i]; while(t){ s+=(char)(t%10+'0'); t/=10; } sort(s.begin(),s.end(),cmp); if(mymap.find(s)==mymap.end()) r[i]=n+1; else r[i]=mymap[s]; mymap[s]=i; } r[n+1]=n+1; one[n+1]=two[n+1]=n+1; for(int i=n;i>=1;i--){ one[i]=min(one[i+1],r[i]); two[i]=min(two[i+1],r[r[i]]); two[i]=min(two[i],max(r[i],one[i+1])); } for(int i=1;i<=n;i++){ sum_one[i]=sum_one[i-1]+one[i]; sum_two[i]=sum_two[i-1]+two[i]; } LL ans=0; while(m--){ LL l,r;int pos; scanf("%lld%lld",&l,&r); l+=ans;r-=ans; pos=upper_bound(one+l,one+r,r)-one-1; ans=(pos-l+1)*r-(sum_one[pos]-sum_one[l-1])+(pos-l+1); pos=upper_bound(two+l,two+r,r)-two-1; ans-=(pos-l+1)*r-(sum_two[pos]-sum_two[l-1])+(pos-l+1); printf("%lld\n",ans); } } return 0; }