从后往前更新,维护一个递减单调栈(队列)
最近很多题都是单调栈。。。
#define _CRT_SECURE_NO_WARNINGS #include<cstdio> #include<algorithm> #include<queue> #include<iostream> //#include<deque> using namespace std; const int maxn = 1e7 + 5; int n, m, k, P, Q, R, MOD,T; int a[maxn]; #define rep(i,t,n) for(int i =(t);i<=(n);++i) #define per(i,n,t) for(int i =(n);i>=(t);--i) #define mmm(a,b) memset(a,b,sizeof(a)) int main() { scanf("%d", &T); while (T--) { deque<int> que; long long A = 0, B = 0,cnt=0,mx=-1; scanf("%d%d%d%d%d%d%d", &n, &m, &k, &P, &Q, &R, &MOD); rep(i,1,k)scanf("%d", &a[i]); rep(i,k+1,n)a[i] = (1LL * P*a[i - 1] + 1LL * Q*i + R) % MOD; per(i, n,1) { while (!que.empty() && que.back() <= a[i])que.pop_back(); que.push_back(a[i]); if (i <= n - m + 1) { if (a[i + m ] == que.front())que.pop_front(); A += i ^ que.front(); B += i ^ (que.size()); } } printf("%lld %lld\n", A, B); } cin >> n; }
原文地址:https://www.cnblogs.com/SuuT/p/9397641.html
时间: 2024-10-25 00:33:26