E. Range Deleting
The meaning of problems: given a sequence of operation a definition of f (x, y) to remove all the sequence number in the [x, y] within the interval. He asked to make the remaining number of nondecreasing operation f (x, y) the number of solutions is.
Solution: do not, can not keep thinking, double pointer is not skilled. Ideas and code are learning https://edwiv.com/archives/587 the huge guys.
Under that I understand: We consider how the operation [l, r] is reasonable? Can easily think of three conditions: ① After deleting the remaining figures [. 1, l-1] is a monotonically increasing position, ② digital [r + 1, x] is the position of monotonically increasing numbers 3 l-1 all positions must be smaller than 1 + r. So our task is to pretreatment this sequence makes it possible to quickly determine these three conditions.
posmin[maxn] posmax[maxn]
Each subscript represents the minimum and maximum number
premax[maxn] sufmin[maxn]
indicating the maximum value / minimum value [1, i] / [i , x] of the target value in the range of the number of occurrences
precan[maxn] sufcan[maxn]
represents [1, i] is legitimate, [i, x] is legitimate
Analyzing then [l, r] is a reasonable condition whether the operation is: precan [l-1] && sufcan [r + 1] && (premax [l-1] <sufmin [r + 1]); // this were corresponding to the above three conditions
So here we will be able to O (1) to quickly determine the legality of an action, the next step is the statistical answer. N ^ 2 statistics course not time out, the double pointer tips used herein, the left end point enumeration l, r of the right end of a left end point of the l-1 r inherited, then after determining the actual movement of the left point to obtain the current l the right should point r, can contribute statistical answer is x-r + 1 slightly. Here is based on the correctness of: [l, r] is reasonable then r is l + 1 must be greater than l, r, the left and right end points are synchronized incremented.
. 1 #include <bits / STDC ++ H.> 2 the using namespace STD; . 3 typedef Long Long LL; . 4 const int N = 1e6 + 10 ; . 5 int n-, X, A [N]; . 6 int posmin [N], posmax [N ], PreMax [N], sufmin [N]; . 7 BOOL precan [N], sufcan [N]; . 8 . 9 BOOL Check ( int L, int R & lt) { // if the judgment obtained by deleting interval [l, r] reasonable answer 10 return precan [L- . 1 ] && sufcan [R & lt + . 1 ] && (PreMax [L-1]<sufmin[r+1]); 11 } 12 13 int main() 14 { 15 cin>>n>>x; 16 memset(posmin,0x3f,sizeof(posmin)); 17 memset(posmax,0,sizeof(posmax)); 18 for (int i=1;i<=n;i++) { 19 scanf("%d",&a[i]); 20 posmin[a[i]]=min(posmin[a[i]],i); 21 posmax[a[i]]=max(posmax[a[i]],i); 22 } 23 for (int i=1;i<=x;i++) premax[i]=max(premax[i-1],posmax[i]); 24 sufmin[x+1]=n+1; for (int i=x;i;i--) sufmin[i]=min(sufmin[i+1],posmin[i]); 25 memset(precan,0,sizeof(precan)); precan[0]=1; 26 for (int i=1;i<=x;i++) precan[i]=precan[i-1]&&(posmin[i]>premax[i-1]); 27 memset(sufcan,0,sizeof(sufcan)); sufcan[x+1]=1; 28 for (int i=x;i;i--) sufcan[i]=sufcan[i+1]&&(posmax[i]<sufmin[i+1]); 29 30 LL ans=0; 31 int l=1,r=1; //双指针 32 for (;l<=x;l++) { //左指针遍历 33 if (l>r) r=l; 34 while(! R & lt <X && Check (l, R & lt)) R & lt ++; // move right pointer 35 IF (Check (l, R & lt)) ANS + = (X-R & lt + . 1 ); // accumulated left pointer contribution l time is (X-R & lt +. 1) 36 } 37 [ COUT ANS << << endl; 38 is return 0 ; 39 }
F. Scalar Queries
Solution: Although the operator is able to guess the ranking numbers multiplied contribution to get the answer, but still did not do it qwq. Reference https://www.cnblogs.com/carcar/p/10877964.html the huge guys.
Talk about their own understanding: In fact, the answer is easy to find operator d [i] * a [i ]. The d [i] coefficients fact that all sections comprising a [i] is the number of a [i] is the sum of the ranking . So how can this be considered fast d [i]? We think the contribution from this angle:
On the left a [i], only a [j] <a [i] (j <i) when a [j] to a [i] will enhance the role of ranking and a ranking of the lifting effect All contains (a [j] and a [i]) are effective interval.
Similarly in a [i] to the right, only a [j] <a [i] (j <i) only when the lifting action of the ranking, and all contain a [j] and a [i] is effective range.
Then for a [i] myself the same way, give yourself a improved ranking.
Then we calculate how fast a [j] <a [i] and all include a a number of the interval [j] [i] it? In a [i] on the left an example analysis, closer examination reveals that in fact the number of interval j * (n-i + 1), for each of the A [j] j This factor will not change, and for a [i] the factor (n-i + 1) is not changed. We all need to do is to quickly count a total of a [i] j factor of [j] <, ah? Is not that Fenwick tree. Yes, we sweep from left to right over the use of statistical Fenwick tree, sweeping from right to left again, statistics obtained after d [i] This problem is solved.
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int N=5e5+10; 5 const int P=1e9+7; 6 int n,m,a[N],b[N],rk[N]; 7 LL d[N]; 8 9 LL sum[N]; 10 void update(int x,int v) { 11 for (;x<=n;x+=x&-x) sum[x]+=v,sum[x]%=P; 12 } 13 LL query(int x) { 14 LL ret=0; 15 for (;x;x-=x&-x) ret+=sum[x],ret%=P; 16 return ret; 17 } 18 19 int main() 20 { 21 cin>>n; 22 for (int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i]; 23 sort(b+1,b+n+1); 24 for (int i=1;i<=n;i++) rk[i]=lower_bound(b+1,b+n+1,a[i])-b; 25 26 for (int i=1;i<=n;i++) { //计算[1,i-1]区间的贡献 27 d[i]=(d[i]+query(rk[i]-1)*(n-i+1)%P)%P; 28 update(rk[i],i); 29 } 30 for (int i=1;i<=n;i++) d[i]=(d[i]+(LL)i*(n-i+1)%P)%P; //计算[i,i]的贡献 31 memset(sum,0,sizeof(sum)); 32 for (int i=n;i;i--) { //计算[i+1,n]的贡献 33 d[i]=(d[i]+query(rk[i]-1)*(i)%P)%P; 34 update(rk[i],n-i+1); 35 } 36 37 LL ans=0; 38 for (int i=1;i<=n;i++) ans=(ans+a[i]*d[i]%P)%P; 39 cout<<ans<<endl; 40 return 0; 41 }