Probelm
Test instructions
Given a sequence and a mod value, the result of defining all the elements that are [l,r]
valid l
r
and subtracting the maximum values can divide the MoD. Ask how many intervals are valid.
Ideas
First thought of the division. For a[l,r]
We can find the largest of these, and then divide this number into two parts, and for the answers that are distributed in two intervals, we can use Lowerbound and Upperbunder to O ( l o g ( n ) ) Time to find out, and then the recursive solution. However, for this problem, the lower bound of this practice will achieve O ( n 2 ) 。 So it's not going to work.
After reading the puzzle, it is possible to enumerate the maximum value directly and then find out the interval of satisfaction. Enlightened. So we just need to sort the original array and record the left and right bounds of each number. From the very beginning, the answer is calculated at the same time to update the left and right bounds. This can be done in O(Nl og(N)) The complexity of the solution.
A good question. Hope to be able to persist in the future of each game to complete the title.
AC Code
/************************************************************************* > File Name:pf.cpp > Author:z nl1087 > Mail: [email protected] > Created time: 46/11 16:36:14 2015 ************************************** **********************************/#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <string>#include <cstdlib>#include <vector>#include <set>#include <map>#define LL Long Longusing namespace STD;intN,k; LL s[300005]; vector<int>f[1000005]; LL num[300005];intpre[300005],nxt[300005]; LL Ask (intLintR,ll in) {returnUpper_bound (F[in].begin (), F[in].end (), R)-lower_bound (F[in].begin (), F[in].end (), l);} LL Cal (intm) {intL = pre[m]+1, R = nxt[m]-1; LL MAXN = num[m]; LL ans =0;if(R-m < M-l) { for(inti=m+1; i<=r;i++) ans+= (Ask (l1, M-1, (s[i]-maxn%k+k)%k)); ans+= (Ask (l1, M-2, (s[m]-maxn%k+k)%k)); }Else{ for(inti=l;i<m;i++) ans+= (Ask (M,r, s[i-1]+MAXN) (%k)); ans+= (Ask (m+1, R, (s[m-1]+MAXN) (%k)); } Pre[nxt[m]] = pre[m]; Nxt[pre[m]] = nxt[m];returnAns;}intord[300005];intcmpintAintb) {returnNUM[A]<NUM[B];}intMain () {Cin>>n>>k; s[0] =0; for(intI=1; i<=n;i++) {Cin>>num[i],s[i] = (s[i-1]+num[i])%k,ord[i] = i; Pre[i] = i1; Nxt[i] = i+1; } nxt[0] =1; Nxt[n] = n+1; pre[n+1] = N; for(intI=0; i<=n;i++) F[s[i]].push_back (i); Sort (ord+1, ord+n+1, CMP); LL ans =0; for(intI=1; i<=n;i++) {intpos = Ord[i]; Ans+=cal (POS); }cout<<ans<<endl;return 0;}
Codeforces 549F Yura and Developers