CSES Solutions
#3221Sliding Window Problems

Sliding Window Minimum

View on CSES

Solution

3221-Sliding_Window_Minimum.cpp
1#include<bits/stdc++.h>
2using namespace std;
3#define int long long
4signed main(){
5  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
6  int n,k; cin>>n>>k;
7  int arr[n];
8  int a,b,c; cin>>arr[0]>>a>>b>>c;
9  for(int i=1,x=arr[0];i<n;++i){
10    arr[i]=(x*a+b);
11    if(arr[i]>=c) arr[i]%=c;
12    x=arr[i];
13  }
14  priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
15  int res=0;
16  for(int i=0;i<k;++i) pq.emplace(arr[i],i);
17  res^=pq.top().first;
18  for(int i=k;i<n;++i){
19    pq.emplace(arr[i],i);
20    while(pq.top().second<=i-k) pq.pop();
21    res^=pq.top().first;
22  }
23  cout<<res<<'\n';
24  return 0;
25}

Editorial not yet generated for this problem. Run the editorial generation script to add hints and detailed explanations.