Problem A. Ascending Rating

yi_yu_發表於2020-04-05

題目

題意:求一定區間內的上升序列以及最大值,用滑窗求值方法從後往前求就好啦

#include <bits/stdc++.h>
#define INF 70000005
#define ll long long

using namespace std;

int t;
ll n,m,i,j,k,p,q,r,MOD,a[INF],win[INF],sub[INF],cmax,ccou,x,y;

int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%I64d%I64d%I64d%I64d%I64d%I64d%I64d",&n,&m,&k,&p,&q,&r,&MOD);
        for(i = 1; i <= k; ++i){
            scanf("%d",&a[i]);
        }
        for(i = k+1; i <= n; ++i){
            a[i] = (p*a[i-1]+q*i+r)%MOD;
        }

        cmax = ccou = x = y = 0;

        for(int i = n; i > n-m; --i){
            if(x == y || a[i] < win[y-1]){
                win[y] = a[i];
                sub[y++] = i;
            }else if(a[i] > win[y-1]){
                while(x != y && a[i] > win[y-1]){
                    y--;
                }
                win[y] = a[i];
                sub[y++] = i;
            }
        }

        cmax += win[x]^(n-m+1);
        ccou += (y-x)^(n-m+1);

        for(i = n-m; i > 0; --i){
            if(sub[x] > i+m-1)
                x++;
            if(x == y || a[i] < win[y-1]){
                win[y] = a[i];
                sub[y++] = i;
            }else if(a[i] > win[y-1]){
                while(x != y && a[i] > win[y-1]){
                    y--;
                }
                win[y] = a[i];
                sub[y++] = i;
            }
            cmax += win[x]^i;
            ccou += (y-x)^i;;
        }
        printf("%I64d %I64d\n",cmax,ccou);
    }
}