单调队列
反着遍历数组,放进单调队列,每次维护最大值,在维护最大值的时候,队列里是一个递减的序列,也就是正向递增的序列啦~
注意一开始绝对不要memset一次队列,否则会T的很惨。。别问我为什么知道。。。
#include#define INF 0x3f3f3f3f#define full(a, b) memset(a, b, sizeof a)#define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)using namespace std;typedef long long ll;inline int lowbit(int x){ return x & (-x); }inline int read(){ int X = 0, w = 0; char ch = 0; while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); } while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return w ? -X : X;}inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }inline int lcm(int a, int b){ return a / gcd(a, b) * b; }template inline T max(T x, T y, T z){ return max(max(x, y), z); }template inline T min(T x, T y, T z){ return min(min(x, y), z); }template inline A fpow(A x, B p, C lyd){ A ans = 1; for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd; return ans;}const int N = 10000005;int n, m, k, q, p, r, MOD, _, dq[N];int a[N];int main(){ for(_ = read(); _; _ --){ n = read(), m = read(), k = read(), p = read(), q = read(), r = read(), MOD = read(); p %= MOD, q %= MOD, r %= MOD; for(int i = 1; i <= k; i ++) a[i] = read(); for(int i = k + 1; i <= n; i ++){ a[i] = (1LL * p * a[i - 1] + 1LL * q * i + r) % MOD; } ll x = 0, y = 0; int l = 1, r = 0; for(int i = n; i > 0; i --){ while(l <= r && a[dq[r]] <= a[i]) r --; while(l <= r && dq[l] > i + m - 1) l ++; dq[++r] = i; if(i > n - m + 1) continue; x += (a[dq[l]] ^ i); y += ((r - l + 1) ^ i); } printf("%lld %lld\n", x, y); } return 0;}