hdu6319(单调队列)

给出一个数列长度为n 求从左往右的每个长度为m的子区间的最大值maxnum,以及每个子区间从左往右数最大值变化的次数count、

 

maxnum好求,单调队列维护一下就行了, count想了很久都有bug,看了题解是从又往左进行单调队列这样单调队列里就会依次保存这个区间内的最大数,次大数,第三大数。。。,队列的size就是count值

#include 
#include 
#include 

using namespace std;
const int mx = 1e7+5;
const int INF = 0x3f3f3f3f;
int n, m, k, p, q, r, mod;
int num[mx];
int Q[mx], rear, front;

int main()
{
	int T;
	scanf("%d", &T);
	
	while (T--)
	{
		scanf("%d%d%d%d%d%d%d", &n, &m, &k, &p, &q, &r, &mod);
		for (int i = 1; i <= k; i++)
			scanf("%d", &num[i]);
		for (int i = k+1; i <= n; i++)
			num[i] = (1LL*p*num[i-1] + 1LL*q*i + r) % mod;
			
		front = rear = 0;
		int maxrt = -1, count = 0;
		long long A = 0, B = 0;
		for (int i = n; i >= 1; i--)
		{	
			while (rear > front && num[Q[rear-1]] <= num[i]) rear--;
			Q[rear++] = i;
			while (rear > front && Q[front] - i + 1 > m) front++;
			if (i <= n-m+1)
			{
				A += (long long)num[Q[front]] ^ i;
				B += (long long)(rear-front) ^ i;
				//printf("i = %d maxrt = %d count = %d\n",i-m+1, num[Q[front]], rear-front+1);
			}
		}
		printf("%lld %lld\n",A, B);
	}
	
	return 0;
}

 

你可能感兴趣的:(2018多校训练)