Please note that the memory limit differs from the standard.You really love to listen to music. During the each of next s days you will listen to exactly m songs from the playlist that consists of exactly n songs. Let's number the songs from the playlist with numbers from 1 to n, inclusive. The quality of song number i is ai.On the i-th day you choose some integer v (li ≤ v ≤ ri) and listen to songs number v, v + 1, ..., v + m - 1. On the i-th day listening to one song with quality less than qi increases your displeasure by exactly one.Determine what minimum displeasure you can get on each of the s next days.

The first line contains two positive integers n, m (1 ≤ m ≤ n ≤ 2·105). The second line contains n positive integers a1, a2, ..., an (0 ≤ ai < 230) — the description of songs from the playlist.  The next line contains a single number s (1 ≤ s ≤ 2·105) — the number of days that you consider. The next s lines contain three integers each li, ri, xi (1 ≤ li ≤ ri ≤ n - m + 1; 0 ≤ xi < 230) — the description of the parameters for the i-th day. In order to calculate value qi, you need to use formula: , where ansi is the answer to the problem for day i. Assume that ans0 = 0.

Print exactly s integers ans1, ans2, ..., anss, where ansi is the minimum displeasure that you can get on day i.

#include <stdio.h>
#include <string.h>
#include <algorithm>

#define fi first
#define se second
#define L(x) p[x].l
#define R(x) p[x].r
#define MX(x) p[x].mx
#define UPT(x) max(int(mid>l?MX(L(x)):L(x)),int(r>mid+1?MX(R(x)):R(x)))
#define TAG(x) MX(x)-UPT(x)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define inf 0x7f7f7f7f
using namespace std;

#define g() getchar()
template<class Q>void Scan(Q&x){
    char c;while(c=g(),c<48||c>57);
    for(x=0;c>47&&c<58;c=g())x=10*x+c-48;
}

const int MAXN=2e5+5;
typedef unsigned long long ull;

typedef struct node*star;
struct node{
    ull mx:18,l:23,r:23;
}p[MAXN*40];

int tail;

int update(int x,int l,int r,int ll,int rr){
    if(l==r)return x+1;
    int y=++tail;
    p[y]=p[x];
    if(ll<=l&&r<=rr)return ++MX(y),y;
    int mid=(l+r)>>1;
    int tag=TAG(x);
    if(ll<=mid)L(y)=update(L(x),l,mid,ll,rr);
    if(rr>mid) R(y)=update(R(x),mid+1,r,ll,rr);
    MX(y)=UPT(y)+tag;
    return y;
}

int query(int x,int l,int r,int ll,int rr){
    if(l==ll&&r==rr)return r>l?MX(x):x;
    int mid=(l+r)>>1;
    int tag=TAG(x);
    if(rr<=mid)return query(L(x),l,mid,ll,rr)+tag;
    if(ll>mid) return query(R(x),mid+1,r,ll,rr)+tag;
    int t1=query(L(x),l,mid,ll,mid);
    int t2=query(R(x),mid+1,r,mid+1,rr);
    return max(t1,t2)+tag;
}

int n,m,Q,rt[MAXN];

pair<int,int>v[MAXN];

inline void Init(){
    Scan(n),Scan(m);
    rep(i,1,n){
        Scan(v[i].fi);
        v[i].fi*=-1;
        v[i].se=i;
    }
    sort(v+1,v+1+n);
    rep(i,1,n){
        int l=max(1,v[i].se-m+1),r=min(n-m+1,v[i].se);
        rt[i]=update(rt[i-1],1,n-m+1,l,r);
    }
}

int ans;

inline void Solve(){
    Scan(Q);
    rep(i,1,Q){
        int l,r,k;
        Scan(l),Scan(r),Scan(k);
        k^=ans;
        k=upper_bound(v+1,v+1+n,make_pair(-k,inf))-v-1;
        ans=m-query(rt[k],1,n-m+1,l,r);
        printf("%d\n",ans);
    }
}

int main(){
    Init(); Solve(); return 0;
}

#include <bits/stdc++.h>

#define forn(i, n) for(int i = 0; i < int(n); i++)
#define sz(a) int((a).size())
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define mp make_pair
#define sc second
#define ft first

using namespace std;

const int M = 512;
const int N = M * M;
int blvalue[M][M];
int ftvalue[M][M];
int exvalue[M][2 * M];
int a[N];
pair<int, int> na[N];
int va[N];
int push[M];
int bl[M];
int n, m;
int blcount;
const int mult = 9;
const int blsz = (1 << mult);

inline bool is_first(int idx) {
  return (idx & (blsz - 1)) == 0;
}

inline int bl_idx(int idx) {
  if (idx >= n) {
    return (idx >> mult) + 1;
  }
  return idx >> mult;
}

inline int first_idx(int blid) {
  return blid << mult;
}

inline int last_idx(int blid) {
  return min(n - 1, first_idx(blid) + blsz - 1);
}

const int INF = 1000 * 1000 * 1000;

int main()
{
#ifdef gridnevvvit
  freopen("input.txt", "rt", stdin);
#endif
  scanf("%d %d", &n, &m);
  blcount = (n + blsz - 1) / blsz;
  forn(i, n) scanf("%d", &a[i]);
  forn(i, n)
    na[i] = mp(-a[i], i);
  sort(na, na + n);

  forn(iter, n) {
    if (is_first(iter)){
      int curbl = bl_idx(iter);
      forn(i, n)
        va[i] = va[i] + push[bl_idx(i)];
      forn(i, blcount) {
        bl[i] += push[i];
        blvalue[curbl][i] = bl[i];
        ftvalue[curbl][i] = va[first_idx(i)];
        push[i] = 0;
      }
    }
    int r = na[iter].sc;
    int l = max(0, r - m + 1);
    int blocks[] = {blcount, blcount};
    int szb = 0;
    for(int i = l; i <= r; ) {
      int curbl = bl_idx(i);
      if (is_first(i) && last_idx(curbl) <= r) {
        push[curbl] ++;
        i = last_idx(curbl) + 1;
      } else {
        va[i]++;
        bl[curbl] = max(bl[curbl], va[i]);
        if (szb == 0 || blocks[szb - 1] != curbl) {
          blocks[szb++] = curbl;
        }
        i++;
      }
    }
    szb = 2;
    int curbl = bl_idx(iter);
    int idx = iter - first_idx(curbl);
    forn(i, szb) {
      exvalue[curbl][2 * idx + i] = (bl[blocks[i]] << mult) + blocks[i];
    }
  }
  int query;
  scanf("%d", &query);
  int last_ans = 0;
  forn(it, query) {
    int l, r, q;
    scanf("%d %d %d", &l, &r, &q); l--, r--;
    q ^= last_ans;
    int idx = upper_bound(na, na + n, mp(-q, INF)) - na;
    idx--;
    if (idx < 0) {
      printf("%d\n", m);
      last_ans = m;
      continue;
    }
    int curbl = bl_idx(idx);
    forn(i, blcount) {
      bl[i] = blvalue[curbl][i];
      push[i] = 0;
    }
    int ft = first_idx(curbl);
    for(int i = ft; i <= idx; i++) {
      int nr = na[i].sc;
      int nl = max(0, nr - m + 1);
      if (nl != first_idx(bl_idx(nl))) {
        nl = last_idx(bl_idx(nl)) + 1;
      }
      if (nr == last_idx(bl_idx(nr))){
        nr++;
      }
      if (bl_idx(nl) <= bl_idx(nr)) {
        push[bl_idx(nl)] ++;
        push[bl_idx(nr)] --;
      }
      forn(j, 2) {
        int blid = exvalue[curbl][2 * (i - ft) + j];
        int vt = blid >> mult;
        blid &= (blsz - 1);
        bl[blid] = vt;
      }
    }
    forn(i, blcount) {
      if (i > 0) push[i] += push[i - 1];
    }
    int maxAns = 0;
    int pv = -1;
    for(int i = l; i <= r; ) {
       int blid = bl_idx(i);
       if (is_first(i) && last_idx(blid) <= r) {
         maxAns = max(maxAns, bl[blid] + push[blid]);
         i = last_idx(blid) + 1;
         pv = -1;
       } else {
         if (pv == -1) {
            pv = ftvalue[curbl][blid];
            int ftp = first_idx(blid);
            for(int j = ft; j <= idx; j++) {
              if (ftp <= na[j].sc && ftp >= max(na[j].sc - m + 1, 0)) {
                pv++;
              }
            }
            while (ftp != i) {
              pv -= (a[ftp] >= q);
              pv += (a[ftp + m] >= q);
              ftp++;
            }
         }
         maxAns = max(maxAns, pv);
         pv -= (a[i] >= q);
         pv += (a[i + m] >= q);
         i++;
      }
    }
    maxAns = m - maxAns;
    printf("%d\n", maxAns);
    last_ans = maxAns;
  }
}

/* --- author: dxm --- */
#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;

#define INF 1e9
#define Inf 1e18
#define rep(i,n) for(int (i)=0;(i)<n;i++)
#define REP(i,n) for(int (i)=1;(i)<=n;i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;

void qread(int &x){
	int neg=1;x=0;
	char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')neg=-1;c=getchar();}
	while(c>='0'&&c<='9')x=10*x+c-'0',c=getchar();
	x*=neg;
}

const int maxn=200005;
const int bit=9;
const int maxm=(1<<bit)-1;
const int maxs=(maxn+maxm-1)/maxm;
int n,m,q,last;
pii a[maxn];
int mn[maxs][maxs],mn2[maxs][maxs];
int tag[maxs],val[maxn],from[maxn],to[maxn],bfrom[maxn],bto[maxn],ind[2],bmn[2];
class Data{
public:
	int t,pos,val;
	Data(int _t=0,int _pos=0,int _val=0):t(_t),pos(_pos),val(_val){}
	bool operator < (const Data &o)const{if(t!=o.t)return t<o.t;return pos<o.pos;}
};
vector<Data>upd[maxs];
vector<vector<int> >st[maxs];

int calc(int i){
	int res=INF;
	for(int j=i<<bit;j<min(n-m+1,(i+1)<<bit);j++)res=min(res,tag[i]+val[j]);
	return res;
}

void add(int i){
	from[i]=max(0,a[i].snd-m+1),to[i]=min(a[i].snd,n-m);
	if(!(i&maxm)){
		int cur=i>>bit;
		rep(j,maxs){
			mn[cur][j]=mn2[cur][j]=INF;
			for(int k=j<<bit;k<min((j+1)<<bit,n-m+1);k++){
				mn[cur][j]=min(mn[cur][j],tag[j]+val[k]);
			}
			mn2[cur][j]=tag[j];
		}
	}
	if(i==n)return;
	int l=(from[i]>>bit),r=(to[i]>>bit);
	bmn[0]=calc(l);bmn[1]=calc(r);
	for(int j=from[i];j<=to[i];){
		if(!(j&maxm)&&j+maxm<=to[i])tag[j>>bit]++,j+=(1<<bit);
		else val[j]++,j++;
	}
	bfrom[i]=l;bto[i]=r;
	if(calc(l)==bmn[0])bfrom[i]++;
	if(calc(r)==bmn[1])bto[i]--;
	if((from[i]&maxm)||to[i]-from[i]<maxm)upd[l].pb(Data(i,from[i]&maxm,1));
	if((to[i]&maxm)!=maxm||to[i]-from[i]<maxm){
		if(l!=r)upd[r].pb(Data(i,0,1));
		upd[r].pb(Data(i,(to[i]&maxm)+1,-1));
	}
}

int qry(int tm,int from,int to){
	int l=(from>>bit),r=(to>>bit),t=(tm>>bit),ans=INF;
	ind[0]=l;ind[1]=r;
	rep(p,2){
		if(p==1&&ind[0]==ind[1])continue;
		int cur=ind[p];
		vector<int>tmp;
		int pos=lower_bound(upd[cur].begin(),upd[cur].end(),Data(tm,-1,-1))-upd[cur].begin();
		if((pos>>bit)-1>=0)tmp=st[cur][(pos>>bit)-1];
		else tmp=vector<int>(maxm+5);
		for(int j=(pos>>bit)<<bit;j<pos;j++)tmp[upd[cur][j].pos]+=upd[cur][j].val;
		int base=mn2[t][cur];
		for(int i=t<<bit;i<tm;i++)if(::from[i]<=(cur<<bit)&&::to[i]>=((cur+1)<<bit)-1)base++;
		rep(i,maxm+1){
			if(i)tmp[i]+=tmp[i-1];
			if((cur<<bit)+i>=from&&(cur<<bit)+i<=to)ans=min(ans,base+tmp[i]);
		}
	}
	vector<int>tmp(maxs+5);
	rep(i,maxs)tmp[i]=mn[t][i];
	for(int i=maxs-1;i>=1;i--)tmp[i]-=tmp[i-1];
	for(int i=t<<bit;i<tm;i++)if(bfrom[i]<=bto[i]){
		tmp[bfrom[i]]++;
		tmp[bto[i]+1]--;
	}
	rep(i,maxs){
		if(i)tmp[i]+=tmp[i-1];
		if(i>l&&i<r)ans=min(ans,tmp[i]);
	}
	return ans;
}

int main(){
#ifdef FILE
	freopen("E.in","r",stdin);
	freopen("E.out","w",stdout);
#endif
	scanf("%d%d",&n,&m);
	rep(i,n)scanf("%d",&a[i].fst),a[i].snd=i;
	sort(a,a+n);
	rep(i,n+1)add(i);
	rep(i,maxs){
		vector<int>tmp(maxm+5);
		rep(j,upd[i].size()){
			tmp[upd[i][j].pos]+=upd[i][j].val;
			if((j&maxm)==maxm)st[i].pb(tmp);
		}
	}
	scanf("%d",&q);
	while(q--){
		int l,r,x;
		scanf("%d%d%d",&l,&r,&x);
		l--;r--;
		x^=last;
		int pos=lower_bound(a,a+n,mp(x,-1))-a;
		int ans=qry(pos,l,r);
		printf("%d\n",ans);
		last=ans;
	}
#ifdef TIME
	printf("Running Time = %d ms\n",int(clock()*1000.0/CLOCKS_PER_SEC));
#endif
	return 0;
}

/*
Input:
-----------------
Output:
*/

#include <stdio.h>
#include <algorithm>

using std :: pair;
using std :: sort;

template < class T > void read(T &x) { char c, mi = 0; while(c = getchar(), c <= 32); if(c == '-') mi = 1, x = 0; else if(c < 48 || c > 57) return void(x = c); else x = c - 48; while(c = getchar(), c > 32) x = 10 * x + c - 48; if(mi == 1) x = -x; }
template < class T > void read(T &x, T &y) { read(x); read(y); }
template < class T > void read(T &x, T &y, T &z) { read(x, y); read(z); }
template < class T > void reada(T *a, int n) { for(int i = 0; i < n; ++i) read(a[i]); }
template < class T > void write(T x) { static char s[20]; char pt = 0, mi = (x < 0); if(mi == 1) x = -x; while(!pt || x > 0) { s[++pt] = (char)(x % 10 + '0'); x /= 10; } if(mi == 1) putchar('-'); while(pt > 0) putchar(s[pt--]); }
template < class T > void write(T x, T y) { write(x); putchar(' '); write(y); }
template < class T > void write(T x, T y, T z) { write(x, y); putchar(' '); write(z); }
template < class T > void writeln(T x) { write(x); puts(""); }
template < class T > void writea(T *a, int n) { for(int i = 0; i < n; ++i) { write(a[i]); putchar(i + 1 == n ? '\n' : ' '); } }

#define nmax 200010
#define fs first
#define sd second

int pt = 0;
int roots[nmax];
int n, m, q;
int ans, l, r, x;
pair < int, int > p[nmax];

int mi(const int &a, const int &b) {
	return a < b ? a : b;
}

int ma(const int &a, const int &b) {
	return a > b ? a : b;
}

struct Node {
	unsigned long long mi : 18, l : 23, r : 23;
} t[7977777];

int findAdd(int ret, int xl, char dl, int xr, char dr) {
	return t[ret].mi - mi(dl == 0 ? t[xl].mi : xl, dr == 0 ? t[xr].mi : xr);
}

void f(int ret, int xl, char dl, int xr, char dr, int add = 0) {
	t[ret].mi = add + mi(dl == 0 ? t[xl].mi : xl, dr == 0 ? t[xr].mi : xr);
	t[ret].l = xl;
	t[ret].r = xr;
}

int upd(int v, int L, int R, int l, int r) {
	if(L == R) {
		return v + 1;
	}
	int ret = ++pt;
	if(L == l && r == R) {
		t[ret].mi = t[v].mi + 1;
		t[ret].l = t[v].l;
		t[ret].r = t[v].r;
		return ret;
	}
	int M = (L + R) / 2;
	if(r <= M) {
		t[ret].l = upd(t[v].l, L, M, l, r);
		t[ret].r = t[v].r;
	} else if(l > M) {
		t[ret].r = upd(t[v].r, M + 1, R, l, r);
		t[ret].l = t[v].l;
	} else {
		t[ret].l = upd(t[v].l, L, M, l, M);
		t[ret].r = upd(t[v].r, M + 1, R, M + 1, r);
	}
	f(ret, t[ret].l, L == M, t[ret].r, M + 1 == R,
	findAdd(v, t[v].l, L == M, t[v].r, M + 1 == R));
	return ret;
}

int get(int v, int L, int R, int l, int r) {
	if(v == 0) {
		return 0;
	}
	if(L == R) {
		return v;
	}
	if(L == l && r == R) {
		return t[v].mi;
	}
	int M = (L + R) / 2;
	int z = findAdd(v, t[v].l, L == M, t[v].r, M + 1 == R);
	if(r <= M) {
		return z + get(t[v].l, L, M, l, r);
	}
	if(l > M) {
		return z + get(t[v].r, M + 1, R, l, r);
	}
	return z + mi(get(t[v].l, L, M, l, M),
		get(t[v].r, M + 1, R, M + 1, r));
}

int main() {
//	freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
//	freopen("elevator.in", "r", stdin); freopen("elevator.out", "w", stdout);
//	printf("%d\n", sizeof(t));
	read(n, m);
	for(int i = 1; i <= n; ++i) {
		read(p[i].fs);
		p[i].sd = i;
	}
	roots[0] = 0;
	sort(p + 1, p + 1 + n);
	for(int i = 1; i <= n; ++i) {
		roots[i] = upd(roots[i - 1], 1, n - m + 1, ma(p[i].sd - m + 1, 1), mi(p[i].sd, n - m + 1));
	}
	read(q);
	while(q --> 0) {
		read(l, r, x);
		x ^= ans;
		int id = upper_bound(p + 1, p + 1 + n, std :: make_pair(x, -1)) - (p + 1);
		ans = get(roots[id], 1, n - m + 1, l, r);
		writeln(ans);
	}
	getchar(); getchar();
	return 0;
}

//written by newbiechd
#include <cstdio>
#include <cctype>
#include <algorithm>
#define R register
#define I inline
using namespace std;
const int N = 200003;
#define gc getchar
I int rd() {
    R int f = 0;
    R char c = gc();
    while (c < 48 || c > 57)
        c = gc();
    while (c > 47 && c < 58)
        f = f * 10 + (c ^ 48), c = gc();
    return f;
}
int rt[N], T;
struct Data {
    int val, id;
    Data() {}
    Data(int val, int id) : val(val), id(id) {}
    I friend int operator < (const Data &x, const Data &y) { return x.val != y.val ? x.val > y.val : x.id < y.id; }
}a[N];
struct Segtree {
    unsigned long long val : 18, p : 23, q : 23;
}e[N * 40];
int insert(int k, int l, int r, int x, int y) {
    if (l == r)
        return k + 1;
    R int t = ++T;
    e[t] = e[k];
    if (x <= l && r <= y) {
        ++e[t].val;
        return t;
    }
    R int m = (l + r) >> 1, tag = e[t].val - max(l == m ? e[t].p : e[e[t].p].val, m + 1 == r ? e[t].q : e[e[t].q].val);
    if (x <= m)
        e[t].p = insert(e[k].p, l, m, x, y);
    if (m < y)
        e[t].q = insert(e[k].q, m + 1, r, x, y);
    e[t].val = max(l == m ? e[t].p : e[e[t].p].val, m + 1 == r ? e[t].q : e[e[t].q].val), e[t].val += tag;
    return t;
}
int query(int k, int l, int r, int x, int y) {
    if (l == r)
        return k;
    if (x <= l && r <= y)
        return e[k].val;
    R int m = (l + r) >> 1, tag = e[k].val - max(l == m ? e[k].p : e[e[k].p].val, m + 1 == r ? e[k].q : e[e[k].q].val), o = 0;
    if (x <= m)
        o = max(o, query(e[k].p, l, m, x, y));
    if (m < y)
        o = max(o, query(e[k].q, m + 1, r, x, y));
    return o + tag;
}
int main() {
    int n = rd(), m = rd(), Q, i, x, y, val, ans = 0;
    for (i = 1; i <= n; ++i)
        a[i] = Data(rd(), i);
    sort(a + 1, a + n + 1);
    for (i = 1; i <= n; ++i)
        rt[i] = insert(rt[i - 1], 1, n - m + 1, max(1, a[i].id - m + 1), min(a[i].id, n - m + 1));
    for (Q = rd(); Q; --Q)
        x = rd(), y = rd(), val = rd() ^ ans,i = lower_bound(a + 1, a + n + 1, Data(val, N)) - a - 1,
            printf("%d\n", ans = m - query(rt[i], 1, n - m + 1, x, y));
    return 0;
}

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
inline int read()
{
	int x = 0 , f = 1; char ch = getchar();
	while(ch < '0' || ch > '9')	{if(ch == '-')	f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();}
	return x * f;
}
const int N = 2e5 + 1 , inf = 2e9 + 1;
int n , m , cnt , tot;
typedef pair<int,int> pp;
#define mp make_pair
pp a[N];
#define pb push_back
typedef unsigned long long ull;
namespace PSMT
{
	struct node
	{
		ull mx : 18 , ls : 23 , rs : 23;
	} t[N * 40];
	int root[N + 1];
	int insert(int p,int l,int r,int x,int y)
	{
		if(l == r) return p + 1;
		int o = ++ tot; t[o] = t[p];
		if(x <= l && r <= y) return t[o].mx ++ , o;
		int mid = (l + r) >> 1 , tag = t[o].mx - max((l == mid ? (int)t[o].ls : (int)t[t[o].ls].mx),(mid + 1 == r ? (int)t[o].rs : (int)t[t[o].rs].mx));
		if(x <= mid) t[o].ls = insert(t[p].ls,l,mid,x,y);
		if(y > mid) t[o].rs = insert(t[p].rs,mid + 1,r,x,y);
		t[o].mx = max((l == mid ? (int)t[o].ls : (int)t[t[o].ls].mx),(mid + 1 == r ? (int)t[o].rs : (int)t[t[o].rs].mx)) + tag;
		return o;
	}
	int query(int o,int l,int r,int x,int y)
	{
		if(l == r) return o;
		if(x <= l && r <= y) return t[o].mx;
		int mid = (l + r) >> 1 , ans = 0 , tag = t[o].mx - max((l == mid ? (int)t[o].ls : (int)t[t[o].ls].mx),(mid + 1 == r ? (int)t[o].rs : (int)t[t[o].rs].mx));
		if(x <= mid) ans = max(ans,query(t[o].ls,l,mid,x,y));
		if(y > mid) ans = max(ans,query(t[o].rs,mid + 1,r,x,y));
		return ans + tag;
	}
}
int main()
{
//	freopen("b.in","r",stdin);
//	freopen("b.out","w",stdout);
	n = read(); m = read(); using namespace PSMT;
	for(int i = 1;i <= n;i ++) a[i].first = read() , a[i].second = i;
	sort(a + 1,a + 1 + n);
	for(int i = n;i;i --) root[i] = insert(root[i + 1],1,n - m + 1,max(1,a[i].second - m + 1),min(a[i].second,n - m + 1));
	int Q = read() , ans = 0;
	while(Q --> 0)
	{
		int l = read() , r = read() , x = read() ^ ans , t = lower_bound(a + 1,a + 1 + n,mp(x,0)) - a;
		printf("%d\n",ans = (m - query(root[t],1,n - m + 1,l,r)));
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}

#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <ctime>
#include <cmath>
#define fi first
#define se second
#define PA pair<int,int>
#define VI vector<int>
#define VP vector<PA >
#define mk(x,y) make_pair(x,y)
#define int64 long long
#define N 200010
#define M 500
#define For(i,x,y) for (i=x;i<=y;i++)
using namespace std;
int i,j,k,n,m,q,an;
int a[N],ma[N][2],zhi[M][M],tma[M],ge[N],id[N],cov[M],L[N],R[N],fu[M][M]
	,yi[M][M];
inline bool cc1(const int &A,const int &B) {
	return a[A]<a[B];
}
inline int get(int x) {
	return x/M;
}
inline int xia(int x) {
	return (x/M+1)*M-1;
}
inline int qian(int x) {
	return x/M*M;
}
inline void pre() {
	For(i,0,n) id[i]=i;
	sort(id,id+n+1,cc1);
	For(i,0,n) {
		int A=id[i];
		int wei=get(i);
		if (i%M==0) {
			For(j,0,M-1) {
				zhi[wei][j]=tma[j];
				fu[wei][j]=cov[j];
				yi[wei][j]=ge[j*M]+cov[j];
			}
		}
		L[i]=R[i]=-1;
		for (j=max(0,A-m+1);j<=A;) {
			int _=get(j);
			if (j%M==0&&j+M-1<=A) {
				cov[_]++;
				if (L[i]<0) L[i]=_;
				R[i]=_;
				j+=M;
			} else {
				int f=_==get(A)?1:0;
				ge[j]++;
				tma[_]=max(tma[_],ge[j]);
				ma[i][f]=max(ma[i][f],ge[j]);
				j++;
			}
		}
	}
}
inline int find(int x) {
	int l=0,r=n,mid;
	for (;l<=r;) {
		mid=(l+r)/2;
		if (a[id[mid]]<=x) l=mid+1;
		else r=mid-1;
	}
	return l-1;
}
inline void work(int l,int r,int x) {
	int shu=find(x),i,j;
	int wei=get(shu);
	int zuo=get(l)+1,you=get(r)-1;
	an=0;
	For(i,0,M-1) tma[i]=0;
	For(i,qian(shu),shu) {
		if (L[i]<0) continue;
		tma[L[i]]++,tma[R[i]+1]--;
	}
	For(i,1,M-1) tma[i]+=tma[i-1];
	For(i,0,M-1) tma[i]+=fu[wei][i];
	For(i,qian(shu),shu) {
		int A=id[i];
		int le=max(0,A-m+1),ri=A;
		if (get(le)>=zuo&&get(le)<=you) an=max(an,ma[i][0]+tma[get(le)]);
		if (get(ri)>=zuo&&get(ri)<=you) an=max(an,ma[i][1]+tma[get(ri)]);
	}
	For(i,zuo,you) an=max(an,tma[i]+zhi[wei][i]);
	{
		int s; j=qian(l);
		s=yi[wei][get(j)];
		For(i,qian(shu),shu) {
			int A=id[i];
			int le=max(0,A-m+1),ri=A;
			if (le<=j&&j<=ri) s++;
		}
		j+=m;
		For(i,qian(l),xia(l)) {
			for (;j<=n&&i+m-1>=j;j++) s+=a[j]<=x;
			if (i>=l&&i<=r) an=max(an,s);
			s-=a[i]<=x;
		}
	}
	{
		int s; j=qian(r);
		s=yi[wei][get(j)];
		For(i,qian(shu),shu) {
			int A=id[i];
			int le=max(0,A-m+1),ri=A;
			if (le<=j&&j<=ri) s++;
		}
		j+=m;
		For(i,qian(r),xia(r)) {
			for (;j<=n&&i+m-1>=j;j++) s+=a[j]<=x;
			if (i>=l&&i<=r) an=max(an,s);
			s-=a[i]<=x;
		}
	}
	an=m-an;
	printf("%d\n",an);
}
int main() {
	scanf("%d%d",&n,&m); n--;
	For(i,0,n) scanf("%d",&a[i]),a[i]*=-1;
	pre();
	scanf("%d",&q);
	For(i,1,q) {
		int l,r,x;
		scanf("%d%d%d",&l,&r,&x);
		x^=an; l--,r--;
		work(l,r,-x);
	}
	//for(;;);
	return 0;
}

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <map>
using namespace std;

#define _ (size_t)
#define all(a) a.begin(), a.end()
#define rep(i, a, b) for(int i=int(a); i<=int(b); ++i)
#define irep(i, a, b) for(int i=int(a); i>=int(b); --i)
#define rep2(i, n) rep(i, 0, (n)-1)
#define irep2(i, n) irep(i, (n)-1, 0)
#define fillchar(a, x) memset(a, x, sizeof(a))
#define fi first
#define se second
#define PB push_back
#define MP make_pair
typedef long long LL;
typedef pair<LL,LL> PLL;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
typedef vector<LL> VL;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<string> VS;
template<class T> void setmax(T &a, T b) { if(b>a) a=b; }
template<class T> void setmin(T &a, T b) { if(b<a) a=b; }
template<class T> T gcd(T a, T b) { return b==0?a:gcd(b,a%b); }

const int MAXN=200010;
const int MAXNum=655;
const int MAXSize=455;

PII a[MAXN];
int n, m;

int bv[MAXNum][MAXNum], bv0[MAXNum][MAXNum], bvl[MAXN], bvr[MAXN];
VI bevents[MAXNum];
int bsize, bnum;

int bfirst(int p) { return (p-1)*bsize+1; }
int blast(int p) { return min(n, p*bsize); }
int bid(int i) { return (i-1)/bsize+1; }

void preprocess() {
    sort(a+1, a+n+1, greater<PII>());

    static int nums[MAXN], val[MAXNum], buf[MAXNum];
    bsize=450;
    bnum=(n-1)/bsize+1;
    fillchar(nums, 0); fillchar(val, 0); fillchar(buf, 0);
    fillchar(bv[0], 0);
    fillchar(bv0[0], 0);
    rep(i, 1, bnum) bevents[i].clear();
    rep(k, 1, n) {
        int l=max(1, a[k].se-m+1), r=a[k].se;
        int vl0=val[bid(l)], vr0=val[bid(r)];
        // update: add [l,r] by one
        for(int i=l; i<=r; ) {
            int p=bid(i);
            if(i==bfirst(p) && blast(p)<=r) {
                val[p]++; buf[p]++;
                i=blast(p)+1;
            }
            else {
                if(buf[p]!=0) {
                    int x=bfirst(p), y=blast(p);
                    rep(j, x, y) nums[j]+=buf[p];
                    buf[p]=0;
                }
                for(int j=min(r, blast(p)); i<=j; ++i) {
                    nums[i]++;
                    setmax(val[p], nums[i]);
                }
            }
        }
        // save bvl & bvr
        bvl[k]=val[bid(l)]-vl0;
        bvr[k]=val[bid(r)]-vr0;
        if(bid(l)==bid(r)) bvr[k]=0;
        // save bv & bv0
        if(k%bsize==0) {
            rep(p, 1, bnum) {
                bv[k/bsize][p]=val[p];
                bv0[k/bsize][p]=buf[p]+nums[bfirst(p)];
            }
        }
        // save bevent
        bool z=false;
        if(l!=bfirst(bid(l))) bevents[bid(l)].PB(k), z=true;
        if(bid(l)!=bid(r) || !z) bevents[bid(r)].PB(k);
    }
}

int cnt;
int query_block(int k0, int l, int r) {
    //printf("query_block %d %d %d\n",k0,l,r);
    assert(bid(l)==bid(r));
    int p=bid(l), first=bfirst(p), k1=k0/bsize*bsize;

    int v[MAXSize];
    fillchar(v, 0);
    //printf("%d\n", (int)bevents[p].size());
    for(int k: bevents[p]) {
        ++cnt;
        if(k>k1) break;
        int x=max(0, a[k].se-m+1-first);
        int y=min(bsize-1, a[k].se-first);
        if(x<=y) v[x]++, v[y+1]--;
    }
    int delta=bv0[k0/bsize][p]-v[0];
    rep(k, k1+1, k0) {
        int x=max(0, a[k].se-m+1-first);
        int y=min(bsize-1, a[k].se-first);
        if(x<=y) v[x]++, v[y+1]--;
    }
    rep(i, 0, bsize-1) {
        v[i+1]+=v[i];
        v[i]+=delta;
    }
    l=max(l-first, 0);
    r=min(r-first, bsize-1);
    int ans=0;
    rep(i, l, r) setmax(ans, v[i]);
    return ans;
}

int query_history(int k, int l, int r) {
    //printf("query_history %d %d %d\n",k,l,r);fflush(stdout);
    static int val[MAXNum], buf[MAXNum];
    memmove(val, bv[k/bsize], sizeof(val));
    fillchar(buf, 0);
    //rep(i, 1, bnum) printf("  val[%d]=%d\n",i,val[i]);
    rep(cur, k/bsize*bsize+1, k) {
        int x=max(1, a[cur].se-m+1), y=a[cur].se;
        x=bid(x), y=bid(y);
        if(bvl[cur]!=0) {
            buf[x]+=bvl[cur]; buf[x+1]-=bvl[cur];
        }
        if(bvr[cur]!=0) {
            buf[y]+=bvr[cur]; buf[y+1]-=bvr[cur];
        }
        ++x, --y;
        if(x<=y) buf[x]+=1, buf[y+1]-=1;
    }
    rep(i, 0, bnum) {
        buf[i+1]+=buf[i];
        val[i]+=buf[i];
    }
    //rep(i, 1, bnum) printf("  val[%d]=%d\n",i,val[i]);

    int ans=0;
    int cid=1, cfirst=1, clast=bsize;
    while(clast<l) ++cid, cfirst+=bsize, clast+=bsize;

    for(int i=l; i<=r; ) {
        //int p=bid(i);
        //if(i==bfirst(p) && blast(p)<=r) {
        if(i==cfirst && clast<=r) {
            setmax(ans, val[cid]);
            i=clast+1;
            //i=blast(p)+1;
        }
        else {
            int j=min(clast, r);
            //int j=min(blast(p), r);
            setmax(ans, query_block(k, i, j));
            i=j+1;
        }
        ++cid, cfirst+=bsize, clast+=bsize;
    }
    return ans;
}

int query(int l, int r, int q) {
    int i=int(lower_bound(a+1, a+n+1, MP(q,0), greater<PII>())-a);
    assert(i>=1 && i<=n+1);
    int ans=m-query_history(i-1, l, r);
    return ans;
}

int main() {
    /*
    n=m=200000; rep(i, 1, n) a[i].fi=rand(), a[i].se=i;
    preprocess();
    //rep(i, 1, 200000) { int l=1+rand()%100, r=100-rand()%100; if(l>r) swap(l,r); query(l,r,rand()); }
    cnt=0;
    rep(i, 1, 200000) {
        if(i%1000==0) printf("i=%d cnt=%d\n", i,cnt), fflush(stdout), cnt=0;
        if(i%1000==0) printf("bsize=%d\n",bsize);
        //int l=rand()%n+1, r=rand()%n+1; if(l>r) swap(l,r); query(l,r,rand());
        int p=rand()%n+1;
        int l=1, r=1; if(l>r) swap(l,r); query(l,r,rand());
        //int l=1+rand()%100, r=n-rand()%100; if(l>r) swap(l,r); query(l,r,rand());
    }
    return 0;
    //*/

    scanf("%d%d", &n,&m);
    rep(i, 1, n) scanf("%d", &a[i].fi), a[i].se=i;
    preprocess();

    int cs; scanf("%d", &cs);
    int pans=0;
    while(cs--) {
        int l, r, x;
        scanf("%d%d%d", &l,&r,&x);
        int q=x^pans;
        int ans=query(l, r, q);
        printf("%d\n", ans);
        pans=ans;
    }

    return 0;
}

#include <bits/stdc++.h>
#define min(a,b) (a<b?a:b)
using namespace std;
const int N=400050,inf=1e9;
int gi(){
    char ch=getchar(); int x=0,q=0;
    while(ch<'0'||ch>'9') q=ch=='-'?1:q,ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return q?-x:x;
}
struct date{
    int x,s;
    bool operator < (const date &c) const {
	return s<c.s;
    }
}b[N];
struct Blo{
    int a[500],fr[500],laz[500];
}B[500];
int n,m,a[N],c[500][500],bl[N],L[N],R[N],w[N],mn[500],laz[500],fr[500],las,P;
int f[N][2];
int find(int s){
    int l=1,r=n,ans=0,mid;
    while(l<=r){
	mid=(l+r)>>1;
	if (b[mid].s<=s) l=mid+1,ans=mid;
	else r=mid-1;
    }
    return ans;
}
void work(int l,int r,int x){
    int ans=inf,p=find(x-1);
    if (p==0) { printf("%d\n",las=0); return; }
    for (int i=1; i<=P; ++i) laz[i]=0,mn[i]=B[bl[p]-1].a[i],fr[i]=B[bl[p]-1].fr[i];
    for (int i=L[bl[p]]; i<=p; ++i){
	int zl=max(1,b[i].x-m+1),zr=b[i].x;
	if (bl[zl]!=bl[zr]) ++laz[bl[zl]+1],--laz[bl[zr]];
	mn[bl[zl]]=f[i][0],mn[bl[zr]]=f[i][1];
	if (zl==L[bl[zl]]) ++fr[bl[zl]];
	if (bl[zl]!=bl[zr]) ++fr[bl[zr]];
    }
    for (int i=1; i<=P; ++i) laz[i]+=laz[i-1];
    for (int i=1; i<=P; ++i) laz[i]+=B[bl[p]-1].laz[i];
    int sum=fr[bl[l]]+laz[bl[l]];
    for (int i=L[bl[l]]; i<=R[bl[l]]; ++i){
	if (i>=l&&i<=r) ans=min(ans,sum);
	if (a[i]<x) --sum;
	if (a[i+m]<x) ++sum;
    }
    for (int i=bl[l]+1; i<bl[r]; ++i) ans=min(ans,laz[i]+mn[i]);
    sum=fr[bl[r]]+laz[bl[r]];
    for (int i=L[bl[r]]; i<=R[bl[r]]; ++i){
	if (i>=l&&i<=r) ans=min(ans,sum);
	if (a[i]<x) --sum;
	if (a[i+m]<x) ++sum;
    }
    las=ans;
    printf("%d\n",ans);
    return;
}
void rebuild(int x){
    mn[x]=inf,fr[x]=w[L[x]];
    for (int i=L[x]; i<=R[x]; ++i)
	mn[x]=min(mn[x],w[i]);
    return;
}
int main(){
    n=gi(),m=gi();
    for (int i=1; i<=n; ++i) b[i]=(date){i,a[i]=gi()};
    sort(b+1,b+n+1);
    int blo=sqrt(n); P=(n-1)/blo+1;
    for (int i=1; i<=n; ++i){
	bl[i]=(i-1)/blo+1,R[bl[i]]=i;
	if (!L[bl[i]]) L[bl[i]]=i;
    }
    for (int i=1; i<=n; ++i){
	int l=max(1,b[i].x-m+1),r=b[i].x;
	if (bl[l]==bl[r]){
	    for (int j=l; j<=r; ++j) ++w[j];
	    rebuild(bl[l]);
	}
	else{
	    for (int j=bl[l]+1; j<bl[r]; ++j) ++laz[j];
	    for (int j=l; j<=R[bl[l]]; ++j) ++w[j];
	    for (int j=L[bl[r]]; j<=r; ++j) ++w[j];
	    rebuild(bl[l]),rebuild(bl[r]);
	}
	if (bl[i+1]!=bl[i])
	    for (int j=1; j<=P; ++j)
		B[bl[i]].a[j]=mn[j],B[bl[i]].fr[j]=fr[j],B[bl[i]].laz[j]=laz[j];
	f[i][0]=mn[bl[l]],f[i][1]=mn[bl[r]];
    }
    int Q=gi();
    for (int i=1; i<=Q; ++i){
	int l=gi(),r=gi(),x=gi();
	x^=las,work(l,r,x);
    }
    return 0;
}

//lxrtxdy!
#include <cstdio>
#include <algorithm>
#include <queue>
using std::min; using std::max;
using std::swap; using std::sort;
typedef unsigned long long ull;

template<typename T>
void read(T &x) {
    int flag = 1; x = 0; char ch = getchar();
    while(ch < '0' || ch > '9') { if(ch == '-') flag = -flag; ch = getchar(); }
    while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); x *= flag;
}

#define lson t[o].ls
#define rson t[o].rs
#define lmax (l == mid ? int(lson) : int(t[lson].mx))
#define rmax (mid + 1 == r ? int(rson) : int(t[rson].mx))
const int Inf = 1e9 + 7, N = 2e5 + 10;
struct Node { ull mx : 18, ls : 23, rs : 23; } t[N * 40];
int n, m, tot, rt[N], q, ans;
std::pair<int, int> a[N];

int modify(int p, int l, int r, int ql, int qr) {
	if(l == r) return p + 1;
	int o = ++tot; t[o] = t[p];
	if(ql <= l && r <= qr) return ++t[o].mx, o;
	int mid = (l + r) >> 1, tag = t[o].mx - max(lmax, rmax);
	if(ql <= mid) lson = modify(t[p].ls, l, mid, ql, qr);
	if(qr > mid) rson = modify(t[p].rs, mid + 1, r, ql, qr);
	t[o].mx = max(lmax, rmax), t[o].mx += tag; return o;
}
int query(int o, int l, int r, int ql, int qr) {
	if(l == r) return o;
	if(ql <= l && r <= qr)  return t[o].mx;
	int mid = (l + r) >> 1, ret = 0, tag = t[o].mx - max(lmax, rmax);
	if(ql <= mid) ret = max(ret, query(lson, l, mid, ql, qr));
	if(qr > mid) ret = max(ret, query(rson, mid + 1, r, ql, qr));
	return ret + tag;
}

int main () {
#ifdef OFFLINE_JUDGE
    freopen("233.in", "r", stdin);
    freopen("233.out", "w", stdout);
#endif
	read(n), read(m);
	for(int i = 1; i <= n; ++i)
		read(a[i].first), a[i].first = 0 - a[i].first, a[i].second = i;
	sort(&a[1], &a[n + 1]);
	for(int i = 1; i <= n; ++i) {
		int ql = max(1, a[i].second - m + 1);
		int qr = min(a[i].second, n - m + 1);
		rt[i] = modify(rt[i - 1], 1, n - m + 1, ql, qr);
	} read(q); int l, r, x;
	while(q--) {
		read(l), read(r), read(x), x ^= ans;
		int id = std::upper_bound(&a[1], &a[n + 1], std::make_pair(-x, Inf)) - a - 1;
		printf("%d\n", ans = m - query(rt[id], 1, n - m + 1, l, r));
	}
	return 0;
}
///123312
		  		  		  	 		      				  			//hellobig_tq
//orzlxr!

// {{{ by shik
#include <bits/stdc++.h>
#include <unistd.h>
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x),end(x)
#define REP(i,n) for ( int i=0; i<int(n); i++ )
#define REP1(i,a,b) for ( int i=(a); i<=int(b); i++ )
#define FOR(it,c) for ( auto it=(c).begin(); it!=(c).end(); it++ )
#define MP make_pair
#define PB push_back
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

#ifdef SHIK
template<typename T>
void _dump( const char* s, T&& head ) { cerr<<s<<"="<<head<<endl; }

template<typename T, typename... Args>
void _dump( const char* s, T&& head, Args&&... tail ) {
    int c=0;
    while ( *s!=',' || c!=0 ) {
        if ( *s=='(' || *s=='[' || *s=='{' ) c++;
        if ( *s==')' || *s==']' || *s=='}' ) c--;
        cerr<<*s++;
    }
    cerr<<"="<<head<<", ";
    _dump(s+1,tail...);
}

#define dump(...) do { \
    fprintf(stderr, "%s:%d - ", __PRETTY_FUNCTION__, __LINE__); \
    _dump(#__VA_ARGS__, __VA_ARGS__); \
} while (0);

template<typename Iter>
ostream& _out( ostream &s, Iter b, Iter e ) {
    s<<"[";
    for ( auto it=b; it!=e; it++ ) s<<(it==b?"":" ")<<*it;
    s<<"]";
    return s;
}

template<typename A, typename B>
ostream& operator <<( ostream &s, const pair<A,B> &p ) { return s<<"("<<p.first<<","<<p.second<<")"; }
template<typename T>
ostream& operator <<( ostream &s, const vector<T> &c ) { return _out(s,ALL(c)); }
template<typename T>
ostream& operator <<( ostream &s, const set<T> &c ) { return _out(s,ALL(c)); }
template<typename A, typename B>
ostream& operator <<( ostream &s, const map<A,B> &c ) { return _out(s,ALL(c)); }
#else
#define dump(...)
#endif

void RI() {}

template<typename... T>
void RI( int& head, T&... tail ) {
    scanf("%d",&head);
    RI(tail...);
}
// }}}

const int N=2e5+10;

struct Seg {
    uint64_t lc:23, rc:23, mx:18;
} mem[40 * N];

int get_mx(int id, int l, int r) {
    return l == r ? id : mem[id].mx;
}

int c_max(Seg *t, int l, int m, int r) {
    return max(get_mx(t->lc, l, m), get_mx(t->rc, m + 1, r));
}

int n_seg;

int ins(int id, int l, int r, int ql, int qr) {
    if (l == r) return id + 1;
    Seg *t = &mem[++n_seg];
    *t = mem[id];
    if (ql <= l && r <= qr) {
        t->mx++;
    } else {
        int m = (l + r) / 2;
        int add = t->mx - c_max(t, l, m, r);
        if (ql <= m) t->lc = ins(t->lc, l, m, ql, qr);
        if (qr > m) t->rc = ins(t->rc, m + 1, r, ql, qr);
        t->mx = c_max(t, l, m, r) + add;
    }
    return t - mem;
}
int get_max(int id, int l, int r, int ql, int qr) {
    if (l == r) return id;
    Seg *t = &mem[id];
    if (ql <= l && r <= qr) {
        return t->mx;
    } else {
        int m = (l + r) / 2;
        int ret = 0;
        if (ql <= m) ret = max(ret, get_max(t->lc, l, m, ql, qr));
        if (qr > m) ret = max(ret, get_max(t->rc, m + 1, r, ql, qr));
        return ret + (t->mx - c_max(t, l, m, r));
    }
}

int n,m,seg[N];
PII a[N];
int main() {
    RI(n,m);
    REP1(i,1,n) RI(a[i].first);
    REP1(i,1,n) a[i].second=i;
    sort(a+1,a+n+1);
    for ( int i=n; i>=1; i-- ) {
        int r=min(n-m+1,a[i].second);
        int l=max(1,a[i].second-m+1);
        seg[i]=ins(seg[i+1],1,n-m+1,l,r);
    }
    int s,ans=0;
    RI(s);
    while ( s-- ) {
        int l,r,x;
        RI(l,r,x);
        x^=ans;
        int idx=lower_bound(a+1,a+n+1,MP(x,0))-a;
        ans=m-get_max(seg[idx],1,n-m+1,l,r);
        printf("%d\n",ans);
    }
    return 0;
}

//{{{
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
void SR(int &x){scanf("%d",&x);}void SR(ll &x){scanf("%lld",&x);}
void SR(double &x){scanf("%lf",&x);}void SR(char *s){scanf("%s",s);}
void RI(){}
template<typename I,typename... T>void RI(I &x,T&... tail){SR(x);RI(tail...);}
void SP(const int x){printf("%d",x);}void SP(const ll x){printf("%lld",x);}
void SP(const double x){printf("%.16lf",x);}void SP(const char *s){printf(s);}
void PS(){puts("");}
template<typename I,typename... T>void PS(I &x,T&... tail){putchar(' ');SP(x);PS(tail...);}
void PL(){puts("");}
template<typename I,typename... T>void PL(const I x,const T... tail){SP(x);PS(tail...);}
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x),end(x)
#define REP(i,n) for(int i=0;i<int(n);i++)
#define REP1(i,a,b) for(int i=(a);i<=int(b);i++)
#define pb push_back
#define mkp make_pair
#define F first
#define S second
//}}}
const int TN=8.06e6;
typedef unsigned long long ull;
struct Node
{
    static Node mem[TN],*pmem;
    Node():
        hi(0),l(0),r(0){}
    Node(const Node &o):
        hi(o.hi),l(o.l),r(o.r){}
    ull hi:18,l:23,r:23;
} Node::mem[TN],*Node::pmem=Node::mem;
struct Seg
{
    Node* dat=Node::mem;
    Node *&siz=Node::pmem;
#define Mid int mid=(low+high)/2
#define lc l(t),low,mid
#define rc r(t),mid+1,high
#define me t,low,high
    inline int hi(int o,int low,int high)
    {
        if(low==high) return o;
        return dat[o].hi;
    }
    inline int l(int o){return dat[o].l;}
    inline int r(int o){return dat[o].r;}
    int getNode(int x)
    {
        siz++;
        *siz=Node(dat[x]);
        return siz-dat;
    }
    int tag(int x,int low,int high)
    {
        Mid;
        return hi(x,low,high)-max(hi(l(x),low,mid),hi(r(x),mid+1,high));
    }
    void pull(int x,int low,int high,int d)
    {
        Mid;
        dat[x].hi=max(hi(l(x),low,mid),hi(r(x),mid+1,high))+d;
    }
    int add(int x,int low,int high,int a,int b,int k)
    {
        if(low==high){return x+k;}
        int t=getNode(x);
        if(low==a&&b==high)
        {
            dat[t].hi+=k;
            return t;
        }
        Mid;
        int d=tag(me);
        if(b<=mid) dat[t].l=add(lc,a,b,k);
        else if(mid+1<=a) dat[t].r=add(rc,a,b,k);
        else dat[t].l=add(lc,a,mid,k),dat[t].r=add(rc,mid+1,b,k);
        pull(me,d);return t;
    }
    int ask(int x,int low,int high,int a,int b)
    {
        if(low==a&&b==high) return hi(x,low,high);
        int t=x;
        Mid;
        int ret=0;
        if(b<=mid) ret=ask(lc,a,b);
        else if(mid+1<=a) ret=ask(rc,a,b);
        else ret=max(ask(lc,a,mid),ask(rc,mid+1,b));
        return ret+tag(me);
    }
} seg;
const int maxn=2e5+5;
int n,m,a[maxn];
void read()
{
    RI(n,m);
    REP1(i,1,n) RI(a[i]);
}
int rnk[maxn];
int *t=a;
bool cmp(int x,int y){return a[x]<a[y];}
void build()
{
    memcpy(rnk,a+1,sizeof(int)*n);
    sort(rnk,rnk+n);
    REP1(i,1,n) a[i]=lower_bound(rnk,rnk+n,a[i])-rnk;

    static int tmp[maxn];
    REP(i,n) tmp[i]=i+1;
    sort(tmp,tmp+n,cmp);

    t[n]=0;
    for(int i=n-1;i>=0;i--)
        t[i]=seg.add(t[i+1],1,n-m+1,max(tmp[i]-m+1,1),min(tmp[i],n-m+1),1);
}
void sol()
{
    int s;RI(s);
    int ans=0;
    while(s--)
    {
        int l,r,x;RI(l,r,x);
        int q=x^ans;
        int v=lower_bound(rnk,rnk+n,q)-rnk;
        ans=m-seg.ask(t[v],1,n-m+1,l,r);
        PL(ans);
    }
}
int main()
{
    read();
    build();
    sol();
    return 0;
}

//
//  main.cpp
//  Codeforces Round #302 (Div. 1) E
//
//  Created by Scholar on 6/10/15.
//  Copyright (c) 2015 Scholar. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>

#define lson t[o].ls
#define rson t[o].rs
#define lmax (l==mid?int(lson):int(t[lson].mx))
#define rmax (mid+1==r?int(rson):int(t[rson].mx))

using namespace std;

typedef unsigned long long ULL;

const int INF=0x3f3f3f3f;
int ql,qr,root[200010],tot;
struct treenode{
    ULL ls:23,rs:23,mx:18;
}t[8000000];
pair<int,int>a[200010];

int update(int l,int r,int p){
    if(l==r)return p+1;
    int o=++tot;
    t[o]=t[p];
    if(ql<=l&&r<=qr){
        t[o].mx++;
        return o;
    }
    int mid=(l+r)>>1,tmp=t[o].mx-max(lmax,rmax);
    if(ql<=mid)lson=update(l,mid,t[p].ls);
    if(mid<qr)rson=update(mid+1,r,t[p].rs);
    t[o].mx=max(lmax,rmax);
    t[o].mx+=tmp;
    return o;
}

int query(int o,int l,int r){
    if(l==r)return o;
    if(ql<=l&&r<=qr)return t[o].mx;
    int mid=(l+r)>>1,tmx=0,tmp=t[o].mx-max(lmax,rmax);
    if(ql<=mid)tmx=max(tmx,query(lson,l,mid));
    if(mid<qr)tmx=max(tmx,query(rson,mid+1,r));
    return tmx+tmp;
}

int n,m,s;

int main(int argc, const char * argv[]) {
    ios::sync_with_stdio(0);
    int i;
    cin>>n>>m;
    int res;
    for(i=1;i<=n;i++){
        cin>>res;
        a[i]=make_pair(-res,i);
    }
    sort(a+1,a+n+1);
    for(i=1;i<=n;i++){
        ql=max(1,a[i].second-m+1);
        qr=min(n-m+1,a[i].second);
        root[i]=update(1,n-m+1,root[i-1]);
    }
    cin>>s;
    int last=0;
    while(s--){
        int q;
        cin>>ql>>qr>>q;
        q^=last;
        int id=upper_bound(a+1,a+n+1,make_pair(-q,INF))-a-1;
        printf("%d\n",last=m-query(root[id],1,n-m+1));
    }
    return 0;
}

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define R register
#define MN 200000
#define MB 100
#define SIZE 2000
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int n,m,N,a[MN+5],num,block[MN+5],mn[MB+5][SIZE*2+5],val[SIZE*2+5],rt[MN+5],cnt=0;
struct disc
{
	int*s,num;
	disc(int size=SIZE<<1){s=new int[size+5];num=0;}
	void Add(int x){s[++num]=x;}
	void Build(){sort(s+1,s+num+1);num=unique(s+1,s+num+1)-s-1;}
	int Calc(int x){return upper_bound(s+1,s+num+1,x)-s-1;}
}lb[MB+5],LB(MN+5);
struct Tree{int l,r,x;}T[MN*21+5];
void Init_Mn(int x)
{
	int l=(x-1)*SIZE+1,r=min(x*SIZE,N);
	memset(val,0,sizeof(val));
	for(R int i=l;i<=r;++i) lb[x].Add(a[i]),lb[x].Add(a[i+m-1]);
	lb[x].Build();int tot=lb[x].num;
	for(R int i=l;i<r;++i)
	{
		int A=lb[x].Calc(a[i]),B=lb[x].Calc(a[i+m]);
		for(R int i=A;i<=tot;++i) --val[i];
		for(R int i=B;i<=tot;++i) ++val[i];
		for(R int i=min(A,B);i<=tot;++i) mn[x][i]=min(mn[x][i],val[i]);
	}
}
void Modify(int x,int y,int l,int r,int k,int v)
{
	T[y].x=T[x].x+v;
	if(l==r) return;int mid=l+r>>1;
	if(k<=mid) T[y].r=T[x].r,Modify(T[x].l,T[y].l=++cnt,l,mid,k,v);
	else T[y].l=T[x].l,Modify(T[x].r,T[y].r=++cnt,mid+1,r,k,v);
}
int Query(int x,int l,int r,int k)
{
	if(k<l||!x) return 0;
	if(l==r) return T[x].x;
	int mid=l+r>>1;
	if(k<=mid) return Query(T[x].l,l,mid,k);
	else return T[T[x].l].x+Query(T[x].r,mid+1,r,k);
}
int Solve(int x,int v,int V)
{
	int l=(x-1)*SIZE+1,r=min(x*SIZE,n);
	int num=Query(rt[l+m-1],1,LB.num,v)-Query(rt[l-1],1,LB.num,v);
	return num+mn[x][lb[x].Calc(V)];
}
int main()
{
	n=read();m=read();N=n-m+1;
	for(R int i=1;i<=n;++i)a[i]=read(),LB.Add(a[i]);
	for(R int i=1;i<=N;++i) block[i]=(i-1)/SIZE+1;
	for(R int i=1;i<=block[N];++i) Init_Mn(i);
	LB.Build();int tot=LB.num;
	for(R int i=1;i<=n;++i) Modify(rt[i-1],rt[i]=++cnt,1,tot,LB.Calc(a[i]),1);
	for(int Q=read(),ans=0;Q;--Q)
	{
		int l=read(),r=read(),x=(read()^ans)-1;
		int nx=LB.Calc(x);ans=1e9;
		for(int i=block[l]+1;i<block[r];++i) ans=min(ans,Solve(i,nx,x));
		int L=min(r,block[l]*SIZE),lnum=Query(rt[L+m-1],1,tot,nx)-Query(rt[L-1],1,tot,nx);
		ans=min(ans,lnum);
		for(R int i=L-1;i>=l;--i) lnum-=(a[i+m]<=x),lnum+=(a[i]<=x),ans=min(ans,lnum);
		if(block[l]!=block[r])
		{
			int RR=(block[r]-1)*SIZE+1,rnum=Query(rt[RR+m-1],1,tot,nx)-Query(rt[RR-1],1,tot,nx);
			ans=min(ans,rnum);
			for(int j=RR+1;j<=r;++j) rnum-=(a[j-1]<=x),rnum+=(a[j+m-1]<=x),ans=min(ans,rnum);
		}
		printf("%d\n",ans);
	}
	return 0;
}

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define clear(x, y) memset(x, y, sizeof(x))

inline int read()
{
	int data = 0, w = 1; char ch = getchar();
	while(ch != '-' && (!isdigit(ch))) ch = getchar();
	if(ch == '-') w = -1, ch = getchar();
	while(isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar();
	return data * w;
}

const int maxn(2e5 + 10), INF(1e9);
#define lmax (l == mid ? int(t[rt].lson) : int(t[t[rt].lson].max))
#define rmax (mid + 1 == r ? int(t[rt].rson) : int(t[t[rt].rson].max))

struct node { unsigned long long max : 18, lson : 23, rson : 23; } t[maxn * 40];
int cur, root[maxn], n, m, Q;
std::pair<int, int> a[maxn];

int Insert(int p, int l, int r, int ql, int qr)
{
	if(l == r) return p + 1;
	int rt = ++cur; t[rt] = t[p];
	if(ql <= l && r <= qr) return ++t[rt].max, rt;
	int mid = (l + r) >> 1, tag = t[rt].max - std::max(lmax, rmax);
	if(ql <= mid) t[rt].lson = Insert(t[p].lson, l, mid, ql, qr);
	if(qr  > mid) t[rt].rson = Insert(t[p].rson, mid + 1, r, ql, qr);
	t[rt].max = std::max(lmax, rmax) + tag; return rt;
}

int query(int rt, int l, int r, int ql, int qr)
{
	if(l == r) return rt;
	if(ql <= l && r <= qr) return t[rt].max;
	int mid = (l + r) >> 1, ans = 0, tag = t[rt].max - std::max(lmax, rmax);
	if(ql <= mid) ans = std::max(ans, query(t[rt].lson, l, mid, ql, qr));
	if(qr  > mid) ans = std::max(ans, query(t[rt].rson, mid + 1, r, ql, qr));
	return ans + tag;
}

int main()
{
#ifndef ONLINE_JUDGE
	file(cpp);
#endif
	n = read(), m = read();
	for(RG int i = 1; i <= n; i++) a[i] = std::make_pair(-read(), i);
	std::sort(a + 1, a + n + 1);
	for(RG int i = 1; i <= n; i++)
	{
		int ql = std::max(1, a[i].second - m + 1);
		int qr = std::min(a[i].second, n - m + 1);
		root[i] = Insert(root[i - 1], 1, n - m + 1, ql, qr);
	}
	Q = read(); int ans = 0;
	while(Q--)
	{
		int l = read(), r = read(), x = read() ^ ans;
		int id = std::upper_bound(a + 1, a + n + 1,
				std::make_pair(-x, INF)) - a - 1;
		printf("%d\n", ans = m - query(root[id], 1, n - m + 1, l, r));
	}
	return 0;
}

#include <iostream>
#include <ctime>
#include <vector>
#include <algorithm>
using namespace std;
#define BSIZE 447

int N,M,Q;
int a[200000];
int cid[200000];
int pInc[200000],pDec[200000];

bool cmp(int x,int y)
{
	return a[x]<a[y];
}

vector<int> spoints[500];
vector<int> first[500];
vector<int> best[500];
vector<int> last[500];
int tmp[2000];

int dif[200001];

int slow(int l,int r,int x)
{
	int ans = 1000000000;
	for(int i=l;i<=r;i++)
	{
		int cur = 0;
		for(int j=i;j<i+M;j++)
			if(a[j]<x)
				cur++;
		ans = min(ans,cur);
	}
	return ans;
}

int main()
{
	//srand(1451781644);
	ios::sync_with_stdio(0);
	cin >> N >> M;
	for(int i=0;i<N;i++)
	{
		cin >> a[i];
		//a[i] = rand()%1000000;
		//cout << a[i] << ' ';
		cid[i] = i;
	}
	//cout << '\n';
	sort(cid,cid+N,cmp);
	for(int i=0;i<N;i++)
	{
		spoints[i/BSIZE].push_back(max(0,cid[i]-M+1));
		spoints[i/BSIZE].push_back(cid[i]+1);
	}
	for(int i=0;i*BSIZE<N;i++)
	{
		for(int j=0;j<N;j+=BSIZE)
			spoints[i].push_back(j);
		spoints[i].push_back(N);
		sort(spoints[i].begin(),spoints[i].end());
		spoints[i].resize(distance(spoints[i].begin(),unique(spoints[i].begin(),spoints[i].end())));
	}
	for(int i=0;i<N;i++)
	{
		pInc[i] = lower_bound(spoints[i/BSIZE].begin(),spoints[i/BSIZE].end(),max(0,cid[i]-M+1))-spoints[i/BSIZE].begin();
		pDec[i] = lower_bound(spoints[i/BSIZE].begin(),spoints[i/BSIZE].end(),cid[i]+1)-spoints[i/BSIZE].begin();
	}
	int lb = ((N-1)/BSIZE);
	for(int k=0;k<spoints[lb].size();k++)
	{
		best[lb].push_back(0), first[lb].push_back(0), last[lb].push_back(0);
	}
	for(int i=N-1;i>0;i--)
	{
		dif[max(0,cid[i]-M+1)]++, dif[cid[i]+1]--;
		if(i%BSIZE == 0)
		{
			int b = (i/BSIZE)-1;
			int v = 0;
			int j = 0;
			int mx = 0;
			for(int k=0;k<=N;k++)
			{
				v += dif[k];
				if(k == spoints[b][j])
				{
					first[b].push_back(v);
					if(j)
					{
						last[b].push_back(v-dif[k]);
						best[b].push_back(mx);
					}
					mx = v;
					j++;
				}
				else mx = max(mx,v);
			}
/*			for(int k=0;k<spoints[b].size();k++)
				cout << spoints[b][k] << ' ';
			cout << '\n';
			for(int k=0;k<best[b].size();k++)
				cout << best[b][k] << ' ';
			cout << "\n\n";
*/
		}
	}
	cin >> Q;
	int l,r,x;
	int ans;
	for(int i=0;i<Q;i++)
	{
		//l = rand()%N, r = rand()%(N-M), x = rand()%1000000;
		//if(l>r) swap(l,r);
		cin >> l >> r >> x;
		if(i) x ^= ans;
		l--,r--;
		int b = 0;
		while((b+1)*BSIZE<N && a[cid[(b+1)*BSIZE]] < x)
			b++;
		//cout << b << '\n';
		for(int j=0;j<spoints[b].size();j++)
			tmp[j] = 0;
		for(int k=min(N,(b+1)*BSIZE)-1;k>=0 && a[cid[k]] >= x;k--)
			tmp[pInc[k]]++,tmp[pDec[k]]--;
		int lBlock = lower_bound(spoints[b].begin(),spoints[b].end(),l)-spoints[b].begin();
		ans = 0;
		int v = first[b][lBlock];
		int ps = 0;
		for(int j=0;j<lBlock;j++)
			ps += tmp[j];
		v += ps+tmp[lBlock];
		for(int j=spoints[b][lBlock]-1;j>=l;j--)
		{
			if(a[j]>=x) v++;
			if(j+M<N && a[j+M]>=x) v--;
			if(j<=r) ans = max(ans,v);
		}
		int rBlock = upper_bound(spoints[b].begin(),spoints[b].end(),r)-spoints[b].begin()-1;
		v = first[b][rBlock]+ps;
		for(int j=lBlock;j<=rBlock;j++)
			v += tmp[j];
		for(int j=spoints[b][rBlock];j<=r;j++)
		{
			if(j>=l) ans = max(ans,v);
			if(a[j]>=x) v--;
			if(j+M<N && a[j+M]>=x) v++;
		}
		for(int j=lBlock;j<rBlock;j++)
		{
			ps += tmp[j];
			if(j>=lBlock && j<rBlock)
			{
				ans = max(ans,ps+best[b][j]);
			}
		}
		ans = M-ans;
		cout << ans << '\n';
		/*if(slow(l,r,x)!=ans)
		{
			cout << l << ' ' << r << ' ' << x << '\n';
			cout << ans << '\n';
		}*/
	}
}

#include<bits/stdc++.h>
using namespace std;
int n,m,a[222222],b[222222],c[222222];
int ib[222222],bl[1111],br[1111],mn[1111][1111],st[1111][1111],w[222222],ad[1111],cnt;
int pre[222222],suf[222222],q,l,r,x,ans,W[1111],bt,ls,rs,lp,rp;
const int magic=444;
bool cmp(int i,int j)
{
	return a[i]<a[j];
}
void update(int k,int l,int r)
{
	pre[k]=suf[k]=1e9;
	if (ib[l]==ib[r])
	{
		for (int i=l;i<=r;i++) w[i]++;
		for (int i=bl[ib[l]];i<=br[ib[l]];i++) pre[k]=min(pre[k],w[i]);
		return;
	}
	int il=ib[l],ir=ib[r];
	for (int i=il+1;i<ir;i++) ad[i]++;
	for (int i=l;i<=br[il];i++) w[i]++;
	for (int i=bl[il];i<=br[il];i++) pre[k]=min(pre[k],w[i]);
	for (int i=bl[ir];i<=r;i++) w[i]++;
	for (int i=bl[ir];i<=br[ir];i++) suf[k]=min(suf[k],w[i]);
}
void restore(int x)
{
	for (int i=1;i<=n;i++)
	{
		w[i]+=ad[ib[i]];
	}
	memset(ad,0,sizeof(ad));
	for (int i=1;i<=cnt;i++) mn[x][i]=1e9;
	for (int i=1;i<=n;i++)
	{
		if (ib[i]!=ib[i-1]) st[x][ib[i]]=w[i];
		mn[x][ib[i]]=min(mn[x][ib[i]],w[i]);
	}
}
int query(int l,int r,int id,int s,int x)
{
	int now=s,ans=1e9;
	for (int i=bl[id];i<l;i++) now+=((a[i+m]<x)-(a[i]<x));
	for (int i=l;i<=r;i++)
	{
		ans=min(ans,now);
		now+=((a[i+m]<x)-(a[i]<x));
	}
	return ans;
}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	for (int i=1;i<=n;i++) b[i]=i;
	sort(b+1,b+n+1,cmp);
	for (int i=1;i<=n;i++) c[i]=a[b[i]];
	for (int i=1;i<=n;i++)
	{
		if ((i-1)*magic+1>n) break;
		bl[i]=(i-1)*magic+1;br[i]=min(n,i*magic);
		for (int j=bl[i];j<=br[i];j++) ib[j]=i;
	}
	cnt=ib[n];
	for (int i=1;i<=n;i++)
	{
		update(i,max(1,b[i]-m+1),b[i]);
		if (ib[i]!=ib[i+1]) restore(ib[i]);
	}
	scanf("%d",&q);
	while(q--)
	{
		scanf("%d%d%d",&l,&r,&x);
		x^=ans;
		int pos=lower_bound(c+1,c+n+1,x)-c;
		if (pos>n || c[pos]>=x) pos--;
		memset(ad,0,sizeof(ad));
		bt=ib[pos];
		for (int i=1;i<=cnt;i++)
		{
			W[i]=mn[bt-1][i];
		}
		lp=bl[ib[l]];rp=bl[ib[r]];ls=st[bt-1][ib[l]];rs=st[bt-1][ib[r]];
		for (int i=bl[bt];i<=br[bt] && i<=pos;i++)
		{
			int L=max(1,b[i]-m+1),R=b[i],il=ib[L],ir=ib[R];
			if (il==ir)
			{
				W[il]=pre[i];
			}
			else
			{
				W[il]=pre[i];W[ir]=suf[i];
				ad[il+1]++;ad[ir]--;
			}
			if (L<=lp && lp<=R) ls++;
			if (L<=rp && rp<=R) rs++;
		}
		for (int i=1;i<=cnt;i++) ad[i]+=ad[i-1];
		if (ib[l]==ib[r])
		{
			ans=query(l,r,ib[l],ls,x);
		}
		else
		{
			ans=min(query(l,br[ib[l]],ib[l],ls,x),query(bl[ib[r]],r,ib[r],rs,x));
			for (int i=ib[l]+1;i<ib[r];i++) ans=min(ans,W[i]+ad[i]);
		}
		printf("%d\n",ans);
	}
	return 0;
}

#include <bits/stdc++.h>

#define forn(i, n) for(int i = 0; i < int(n); i++)
#define sz(a) int((a).size())
#define all(a) (a).begin(),(a).end()
#define pb push_back
#define mp make_pair
#define sc second
#define ft first

using namespace std;

const int M = 520;
const int N = M * M;
int blvalue[M][M];
int ftvalue[M][M];
int exvalue[M][2 * M];
int a[N];
pair<int, int> na[N];
int va[N];
int push[M];
int bl[M];
int n, m;
int blcount;
const int mult = 9;
const int blsz = (1 << mult);

inline bool is_first(int idx) {
  return (idx & (blsz - 1)) == 0;
}

inline int bl_idx(int idx) {
  if (idx >= n) {
    return (idx >> mult) + 1;
  }
  return idx >> mult;
}

inline int first_idx(int blid) {
  return blid << mult;
}

inline int last_idx(int blid) {
  return min(n - 1, first_idx(blid) + blsz - 1);
}

const int INF = 1000 * 1000 * 1000;

int main()
{
#ifdef gridnevvvit
  freopen("input.txt", "rt", stdin);
#endif
  scanf("%d %d", &n, &m);
  blcount = (n + blsz - 1) / blsz;
  forn(i, n) scanf("%d", &a[i]);
  forn(i, n)
    na[i] = mp(-a[i], i);
  sort(na, na + n);

  forn(iter, n) {
    if (is_first(iter)){
      int curbl = bl_idx(iter);
      forn(i, n)
        va[i] = va[i] + push[bl_idx(i)];
      forn(i, blcount) {
        bl[i] += push[i];
        blvalue[curbl][i] = bl[i];
        ftvalue[curbl][i] = va[first_idx(i)];
        push[i] = 0;
      }
    }
    int r = na[iter].sc;
    int l = max(0, r - m + 1);
    int blocks[] = {blcount, blcount};
    int szb = 0;
    for(int i = l; i <= r; ) {
      int curbl = bl_idx(i);
      if (is_first(i) && last_idx(curbl) <= r) {
        push[curbl] ++;
        i = last_idx(curbl) + 1;
      } else {
        va[i]++;
        bl[curbl] = max(bl[curbl], va[i]);
        if (szb == 0 || blocks[szb - 1] != curbl) {
          blocks[szb++] = curbl;
        }
        i++;
      }
    }
    szb = 2;
    int curbl = bl_idx(iter);
    int idx = iter - first_idx(curbl);
    forn(i, szb) {
      exvalue[curbl][2 * idx + i] = (bl[blocks[i]] << mult) + blocks[i];
    }
  }
  int query;
  scanf("%d", &query);
  int last_ans = 0;
  forn(it, query) {
    int l, r, q;
    scanf("%d %d %d", &l, &r, &q); l--, r--;
    q ^= last_ans;
    int idx = upper_bound(na, na + n, mp(-q, INF)) - na;
    idx--;
    if (idx < 0) {
      printf("%d\n", m);
      last_ans = m;
      continue;
    }
    int curbl = bl_idx(idx);
    forn(i, blcount) {
      bl[i] = blvalue[curbl][i];
      push[i] = 0;
    }
    int ft = first_idx(curbl);
    for(int i = ft; i <= idx; i++) {
      int nr = na[i].sc;
      int nl = max(0, nr - m + 1);
      if (nl != first_idx(bl_idx(nl))) {
        nl = last_idx(bl_idx(nl)) + 1;
      }
      if (nr == last_idx(bl_idx(nr))){
        nr++;
      }
      if (bl_idx(nl) <= bl_idx(nr)) {
        push[bl_idx(nl)] ++;
        push[bl_idx(nr)] --;
      }
      forn(j, 2) {
        int blid = exvalue[curbl][2 * (i - ft) + j];
        int vt = blid >> mult;
        blid &= (blsz - 1);
        bl[blid] = vt;
      }
    }
    forn(i, blcount) {
      if (i > 0) push[i] += push[i - 1];
    }
    int maxAns = 0;
    int pv = -1;
    for(int i = l; i <= r; ) {
       int blid = bl_idx(i);
       if (is_first(i) && last_idx(blid) <= r) {
         maxAns = max(maxAns, bl[blid] + push[blid]);
         i = last_idx(blid) + 1;
         pv = -1;
       } else {
         if (pv == -1) {
            pv = ftvalue[curbl][blid];
            int ftp = first_idx(blid);
            for(int j = ft; j <= idx; j++) {
              if (ftp <= na[j].sc && ftp >= max(na[j].sc - m + 1, 0)) {
                pv++;
              }
            }
            while (ftp != i) {
              pv -= (a[ftp] >= q);
              pv += (a[ftp + m] >= q);
              ftp++;
            }
         }
         maxAns = max(maxAns, pv);
         pv -= (a[i] >= q);
         pv += (a[i + m] >= q);
         i++;
      }
    }
    maxAns = m - maxAns;
    printf("%d\n", maxAns);
    last_ans = maxAns;
  }
}

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
inline int gi(){
	int f=1,sum=0;char ch=getchar();
	while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
	return f*sum;
}
const int N=200010,M=510;
int n,m,lis[N],a[N],B,b[N],bl[N],br[N],p[N],cnt,v[N];
int f[M][M],la[N],w[N],st[M][M],pre[N],suf[N],ed[N];
void Insert(int k,int l,int r){
	if(b[l]==b[r]){
		for(int i=l;i<=r;i++)w[i]++;
		for(int i=bl[b[l]];i<=br[b[l]];i++)pre[k]=max(pre[k],w[i]);
		return;
	}
	for(int i=l;i<=br[b[l]];i++)w[i]++;
	for(int i=bl[b[l]];i<=br[b[l]];i++)pre[k]=max(pre[k],w[i]);
	for(int i=b[l]+1;i<=b[r]-1;i++)la[i]++;
	for(int i=bl[b[r]];i<=r;i++)w[i]++;
	for(int i=bl[b[r]];i<=br[b[r]];i++)suf[k]=max(suf[k],w[i]);
}
void rec(int k){
	for(int i=1;i<=cnt;i++){
		for(int j=bl[i];j<=br[i];j++)
			f[k][i]=max(f[k][i],w[j]+=la[i]);
		st[k][i]=w[bl[i]];la[i]=0;
	}
}
int query(int l,int r,int k,int x){
	int now=v[k],ret=0;
	for(int i=bl[k];i<l;i++)now+=(a[i+m]>=x)-(a[i]>=x);
	for(int i=l;i<=r;i++)ret=max(ret,now),now+=(a[i+m]>=x)-(a[i]>=x);
	return ret;
}
bool cmp(int i,int j){return a[i]>a[j];}
int main(){
	n=gi();m=gi();B=sqrt(n);
	for(int i=1;i<=n;i++)a[i]=gi(),lis[i]=a[i],p[i]=i;
	sort(lis+1,lis+n+1);
	int nn=unique(lis+1,lis+n+1)-lis-1,Q;
	for(int i=1;i<=n;i++)a[i]=lower_bound(lis+1,lis+nn+1,a[i])-lis;
	for(int i=1,t;i<=n;i++){
		t=b[i]=(i-1)/B+1;bl[t]=bl[t]?bl[t]:i;
		br[t]=i;
	}
	sort(p+1,p+n+1,cmp);cnt=b[n];
	for(int i=1;i<=n;i++){
		Insert(i,max(1,p[i]-m+1),p[i]);
		if(b[i]!=b[i+1])rec(b[i]),ed[b[i]]=a[p[i]];
	}
	Q=gi();int ans=0;
	while(Q--){
		int l=gi(),r=gi(),x=gi(),t=1;
		x=lower_bound(lis+1,lis+nn+1,x^ans)-lis;
		while(t<cnt && ed[t]>=x)t++;
		for(int i=1;i<=cnt;i++)w[i]=f[t-1][i],v[i]=st[t-1][i],la[i]=0;
		for(int i=bl[t];i<=br[t] && a[p[i]]>=x;i++){
			int L=max(1,p[i]-m+1),R=p[i],A=b[L],B=b[R];
			if(A==B)w[A]=pre[i];
			else{
				w[A]=pre[i],w[B]=suf[i];
				la[A+1]++;la[B]--;
			}
			if(L<=bl[b[l]] && R>=bl[b[l]])v[b[l]]++;
			if(b[l]!=b[r] && L<=bl[b[r]] && R>=bl[b[r]])v[b[r]]++;
		}
		for(int i=1;i<=cnt;i++)la[i]+=la[i-1];
		int A=b[l],B=b[r];
		if(A==B)ans=query(l,r,A,x);
		else{
			ans=max(query(l,br[A],A,x),query(bl[B],r,B,x));
			for(int i=A+1;i<B;i++)ans=max(ans,la[i]+w[i]);
		}
		ans=m-ans;
		printf("%d\n",ans);
	}
	return 0;
}

#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rsz resize

const int MOD = 1000000007; // 998244353
const ll INF = 1e18;
const int MX = 200005;
const ld PI = 4*atan((ld)1);

template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }

namespace input {
    template<class T> void re(complex<T>& x);
    template<class T1, class T2> void re(pair<T1,T2>& p);
    template<class T> void re(vector<T>& a);
    template<class T, size_t SZ> void re(array<T,SZ>& a);

    template<class T> void re(T& x) { cin >> x; }
    void re(double& x) { string t; re(t); x = stod(t); }
    void re(ld& x) { string t; re(t); x = stold(t); }
    template<class Arg, class... Args> void re(Arg& first, Args&... rest) {
        re(first); re(rest...);
    }

    template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
    template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
    template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
    template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
}

using namespace input;

namespace output {
    template<class T1, class T2> void pr(const pair<T1,T2>& x);
    template<class T, size_t SZ> void pr(const array<T,SZ>& x);
    template<class T> void pr(const vector<T>& x);
    template<class T> void pr(const set<T>& x);
    template<class T1, class T2> void pr(const map<T1,T2>& x);

    template<class T> void pr(const T& x) { cout << x; }
    template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) {
        pr(first); pr(rest...);
    }

    template<class T1, class T2> void pr(const pair<T1,T2>& x) {
        pr("{",x.f,", ",x.s,"}");
    }
    template<class T> void prContain(const T& x) {
        pr("{");
        bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0; // const needed for vector<bool>
        pr("}");
    }
    template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
    template<class T> void pr(const vector<T>& x) { prContain(x); }
    template<class T> void pr(const set<T>& x) { prContain(x); }
    template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }

    void ps() { pr("\n"); }
    template<class Arg> void ps(const Arg& first) {
        pr(first); ps(); // no space at end of line
    }
    template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) {
        pr(first," "); ps(rest...); // print w/ spaces
    }
}

using namespace output;

namespace io {
    void setIn(string s) { freopen(s.c_str(),"r",stdin); }
    void setOut(string s) { freopen(s.c_str(),"w",stdout); }
    void setIO(string s = "") {
        ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
        if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
    }
}

using namespace io;

template<class T> T invGeneral(T a, T b) {
    a %= b; if (a == 0) return b == 1 ? 0 : -1;
    T x = invGeneral(b,a);
    return x == -1 ? -1 : ((1-(ll)b*x)/a+b)%b;
}

template<class T> struct modular {
    T val;
    explicit operator T() const { return val; }
    modular() { val = 0; }
    modular(const ll& v) {
        val = (-MOD <= v && v <= MOD) ? v : v % MOD;
        if (val < 0) val += MOD;
    }

    // friend ostream& operator<<(ostream& os, const modular& a) { return os << a.val; }
    friend void pr(const modular& a) { pr(a.val); }
    friend void re(modular& a) { ll x; re(x); a = modular(x); }

    friend bool operator==(const modular& a, const modular& b) { return a.val == b.val; }
    friend bool operator!=(const modular& a, const modular& b) { return !(a == b); }
    friend bool operator<(const modular& a, const modular& b) { return a.val < b.val; }

    modular operator-() const { return modular(-val); }
    modular& operator+=(const modular& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
    modular& operator-=(const modular& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
    modular& operator*=(const modular& m) { val = (ll)val*m.val%MOD; return *this; }
    friend modular pow(modular a, ll p) {
        modular ans = 1; for (; p; p /= 2, a *= a) if (p&1) ans *= a;
        return ans;
    }
    friend modular inv(const modular& a) {
        auto i = invGeneral(a.val,MOD); assert(i != -1);
        return i;
    } // equivalent to return exp(b,MOD-2) if MOD is prime
    modular& operator/=(const modular& m) { return (*this) *= inv(m); }

    friend modular operator+(modular a, const modular& b) { return a += b; }
    friend modular operator-(modular a, const modular& b) { return a -= b; }
    friend modular operator*(modular a, const modular& b) { return a *= b; }

    friend modular operator/(modular a, const modular& b) { return a /= b; }
};

typedef modular<int> mi;
typedef pair<mi,mi> pmi;
typedef vector<mi> vmi;
typedef vector<pmi> vpmi;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int BLOCK = 1<<9;

typedef array<int,BLOCK> T;

struct Stor {
	vector<pair<int,pi>> upds;
	vector<T> stor;
	T cur = T();

	Stor() {
		stor.pb(T());
	}

	void upd(int v, int l, int r) {
		assert(l <= r);
		assert(0 <= l && r < BLOCK);
		upds.pb({v,{l,r}});
		FOR(i,l,r+1) cur[i] ++;
		if (sz(upds) % BLOCK == 0) stor.pb(cur);
	}

	T get(int v) {
		int lo = 0, hi = sz(upds);
		while (lo < hi) {
			int mid = (lo+hi)/2;
			if (upds[mid].f >= v) lo = mid+1;
			else hi = mid;
		}
		// ps("WUT",sz(upds),sz(stor));
		auto res = stor[lo/BLOCK];
		T cum = T();
		FOR(i,lo/BLOCK*BLOCK,lo) {
			cum[upds[i].s.f] ++;
			cum[upds[i].s.s+1] --;
		}
		F0R(i,BLOCK) {
			if (i) cum[i] += cum[i-1];
			res[i] += cum[i];
		}
		return res;
	}

	int mx() {
		int res = 0;
		F0R(i,BLOCK) ckmax(res,cur[i]);
		return res;
	}
};

Stor big, small[BLOCK];

struct Mod {
	vector<T> stor;
	T cur = T();
	vpi upds;
	Mod() {
		stor.pb(cur);
	}
	void upd(int x) {
		int t = small[x].mx();
		if (t == cur[x]) return;
		// ps("MX UPD",x,t);
		upds.pb({small[x].upds.back().f,x});
		cur[x] ++;
		if (sz(upds) % BLOCK == 0) stor.pb(cur);
	}
	T get(int v) {
		int lo = 0, hi = sz(upds);
		while (lo < hi) {
			int mid = (lo+hi)/2;
			if (upds[mid].f >= v) lo = mid+1;
			else hi = mid;
		}
		auto res = stor[lo/BLOCK];
		FOR(i,lo/BLOCK*BLOCK,lo) res[upds[i].s] ++;
		return res;
	}
};

Mod mx;
int n,m,s;
vi a;
vpi A;

void upd(int v, int l, int r) {
	ckmax(l,0);
	// ps("UPD",v,l,r);
	int L = (l+BLOCK-1)/BLOCK, R = (r+1)/BLOCK-1;
	// ps("BOUNDS",L,R);
	if (L <= R) big.upd(v,L,R);
	if (L-2 == R) {
		int dif = (L-1)*BLOCK;
		small[L-1].upd(v,l-dif,r-dif);
		mx.upd(L-1);
		return;
	}
	if (l <= L*BLOCK-1) {
		int dif = (L-1)*BLOCK;
		small[L-1].upd(v,l-dif,BLOCK-1);
		mx.upd(L-1);
	}
	if ((R+1)*BLOCK <= r) {
		int dif = (R+1)*BLOCK;
		small[R+1].upd(v,0,r-dif);
		mx.upd(R+1);
	}
}

int query(int l, int r, int q) {
	auto b = big.get(q);
	// ps("BIG",b[0]);
	int L = (l+BLOCK-1)/BLOCK, R = (r+1)/BLOCK-1, res = 0;
	auto m = mx.get(q);
	// ps("MX",m[0]);
	// ps("BOUNDS",L,R);
	FOR(i,L,R+1) ckmax(res,b[i]+m[i]);
	if (L-2 == R) {
		int dif = (L-1)*BLOCK;
		auto t = small[L-1].get(q);
		FOR(i,l-dif,r-dif+1) ckmax(res,b[L-1]+t[i]);
		return res;
	}
	if (l <= L*BLOCK-1) {
		//ps("HAHA",L);
		int dif = (L-1)*BLOCK;
		auto t = small[L-1].get(q);
		FOR(i,l-dif,BLOCK) ckmax(res,b[L-1]+t[i]);
	}
	if ((R+1)*BLOCK <= r) {
		int dif = (R+1)*BLOCK;
		auto t = small[R+1].get(q);
		FOR(i,0,r-dif+1) ckmax(res,b[R+1]+t[i]);
	}
	return res;
}

int main() {
    setIO(); re(n,m); a.rsz(n); re(a);
    F0R(i,n) A.pb({a[i],i});
    sort(all(A));
    F0Rd(i,sz(A)) upd(A[i].f,A[i].s-m+1,A[i].s);

	int ans = 0;
	int s; re(s);
    F0R(i,s) {
    	int l,r,x; re(l,r,x); l--,r--;
    	ans = m-query(l,r,x^ans);
    	ps(ans);
    }
}

/* stuff you should look for
    * int overflow, array bounds
    * special cases (n=1?), set tle
    * do smth instead of nothing and stay organized
*/

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<ctime>
int tms=clock();
using namespace std;
inline int read()
{
	int x;char c;
	while((c=getchar())<'0'||c>'9');
	for(x=c-'0';(c=getchar())>='0'&&c<='9';)x=x*10+c-'0';
	return x;
}
#define MN 200000
#define K 50
#define KK 1000
#define mp make_pair
struct node{int l,r,s;}t[MN*19+5];
int a[MN+5],c[MN+5],p[MN+5],tn,rt[MN+5],s[MN/K+5],sc[MN/KK+5][MN/K+5],lp[MN+5],rp[MN+5],f[MN/K+5],fc[MN/KK+5][MN/K+5];
vector<int> v[MN/K+5],z[MN/K+5];
int add(int k,int l,int r,int x)
{
	int p=++tn;t[p].s=t[k].s+1;
	if(l==r)return p;
	int mid=l+r>>1;
	if(x<=mid)t[p].l=add(t[k].l,l,mid,x),t[p].r=t[k].r;
	else t[p].l=t[k].l,t[p].r=add(t[k].r,mid+1,r,x);
	return p;
}
#define L (k<<1)
#define R (k<<1|1)
struct tree{int mx,mk;}T[10000];
void build(int k,int l,int r)
{
	if(T[k].mx=T[k].mk=0,l==r)return;
	int mid=l+r>>1;build(L,l,mid);build(R,mid+1,r);
}
inline void up(int k){T[k].mx=min(T[L].mx,T[R].mx);}
inline void add(int k,int x){T[k].mx+=x;T[k].mk+=x;}
inline void down(int k){if(T[k].mk)add(L,T[k].mk),add(R,T[k].mk),T[k].mk=0;}
void add(int k,int l,int r,int ql,int qr)
{
	if(ql<=l&&qr>=r){add(k,1);return;}
	int mid=l+r>>1;down(k);
	if(qr<=mid)add(L,l,mid,ql,qr);
	else if(ql>mid)add(R,mid+1,r,ql,qr);
	else add(L,l,mid,ql,mid),add(R,mid+1,r,mid+1,qr);
	up(k);
}
int query(int k,int l,int r,int x)
{
	if(!k)return 0;
	if(r<=x)return t[k].s;
	int mid=l+r>>1;
	if(x<=mid)return query(t[k].l,l,mid,x);
	return t[t[k].l].s+query(t[k].r,mid+1,r,x);
}
bool CMP(int x,int y){return a[x]<a[y];}
int main()
{//freopen("0.in","r",stdin);
	int n,m,q,i,j,l,r,lk,rk,ans=0;
	n=read();m=read();
	for(i=1;i<=n;++i)a[i]=read(),c[i]=i;
	sort(c+1,c+n+1,CMP);sort(a+1,a+n+1);
	for(i=1;i<=n;++i)p[c[i]]=i;
	for(i=1;i<=n;++i)
	{
		rt[i]=add(rt[i-1],1,n,p[i]);
		lp[i]=max(0,i-m)/K;rp[i]=(i-1)/K;
		v[lp[i]].push_back(p[i]);
		if(rp[i]>lp[i])v[rp[i]].push_back(p[i]);
	}
	for(i=1;i<=n;++i)
	{
		++f[lp[c[i]]];rp[c[i]]>lp[c[i]]?++f[rp[c[i]]]:0;
		for(j=lp[c[i]];++j<rp[c[i]];)++s[j];
		if(i%KK==0)for(j=0;j<=(n-1)/K;++j)sc[i/KK][j]=s[j],fc[i/KK][j]=f[j];
	}
	for(i=0;i<=(n-1)/K;++i)
	{
		sort(v[i].begin(),v[i].end());
		build(1,i*K+1,min(i*K+K,n));
		for(j=0;j<v[i].size();++j)
		{
			add(1,i*K+1,min(i*K+K,n),c[v[i][j]]-m+1,c[v[i][j]]);
			z[i].push_back(T[1].mx);
		}
	}
//	printf("%d\n",clock()-tms);
	for(q=read();q--;)
	{
//		if(q%10000==0)printf("%d\n",clock()-tms);
		l=read();r=read();a[0]=lower_bound(a+1,a+n+1,ans^read())-a-1;ans=MN;
		if(!a[0]){ans=0;puts("0");continue;}
		lk=(l-1)/K;rk=(r-1)/K;
		for(i=l;i<=r&&i<=lk*K+K;++i)
			ans=min(ans,query(rt[i+m-1],1,n,a[0])-query(rt[i-1],1,n,a[0]));
		if(lk!=rk)for(i=r;i>rk*K;--i)
			ans=min(ans,query(rt[i+m-1],1,n,a[0])-query(rt[i-1],1,n,a[0]));
		memset(s,0,sizeof(s));
		for(i=lk;++i<rk;)f[i]=fc[(a[0]-1)/KK][i];
		for(i=(a[0]-1)/KK*KK;++i<=a[0];)
			++f[lp[c[i]]],rp[c[i]]>lp[c[i]]?++s[lp[c[i]]+1],--s[rp[c[i]]],++f[rp[c[i]]]:0;
		for(i=1;i<rk;++i)s[i]+=s[i-1];
		for(i=lk;++i<rk;)ans=min(ans,(f[i]?z[i][f[i]-1]:0)+sc[(a[0]-1)/KK][i]+s[i]);
		printf("%d\n",ans);
	}
//	printf("%d\n",clock()-tms);
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
#include <unordered_map>
#include <ctime>
typedef long long ll;
#define MAX 450
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
bool debug = 1;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
ll ln, lk, lm;

int a[200002], S;
vector<int> v, val[MAX];
int mn[MAX][MAX*2], sum[MAX][MAX*2];
int cal(int l, int r, int x) {
    int ans = inf;
    int now = lower_bound(v.begin(), v.end(), x) - v.begin();
    for(int i=0; i*S < n-m+1 && i*S<r; i++) {
        int st = i*S;
        int ed = min(st+S, n-m+1);
        k = lower_bound(val[i].begin(), val[i].end(), x) - val[i].begin()-1;
        if (k<0) {
            if (max(l,st) < min(r,ed)) ans = min(ans, now);
            continue;
        }
        if (l<=st && ed<=r) {
            ans = min(ans, now+mn[i][k]); now += sum[i][k];
        }
        else {
            if (max(l,st) < min(r,ed)) {
                for(int j=st;j<ed;j++) {
                    if(j>0 && a[j-1]<=val[i][k]) now--;
                    if (a[j-1+m]<=val[i][k]) now++;
                    if (l<=j && j<r) ans = min(ans, now);
                }
            } else now += sum[i][k];
        }

    }
    return ans;
}
int main() {
    scanf("%d%d", &n, &m);
    for(int i=0;i<n;i++) {
        scanf("%d", a+i);
    }
    REP(i,m-1) v.push_back(a[i]);
    sort(v.begin(), v.end());
    S = sqrt(n);
    for(int i=0;i*S <n-m+1; i++) {
        int st = i*S;
        int ed = min(st+S, n-m+1);
        for(int j=st;j<ed;j++) {
            if(j>0) val[i].push_back(a[j-1]);
            val[i].push_back(a[j+m-1]);
        }
        sort(val[i].begin(), val[i].end());
        val[i].resize(unique(val[i].begin(), val[i].end()) - val[i].begin());
        REP(k, val[i].size()) {
            mn[i][k] = inf;
            for(int j=st;j<ed;j++) {
                if (j>0 && a[j-1] <= val[i][k]) sum[i][k]--;
                if (a[j-1 + m] <= val[i][k]) sum[i][k]++;
                mn[i][k]=min(mn[i][k], sum[i][k]);
            }
        }
    }
    int q; scanf("%d", &q);
    for(int l,r,x,i=0,ans=0;i<q;i++) {
        scanf("%d%d%d", &l, &r, &x);
        x ^= ans;
        ans = cal(l-1,r,x);
        printf("%d\n", ans);
    }
    return 0;
}

#include<bits/stdc++.h>
#define LL unsigned long long
int read(){int x;scanf("%d",&x);return x;}
void write(int x){printf("%d\n",x);}
int max(int a,int b){return a>b? a:b;}
int min(int a,int b){return a<b? a:b;}
#define N 200007
#define INF 1000000000
#define mid ((l+r)>>1)
#define lmax (l==mid? int(t[p].L):int(t[t[p].L].max))
#define rmax (mid+1==r? int(t[p].R):int(t[t[p].R].max))
struct node{LL max:18,L:23,R:23;}t[N*40];
int root[N],cnt;
std::pair<int,int>a[N];
int ins(int x,int l,int r,int L,int R)
{
    if(l==r) return x+1;
    int p=++cnt;t[p]=t[x];
    if(L<=l&&r<=R) return ++t[p].max,p;
    int tag=t[p].max-max(lmax, rmax);
    if(L<=mid) t[p].L=ins(t[x].L,l,mid,L,R);
    if(R>mid) t[p].R=ins(t[x].R,mid+1,r,L,R);
    return t[p].max=max(lmax,rmax)+tag,p;
}

int query(int p,int l,int r,int L,int R)
{
    if(l==r) return p;
    if(L<=l&&r<=R) return t[p].max;
    int ans=0,tag=t[p].max-max(lmax,rmax);
    if(L<=mid) ans=max(ans,query(t[p].L,l,mid,L,R));
    if(R>mid) ans=max(ans,query(t[p].R,mid+1,r,L,R));
    return ans+tag;
}
int main()
{
    int n=read(),m=read(),Q,i,l,r,x,id,ans=0;
    for(i=1;i<=n;++i) a[i]=std::make_pair(-read(),i);
    std::sort(a+1,a+n+1);
    for(i=1;i<=n;++i) l=max(1,a[i].second-m+1),r=min(a[i].second,n-m+1),root[i]=ins(root[i-1],1,n-m+1,l,r);
    for(Q=read();Q;--Q) l=read(),r=read(),x=read()^ans,id=std::upper_bound(a+1,a+n+1,std::make_pair(-x,INF))-(a+1),write(ans=m-query(root[id],1,n-m+1,l,r));
    return 0;
}

//
// Created by dream_maker on 2019-06-08.
//

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;
typedef unsigned long long ull;

const int MAXN = 200000;
const int N = 8000000 + 10;
const int T = MAXN + 3;

int n, m, rt[MAXN + 10], Q;
pii a[MAXN + 10];

int ncnt;
ull t[N];

void calc_s(const bool &nl, const bool &nr, ull x, int &ls, int &rs, int &tag) {
  int s = x % T;
  ls = x / T / N;
  rs = x / T % N;
  tag = s - min((nl ? ls : t[ls] % T), (nr ? rs : t[rs] % T));
}

ull calc(const int &ls, const int &rs, const int &val) {
  return (1ull * ls * N + rs) * T + val;
}

void ins(const int &last, int &u, const int &L, const int &R, const int &l, const int &r) {
  if (L == R) {
    u = last + 1;
    return;
  }
  t[u = ++ncnt] = t[last];
  if (l <= L && R <= r) {
    t[u]++;
    return;
  }
  int mid = (L + R) >> 1, ls, rs, val, lson = 0, rson = 0;
  calc_s((L == mid), (R == mid + 1), t[u], ls, rs, val);
  if (l <= mid) ins(ls, lson, L, mid, l, r);
  if (r > mid) ins(rs, rson, mid + 1, R, l, r);
  if (!lson) lson = ls;
  if (!rson) rson = rs;
  val += min(((L == mid) ? lson : t[lson] % T), ((R == mid + 1) ? rson : t[rson] % T));
  t[u] = calc(lson, rson, val);
}

int query(const int &u, const int &L, const int &R, const int &l, const int &r) {
  if (L == R) return u;
  if (!u) return 0;
  if (l <= L && R <= r) return t[u] % T;
  int mid = (L + R) >> 1, ls, rs, val, ret = N;
  calc_s((L == mid), (mid + 1 == R), t[u], ls, rs, val);
  if (l <= mid) ret = min(ret, query(ls, L, mid, l, r));
  if (r > mid) ret = min(ret, query(rs, mid + 1, R, l, r));
  return ret + val;
}

inline void build() {
  for (int i = 1; i <= n; i++)
    ins(rt[i - 1], rt[i], 1, n - m + 1, max(1, a[i].second - m + 1), a[i].second);
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i].first), a[i].second = i;
  sort(a + 1, a + 1 + n);
  build();
  scanf("%d", &Q);
  int ans = 0;
  for (int i = 1; i <= Q; i++) {
    int x, l, r;
    scanf("%d%d%d", &l, &r, &x);
    x ^= ans;
    x = lower_bound(a + 1, a + 1 + n, make_pair(x, 0)) - a;
    if (x == 1) ans = 0;
    else ans = query(rt[x - 1], 1, n - m + 1, l, r);
    printf("%d\n", ans);
  }
  return 0;
}

//#include "stdafx.h"
#include <cstdio>
#include <cassert>
#include <fstream>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <vector>
#include <list>
#include <map>
#include <ctime>

using namespace std;
typedef long long LL;
const int N = 200002;

#define LSOne(S) (S & (-S))
#define Max(u , v) u < v ? v : u
#define Min(u , v) u < v ? u : v

const int L = 1000;

int LAZY[4 * L];
int MIN[4 * L];

void push(int x){
    LAZY[x + x] += LAZY[x];
    LAZY[x + x + 1] += LAZY[x];
}

void gather(int x) {
    MIN[x] += LAZY[x];
    LAZY[x] = 0;
}

int findRank(int* C, int num)
{
    int low = 0;
    int high = L;
    while (low < high) {
        int mid = (low + high) >> 1;
        if (C[mid] < num)
            low = mid + 1;
        else
            high = mid;
    }
    return L - high;
}

int query(){
    return MIN[1];
}

void update(int x, int l, int r, int ll, int rr)
{
    if (l >= ll && rr >= r)
        ++LAZY[x];
    if (l < r)
        push(x);
    gather(x);
    if (l >= ll && rr >= r)
        return;

    if (l < r && l <= rr && ll <= r) {
        int y = (l + r) >> 1;
        update(x + x, l, y, ll, rr);
        update(x + x + 1, y + 1, r, ll, rr);
        MIN[x] = Min(MIN[x + x], MIN[x + x + 1]);
    }
}

struct Move
{
    int M[2 * L+1];
    int C[2 * L];
    void solve()
    {
        memset(MIN, 0, sizeof(MIN));
        memset(LAZY, 0, sizeof(LAZY));
        pair<int, int> D[2 * L];
        for (int i = 0; i < 2 * L; ++i)
            D[i] = { C[i], i };
        sort(D, D + 2 * L);
        for (int i = 2 * L - 1; i >= 0;)
        {
            int j = i;
            while (j >= 0 && D[j].first == D[i].first)
            {
                update(1, 0, L, Max(0, D[j].second + 1 - L), Min(L, D[j].second));
                --j;
            }
            M[2 * L - j - 1] = query();
            i = j;
        }
        M[0] = 0;
        sort(C, C + L);
        sort(C + L, C + L + L);
    }

    void set(int* A, int *B){
        memcpy(C + L, A, sizeof(C) / 2);
        memcpy(C, B, sizeof(C) / 2);
    }
};

void run()
{
    int B[N / L + 1][L];
    int A[N];
    Move MV[N / L + 1];
    int n, m, s;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        scanf("%d", &A[i]);
        B[i / L][i%L] = A[i];
    }
    for (int i = 0; i < N / L; ++i) {
        int bgin = i*L;
        sort(B[i], B[i] + L);
        if (bgin + m + L <= n) {
            MV[i].set(A + bgin, A + bgin + m);
            MV[i].solve();
        }
    }
    cin >> s;
    int ans = m;
    for (int qr = 0; qr < s; ++qr) {
        int l, r, x;
        scanf("%d%d%d", &l, &r, &x);
        x ^= m-ans;
        --l; --r;

        // calculate first
        int val = 0;
        int p = l;
        int q = l + m - 1;
        int bgin = (p + L - 1)/L*L;
        while (p <= q && p < bgin) {
            val += A[p] >= x;
            ++p;
        }
        if (p <= q) {
            int j = p / L;
            while (p + L - 1 <= q) {
                val += findRank(B[j], x);
                p += L;
                ++j;
            }
        }
        while (p <= q) {
            val += A[p] >= x;
            ++p;
        }

        // move
        ans = val;
        p = l;
        while (p < bgin && p < r) {
            val += A[p + m] >= x;
            val -= A[p] >= x;
            ans = Max(ans, val);
            ++p;
        }

        if (p < r) {
            int j = p / L;
            while (p + L <= r) {
                int u = findRank(MV[j].C, x);
                int ttl = val + u;
                int v = findRank(MV[j].C + L, x);
                val = ttl - v;
                ans = Max(ans, ttl - MV[j].M[u + v]);
                p += L;
                ++j;
            }
        }

        while (p < r) {
            val += A[p + m] >= x;
            val -= A[p] >= x;
            ans = Max(ans, val);
            ++p;
        }
        printf("%d\n", m-ans);
    }
}

int main() {
	run();
	return 0;
}

#include<cstdio>
#include<algorithm>
using namespace std;
inline int read()
{
	char c=getchar();
	int out=0;
	while(!(c>='0'&&c<='9'))c=getchar();
	while(c>='0'&&c<='9')out=out*10+c-'0',c=getchar();
	return out;
}
struct node
{
	unsigned long long mx:18,lc:23,rc:23;
}t[8000010];
int nodecnt;
#define lmax (l==mid)?int(t[rt].lc):int(t[t[rt].lc].mx)
#define rmax (mid+1==r)?int(t[rt].rc):int(t[t[rt].rc].mx)
int modify(int p,int l,int r,int ml,int mr)
{
	if(l==r)return p+1;
	int rt=++nodecnt;
	t[rt]=t[p];
	if(ml<=l&&r<=mr)
	{
		t[rt].mx++;
		return rt;
	}
	int mid=(l+r)>>1,tag=t[p].mx-max(lmax,rmax);
	if(ml<=mid)t[rt].lc=modify(t[p].lc,l,mid,ml,mr);
	if(mr>mid)t[rt].rc=modify(t[p].rc,mid+1,r,ml,mr);
	t[rt].mx=max(lmax,rmax)+tag;
	return rt;
}
int query(int rt,int l,int r,int ql,int qr)
{
	if(l==r)return rt;
	if(ql<=l&&r<=qr)return t[rt].mx;
	int mid=(l+r)>>1;
	int tag=t[rt].mx-max(lmax,rmax),ret=0;
	if(ql<=mid)ret=query(t[rt].lc,l,mid,ql,qr);
	if(qr>mid)ret=max(ret,query(t[rt].rc,mid+1,r,ql,qr));
	return ret+tag;
}
int n,m,Q,lastans,rt[200010];
pair<int,int> a[200010];
int main()
{
	n=read();m=read();
	for(int i=1;i<=n;i++)a[i].first=-read(),a[i].second=i;
	sort(&a[1],&a[n+1]);
	for(int i=1;i<=n;i++)
	{
		int ql=max(1,a[i].second-m+1);
		int qr=min(n-m+1,a[i].second);
		rt[i]=modify(rt[i-1],1,n-m+1,ql,qr);
	}
	scanf("%d",&Q);
	while(Q--)
	{
		int l,r,x;
		l=read();r=read();x=read();
		x^=lastans;
		int id=upper_bound(&a[1],&a[n+1],make_pair(-x,0x3f3f3f3f))-a-1;
		printf("%d\n",lastans=m-query(rt[id],1,n-m+1,l,r));
	}
	return 0;
}

#include <bits/stdc++.h>

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define nfor(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define fore(i, l, r) for (int i = int(l); i < int(r); i++)
#define correct(x, y, n, m) (0 <= (x) && (x) < (n) && 0 <= (y) && (y) < (m))
#define all(a) (a).begin(), (a).end()
#define sz(a) int((a).size())
#define pb(a) push_back(a)
#define mp(x, y) make_pair((x), (y))
#define x first
#define y second

using namespace std;

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template<typename X> inline X abs(const X& a) { return a < 0? -a: a; }
template<typename X> inline X sqr(const X& a) { return a * a; }

template<typename A, typename B> inline ostream& operator<< (ostream& out, const pair<A, B>& p) { return out << "(" << p.x << ", " << p.y << ")"; }
template<typename T> inline ostream& operator<< (ostream& out, const vector<T>& a) { out << "["; forn(i, sz(a)) { if (i) out << ','; out << ' ' << a[i]; } return out << " ]"; }
template<typename T> inline ostream& operator<< (ostream& out, const set<T>& a) { return out << vector<T>(all(a)); }
template<typename T> inline ostream& operator<< (ostream& out, pair<T*, int> a) { return out << vector<T>(a.x, a.x + a.y); }

inline ld gett() { return ld(clock()) / CLOCKS_PER_SEC; }

const int INF = int(1e9);
const li INF64 = li(1e18);
const ld EPS = 1e-9, PI = 3.1415926535897932384626433832795;

const int N = 400200;

int n, m, k;
int a[N];
int l[N], r[N], x[N];

inline int rnd() { return abs((rand() << 16) ^ rand()); }

//#define GEN

bool read() {
#ifdef GEN
	n = 200000;
	m = rnd() % n + 1;
	forn(i, n) a[i] = rnd() & ((1 << 30) - 1);
	k = 200000;
	forn(i, k) {
		l[i] = 0, r[i] = n - m;

		/*l[i] = rnd() % (n - m + 1);
		r[i] = rnd() % (n - m + 1);
		if (l[i] > r[i]) swap(l[i], r[i]);
		r[i]++;*/

		x[i] = rnd() & ((1 << 30) - 1);
	}
	return true;
#endif
	if (!(cin >> n >> m)) return false;
	forn(i, n) assert(scanf("%d", &a[i]) == 1);
	assert(cin >> k);
	forn(i, k) {
		assert(scanf("%d%d%d", &l[i], &r[i], &x[i]) == 3);
		l[i]--;
	}
	return true;
}

int blen;
int szcur, cur[N];

int calc(int lf, int rg, int p, int* s) {
	int bnum = lf / blen;
	pt seg(INF, -INF);

	int blfnum = (lf + m - 1) / blen;
	int mm = m;
	szcur = 1;

	fore(i, bnum + 1, blfnum) {
		seg.x = min(seg.x, i);
		seg.y = max(seg.y, i);
		mm -= blen;
	}

	int L1 = lf, R1 = min(n, rg + m - 1);
	R1 = min(R1, (bnum + 1) * blen);
	fore(i, L1, R1)
		cur[szcur++] = a[i] < p;

	int L2 = lf, R2 = rg + m - 1;
	L2 = max(max(R1, L2), blfnum * blen);
	fore(i, L2, R2)
		cur[szcur++] = a[i] < p;

	forn(i, szcur - 1) cur[i + 1] += cur[i];

	int ans = INF;
	fore(i, lf, rg) {
		int l = i - lf, r = l + mm;
		int val = cur[r] - cur[l];
		ans = min(ans, val);
	}
	ans += seg.x <= seg.y ? s[seg.y + 1] - s[seg.x] : 0;
	return ans;
}

int b[N];
pt seg[N];
vector<pt> z[N];

void prepare() {
	blen = int(sqrt(n));
	forn(i, n) b[i] = a[i];
	for (int i = 0; i < n; i += blen) {
		int j = min(n, i + blen);
		sort(b + i, b + j);

		if (i + m <= n) {
			int bnum = i / blen;
			seg[bnum] = pt(INF, -INF);

			int binum = (i + m - 1) / blen;
			int mm = m;
			static int cur[N];
			int szcur = 0;
			fore(k, i, min(n, j + m - 1)) {
				int cbnum = k / blen;
				if (k % blen == 0 && bnum < cbnum && cbnum < binum) {
					seg[bnum].x = min(seg[bnum].x, cbnum);
					seg[bnum].y = max(seg[bnum].y, cbnum);
					mm -= blen;
					k += blen - 1;
				} else cur[szcur++] = a[k];
			}
			fore(k, i, j) assert((k + m <= n) == (k - i + mm <= szcur));

			z[bnum].resize(szcur + 1);
			forn(k, szcur) z[bnum][k] = mp(cur[k], INF);
			z[bnum][szcur] = pt(INT_MAX, INF);
			sort(all(z[bnum]));

			static int t[N], t2[N];
			forn(q, szcur) t[q] = t2[q] = 0;
			auto inc = [&](int i, int val) { t2[i]++; for ( ; i < szcur; i |= i + 1) t[i] += val; };
			auto sum1 = [&](int i) { int ans = 0; for ( ; i >= 0; i = (i & (i + 1)) - 1) ans += t[i]; return ans; };
			auto sum = [&](int i, int j) { return sum1(j) - sum1(i - 1); };

			int ptr = 0;
			static pt pts[N];
			forn(k, szcur) pts[k] = pt(cur[k], k);
			sort(pts, pts + szcur);
			forn(k, sz(z[bnum])) {
				int p = z[bnum][k].x;
				int& ansv = z[bnum][k].y;
				while (ptr < szcur && pts[ptr].x < p) inc(pts[ptr++].y, +1);
				int ss = sum(0, 0 + mm - 1);
				forn(q, szcur - mm + 1) {
					ansv = min(ansv, ss);
					ss -= t2[q] - t2[q + mm];
				}
			}
			assert(ptr == szcur);
		}
	}
}

void prepare(int p, int* s) {
	for (int j = 0; j < n; j += blen) {
		int k = min(n, j + blen);
		int bnum = j / blen;
		int lf = j, rg = k;
		while (lf != rg) {
			int md = (lf + rg) >> 1;
			if (b[md] >= p) rg = md;
			else lf = md + 1;
		}
		int cnt = lf - j;
		s[bnum + 1] = s[bnum] + cnt;
	}
}

void solve() {
	prepare();
#ifdef GEN
	cout << "Prepared in " << gett() << " s." << endl;
#endif

	int ans = 0;
	forn(i, k) {
		int p = x[i] ^ ans;

		static int s[N];
		prepare(p, s);

		ans = INF;
		int bl = l[i] / blen, br = (r[i] - 1) / blen;
		fore(bnum, bl + 1, br) {
			int cur = lower_bound(all(z[bnum]), pt(p, -INF))->y;
			pt p = seg[bnum];
			if (p.x <= p.y) cur += s[p.y + 1] - s[p.x];
			ans = min(ans, cur);
		}
		if (bl == br)
			ans = min(ans, calc(l[i], r[i], p, s));
		else {
			ans = min(ans, calc(l[i], (bl + 1) * blen, p, s));
			ans = min(ans, calc(br * blen, r[i], p, s));
		}
		printf("%d\n", ans);
	}

#ifdef GEN
	cout << "Finished in " << gett() << " s." << endl;
#endif
}

int main() {
#ifdef SU1
    assert(freopen("input.txt", "rt", stdin));
    //assert(freopen("output.txt", "wt", stdout));
#endif

    cout << setprecision(10) << fixed;
    cerr << setprecision(5) << fixed;

    while (read()) {
		ld stime = gett();
		solve();
		cerr << "Time: " << gett() - stime << endl;
#ifdef GEN
		break;
#endif
	}

    return 0;
}

#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <ctime>
#include <cmath>
#define fi first
#define se second
#define PA pair<int,int>
#define VI vector<int>
#define VP vector<PA >
#define mk(x,y) make_pair(x,y)
#define int64 long long
#define N 200010
#define M 500
#define For(i,x,y) for (i=x;i<=y;i++)
using namespace std;
int i,j,k,n,m,q,an;
int a[N],ma[N][2],zhi[M][M],tma[M],ge[N],id[N],cov[M],L[N],R[N],fu[M][M]
	,yi[M][M];
inline bool cc1(const int &A,const int &B) {
	return a[A]<a[B];
}
inline int get(int x) {
	return x/M;
}
inline int xia(int x) {
	return (x/M+1)*M-1;
}
inline int qian(int x) {
	return x/M*M;
}
inline void pre() {
	For(i,0,n) id[i]=i;
	sort(id,id+n+1,cc1);
	For(i,0,n) {
		int A=id[i];
		int wei=get(i);
		if (i%M==0) {
			For(j,0,M-1) {
				zhi[wei][j]=tma[j];
				fu[wei][j]=cov[j];
				yi[wei][j]=ge[j*M]+cov[j];
			}
		}
		L[i]=R[i]=-1;
		for (j=max(0,A-m+1);j<=A;) {
			int _=get(j);
			if (j%M==0&&j+M-1<=A) {
				cov[_]++;
				if (L[i]<0) L[i]=_;
				R[i]=_;
				j+=M;
			} else {
				int f=_==get(A)?1:0;
				ge[j]++;
				tma[_]=max(tma[_],ge[j]);
				ma[i][f]=max(ma[i][f],ge[j]);
				j++;
			}
		}
	}
}
inline int find(int x) {
	int l=0,r=n,mid;
	for (;l<=r;) {
		mid=(l+r)/2;
		if (a[id[mid]]<=x) l=mid+1;
		else r=mid-1;
	}
	return l-1;
}
inline void work(int l,int r,int x) {
	int shu=find(x),i,j;
	int wei=get(shu);
	int zuo=get(l)+1,you=get(r)-1;
	an=0;
	For(i,0,M-1) tma[i]=0;
	For(i,qian(shu),shu) {
		if (L[i]<0) continue;
		tma[L[i]]++,tma[R[i]+1]--;
	}
	For(i,1,M-1) tma[i]+=tma[i-1];
	For(i,0,M-1) tma[i]+=fu[wei][i];
	For(i,qian(shu),shu) {
		int A=id[i];
		int le=max(0,A-m+1),ri=A;
		if (get(le)>=zuo&&get(le)<=you) an=max(an,ma[i][0]+tma[get(le)]);
		if (get(ri)>=zuo&&get(ri)<=you) an=max(an,ma[i][1]+tma[get(ri)]);
	}
	For(i,zuo,you) an=max(an,tma[i]+zhi[wei][i]);
	{
		int s; j=qian(l);
		s=yi[wei][get(j)];
		For(i,qian(shu),shu) {
			int A=id[i];
			int le=max(0,A-m+1),ri=A;
			if (le<=j&&j<=ri) s++;
		}
		j+=m;
		For(i,qian(l),xia(l)) {
			for (;j<=n&&i+m-1>=j;j++) s+=a[j]<=x;
			if (i>=l&&i<=r) an=max(an,s);
			s-=a[i]<=x;
		}
	}
	{
		int s; j=qian(r);
		s=yi[wei][get(j)];
		For(i,qian(shu),shu) {
			int A=id[i];
			int le=max(0,A-m+1),ri=A;
			if (le<=j&&j<=ri) s++;
		}
		j+=m;
		For(i,qian(r),xia(r)) {
			for (;j<=n&&i+m-1>=j;j++) s+=a[j]<=x;
			if (i>=l&&i<=r) an=max(an,s);
			s-=a[i]<=x;
		}
	}
	an=m-an;
	printf("%d\n",an);
}
int main() {
	scanf("%d%d",&n,&m); n--;
	For(i,0,n) scanf("%d",&a[i]),a[i]*=-1;
	pre();
	scanf("%d",&q);
	For(i,1,q) {
		int l,r,x;
		scanf("%d%d%d",&l,&r,&x);
		x^=an; l--,r--;
		work(l,r,-x);
	}
	//for(;;);
	return 0;
}

#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back

using namespace std;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;

const int NN=200500, L=500;
int n, m, a[NN];
VI v, val[L];
int st[L], ed[L];
int sum[L][L<<1], mn[L][L<<1];

int calc(int l, int r, int x){
	int ans=1e9;
	int now=lower_bound(v.begin(), v.end(), x)-v.begin();
	for(int i=0; i*L<n-m+1 && i*L<r; i++){
		int k=lower_bound(val[i].begin(), val[i].end(), x)-val[i].begin()-1;
		if(k<0){
			if(max(l, st[i])<min(r, ed[i])) ans=min(ans, now);
			continue;
		}

		if(l<=st[i] && ed[i]<=r) ans=min(ans, now+mn[i][k]);
		else if(max(l, st[i])<min(r, ed[i])){
			int cur=now;
			for(int j=st[i]; j<ed[i]; j++){
				if(j && a[j-1]<x) cur--;
				if(j+m<=n && a[j+m-1]<x) cur++;
				if(l<=j && j<r) ans=min(ans, cur);
			}
		}
		now+=sum[i][k];
	}
	return ans;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	cin.tie(0), cout.tie(0);

	cin >> n >> m;
	for(int i=0; i<n; i++) cin >> a[i];
	for(int i=0; i<m-1; i++) v.pb(a[i]);
	sort(v.begin(), v.end());

	for(int i=0; i*L<n-m+1; i++){
		st[i]=i*L;
		ed[i]=min(st[i]+L, n-m+1);

		for(int j=st[i]; j<ed[i]; j++){
			if(j) val[i].pb(a[j-1]);
			if(j+m<=n) val[i].pb(a[j+m-1]);
		}

		sort(val[i].begin(), val[i].end());
		val[i].resize(unique(val[i].begin(), val[i].end())-val[i].begin());

		for(int k=0; k<val[i].size(); k++){
			mn[i][k]=1e9;
			for(int j=st[i]; j<ed[i]; j++){
				if(j && a[j-1]<=val[i][k]) sum[i][k]--;
				if(j+m<=n && a[j+m-1]<=val[i][k]) sum[i][k]++;
				mn[i][k]=min(mn[i][k], sum[i][k]);
			}
		}
	}

	int q, l, r, x, ans=0;
	for(cin >> q; q--; ){
		cin >> l >> r >> x;
		x^=ans;
		cout << (ans=calc(l-1, r, x)) << endl;
	}

	return 0;
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
inline int gi() {
    register int data = 0, w = 1;
    register char ch = 0;
    while (!isdigit(ch) && ch != '-') ch = getchar();
    if (ch == '-') w = -1, ch = getchar();
    while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
    return w * data;
}
typedef unsigned long long ull;
#define lson t[o].ls
#define rson t[o].rs
#define lmax (l == mid ? int(lson) : int(t[lson].mx))
#define rmax (mid + 1 == r ? int(rson) : int(t[rson].mx))
const int INF = 1e9;
const int MAX_N = 2e5 + 5;
struct Node {
    ull mx : 18, ls : 23, rs : 23;
} t[MAX_N * 40];
int tot = 0, rt[MAX_N];
int modify(int p, int l, int r, int ql, int qr) {
    if (l == r) { return p + 1; }
    int o = ++tot; t[o] = t[p];
    if (ql <= l && r <= qr) return ++t[o].mx, o;
    int mid = (l + r) >> 1, tag = t[o].mx - max(lmax, rmax);
    if (ql <= mid) lson = modify(t[p].ls, l, mid, ql, qr);
    if (qr > mid) rson = modify(t[p].rs, mid + 1, r, ql, qr);
    t[o].mx = max(lmax, rmax);
    t[o].mx += tag;
    return o;
}
int query(int o, int l, int r, int ql, int qr) {
    if (l == r) return o;
    if (ql <= l && r <= qr) return t[o].mx;
    int mid = (l + r) >> 1, res = 0, tag = t[o].mx - max(lmax, rmax);
    if (ql <= mid) res = max(res, query(lson, l, mid, ql, qr));
    if (qr > mid) res = max(res, query(rson, mid + 1, r, ql, qr));
    return res + tag;
}
int N, M;
pair<int, int> a[MAX_N];
int main () {
    N = gi(), M = gi();
    for (int i = 1; i <= N; i++) a[i].first = 0 - gi(), a[i].second = i;
    sort(&a[1], &a[N + 1]);
    for (int i = 1; i <= N; i++) {
        int ql = max(1, a[i].second - M + 1);
        int qr = min(a[i].second, N - M + 1);
        rt[i] = modify(rt[i - 1], 1, N - M + 1, ql, qr);
    }
    int Q = gi(), ans = 0;
    while (Q--) {
        int l = gi(), r = gi(), x = gi();
        x ^= ans;
        int id = upper_bound(&a[1], &a[N + 1], make_pair(-x, INF)) - a - 1;
        printf("%d\n", ans = M - query(rt[id], 1, N - M + 1, l, r));
    }
    return 0;
}

#include <stdio.h>
#include <algorithm>

using std :: pair;
using std :: sort;

template < class T > void read(T &x) { char c, mi = 0; while(c = getchar(), c <= 32); if(c == '-') mi = 1, x = 0; else if(c < 48 || c > 57) return void(x = c); else x = c - 48; while(c = getchar(), c > 32) x = 10 * x + c - 48; if(mi == 1) x = -x; }
template < class T > void read(T &x, T &y) { read(x); read(y); }
template < class T > void read(T &x, T &y, T &z) { read(x, y); read(z); }
template < class T > void reada(T *a, int n) { for(int i = 0; i < n; ++i) read(a[i]); }
template < class T > void write(T x) { static char s[20]; char pt = 0, mi = (x < 0); if(mi == 1) x = -x; while(!pt || x > 0) { s[++pt] = (char)(x % 10 + '0'); x /= 10; } if(mi == 1) putchar('-'); while(pt > 0) putchar(s[pt--]); }
template < class T > void write(T x, T y) { write(x); putchar(' '); write(y); }
template < class T > void write(T x, T y, T z) { write(x, y); putchar(' '); write(z); }
template < class T > void writeln(T x) { write(x); puts(""); }
template < class T > void writea(T *a, int n) { for(int i = 0; i < n; ++i) { write(a[i]); putchar(i + 1 == n ? '\n' : ' '); } }

#define nmax 200010
#define fs first
#define sd second

int pt = 0;
int roots[nmax];
int n, m, q;
int ans, l, r, x;
pair < int, int > p[nmax];

int mi(const int &a, const int &b) {
	return a < b ? a : b;
}

int ma(const int &a, const int &b) {
	return a > b ? a : b;
}

struct Node {
	unsigned long long mi : 18, l : 23, r : 23;
} t[8088888];

int findAdd(int ret, int xl, char dl, int xr, char dr) {
	return t[ret].mi - mi(dl == 0 ? t[xl].mi : xl, dr == 0 ? t[xr].mi : xr);
}

void f(int ret, int xl, char dl, int xr, char dr, int add = 0) {
	t[ret].mi = add + mi(dl == 0 ? t[xl].mi : xl, dr == 0 ? t[xr].mi : xr);
	t[ret].l = xl;
	t[ret].r = xr;
}

int upd(int v, int L, int R, int l, int r) {
	if(L == R) {
		return v + 1;
	}
	int ret = ++pt;
	if(L == l && r == R) {
		t[ret].mi = t[v].mi + 1;
		t[ret].l = t[v].l;
		t[ret].r = t[v].r;
		return ret;
	}
	int M = (L + R) / 2;
	if(r <= M) {
		t[ret].l = upd(t[v].l, L, M, l, r);
		t[ret].r = t[v].r;
	} else if(l > M) {
		t[ret].r = upd(t[v].r, M + 1, R, l, r);
		t[ret].l = t[v].l;
	} else {
		t[ret].l = upd(t[v].l, L, M, l, M);
		t[ret].r = upd(t[v].r, M + 1, R, M + 1, r);
	}
	f(ret, t[ret].l, L == M, t[ret].r, M + 1 == R,
	findAdd(v, t[v].l, L == M, t[v].r, M + 1 == R));
	return ret;
}

int get(int v, int L, int R, int l, int r) {
	if(v == 0) {
		return 0;
	}
	if(L == R) {
		return v;
	}
	if(L == l && r == R) {
		return t[v].mi;
	}
	int M = (L + R) / 2;
	int z = findAdd(v, t[v].l, L == M, t[v].r, M + 1 == R);
	if(r <= M) {
		return z + get(t[v].l, L, M, l, r);
	}
	if(l > M) {
		return z + get(t[v].r, M + 1, R, l, r);
	}
	return z + mi(get(t[v].l, L, M, l, M),
		get(t[v].r, M + 1, R, M + 1, r));
}

int main() {
//	freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
//	freopen("elevator.in", "r", stdin); freopen("elevator.out", "w", stdout);
//	printf("%d\n", sizeof(t));
	read(n, m);
	for(int i = 1; i <= n; ++i) {
		read(p[i].fs);
		p[i].sd = i;
	}
	roots[0] = 0;
	sort(p + 1, p + 1 + n);
	for(int i = 1; i <= n; ++i) {
		roots[i] = upd(roots[i - 1], 1, n - m + 1, ma(p[i].sd - m + 1, 1), mi(p[i].sd, n - m + 1));
	}
	read(q);
	while(q --> 0) {
		read(l, r, x);
		x ^= ans;
		int id = upper_bound(p + 1, p + 1 + n, std :: make_pair(x, -1)) - (p + 1);
		ans = get(roots[id], 1, n - m + 1, l, r);
		writeln(ans);
	}
	getchar(); getchar();
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//INSERT ABOVE HERE
const int BS = 1<<9;
const int MAX = 1<<18;

int dat[MAX]={}; // state
int val[MAX/BS]={}; // max
int add[MAX/BS]={}; // add
int frt[MAX/BS]={}; // front

int bf[MAX/BS][MAX/BS]; // front
int bm[MAX/BS][MAX/BS]; // max

int cp[3][MAX]={}; // pos
int cv[3][MAX]={}; // value

signed main(){
  int n,m;
  cin>>n>>m;
  vector<int> as(n+n,0);
  for(int i=0;i<n;i++) cin>>as[i];

  using P = pair<int, int>;
  vector<P> vp;
  for(int i=0;i<n;i++) vp.emplace_back(-as[i],i);
  sort(vp.begin(),vp.end());

  auto eval=
    [&](int i){
      int l=(i/BS)*BS;
      if(add[l/BS]==0) return;
      for(int j=l;j<l+BS;j++) dat[j]+=add[j/BS];
      add[l/BS]=0;
    };

  memset(cp,-1,sizeof(cp));
  for(int i=0;i<n;i++){
    //cout<<i<<" "<<vp[i].second<<" "<<as[vp[i].second]<<endl;
    if(i%BS==0){
      for(int j=0;j<MAX;j+=BS){
        val[j/BS]+=add[j/BS];
        eval(j);
        bf[i/BS][j/BS]=dat[j];
        bm[i/BS][j/BS]=val[j/BS];
      }
    }
    // [l, r)
    int r=vp[i].second+1;
    int l=max(0,r-m);

    int idx=0;
    for(int j=l;j<r;j++){
      if(j%BS==0 && j+BS<=r){
        add[j/BS]+=1;
        j+=BS-1;
      }else{
        dat[j]+=1;
        chmax(val[j/BS],dat[j]);
        if(cp[idx][i]!=j/BS){
          idx++;
          cp[idx][i]=j/BS;
        }
        cv[idx][i]=val[j/BS];
      }
    }
    while(idx<2){
      idx++;
      cp[idx][i]=MAX/BS-1;
      cv[idx][i]=0;
    }
  }

  auto calc=
    [&](int l,int r,int v)->int{
      int idx=upper_bound(vp.begin(),vp.end(),P(-v,n))-vp.begin();

      int k=idx/BS*BS;
      for(int j=0;j<MAX;j+=BS){
        val[j/BS]=bm[k/BS][j/BS];
        add[j/BS]=0;
        frt[j/BS]=bf[k/BS][j/BS];
      }

      for(int i=k;i<idx;i++){
        int q=vp[i].second+1;
        int p=max(0,q-m);
        if(p%BS) p=(p/BS+1)*BS;
        if(p<=q){
          add[p/BS]+=1;
          add[q/BS]-=1;
        }
        val[cp[1][i]]=cv[1][i];
        val[cp[2][i]]=cv[2][i];
        if(p<=q && q%BS) frt[q/BS]+=1;
      }

      for(int j=BS;j<MAX;j+=BS)
        add[j/BS]+=add[j/BS-1];

      int res=0,flg=0,tmp=0;
      for(int j=l;j<r;j++){
        if(j%BS==0 && j+BS<=r){
          chmax(res,val[j/BS]+add[j/BS]);
          j+=BS-1;
          flg=0;
        }else{
          if(flg==0){
            flg=1;
            tmp=frt[j/BS]+add[j/BS];
            int p=j/BS*BS;
            while(p<j){
              tmp-=as[p]>=v;
              tmp+=as[p+m]>=v;
              p++;
            }
          }
          chmax(res,tmp);
          tmp-=as[j]>=v;
          tmp+=as[j+m]>=v;
        }
      }
      return m-res;
    };

  int q;
  cin>>q;
  int pre_ans=0;
  for(int i=0;i<q;i++){
    int l,r,x;
    cin>>l>>r>>x;
    l--;
    int ans=calc(l,r,x^pre_ans);
    cout<<ans<<"\n";
    pre_ans=ans;
  }
  cout<<flush;
  return 0;
}

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define clear(x, y) memset(x, y, sizeof(x))

inline int read()
{
	int data = 0, w = 1; char ch = getchar();
	while(ch != '-' && (!isdigit(ch))) ch = getchar();
	if(ch == '-') w = -1, ch = getchar();
	while(isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar();
	return data * w;
}

const int maxn(2e5 + 10), INF(1e9);
#define lmax (l == mid ? int(t[rt].lson) : int(t[t[rt].lson].max))
#define rmax (mid + 1 == r ? int(t[rt].rson) : int(t[t[rt].rson].max))

struct node { unsigned long long max : 18, lson : 23, rson : 23; } t[maxn * 40];
int cur, root[maxn], n, m, Q;
std::pair<int, int> a[maxn];

int Insert(int p, int l, int r, int ql, int qr)
{
	if(l == r) return p + 1;
	int rt = ++cur; t[rt] = t[p];
	if(ql <= l && r <= qr) return ++t[rt].max, rt;
	int mid = (l + r) >> 1, tag = t[rt].max - std::max(lmax, rmax);
	if(ql <= mid) t[rt].lson = Insert(t[p].lson, l, mid, ql, qr);
	if(qr  > mid) t[rt].rson = Insert(t[p].rson, mid + 1, r, ql, qr);
	t[rt].max = std::max(lmax, rmax) + tag; return rt;
}

int query(int rt, int l, int r, int ql, int qr)
{
	if(l == r) return rt;
	if(ql <= l && r <= qr) return t[rt].max;
	int mid = (l + r) >> 1, ans = 0, tag = t[rt].max - std::max(lmax, rmax);
	if(ql <= mid) ans = std::max(ans, query(t[rt].lson, l, mid, ql, qr));
	if(qr  > mid) ans = std::max(ans, query(t[rt].rson, mid + 1, r, ql, qr));
	return ans + tag;
}

int main()
{
#ifndef ONLINE_JUDGE
	file(cpp);
#endif
	n = read(), m = read();
	for(RG int i = 1; i <= n; i++) a[i] = std::make_pair(-read(), i);
	std::sort(a + 1, a + n + 1);
	for(RG int i = 1; i <= n; i++)
	{
		int ql = std::max(1, a[i].second - m + 1);
		int qr = std::min(a[i].second, n - m + 1);
		root[i] = Insert(root[i - 1], 1, n - m + 1, ql, qr);
	}
	Q = read(); int ans = 0;
	while(Q--)
	{
		int l = read(), r = read(), x = read() ^ ans;
		int id = std::upper_bound(a + 1, a + n + 1,
				std::make_pair(-x, INF)) - a - 1;
		printf("%d\n", ans = m - query(root[id], 1, n - m + 1, l, r));
	}
	return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#define lson a[o].ls
#define rson a[o].rs
#define lmax (l==mid?int(lson):int(a[lson].mx))
#define rmax (mid+1==r?int(rson):int(a[rson].mx))
using namespace std;

typedef unsigned long long ULL;

const int INF=0x3f3f3f3f;

struct Node {
    ULL mx:18,ls:23,rs:23;
}a[8000000];
int tot;

int root[200010];
pair<int,int>repr[200010];

int ql,qr;

int update(int l,int r,int p)
{
    if(l==r)
	{
        return p+1;
    }
    int o=++tot;
    a[o]=a[p];
    if(ql<=l&&r<=qr)
	{
        a[o].mx++;
        return o;
    }
    int mid=(l+r)>>1,al=a[o].mx-max(lmax,rmax);
    if(ql<=mid)
	{
        lson=update(l,mid,a[p].ls);
    }
    if(mid<qr)
	{
        rson=update(mid+1,r,a[p].rs);
    }
    a[o].mx=max(lmax,rmax);
    a[o].mx+=al;
    return o;
}

int query(int o,int l,int r)
{
    if(l==r)
	{
        return o;
    }
    if(ql<=l&&r<=qr)
	{
        return a[o].mx;
    }
    int mid=(l+r)>>1,res=0,al=a[o].mx-max(lmax,rmax);
    if(ql<=mid)
	{
        res=max(res,query(lson,l,mid));
    }
    if(mid<qr)
	{
        res=max(res,query(rson,mid+1,r));
    }
    return res+al;
}

int n,m,q;

int main()
{
//	freopen("E.in","r",stdin),freopen("E.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1,aa;i<=n;i++)
	{
        scanf("%d",&aa);
        repr[i]=make_pair(-aa,i);
    }
    sort(repr+1,repr+n+1);
    for(int i=1;i<=n;i++)
	{
        ql=max(1,repr[i].second-m+1);
        qr=min(repr[i].second,n-m+1);//!!
        root[i]=update(1,n-m+1,root[i-1]);
        //printf("%d\n",tot);
    }
    //return 0;
    scanf("%d",&q);
    int lastans=0;
    while(q--)
	{
        int x;
        scanf("%d%d%d",&ql,&qr,&x);
        x^=lastans;
        int id=upper_bound(repr+1,repr+n+1,make_pair(-x,INF))-repr-1;
        printf("%d\n",lastans=m-query(root[id],1,n-m+1));//n-m+1
    }
    return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <bitset>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define FI first
#define SE second
using namespace std;
typedef long long LL;
const int M = 500;
const int N = 200005;
pair <int, int> b[N];
int a[N], n, m;
int fi[M][M], mi[M][M], v[N], tv[N];
int vl[N], vr[N];
int tmi[M], tfi[M], lzmi[M], lzfi[M];

inline int start(int x) {
    return x * M;
}

inline int end(int x) {
    return min(n, (x + 1) * M) - 1;
}

inline int bkmin(int x) {
    return *min_element(tv + start(x), tv + end(x) + 1);
}

void update(int x) {
    int l = max(0, b[x].SE - m + 1);
    int r = b[x].SE;
    int L = l / M, R = r / M;
    ++lzfi[L + (l != start(L))];
    --lzfi[R + 1];
    ++L; --R;
    if(L > R) return;
    ++lzmi[L]; --lzmi[R + 1];
}

int brute(int l, int r, int q) {
    int val = tfi[l / M], ret = INF;
    for(int i = start(l / M); i <= r; ++i) {
        if(i >= l) ret = min(ret, val);
        if(a[i] < q) --val;
        if(a[i + m] < q) ++val;
    }
    return ret;
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 0; i < n; ++i) {
        scanf("%d", a + i);
        b[i] = make_pair(a[i], i);
    }
    sort(b, b + n);
    int bkcnt = (n - 1) / M;
    for(int i = 0; i <= bkcnt; ++i) {
        for(int j = start(i); j <= end(i); ++j) {
            int l = max(0, b[j].SE - m + 1), r = b[j].SE;
            int L = l / M, R = r / M;
            if(L == R) {
                for(int k = l; k <= r; ++k) ++tv[k];
                vl[j] = vr[j] = bkmin(L);
                continue;
            }
            for(int k = l; k <= end(L); ++k) ++tv[k];
            vl[j] = bkmin(L);
            for(int k = start(R); k <= r; ++k) ++tv[k];
            vr[j] = bkmin(R);
        }
        for(int j = start(i); j <= end(i); ++j) {
            ++v[max(0, b[j].SE - m + 1)];
            --v[b[j].SE + 1];
        }
        for(int j = 0; j < n; ++j) tv[j] = v[j];
        for(int j = 1; j < n; ++j) tv[j] += tv[j - 1];
        for(int j = 0; j <= bkcnt; ++j) {
            mi[i][j] = bkmin(j);
            fi[i][j] = tv[start(j)];
        }
    }
    int q, ans = 0;
    scanf("%d", &q);
    while(q--) {
        int l, r, x;
        scanf("%d%d%d", &l, &r, &x);
        --l; --r; x ^= ans;
        int p = lower_bound(b, b + n, make_pair(x, -1)) - b - 1;
        if(p < 0) {
            printf("%d\n", ans = 0);
            continue;
        }
        int idx = p / M;
        memset(tmi, 0, sizeof(tmi));
        memset(tfi, 0, sizeof(tfi));
        memset(lzmi, 0, sizeof(lzmi));
        memset(lzfi, 0, sizeof(lzfi));
        if(idx) {
            for(int i = 0; i <= bkcnt; ++i) {
                tmi[i] = mi[idx - 1][i];
                tfi[i] = fi[idx - 1][i];
            }
        }
        for(int i = start(idx); i <= p; ++i) {
            int L = max(0, b[i].SE - m + 1) / M;
            int R = b[i].SE / M;
            tmi[L] = vl[i]; tmi[R] = vr[i];
            update(i);
        }
        for(int i = 1; i <= bkcnt; ++i) {
            lzfi[i] += lzfi[i - 1];
            lzmi[i] += lzmi[i - 1];
        }
        for(int i = 0; i <= bkcnt; ++i) {
            tmi[i] += lzmi[i];
            tfi[i] += lzfi[i];
        }
        ans = INF;
        int L = l / M, R = r / M;
        for(int i = L + 1; i < R; ++i) ans = min(ans, tmi[i]);
        ans = min(ans, brute(l, min(r, end(L)), x));
        if(L ^ R) ans = min(ans, brute(start(R), r, x));
        printf("%d\n", ans);
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+7;
struct T{
	unsigned long long x:18,l:23,r:23;
}s[N*40];
pair<int,int>a[N];
int rt[N],id,u,v;
#define lx l==m?s[k].l:s[s[k].l].x
#define rx m+1==r?s[k].r:s[s[k].r].x
int upd(int t,int l,int r){
	if(l==r)return t+1;
	int k=++id;
	if(s[k]=s[t],u<=l&&r<=v)return ++s[k].x,k;
	int m=l+r>>1,w=s[k].x-max(lx,rx);
	if(u<=m)s[k].l=upd(s[t].l,l,m);
	if(m<v)s[k].r=upd(s[t].r,m+1,r);
	return s[k].x=max(lx,rx)+w,k;
}
int qry(int k,int l,int r){
	if(l==r)return k;
	if(u<=l&&r<=v)return s[k].x;
	int m=l+r>>1,z=0;
	if(u<=m)z=qry(s[k].l,l,m);
	if(m<v)z=max(z,qry(s[k].r,m+1,r));
	return z+s[k].x-max(lx,rx);
}
int main(){
	int n,m,p,q,i,j,l=0;
	scanf("%d%d",&n,&m),p=n-m+1;
	for(i=1;i<=n;++i)scanf("%d",&j),a[i].first=-j,a[i].second=i;
	sort(a+1,a+n+1);
	for(i=1;i<=n;++i)j=a[i].second,u=max(1,j-m+1),v=min(p,j),rt[i]=upd(rt[i-1],1,p);
	scanf("%d",&q);
	while(q--)scanf("%d%d%d",&u,&v,&i),i^=l,printf("%d\n",l=m-qry(rt[upper_bound(a+1,a+n+1,make_pair(-i,N))-a-1],1,p));
	return 0;
}

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define re register
using namespace std;
inline int read()
{
	int X=0,w=1; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
	while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
	return X*w;
}
const int MAXN=2e5+5;
const int INF=1e9;
struct node {
	unsigned long long mx:18,ls:23,rs:23;
} t[MAXN*40];
int cnt=0,rt[MAXN];
#define lson t[o].ls
#define rson t[o].rs
#define lmax (l==mid?(int)lson:(int)t[lson].mx)
#define rmax (mid+1==r?(int)rson:(int)t[rson].mx)
inline int modify(int p,int l,int r,int ql,int qr)
{
	if (l==r) return p+1;
	int o=++cnt; t[o]=t[p];
	if (ql<=l&&r<=qr) return ++t[o].mx,o;
	int mid=(l+r)>>1,tag=t[o].mx-max(lmax,rmax);
	if (ql<=mid) lson=modify(t[p].ls,l,mid,ql,qr);
	if (qr>mid) rson=modify(t[p].rs,mid+1,r,ql,qr);
	t[o].mx=max(lmax,rmax); t[o].mx+=tag;
	return o;
}
inline int query(int o,int l,int r,int ql,int qr)
{
	if (l==r) return o;
	if (ql<=l&&r<=qr) return t[o].mx;
	int mid=(l+r)>>1,ans=0,tag=t[o].mx-max(lmax,rmax);
	if (ql<=mid) ans=max(ans,query(lson,l,mid,ql,qr));
	if (qr>mid) ans=max(ans,query(rson,mid+1,r,ql,qr));
	return ans+tag;
}
int n,m;
pair<int,int> a[MAXN];
int main()
{
	n=read(),m=read();
	for (re int i=1;i<=n;++i) a[i].first=-read(),a[i].second=i;
	sort(a+1,a+n+1);
	for (re int i=1;i<=n;++i)
	{
		int ql=max(1,a[i].second-m+1);
		int qr=min(a[i].second,n-m+1);
		rt[i]=modify(rt[i-1],1,n-m+1,ql,qr);
	}
	int q=read(),ans=0;
	while (q--)
	{
		int l=read(),r=read(),x=read()^ans;
		int id=upper_bound(a+1,a+n+1,make_pair(-x,INF))-a-1;
		printf("%d\n",ans=m-query(rt[id],1,n-m+1,l,r));
	}
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2e5 + 5, inf = 1e9;
int read() {
	int x = 0, f = 1; char ch;
	while(! isdigit(ch = getchar())) (ch == '-') && (f = -f);
	for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 3) + (x << 1) + (ch ^ 48));
	return x * f;
}
template <class T> T Max(T a, T b) { return a > b ? a : b; }
template <class T> T Min(T a, T b) { return a < b ? a : b; }
#define lmax (l == mid ? int(t[rt].ls) : int(t[t[rt].ls].max))
#define rmax (mid + 1 == r ? int(t[rt].rs) : int(t[t[rt].rs].max))
struct node {
unsigned long long max : 18, ls : 23, rs : 23;
} t[N * 40];
pair <int, int> a[N];
int n, m, q, cnt, rt[N];
int change(int p, int l, int r, int x, int y) {
	if(l == r) return p + 1;
	int rt = ++ cnt; t[rt] = t[p];
	if(x <= l && r <= y) return ++ t[rt].max, rt;
	int mid = (l + r) >> 1, tag = t[rt].max - max(lmax, rmax);
	if(x <= mid) t[rt].ls = change(t[p].ls, l, mid, x, y);
	if(y > mid) t[rt].rs = change(t[p].rs, mid + 1, r, x, y);
	t[rt].max = max(lmax, rmax) + tag;
	return rt;
}
int query(int rt, int l, int r, int x, int y) {
	if(l == r) return rt;
	if(x <= l && r <= y) return t[rt].max;
	int mid = (l + r) >> 1, ans = 0, tag = t[rt].max - max(lmax, rmax);
	if(x <= mid) ans = max(ans, query(t[rt].ls, l, mid, x, y));
	if(y > mid) ans = max(ans, query(t[rt].rs, mid + 1, r, x, y));
	return ans + tag;
}
int main() {
	n = read(); m = read();
	for(int i = 1; i <= n; ++ i) a[i] = make_pair(-read(), i);
	sort(a + 1, a + n + 1);
	for(int i = 1, x, y; i <= n; ++ i) {
		x = max(1, a[i].second - m + 1); y = min(a[i].second, n - m + 1);
		rt[i] = change(rt[i - 1], 1, n - m + 1, x, y);
	}
	q = read();
	for(int i = 1, l, r, x, id, ans = 0; i <= q; ++ i) {
		l = read(); r = read(), x = read() ^ ans;
		id = upper_bound(a + 1, a + n + 1, make_pair(- x, inf)) - a - 1;
		printf("%d\n", ans = m - query(rt[id], 1, n - m + 1, l, r));
	}
	return 0;
}
/*
5 3
1 2 1 2 3
5
1 1 2
1 3 2
1 3 3
1 3 5
1 3 1
*/

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = (a), _end_ = (b); i < _end_; ++i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()

template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

typedef long long LL;

const int oo = 0x3f3f3f3f;

const int maxn = 200000, max0 = 300, max1 = 700, max2 = 700, maxabs = 1 << 30;

int n, m;
vector<int> X;
int a[maxn + 5];

vector<int> id[max1 + 5], Min[max1 + 5], cnt[max1 + 5], to_add[max1 + 5];

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	scanf("%d%d", &n, &m);
	REP(i, 0, n) scanf("%d", a + i), X.pb(a[i]);
	X.pb(maxabs);
	sort(ALL(X));
	X.resize(unique(ALL(X)) - X.begin());
	int tot = 0;
	REP(i, 0, SZ(X))
	{
		if (!(i % max0))
		{
			static int sum[maxn + 5];
			static bool imp[maxn + 5];
			memset(sum, 0, sizeof sum);
			memset(imp, 0, sizeof imp);
			REP(j, 0, n)
			{
				if (!(j % max2)) imp[j] = 1;
				if (a[j] < X[i])
				{
					--sum[j + 1];
					++sum[max(0, j - m + 1)];
				}
				else if (a[j] >= X[i] && a[j] < X[min(SZ(X) - 1, i + max0)])
				{
					to_add[tot].pb(j);
					imp[j + 1] = 1;
					imp[max(0, j - m + 1)] = 1;
				}
			}
			int now = -1, pre = 0;
			REP(j, 0, n)
			{
				pre += sum[j];
				if (imp[j])
				{
					++now;
					id[tot].pb(j);
					Min[tot].pb(pre);
					cnt[tot].pb(pre);
				}
				chkmin(Min[tot][now], pre);
			}
			id[tot].pb(n);
			++tot;
		}
	}
	int qn, lastans = 0;
	scanf("%d", &qn);
	static int lst[maxn + 5], sum[maxn + 5];
	memset(lst, -1, sizeof lst);
	REP(i, 0, qn)
	{
		int l, r, z;
		scanf("%d%d%d", &l, &r, &z), --l;
		z ^= lastans;
		int tmp = lower_bound(ALL(X), z) - X.begin();
		int p = tmp / max0;
		for (auto x : to_add[p])
			if (a[x] < z)
			{
				if (lst[x + 1] != i) sum[x + 1] = 0, lst[x + 1] = i;
				--sum[x + 1];
				if (lst[max(0, x + 1 - m)] != i) sum[max(0, x + 1 - m)] = 0, lst[max(0, x + 1 - m)] = i;
				++sum[max(0, x + 1 - m)];
			}
		int pre = 0;
		int ans = oo;
		REP(j, 0, SZ(id[p]) - 1)
		{
			int u = id[p][j], v = id[p][j + 1];
			if (lst[id[p][j]] == i) pre += sum[id[p][j]];
			if (u >= l && v < r) chkmin(ans, pre + Min[p][j]);
			else
			{
				if (v <= l || u >= r) continue;
				int now = cnt[p][j] + pre;
				REP(k, u, v)
				{
					if (k >= l && k < r) chkmin(ans, now);
					if (a[k] < X[p * max0]) --now;
					if (k + m < n && a[k + m] < X[p * max0]) ++now;
				}
			}
		}
		printf("%d\n", ans);
		lastans = ans;
	}
	return 0;
}

#include<bits/stdc++.h>
#define LL unsigned long long
int read(){int x;scanf("%d",&x);return x;}
void write(int x){printf("%d\n",x);}
int max(int a,int b){return a>b? a:b;}
int min(int a,int b){return a<b? a:b;}
#define N 200007
#define INF 1000000000
#define mid ((l+r)>>1)
#define lmax (l==mid? int(t[p].L):int(t[t[p].L].max))
#define rmax (mid+1==r? int(t[p].R):int(t[t[p].R].max))
struct node{LL max:18,L:23,R:23;}t[N*40];
int root[N],cnt;
std::pair<int,int>a[N];
int ins(int x,int l,int r,int L,int R)
{
    if(l==r) return x+1;
    int p=++cnt;t[p]=t[x];
    if(L<=l&&r<=R) return ++t[p].max,p;
    int tag=t[p].max-max(lmax, rmax);
    if(L<=mid) t[p].L=ins(t[x].L,l,mid,L,R);
    if(R>mid) t[p].R=ins(t[x].R,mid+1,r,L,R);
    return t[p].max=max(lmax,rmax)+tag,p;
}

int query(int p,int l,int r,int L,int R)
{
    if(l==r) return p;
    if(L<=l&&r<=R) return t[p].max;
    int ans=0,tag=t[p].max-max(lmax,rmax);
    if(L<=mid) ans=max(ans,query(t[p].L,l,mid,L,R));
    if(R>mid) ans=max(ans,query(t[p].R,mid+1,r,L,R));
    return ans+tag;
}
int main()
{
    int n=read(),m=read(),Q,i,l,r,x,id,ans=0;
    for(i=1;i<=n;++i) a[i]=std::make_pair(-read(),i);
    std::sort(a+1,a+n+1);
    for(i=1;i<=n;++i) l=max(1,a[i].second-m+1),r=min(a[i].second,n-m+1),root[i]=ins(root[i-1],1,n-m+1,l,r);
    for(Q=read();Q;--Q) l=read(),r=read(),x=read()^ans,id=std::upper_bound(a+1,a+n+1,std::make_pair(-x,INF))-(a+1),write(ans=m-query(root[id],1,n-m+1,l,r));
    return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
#include <unordered_map>
#include <ctime>
typedef long long ll;
#define MAX 450
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
bool debug = 1;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
ll ln, lk, lm;

int a[200002], S;
vector<int> v, val[MAX];
int mn[MAX][MAX*2], sum[MAX][MAX*2];
int cal(int l, int r, int x) {
    int ans = inf;
    int now = lower_bound(v.begin(), v.end(), x) - v.begin();
    for(int i=0; i*S < n-m+1 && i*S<r; i++) {
        int st = i*S;
        int ed = min(st+S, n-m+1);
        k = lower_bound(val[i].begin(), val[i].end(), x) - val[i].begin()-1;
        if (k<0) {
            if (max(l,st) < min(r,ed)) ans = min(ans, now);
            continue;
        }
        if (l<=st && ed<=r) {
            ans = min(ans, now+mn[i][k]); now += sum[i][k];
        }
        else {
            if (max(l,st) < min(r,ed)) {
                for(int j=st;j<ed;j++) {
                    if(j>0 && a[j-1]<=val[i][k]) now--;
                    if (a[j-1+m]<=val[i][k]) now++;
                    if (l<=j && j<r) ans = min(ans, now);
                }
            } else now += sum[i][k];
        }

    }
    return ans;
}
int main() {
    scanf("%d%d", &n, &m);
    for(int i=0;i<n;i++) {
        scanf("%d", a+i);
    }
    REP(i,m-1) v.push_back(a[i]);
    sort(v.begin(), v.end());
    S = sqrt(n);
    for(int i=0;i*S <n-m+1; i++) {
        int st = i*S;
        int ed = min(st+S, n-m+1);
        for(int j=st;j<ed;j++) {
            if(j>0) val[i].push_back(a[j-1]);
            val[i].push_back(a[j+m-1]);
        }
        sort(val[i].begin(), val[i].end());
        val[i].resize(unique(val[i].begin(), val[i].end()) - val[i].begin());
        REP(k, val[i].size()) {
            mn[i][k] = inf;
            for(int j=st;j<ed;j++) {
                if (j>0 && a[j-1] <= val[i][k]) sum[i][k]--;
                if (a[j-1 + m] <= val[i][k]) sum[i][k]++;
                mn[i][k]=min(mn[i][k], sum[i][k]);
            }
        }
    }
    int q; scanf("%d", &q);
    for(int l,r,x,i=0,ans=0;i<q;i++) {
        scanf("%d%d%d", &l, &r, &x);
        x ^= ans;
        ans = cal(l-1,r,x);
        printf("%d\n", ans);
    }
    return 0;
}

#include <bits/stdc++.h>

#define	st first
#define	nd second
#define	mp make_pair
#define	pb push_back
#define	lli long long int
#define	all( gg )	gg.begin(),gg.end()
#define	foreach( gg,itit )	for( typeof(gg.begin()) itit=gg.begin();itit!=gg.end();itit++ )
#define	FP( ii,aa,bb ) for( int ii=aa;ii<=bb;ii++ )
#define	FM( ii,aa,bb ) for( int ii=aa;ii>=bb;ii-- )
#define	debug(ccc)	cout << #ccc << " = " << ccc << endl;

#define	mod	1000000007LL

#define	maxn	((1<<18))
#define	kokn	((1<<13))

#define	orta	(bas+son)/2
#define	sol		root*2,bas,orta
#define	sag		root*2+1,orta+1,son

using namespace std;

int	n,m;
int	arr[maxn];
pair<int,int>	a[maxn];

int	kd[19][maxn];
void	Init( int root,int bas,int son,int d=0 ){
	assert( d<19 and son<=200000 );
	if( bas==son ){
		kd[d][bas] = arr[bas];
		return;
	}
	Init( sol,d+1 );
	Init( sag,d+1 );
	merge( kd[d+1]+bas,kd[d+1]+orta+1,kd[d+1]+orta+1,kd[d+1]+son+1,kd[d]+bas );
}
int	Query( int root,int bas,int son,int x,int y,int val,int d=0 ){
	assert( d<19 and son<=200000 );
	if( son<x or y<bas )	return	0;
	if( x<=bas and son<=y )	return	lower_bound( kd[d]+bas,kd[d]+son+1,val )-(kd[d]+bas);
	return	Query( sol,x,y,val,d+1 )+Query( sag,x,y,val,d+1 );
}
int	minimal_solver( int l,int r,int v ){
	int	res=Query( 1,0,n-1,l,min(n-1,l+m-1),v );
	int	mini=res;
	FP( i,l,r-1 ){
		res -= (arr[i]<v);
		res += (arr[i+m]<v);
		mini = min( mini,res );
	}
	return	mini;
}

int	MINI[2*maxn],art[2*maxn];
void	init( int root,int bas,int son ){
	MINI[root] = art[root] = 0;
	if( bas==son )	return;
	init( sol );
	init( sag );
}
int	update( int root,int bas,int son,int x,int y ){
	if( son<x or y<bas )	return	MINI[root];
	if( x<=bas and son<=y ){
		art[root]++;
		MINI[root]++;
		return	MINI[root];
	}
	return	MINI[root] = min( update( sol,x,y ),update( sag,x,y ) )+art[root];
}
int	query( int root,int bas,int son,int x,int y ){
	if( son<x or y<bas )	return	mod;
	if( x<=bas and son<=y )	return	MINI[root];
	return	min( query( sol,x,y ),query( sag,x,y ) )+art[root];
}

int	s[maxn/kokn][maxn];
void	generate(){
	init( 1,0,n-1 );
	FP( i,0,n-1 ){
		FP( g,0,(n-1)/kokn )
			s[g][i] = query( 1,0,n-1,g*kokn,min( n-1,(g+1)*kokn-1 ) );
		update( 1,0,n-1,max(0,a[i].nd-m+1),a[i].nd );
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin >> n >> m;
	FP( i,0,n-1 ){
		cin >> arr[i];
		a[i] = mp( arr[i],i );
	}
	sort( a,a+n );
	Init( 1,0,n-1 );
	generate();
	int	q;
	cin >> q;
	int	last=0;
	int	cnt=0;
	while( q-- ){
		cnt++;
		int	l,r,v;
		cin >> l >> r >> v;
		v ^= last;
		l--;
		r--;
		int	res;
		int	t = lower_bound( a,a+n,mp( v,0 ) )-a;
		if( t==n ){
			res = m;
			last = res;
			cout << res << endl;
			continue;
		}
		if( l/kokn==r/kokn ){
			res = minimal_solver( l,r,v );
			last = res;
			cout << res << endl;
			continue;
		}
		res = min( minimal_solver( l,(l/kokn+1)*kokn-1,v ),minimal_solver( r/kokn*kokn,r,v ) );
		FP( i,l/kokn+1,r/kokn-1 )
			res = min( res,s[i][t] );
		last = res;
		cout << res << endl;
	}
}

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define IT iterator
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
#define debug puts("wzpakking")
#define y1 ysghysgsygsh
using namespace std;
const int N=200005;
struct node{
	ull mx:18,ls:23,rs:23;
}t[N*40];
int n,m,nd,rt[N];
pii v[N];
#define F(p) max(l==mid?t[p].ls:t[t[p].ls].mx,mid+1==r?t[p].rs:t[t[p].rs].mx)
int upd(int k,int l,int r,int x,int y){
	if (l==r) return k+1;
	int nk=++nd; t[nk]=t[k];
	if (x<=l&&r<=y) return ++t[nk].mx,nk;
	int mid=(l+r)/2,tg=t[k].mx-F(k);
	if (x<=mid) t[nk].ls=upd(t[k].ls,l,mid,x,y);
	if (y>mid) t[nk].rs=upd(t[k].rs,mid+1,r,x,y);
	t[nk].mx=F(nk)+tg;
	return nk;
}
int ask(int k,int l,int r,int x,int y){
	if (x<=l&&r<=y) return (l==r?k:t[k].mx);
	int mid=(l+r)/2,tg=t[k].mx-F(k);
	if (y<=mid) return ask(t[k].ls,l,mid,x,y)+tg;
	if (x>mid) return ask(t[k].rs,mid+1,r,x,y)+tg;
	return max(ask(t[k].ls,l,mid,x,mid),ask(t[k].rs,mid+1,r,mid+1,y))+tg;
}
int main(){
	scanf("%d%d",&n,&m);
	For(i,1,n) scanf("%d",&v[i].fi),v[i].se=i;
	sort(v+1,v+n+1,greater<pii>());
	For(i,1,n){
		int l=max(1,v[i].se-m+1);
		int r=min(n-m+1,v[i].se);
		//cout<<v[i].se<<' '<<l<<' '<<r<<endl;
		rt[i]=upd(rt[i-1],1,n-m+1,l,r);
	}
	int las=0,Q;
	scanf("%d",&Q);
	while (Q--){
		int l,r,k;
		scanf("%d%d%d",&l,&r,&k);
		k=lower_bound(v+1,v+n+1,pii(k^las,0),greater<pii>())-v-1;
		//cout<<k<<endl;
		printf("%d\n",las=m-ask(rt[k],1,n-m+1,l,r));
	}
}//hello

#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+5;
const int mult=9;
const int blsize=(1<<mult);
const int INF=(1<<30);
pair<int,int> music[maxn];
int Maxval[blsize][blsize],front[blsize][blsize],update[blsize][2*blsize]; // first: time , second: pos
int arr[maxn],curval[maxn];
int curMax[blsize],curtag[blsize];
int n,m;

inline bool is_first(int idx) { return (idx & (blsize - 1)) == 0;}
inline int bl_idx(int idx) {return (idx >> mult) + ( idx >= n ? 1 : 0 );}
inline int first_idx(int blid) {return blid << mult;}
inline int last_idx(int blid) {return min(n - 1, first_idx(blid) + blsize - 1);}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
    {
        scanf("%d",arr+i);
        music[i].first=-arr[i];
        music[i].second=i;
    }
    sort(music,music+n);

    int totbl= (n+blsize-1)/blsize;
    for(int i=0;i<n;i++)
    {
        if(is_first(i))
        {
            int curtime=bl_idx(i);
            for(int j=0;j<n;j++) curval[j]+=curtag[bl_idx(j)];
            for(int j=0;j<totbl;j++)
            {
                curMax[j]+=curtag[j];
                Maxval[curtime][j]=curMax[j];
                front[curtime][j]=curval[first_idx(j)];
                curtag[j]=0;
            }
        }
        int R=music[i].second,L=max(0,R-m+1),sizeb=0,tochange[]={totbl,totbl};
        for(int j=L;j<=R;j++)
        {
            int curbl=bl_idx(j);
            if(is_first(j) && R>=last_idx(curbl)) // jump whole block
                curtag[curbl]++,j=last_idx(curbl);
            else
            {
                curval[j]++;
                curMax[curbl]=max(curMax[curbl],curval[j]);
                if(sizeb==0 || tochange[sizeb-1]!=curbl) tochange[sizeb++]=curbl;
            }
        }
        int curtime=bl_idx(i),idx=i-first_idx(curtime);
        for(int j=0;j<2;j++) update[curtime][2*idx+j]=(curMax[tochange[j]]<<mult)+tochange[j];
    }
    int query,ans=0;
    scanf("%d",&query);
    while(query--)
    {
        int qL,qR,qv;
        scanf("%d%d%d",&qL,&qR,&qv);qL--,qR--,qv^=ans;
        int idx=upper_bound(music,music+n,make_pair(-qv,INF))-music-1;
        if(idx<0){printf("%d\n",ans=m);continue;}

        int curtime=bl_idx(idx);
        for(int i=0;i<totbl;i++) curMax[i]=Maxval[curtime][i],curtag[i]=0;

        int st=first_idx(curtime);
        for(int i=st;i<=idx;i++)
        {
            int R=music[i].second,L=max(0,R-m+1);
            if(L!=first_idx(bl_idx(L))) L=last_idx(bl_idx(L))+1;
            if(R==last_idx(bl_idx(R))) R++;
            if(bl_idx(L)<=bl_idx(R)) curtag[bl_idx(L)]++,curtag[bl_idx(R)]--;
            for(int j=0;j<2;j++)
            {
                int xx=update[curtime][2*(i-st)+j];
                curMax[xx&(blsize-1)]=xx>>mult;
            }
        }
        for(int i=1;i<totbl;i++) curtag[i]+=curtag[i-1];

        ans=0;
        int val=-1;
        for(int i=qL;i<=qR;i++)
        {
            int curbl=bl_idx(i);
            if(is_first(i) && qR>=last_idx(curbl))
                ans=max(ans,curMax[curbl]+curtag[curbl]),i=last_idx(curbl),val=-1;
            else
            {
                if(val==-1)
                {
                    val=front[curtime][curbl];
                    int ftp=first_idx(curbl);
                    for(int j=st;j<=idx;j++)
                        if(ftp<=music[j].second && ftp>=max(0,music[j].second-m+1)) val++;
                    while(ftp!=i)
                    {
                        val-=(arr[ftp]>=qv),val+=(arr[ftp+m]>=qv);
                        ftp++;
                    }
                }
                ans=max(ans,val);
                val-=(arr[i]>=qv);val+=(arr[i+m]>=qv);
            }
        }
        printf("%d\n",ans=m-ans);
    }
}

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<utility>
#define M 19
#define N 200010
#define B (N*2)
#define inf 1023456789
using namespace std;
//sum*(2N)^2+min*(2N)+lc
//offset=(N,N,1)
long long tr[M][N*2];
int buffer[2][N*2],a[N],p,mlen;
int nxt[N];
char single[N];
const long long zero=1LL*N*B*B+1LL*N*B+1;
bool cmp(int i,int j){
	return a[abs(i)]<a[abs(j)];
}
void build(int n,int m){
	int i,l,r,md,nid,sum,mn,lsum,rsum,lmn,rmn,lc,rc,lv=0;
	bool final;
	for(i=0;i<n-m;i++){
		buffer[p][i*2]=i;
		buffer[p][i*2+1]=-(i+m);
	}
	sort(buffer[p],buffer[p]+(n-m)*2,cmp);
	nxt[0]=n-m;
	while(1){
		final=true;
		for(l=0;l<n-m;l=r){
			r=nxt[l];
			if(l+1==r){
				single[l]++;
				if(buffer[p][l*2]>=0){
					tr[lv][l*2]=zero-1LL*B*B-B;
					tr[lv][l*2+1]=zero;
				}
				else{
					tr[lv][l*2]=zero+1LL*B*B;
					tr[lv][l*2+1]=zero;
				}
				continue;
			}
			final=false;
			md=(l+r)/2;
			nxt[l]=md;
			nxt[md]=r;
			lc=l*2;
			rc=md*2;
			for(i=l*2;i<r*2;i++){
				if(buffer[p][i]>=0){
					nid=buffer[p][i];
				}
				else{
					nid=-buffer[p][i]-m;
				}
				if(nid<md){
					buffer[p^1][lc++]=buffer[p][i];
				}
				else{
					buffer[p^1][rc++]=buffer[p][i];
				}
				tr[lv][i]=zero+lc-1;
			}
		}
		if(final) break;
		p^=1;
		lv++;
	}
	lv--;
	while(lv>=0){
		for(l=0;l<n-m;l=nxt[l]){
			if(single[l]>1){
				single[l]--;
				continue;
			}
			md=nxt[l];
			nxt[l]=r=nxt[md];
			if(l+1==r){
				continue;
			}
			for(i=l*2;i<r*2;i++){
				lc=tr[lv][i]%B-1;
				rc=md*2+(i-lc)-1;
				if(lc<l*2){
					lsum=lmn=0;
				}
				else{
					lsum=tr[lv+1][lc]/B/B-N;
					lmn=tr[lv+1][lc]/B%B-N;
				}
				if(i==lc){
					rsum=rmn=0;
				}
				else{
					rsum=tr[lv+1][rc]/B/B-N;
					rmn=tr[lv+1][rc]/B%B-N;
				}
				sum=lsum+rsum;
				mn=min(lsum+rmn,lmn);
				tr[lv][i]+=1LL*sum*B*B+1LL*mn*B;
			}
		}
		lv--;
	}
	memcpy(buffer[0],a,sizeof(int)*m);
	sort(buffer[0],buffer[0]+m);
	memcpy(buffer[1],a,sizeof(int)*(n-m));
	memcpy(buffer[1]+n-m,a+m,sizeof(int)*(n-m));
	sort(buffer[1],buffer[1]+(n-m)*2);
}
int qpsum(int lv,int l,int r,int qr,int i){
	if(i<l*2) return 0;
	if(qr==r) return tr[lv][i]/B/B-N;
	int md=(l+r)/2,lc=tr[lv][i]%B-1,rc=md*2+i-lc-1;
	if(qr<=md) return qpsum(lv+1,l,md,qr,lc);
	else return qpsum(lv+1,l,md,md,lc) + qpsum(lv+1,md,r,qr,rc);
}
//first:sum second:min
//return value: (sum+N)*B+(min+N)
long long qry(int lv,int l,int r,int ql,int qr,int i){
	if(i<l*2) return zero/B;
	if(l==ql&&r==qr){
		return tr[lv][i]/B;
	}
	int md=(l+r)/2,lc=tr[lv][i]%B-1,rc=md*2+i-lc-1;
	if(qr<=md){
		return qry(lv+1,l,md,ql,qr,lc);
	}
	else if(ql>=md){
		return qry(lv+1,md,r,ql,qr,rc);
	}
	else{
		long long lres=qry(lv+1,l,md,ql,md,lc);
		long long rres=qry(lv+1,md,r,md,qr,rc);
		return (lres/B+rres/B-N)*B+min(lres%B,lres/B+rres%B-N);
	}
}
int cal(int l,int r,int n,int m,int q){
	l--;
	r--;
	int base=lower_bound(buffer[0],buffer[0]+m,q)-buffer[0];
	int i=(upper_bound(buffer[1],buffer[1]+(n-m)*2,q-1)-buffer[1])-1;
	return base+(l?qpsum(0,0,n-m,l,i):0)+(l<r?qry(0,0,n-m,l,r,i)%B-N:0);
}
int main(){
	int n,m,q,ans=0,l,r,x,i;
	scanf("%d%d",&n,&m);
	for(i=0;i<n;i++) scanf("%d",&a[i]);
	build(n,m);
	scanf("%d",&q);
	while(q--){
		scanf("%d%d%d",&l,&r,&x);
		x^=ans;
		ans=cal(l,r,n,m,x);
		printf("%d\n",ans);
	}
	return 0;
}

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
inline int read()
{
	int x = 0 , f = 1; char ch = getchar();
	while(ch < '0' || ch > '9')	{if(ch == '-')	f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();}
	return x * f;
}
const int N = 2e5 + 1 , inf = 2e9 + 1;
int n , m , cnt , tot;
typedef pair<int,int> pp;
#define mp make_pair
pp a[N];
#define pb push_back
typedef unsigned long long ull;
namespace PSMT
{
	int root[N + 1];
	struct node
	{
		ull mx : 18 , ls : 23 , rs : 23;
	} t[N * 40];
	int insert(int p,int l,int r,int x,int y)
	{
		if(l == r) return p + 1;
		int o = ++ tot; t[o] = t[p];
		if(x <= l && r <= y) return t[o].mx ++ , o;
		int mid = (l + r) >> 1 , tag = t[o].mx - max((l == mid ? (int)t[o].ls : (int)t[t[o].ls].mx),(mid + 1 == r ? (int)t[o].rs : (int)t[t[o].rs].mx));
		if(x <= mid) t[o].ls = insert(t[p].ls,l,mid,x,y);
		if(y > mid) t[o].rs = insert(t[p].rs,mid + 1,r,x,y);
		t[o].mx = max((l == mid ? (int)t[o].ls : (int)t[t[o].ls].mx),(mid + 1 == r ? (int)t[o].rs : (int)t[t[o].rs].mx)) + tag;
		return o;
	}
	int query(int o,int l,int r,int x,int y)
	{
		if(l == r) return o;
		if(x <= l && r <= y) return t[o].mx;
		int mid = (l + r) >> 1 , ans = 0 , tag = t[o].mx - max((l == mid ? (int)t[o].ls : (int)t[t[o].ls].mx),(mid + 1 == r ? (int)t[o].rs : (int)t[t[o].rs].mx));
		if(x <= mid) ans = max(ans,query(t[o].ls,l,mid,x,y));
		if(y > mid) ans = max(ans,query(t[o].rs,mid + 1,r,x,y));
		return ans + tag;
	}
}
int main()
{
//	freopen("b.in","r",stdin);
//	freopen("b.out","w",stdout);
	n = read(); m = read(); using namespace PSMT;
	for(int i = 1;i <= n;i ++) a[i].first = read() , a[i].second = i;
	sort(a + 1,a + 1 + n);
	for(int i = n;i;i --) root[i] = insert(root[i + 1],1,n - m + 1,max(1,a[i].second - m + 1),min(a[i].second,n - m + 1));
	int Q = read() , ans = 0;
	while(Q --> 0)
	{
		int l = read() , r = read() , x = read() ^ ans , t = lower_bound(a + 1,a + 1 + n,mp(x,0)) - a;
		printf("%d\n",ans = (m - query(root[t],1,n - m + 1,l,r)));
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#define inf 0x3f3f3f3f
#define N 200005
#define M 450
using namespace std;
int gi()
{
    int res=0,s=1; char ch;
    for(ch=getchar();(ch<'0'||ch>'9')&&ch!='-';ch=getchar());
    if(ch=='-') s=-1,ch=getchar();
    for(;ch>='0'&&ch<='9';ch=getchar()) res=res*10+ch-48;
    return res*s;
}

int n,m,val[N],map[N],id[N],siz,lastans;
int bel[N],lef[M],rig[M],mn[M][M],laz[M][M],fir[M][M],value[N],tmpl[N],tmpr[N],lazy[M];

int cmp(int x,int y) {return val[x]<val[y];}
void add(int now,int p,int l,int r) {
    if(lef[p]>=l) fir[now][p]++;
    for(int k=l;k<=r;++k) value[k]++;
    mn[now][p]=inf;
    for(int k=lef[p];k<=rig[p];++k)
	mn[now][p]=min(mn[now][p],value[k]);
}
int ask(int p,int l,int r,int v) {
    int tmp=fir[0][p]+laz[0][p],res=inf;
    if(lef[p]>=l) res=tmp;
    for(int i=lef[p]+1;i<=r;++i) {
	tmp+=(val[i+m-1]<v)-(val[i-1]<v);
	if(i>=l) res=min(res,tmp);
    }
    return res;
}
int main()
{
    n=gi(),m=gi(); siz=sqrt(n);
    for(int i=1;i<=n;++i) {
	val[i]=gi(),id[i]=i;
	bel[i]=(i-1)/siz+1;
	if(bel[i]!=bel[i-1]) lef[bel[i]]=i,rig[bel[i-1]]=i-1;
    } rig[bel[n]]=n;
    sort(id+1,id+n+1,cmp);
    for(int i=1;i<=n;++i) map[i]=val[id[i]];
    for(int i=1,j=0,L,R;i<=n;++i) {
	if(bel[i]!=bel[i-1]) {   //new store point
	    ++j;
	    for(int k=1;k<=bel[n];++k)
		mn[j][k]=mn[j-1][k],laz[j][k]=laz[j-1][k],fir[j][k]=fir[j-1][k];
	}
	L=max(1,id[i]-m+1),R=id[i];
	if(bel[L]==bel[R])
	    add(j,bel[L],L,R),tmpl[i]=mn[j][bel[L]];
	else {
	    add(j,bel[L],L,rig[bel[L]]);
	    add(j,bel[R],lef[bel[R]],R);
	    tmpl[i]=mn[j][bel[L]],tmpr[i]=mn[j][bel[R]];
	    for(int i=bel[L]+1;i<bel[R];++i) laz[j][i]++;
	}
    }
    for(int q=gi(),l,r,x,X;q;--q) {
	l=gi(),r=gi(),x=gi()^lastans; X=x;
	x=lower_bound(map+1,map+n+1,x)-map-1;
	if(!x) {lastans=0;puts("0");continue;}
	for(int k=1;k<=bel[n];++k) {
	    mn[0][k]=laz[0][k]=fir[0][k]=lazy[k]=0;
	    mn[0][k]=mn[bel[x]-1][k],laz[0][k]=laz[bel[x]-1][k],fir[0][k]=fir[bel[x]-1][k];
	}
	for(int i=lef[bel[x]],L,R;i<=x;++i) {
	    L=max(1,id[i]-m+1),R=id[i];
	    if(bel[L]==bel[R]) {
		if(lef[bel[L]]>=L) fir[0][bel[L]]++;
		mn[0][bel[L]]=tmpl[i];
	    }
	    else {
		mn[0][bel[L]]=tmpl[i],mn[0][bel[R]]=tmpr[i];
		if(lef[bel[L]]>=L) fir[0][bel[L]]++;
		fir[0][bel[R]]++;
		lazy[bel[L]+1]++,lazy[bel[R]]--;
	    }
	}
	for(int i=1;i<=bel[n];++i)
	    lazy[i]+=lazy[i-1],laz[0][i]+=lazy[i];
	if(bel[l]==bel[r]) lastans=ask(bel[l],l,r,X);
	else {
	    lastans=min(ask(bel[l],l,rig[bel[l]],X),ask(bel[r],lef[bel[r]],r,X));
	    for(int i=bel[l]+1;i<bel[r];++i)
		lastans=min(lastans,mn[0][i]+laz[0][i]);
	}
	printf("%d\n",lastans);
    }
    return 0;
}

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
inline int gi() {
    register int data = 0, w = 1;
    register char ch = 0;
    while (!isdigit(ch) && ch != '-') ch = getchar();
    if (ch == '-') w = -1, ch = getchar();
    while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
    return w * data;
}
typedef unsigned long long ull;
#define lson t[o].ls
#define rson t[o].rs
#define lmax (l == mid ? int(lson) : int(t[lson].mx))
#define rmax (mid + 1 == r ? int(rson) : int(t[rson].mx))
const int INF = 1e9;
const int MAX_N = 2e5 + 5;
struct Node {
    ull mx : 18, ls : 23, rs : 23;
} t[MAX_N * 40];
int tot = 0, rt[MAX_N];
int modify(int p, int l, int r, int ql, int qr) {
    if (l == r) { return p + 1; }
    int o = ++tot; t[o] = t[p];
    if (ql <= l && r <= qr) return ++t[o].mx, o;
    int mid = (l + r) >> 1, tag = t[o].mx - max(lmax, rmax);
    if (ql <= mid) lson = modify(t[p].ls, l, mid, ql, qr);
    if (qr > mid) rson = modify(t[p].rs, mid + 1, r, ql, qr);
    t[o].mx = max(lmax, rmax);
    t[o].mx += tag;
    return o;
}
int query(int o, int l, int r, int ql, int qr) {
    if (l == r) return o;
    if (ql <= l && r <= qr) return t[o].mx;
    int mid = (l + r) >> 1, res = 0, tag = t[o].mx - max(lmax, rmax);
    if (ql <= mid) res = max(res, query(lson, l, mid, ql, qr));
    if (qr > mid) res = max(res, query(rson, mid + 1, r, ql, qr));
    return res + tag;
}
int N, M;
pair<int, int> a[MAX_N];
int main () {
    N = gi(), M = gi();
    for (int i = 1; i <= N; i++) a[i].first = 0 - gi(), a[i].second = i;
    sort(&a[1], &a[N + 1]);
    for (int i = 1; i <= N; i++) {
        int ql = max(1, a[i].second - M + 1);
        int qr = min(a[i].second, N - M + 1);
        rt[i] = modify(rt[i - 1], 1, N - M + 1, ql, qr);
    }
    int Q = gi(), ans = 0;
    while (Q--) {
        int l = gi(), r = gi(), x = gi();//adfssdsaasdasdssdffasdfdf
        x ^= ans;
        int id = upper_bound(&a[1], &a[N + 1], make_pair(-x, INF)) - a - 1;
        printf("%d\n", ans = M - query(rt[id], 1, N - M + 1, l, r));
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//INSERT ABOVE HERE
const int X = 9;
const int BS = 1<<X;
const int MAX = 1<<18;

int dat[MAX]={}; // state
int val[MAX>>X]={}; // max
int add[MAX>>X]={}; // add
int frt[MAX>>X]={}; // front

int bf[MAX>>X][MAX>>X]; // front
int bm[MAX>>X][MAX>>X]; // max

int cp[3][MAX]={}; // pos
int cv[3][MAX]={}; // value

signed main(){
  int n,m;
  cin>>n>>m;
  vector<int> as(n+n,0);
  for(int i=0;i<n;i++) cin>>as[i];

  using P = pair<int, int>;
  vector<P> vp;
  for(int i=0;i<n;i++) vp.emplace_back(-as[i],i);
  sort(vp.begin(),vp.end());

  auto eval=
    [&](int i){
      int l=(i>>X)<<X;
      if(add[l>>X]==0) return;
      for(int j=l;j<l+BS;j++) dat[j]+=add[j>>X];
      add[l>>X]=0;
    };

  for(int i=0;i<n;i++){
    if((i&(BS-1))==0){
      for(int j=0;j<MAX;j+=BS){
        val[j>>X]+=add[j>>X];
        eval(j);
        bf[i>>X][j>>X]=dat[j];
        bm[i>>X][j>>X]=val[j>>X];
      }
    }

    // [l, r)
    int r=vp[i].second+1;
    int l=max(0,r-m);

    int idx=0;
    cp[idx][i]=-1;
    for(int j=l;j<r;j++){
      if((j&(BS-1))==0 && j+BS<=r){
        add[j>>X]+=1;
        j+=BS-1;
      }else{
        dat[j]+=1;
        chmax(val[j>>X],dat[j]);
        if(cp[idx][i]!=j>>X){
          idx++;
          cp[idx][i]=j>>X;
        }
        cv[idx][i]=val[cp[idx][i]];
      }
    }
    while(idx<2){
      idx++;
      cp[idx][i]=(MAX>>X)-1;
      cv[idx][i]=val[cp[idx][i]];
    }
  }

  auto calc=
    [&](int l,int r,int v)->int{
      int idx=upper_bound(vp.begin(),vp.end(),P(-v,n))-vp.begin();

      int k=(idx>>X)<<X;
      for(int j=0;j<MAX;j+=BS){
        val[j>>X]=bm[k>>X][j>>X];
        add[j>>X]=0;
        frt[j>>X]=bf[k>>X][j>>X];
      }

      for(int i=k;i<idx;i++){
        int q=vp[i].second+1;
        int p=max(0,q-m);
        if(p&(BS-1)) p=((p>>X)+1)<<X;
        if(p<=q){
          add[p>>X]+=1;
          add[q>>X]-=1;
        }
        val[cp[1][i]]=cv[1][i];
        val[cp[2][i]]=cv[2][i];
        if(p<=q && (q&(BS-1))) frt[q>>X]+=1;
      }

      for(int j=BS;j<MAX;j+=BS)
        add[j>>X]+=add[(j>>X)-1];

      int res=0,flg=0,tmp=0;
      for(int j=l;j<r;j++){
        if((j&(BS-1))==0 && j+BS<=r){
          chmax(res,val[j>>X]+add[j>>X]);
          j+=BS-1;
          flg=0;
        }else{
          if(flg==0){
            flg=1;
            tmp=frt[j>>X]+add[j>>X];
            int p=(j>>X)<<X;
            while(p<j){
              tmp-=as[p]>=v;
              tmp+=as[p+m]>=v;
              p++;
            }
          }
          chmax(res,tmp);
          tmp-=as[j]>=v;
          tmp+=as[j+m]>=v;
        }
      }
      return m-res;
    };

  int q;
  cin>>q;
  int pre_ans=0;
  for(int i=0;i<q;i++){
    int l,r,x;
    cin>>l>>r>>x;
    l--;
    int ans=calc(l,r,x^pre_ans);
    cout<<ans<<"\n";
    pre_ans=ans;
  }
  cout<<flush;
  return 0;
}

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
inline int read()
{
	int x = 0 , f = 1; char ch = getchar();
	while(ch < '0' || ch > '9')	{if(ch == '-')	f = -1; ch = getchar();}
	while(ch >= '0' && ch <= '9') {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();}
	return x * f;
}
const int N = 2e5 + 1 , inf = 2e9 + 1;
int n , m , cnt , tot;
typedef pair<int,int> pp;
#define mp make_pair
pp a[N];
#define pb push_back
typedef unsigned long long ull;
namespace PSMT
{
	struct node
	{
		ull mx : 18 , ls : 23 , rs : 23;
	} t[N * 40];
	int root[N + 1];
	int insert(int p,int l,int r,int x,int y)
	{
		if(l == r) return p + 1;
		int o = ++ tot; t[o] = t[p];
		if(x <= l && r <= y) return t[o].mx ++ , o;
		int mid = (l + r) >> 1 , tag = t[o].mx - max((l == mid ? (int)t[o].ls : (int)t[t[o].ls].mx),(mid + 1 == r ? (int)t[o].rs : (int)t[t[o].rs].mx));
		if(x <= mid) t[o].ls = insert(t[p].ls,l,mid,x,y);
		if(y > mid) t[o].rs = insert(t[p].rs,mid + 1,r,x,y);
		t[o].mx = max((l == mid ? (int)t[o].ls : (int)t[t[o].ls].mx),(mid + 1 == r ? (int)t[o].rs : (int)t[t[o].rs].mx)) + tag;
		return o;
	}
	int query(int o,int l,int r,int x,int y)
	{
		if(l == r) return o;
		if(x <= l && r <= y) return t[o].mx;
		int mid = (l + r) >> 1 , ans = 0 , tag = t[o].mx - max((l == mid ? (int)t[o].ls : (int)t[t[o].ls].mx),(mid + 1 == r ? (int)t[o].rs : (int)t[t[o].rs].mx));
		if(x <= mid) ans = max(ans,query(t[o].ls,l,mid,x,y));
		if(y > mid) ans = max(ans,query(t[o].rs,mid + 1,r,x,y));
		return ans + tag;
	}
}
int main()
{
//	freopen("b.in","r",stdin);
//	freopen("b.out","w",stdout);
	n = read(); m = read(); using namespace PSMT;
	for(int i = 1;i <= n;i ++) a[i].first = read() , a[i].second = i;
	sort(a + 1,a + 1 + n);
	for(int i = n;i;i --) root[i] = insert(root[i + 1],1,n - m + 1,max(1,a[i].second - m + 1),min(a[i].second,n - m + 1));
	int Q = read() , ans = 0;
	while(Q --> 0)
	{
		int l = read() , r = read() , x = read() ^ ans , t = lower_bound(a + 1,a + 1 + n,mp(x,0)) - a;
		printf("%d\n",ans = (m - query(root[t],1,n - m + 1,l,r)));
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}

#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<stdio.h>
#include<map>
#include<vector>
#include<cassert>
using namespace std;
const int inf=1e9;
const int bound=500;
struct qq
{
	int id,vl;
	bool operator <(const qq &temp)const
	{
		return vl<temp.vl;
	}
};
qq bk[210000/bound][bound],ns;
int nk[210000/bound];
struct pp
{
	int vl,id,min_value,value;
	int flag;
	bool operator <(const pp &temp)const
	{
		return vl<temp.vl;
	}
};
pp bl[210000/bound][2*bound],now;
int num[210000/bound],num_bl,cnt_list,np[2*bound];
int m,n,a[220000],num_q,ans,nw;
int lower_bound(int left,int right,qq bl[],int x)
{
	int mid;
	while(left<=right)
	{
		mid=(left+right)>>1;
		if(bl[mid].vl<x)
		   left=mid+1;
        else
           right=mid-1;
	}
	return right;
}
int lower_bound(int left,int right,pp bl[],int x)
{
	int mid;
	while(left<=right)
	{
		mid=(left+right)>>1;
		if(bl[mid].vl<x)
		   left=mid+1;
        else
           right=mid-1;
	}
	return right;
}
int main()
{
	int i,j,s,p,q,ip,id,l,r,x,fir;
	scanf("%d%d",&n,&m);
	for(i=0;i<n;i++)
	    scanf("%d",&a[i]);
    memset(num,0,sizeof(num));
    memset(nk,0,sizeof(nk));
    num_bl=0;
	for(i=0;i<n;i++)
    {
    	if(i%bound==0)
    	{
	    	for(j=i;j<min(n,i+bound);j++)
	    	{
	    		if(j+m<n)
	    		{
	        		now.id=j;
	        		now.vl=a[j];
	        		now.flag=-1;
	        	    bl[num_bl][num[num_bl]++]=now;
    	    		now.id=j+m;
    	    		now.vl=a[j+m];
    	    		now.flag=1;
    	    		bl[num_bl][num[num_bl]++]=now;
    	    	}
    	    	ns.id=j;
    	    	ns.vl=a[j];
    	    	bk[num_bl][nk[num_bl]++]=ns;
			}
			num_bl++;
	    }
    }
    for(i=0;i<num_bl;i++)
    {
        sort(bl[i],bl[i]+num[i]);
        sort(bk[i],bk[i]+nk[i]);
        for(j=i*bound;j<min((i+1)*bound,n);j++)
             np[j-i*bound]=0;
        for(j=0;j<num[i];j++)
        {
       	    bl[i][j].value=bl[i][j].flag;
            ip=bl[i][j].id;
            if(bl[i][j].flag>0)
               ip-=m;
            np[ip-i*bound]+=bl[i][j].flag;
            if(bl[i][j].flag<0)
            {
            	if(bl[i][j].id+m>=n)
            	{
            	     bl[i][j].value=0;
       	             np[ip-i*bound]++;
            	}
			}
			if(j)
			{
               bl[i][j].value+=bl[i][j-1].value;
			}
			bl[i][j].min_value=0;
			nw=0;
			for(s=i*bound;s<min((i+1)*bound,n);s++)
			{
				nw+=np[s-i*bound];
				if(bl[i][j].min_value>nw)
				   bl[i][j].min_value=nw;
			}
        }
	}
	scanf("%d",&num_q);
	ans=0;
	while(num_q--)
	{
		scanf("%d%d%d",&l,&r,&x);
		l--;
		r--;
		x^=ans;
		ans=0;
		id=l/bound;
		nw=fir=0;
		for(i=l;i<=min(l+m-1,(l/bound+1)*bound-1);i++)
		{
			if(a[i]<x)
			    fir++;
		}
	    if(l+m-1>=(l/bound+1)*bound)
	    {
    		for(i=(l+m-1)/bound*bound;i<=l+m-1;i++)
    		{
		    	if(a[i]<x)
		    	   fir++;
		    }
		    for(i=l/bound+1;i<(l+m-1)/bound;i++)
		    {
    			id=lower_bound(0,nk[i]-1,bk[i],x);
    			fir+=(id+1);
    		}
    	}
        for(i=l;i<=min(r-1,(l/bound+1)*bound-1);i++)
	    {
	    	if(i+m<n)
	    	{
    	       if(a[i]<x)
     	       	  nw--;
     	       if(a[i+m]<x)
     	          nw++;
  	           if(ans>nw)
  	              ans=nw;
	    	}
		}
		if(r-1>=(l/bound+1)*bound)
		{
			for(i=l/bound+1;i<(r-1)/bound;i++)
			{
				id=lower_bound(0,num[i]-1,bl[i],x);
				if(id>=0)
				{
				   if(nw+bl[i][id].min_value<ans)
				      ans=nw+bl[i][id].min_value;
				    nw+=bl[i][id].value;
				}
			}
			for(i=((r-1)/bound)*bound;i<=r-1;i++)
			{
				if(i+m<n)
				{
			    	if(a[i]<x)
			    	   nw--;
	                if(a[i+m]<x)
	                   nw++;
                    if(ans>nw)
                       ans=nw;
				}
			}
		}
        ans+=fir;
        printf("%d\n",ans);
	}
	return 0;
}

#include <bits/stdc++.h>
#define min(a,b) (a<b?a:b)
using namespace std;
const int N=400050,inf=1e9;
int gi(){
    char ch=getchar(); int x=0,q=0;
    while(ch<'0'||ch>'9') q=ch=='-'?1:q,ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return q?-x:x;
}
struct date{
    int x,s;
    bool operator < (const date &c) const {
	return s<c.s;
    }
}b[N];
struct Blo{
    int a[500],fr[500],laz[500];
}B[500];
int n,m,a[N],c[500][500],bl[N],L[N],R[N],w[N],mn[500],laz[500],fr[500],las,P;
int f[N][2];
int find(int s){
    int l=1,r=n,ans=0,mid;
    while(l<=r){
	mid=(l+r)>>1;
	if (b[mid].s<=s) l=mid+1,ans=mid;
	else r=mid-1;
    }
    return ans;
}
void work(int l,int r,int x){
    int ans=inf,p=find(x-1);
    if (p==0) { printf("%d\n",las=0); return; }
    for (int i=1; i<=P; ++i) laz[i]=0,mn[i]=B[bl[p]-1].a[i],fr[i]=B[bl[p]-1].fr[i];
    for (int i=L[bl[p]]; i<=p; ++i){
	int zl=max(1,b[i].x-m+1),zr=b[i].x;
	if (bl[zl]!=bl[zr]) ++laz[bl[zl]+1],--laz[bl[zr]];
	mn[bl[zl]]=f[i][0],mn[bl[zr]]=f[i][1];
	if (zl==L[bl[zl]]) ++fr[bl[zl]];
	if (bl[zl]!=bl[zr]) ++fr[bl[zr]];
    }
    for (int i=1; i<=P; ++i) laz[i]+=laz[i-1];
    for (int i=1; i<=P; ++i) laz[i]+=B[bl[p]-1].laz[i];
    int sum=fr[bl[l]]+laz[bl[l]];
    for (int i=L[bl[l]]; i<=R[bl[l]]; ++i){
	if (i>=l&&i<=r) ans=min(ans,sum);
	if (a[i]<x) --sum;
	if (a[i+m]<x) ++sum;
    }
    for (int i=bl[l]+1; i<bl[r]; ++i) ans=min(ans,laz[i]+mn[i]);
    sum=fr[bl[r]]+laz[bl[r]];
    for (int i=L[bl[r]]; i<=R[bl[r]]; ++i){
	if (i>=l&&i<=r) ans=min(ans,sum);
	if (a[i]<x) --sum;
	if (a[i+m]<x) ++sum;
    }
    las=ans;
    printf("%d\n",ans);
    return;
}
void rebuild(int x){
    mn[x]=inf,fr[x]=w[L[x]];
    for (int i=L[x]; i<=R[x]; ++i)
	mn[x]=min(mn[x],w[i]);
    return;
}
int main(){
    n=gi(),m=gi();
    for (int i=1; i<=n; ++i) b[i]=(date){i,a[i]=gi()};
    sort(b+1,b+n+1);
    int blo=sqrt(n); P=(n-1)/blo+1;
    for (int i=1; i<=n; ++i){
	bl[i]=(i-1)/blo+1,R[bl[i]]=i;
	if (!L[bl[i]]) L[bl[i]]=i;
    }
    for (int i=1; i<=n; ++i){
	int l=max(1,b[i].x-m+1),r=b[i].x;
	if (bl[l]==bl[r]){
	    for (int j=l; j<=r; ++j) ++w[j];
	    rebuild(bl[l]);
	}
	else{
	    for (int j=bl[l]+1; j<bl[r]; ++j) ++laz[j];
	    for (int j=l; j<=R[bl[l]]; ++j) ++w[j];
	    for (int j=L[bl[r]]; j<=r; ++j) ++w[j];
	    rebuild(bl[l]),rebuild(bl[r]);
	}
	if (bl[i+1]!=bl[i])
	    for (int j=1; j<=P; ++j)
		B[bl[i]].a[j]=mn[j],B[bl[i]].fr[j]=fr[j],B[bl[i]].laz[j]=laz[j];
	f[i][0]=mn[bl[l]],f[i][1]=mn[bl[r]];
    }
    int Q=gi();
    for (int i=1; i<=Q; ++i){
	int l=gi(),r=gi(),x=gi();
	x^=las,work(l,r,x);
    }
    return 0;
}

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#define N 230005
#define size 650
//#define N 1005
//#define size 3
#define mo 1000000007
#define inf 100000000
#define eps 0.00000001
#define Long long long
#define uint unsigned int
#define lowbit(x) (x&(-x))
#define For(i,l,r) for(int i=l;i<=r;i++)
#define Dor(i,r,l) for(int i=r;i>=l;i--)
#define Rep(i,t) for(int i=son[t];i;i=Next[i]) if(ed[i]!=f)
#define y1 Y1
#define next Next
#define link Link
using namespace std;
struct rec{
    int x,key;
}F[N/size][(size+5)*2];
int Ans,n;
int G[N/size][(size+5)*2];
int S[N/size][(size+5)*2],SR[3*N];
int V[N/size][(size+5)*2],VR[3*N];
int L[N/size],Tmp[N],be[N];
int ml[N/size],mr[N/size],num[N/size];
int Doit(int t,int num){
    rec *Ft=F[t];
    For(i,1,num){
        rec Now=Ft[i];
        Tmp[Now.x]+=Now.key;
    }
    int Ret=inf,now=0;
    For(i,ml[t],mr[t]){
        now+=Tmp[i]; Tmp[i]=0;
        if(now<Ret) Ret=now;
    }
/*  For(i,ml[t],mr[t]) Tmp[i]+=Tmp[i-1];
    For(i,ml[t],mr[t]) if(Tmp[i]<Ret) Ret=Tmp[i];
    For(i,ml[t],mr[t]) Tmp[i]=0;*/
    return Ret;
}
int Find(int key){
    int l=1,r=num[0],ret=0;
    while(l<=r){
        int mid=(l+r)>>1;
        if(VR[mid]<key) l=mid+1,ret=mid;
        else r=mid-1;
    }
    if(!ret) return 0;
    return SR[ret];
}
int Find(int t,int key){
    int l=1,r=num[t],ret=0;
    while(l<=r){
        int mid=(l+r)>>1;
        if(V[t][mid]<key) l=mid+1,ret=mid;
        else r=mid-1;
    }
    if(!ret) return 0;
    return S[t][ret];
}
int Cnt;
void Query(int t,int key){
    int l=1,r=num[t],ret=0;
    while(l<=r){
        int mid=(l+r)>>1;
        if(V[t][mid]<key) l=mid+1,ret=mid;
        else r=mid-1;
    }
    if(!ret) return;
    Ans=min(Ans,G[t][ret]+L[t-1]);
}
void Query(int t,int key,int ll,int rr){
    rec *Ft=F[t]; int *Vt=V[t];
//  For(i,1,num[t])
    For(i,1,num[t]){
        if(Vt[i]>=key) break;
        rec Now=Ft[i];
        Tmp[Now.x]+=Now.key;
    }

//  printf("Query t %d key %d ll rr %d %d --> Num %d\n",t,key,ll,rr,Num);
//  For(i,1,Num) printf("Add (%d -> %d)\n",F[t][i].x,F[t][i].key);
    int Ret=inf,now=0;
    For(i,ml[t],mr[t]){
        now+=Tmp[i]; Tmp[i]=0;
        if(ll<=i && i<=rr)
        if(now<Ret) Ret=now;
    }
//  For(i,ml[t],mr[t]) Tmp[i]+=Tmp[i-1];
//  For(i,ll,rr) if(Tmp[i]<Ret) Ret=Tmp[i];
//  For(i,ml[t],mr[t]) Tmp[i]=0;

//  printf("---> Ret %d L %d Ans %d\n",Ret,L[t-1],Ans);

    Ans=min(Ans,Ret+L[t-1]);
}
void Add(int x,int key,int val){
    if(x>n) return;
    if(x<1){
        int t=0; ++num[t];
        SR[num[t]]=SR[num[t]-1]+key;
        VR[num[t]]=val;
        return;
    }
    int t=be[x]; ++num[t];
    F[t][num[t]]=(rec){x,key};
    G[t][num[t]]=Doit(t,num[t]);
    S[t][num[t]]=S[t][num[t]-1]+key;
    V[t][num[t]]=val;
}
struct opt{
    int val,id;
}E[N];
bool cmp(const opt &i,const opt &j){return i.val<j.val;}
int main(){
    int m; scanf("%d%d",&n,&m); int sum=(n+1)/size+1;
    For(i,1,n) scanf("%d",&E[i].val),E[i].id=i;
    sort(E+1,E+1+n,cmp);
    For(i,1,sum) ml[i]=max(1,i*size-size);
    For(i,1,sum) mr[i]=min(n,i*size-1);
    For(i,1,sum) For(j,ml[i],mr[i]) be[j]=i;

//  printf("sum %d n %d size %d\n",sum,n,size);
//  For(i,1,sum) printf("(%d,%d) ",ml[i],mr[i]); printf("seg\n");

//  cerr<<"Time -> "<<clock()<<endl;

    For(i,1,n){
//      ll=E[i].id-m+1; rr=E[i].id;

//      printf("Add %d -> key %d val %d\n",E[i].id-m+1,1,E[i].val);
//      printf("Add %d -> key %d val %d\n",E[i].id+1,-1,E[i].val);

        Add(E[i].id-m+1,1,E[i].val);
        Add(E[i].id+1,-1,E[i].val);
    }

//  For(i,0,sum) printf("%d ",num[i]); printf("num[]\n");

//  cerr<<"Time -> "<<clock()<<endl;

    int Q_Q; scanf("%d",&Q_Q);
    while(Q_Q--){
        int t,l,r,key; scanf("%d%d%d",&l,&r,&key);
      key^=Ans;
        Ans=inf;
        L[0]=Find(key); For(t,1,be[r]) L[t]=L[t-1]+Find(t,key);
        if(be[l]==be[r]){
            t=be[l]; Query(t,key,l,r);
        }else{
            t=be[l]; Query(t,key,l,mr[t]);
            t=be[r]; Query(t,key,ml[t],r);
            For(d,be[l]+1,be[r]-1) Query(d,key);
        }
        printf("%d\n",Ans);
    }

}

#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int N = 2e5+10;
const int M = 500;
int x[N], d[M][M<<1], e[M][M<<1], s[M], t[M];
int n, m, q;
vector<int> O, P[M];
int calc(int L, int R, int X) {
  int a=0x3f3f3f3f;
  int g=lower_bound(O.begin(), O.end(), X)-O.begin();
  if(!L) a=g;
  for(int i=0, y; i*M<R; i++) {
    y=lower_bound(P[i].begin(), P[i].end(), X)-P[i].begin()-1;
    if(y<0) {
      if(L<=t[i]) a=min(a, g);
      continue;
    }
    if(L<=s[i] && R>=t[i]) {
      a=min(a, g+d[i][y]);
    }else if(max(L, s[i])<=min(R, t[i])) {
      for(int j=s[i], f=g; j<=t[i]; j++) {
        f-=x[j-1]<X;
        f+=x[j+m-1]<X;
        if(L<=j && j<=R && a>f) {
          a=f;
        }
      }
    }
    g+=e[i][y];
  }
  return a;
}
int main() {
#ifdef TRUE
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  cin>>n>>m;
  for(int i=0; i<n; i++) {
    scanf("%d", x+i);
    if(i<m) O.pb(x[i]);
  }
  sort(O.begin(), O.end());
  for(int i=0; i*M+1<=n-m; i++) {
    s[i]=i*M+1, t[i]=min(n-m, (i+1)*M);
    for(int j=s[i]; j<=t[i]; j++) {
      P[i].pb(x[j-1]);
      P[i].pb(x[j+m-1]);
    }
    sort(P[i].begin(), P[i].end());
    for(int j=0; j<P[i].size(); j++) {
      d[i][j]=0x3f3f3f3f;
      for(int k=s[i]; k<=t[i]; k++) {
        e[i][j]-=x[k-1]<=P[i][j];
        e[i][j]+=x[k+m-1]<=P[i][j];
        d[i][j]=min(d[i][j], e[i][j]);
      }
    }
  }
  cin>>q;
  for(int l, r, x, a=0; q--;) {
    scanf("%d %d %d", &l, &r, &x);
    printf("%d\n", a=calc(l-1, r-1, x^a));
  }
  return 0;
}

#include<bits/stdc++.h>
#define LL unsigned long long
int read(){int x;scanf("%d",&x);return x;}
void write(int x){printf("%d\n",x);}
int max(int a,int b){return a>b? a:b;}
int min(int a,int b){return a<b? a:b;}
#define N 200007
#define INF 1000000000
#define mid ((l+r)>>1)
#define lmax (l==mid? int(t[p].L):int(t[t[p].L].max))
#define rmax (mid+1==r? int(t[p].R):int(t[t[p].R].max))
struct node{LL max:18,L:23,R:23;}t[N*40];
int root[N],cnt;
std::pair<int,int>a[N];
int ins(int x,int l,int r,int L,int R)
{
    if(l==r) return x+1;
    int p=++cnt;t[p]=t[x];
    if(L<=l&&r<=R) return ++t[p].max,p;
    int tag=t[p].max-max(lmax, rmax);
    if(L<=mid) t[p].L=ins(t[x].L,l,mid,L,R);
    if(R>mid) t[p].R=ins(t[x].R,mid+1,r,L,R);
    return t[p].max=max(lmax,rmax)+tag,p;
}

int query(int p,int l,int r,int L,int R)
{
    if(l==r) return p;
    if(L<=l&&r<=R) return t[p].max;
    int ans=0,tag=t[p].max-max(lmax,rmax);
    if(L<=mid) ans=max(ans,query(t[p].L,l,mid,L,R));
    if(R>mid) ans=max(ans,query(t[p].R,mid+1,r,L,R));
    return ans+tag;
}
int main()
{
    int n=read(),m=read(),Q,i,l,r,x,id,ans=0;
    for(i=1;i<=n;++i) a[i]=std::make_pair(-read(),i);
    std::sort(a+1,a+n+1);
    for(i=1;i<=n;++i) l=max(1,a[i].second-m+1),r=min(a[i].second,n-m+1),root[i]=ins(root[i-1],1,n-m+1,l,r);
    for(Q=read();Q;--Q) l=read(),r=read(),x=read()^ans,id=std::upper_bound(a+1,a+n+1,std::make_pair(-x,INF))-(a+1),write(ans=m-query(root[id],1,n-m+1,l,r));
    return 0;
}

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<queue>
#define SF scanf
#define PF printf
#define mp make_pair
#define fir first
#define sec second
#define sqr(x) ((x)*(x))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef unsigned long long ull;
const int MAXN = 200000;
int n, m, rt[MAXN+10], Q;
pii a[MAXN+10];
struct Seg_Tree {
	static const int N = 8000000 + 10;
	static const int T = MAXN + 3;
	int ncnt;
	ull t[N];
	inline void calc_s(const bool &nl, const bool &nr, ull x, int &ls, int &rs, int &tag) {
		int s = x % T;
		ls = x / T / N; rs = x / T % N;
		tag = s - min((nl ? ls : t[ls] % T), (nr ? rs : t[rs] % T));
	}
	inline ull calc(const int &ls, const int &rs, const int &val) {
		return (1ull * ls * N + rs) * T + val;
	}
	void ins(const int &last, int &u, const int &L, const int &R, const int &l, const int &r) {
		if(L == R) {
			u = last+1;
			return ;
		}
		t[u = ++ncnt] = t[last];
		if(l <= L && R <= r) {
			t[u]++;
			return ;
		}
		int mid = (L+R) >> 1, ls, rs, val, lson = 0, rson = 0;
		calc_s((L == mid), (R == mid+1), t[u], ls, rs, val);
		if(l <= mid) ins(ls, lson, L, mid, l, r);
		if(r > mid) ins(rs, rson, mid+1, R, l, r);
		if(!lson) lson = ls;
		if(!rson) rson = rs;
		val += min(((L == mid) ? lson : t[lson] % T), ((R == mid+1) ? rson : t[rson] % T));
		t[u] = calc(lson, rson, val);
	}
	int query(const int &u, const int &L, const int &R, const int &l, const int &r) {
		if(L == R) return u;
		if(!u) return 0;
		if(l <= L && R <= r) return t[u] % T;
		int mid = (L+R) >> 1, ls, rs, val, ret = N;
		calc_s((L == mid), (mid+1 == R), t[u], ls, rs, val);
		if(l <= mid) ret = min(ret, query(ls, L, mid, l, r));
		if(r > mid) ret = min(ret, query(rs, mid+1, R, l, r));
		return ret + val;
	}
	inline void build() {
		for(int i = 1; i <= n; i++)
			ins(rt[i-1], rt[i], 1, n-m+1, max(1, a[i].sec-m+1), a[i].sec);
	}
} seg;
int main() {
	SF("%d%d", &n, &m);
	for(int i = 1; i <= n; i++) SF("%d", &a[i].fir), a[i].sec = i;
	sort(a+1, a+1+n);
	seg.build();
	SF("%d", &Q);
	int ans = 0;
	for(int i = 1; i <= Q; i++) {
		int x, l, r;
		SF("%d%d%d", &l, &r, &x);
		x ^= ans;
		x = lower_bound(a+1, a+1+n, mp(x, 0)) - a;
		if(x == 1) ans = 0;
		else ans = seg.query(rt[x-1], 1, n-m+1, l, r);
		PF("%d\n", ans);
	}
	return 0;
}

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn(2e5 + 5);

int n, m, s, a[maxn], bl[maxn], br[maxn], b[maxn], o[maxn], len, id[maxn], blk;
int mn[505][505], st[505][505], tag[505], w[maxn], pmn[maxn], smn[maxn];
int curmn[505], curst[505], curtag[505];

inline void Modify(int l, int r, int id) {
	int i;
	pmn[id] = smn[id] = 1 << 30;
	if (b[l] == b[r]) for (i = l; i <= r; ++i) ++w[i];
	else {
		for (i = l; i <= br[l]; ++i) ++w[i];
		for (i = bl[r]; i <= r; ++i) ++w[i];
		for (i = b[l] + 1; i < b[r]; ++i) ++tag[i];
	}
	for (i = bl[l]; i <= br[l]; ++i) pmn[id] = min(pmn[id], w[i]);
	for (i = bl[r]; i <= br[r]; ++i) smn[id] = min(smn[id], w[i]);
}

inline int Binary(int v) {
	int l = 1, r = n, mid, ret = -1;
	while (l <= r) {
		mid = (l + r) >> 1;
		if (o[a[id[mid]]] <= v) ret = mid, l = mid + 1;
		else r = mid - 1;
	}
	return ret;
}

inline int Query(int l, int r, int x) {
	int val = curst[b[l]], i, ret = 1 << 30;
	for (i = bl[l]; i < l; ++i) val += (a[i + m] <= x) - (a[i] <= x);
	for (i = l; i <= r; ++i) ret = min(ret, val), val += (a[i + m] <= x) - (a[i] <= x);
	return ret;
}

int main() {
	int i, j, ans = 0, l, r, x, ql, qr;
	scanf("%d%d", &n, &m), len = n;
	for (i = 1; i <= n; ++i) scanf("%d", &a[i]), o[i] = a[i], id[i] = i;
	sort(o + 1, o + len + 1), len = unique(o + 1, o + len + 1) - o - 1;
	for (i = 1; i <= n; ++i) a[i] = lower_bound(o + 1, o + len + 1, a[i]) - o;
	sort(id + 1, id + n + 1, [](int x, int y) {  return a[x] < a[y];  });
	for (blk = sqrt(n), i = 1; i <= n; ++i) b[i] = (i - 1) / blk + 1, bl[i] = b[i] == b[i - 1] ? bl[i - 1] : i;
	for (i = n; i; --i) br[i] = b[i] == b[i + 1] ? br[i + 1] : i;
	memset(mn, 63, sizeof(mn)), memset(mn[0], 0, sizeof(mn[0]));
	for (i = 1; i <= n; ++i) {
		Modify(max(1, id[i] - m + 1), id[i], i);
		if (b[i] != b[i + 1]) {
			for (j = 1; j <= n; ++j) w[j] += tag[b[j]];
			for (j = 1; j <= b[n]; ++j) tag[j] = 0;
			for (j = 1; j <= n; ++j) {
				mn[b[i]][b[j]] = min(mn[b[i]][b[j]], w[j]);
				if (b[j] != b[j - 1]) st[b[i]][b[j]] = w[j];
			}
		}
	}
	scanf("%d", &s);
	while (s--) {
		scanf("%d%d%d", &l, &r, &x), x ^= ans, x = Binary(x - 1);
		if (x < 0) {  printf("%d\n", ans = 0); continue;  }
		for (i = 1; i <= b[n]; ++i) curmn[i] = mn[b[x] - 1][i], curst[i] = st[b[x] - 1][i], curtag[i] = 0;
		for (i = bl[x]; i <= x; ++i) {
			ql = max(1, id[i] - m + 1), qr = id[i];
			curmn[b[ql]] = pmn[i], curmn[b[qr]] = smn[i];
			if (b[ql] < b[qr]) ++curtag[b[ql] + 1], --curtag[b[qr]];
			if (bl[ql] >= ql && (b[ql] ^ b[qr])) ++curst[b[ql]];
			if (bl[qr] >= ql) ++curst[b[qr]];
		}
		for (i = 1; i <= b[n]; ++i) curtag[i] += curtag[i - 1], curmn[i] += curtag[i], curst[i] += curtag[i];
		if (b[l] == b[r]) printf("%d\n", ans = Query(l, r, a[id[x]]));
		else {
			ans = min(Query(l, br[l], a[id[x]]), Query(bl[r], r, a[id[x]]));
			for (i = b[l] + 1; i < b[r]; ++i) ans = min(ans, curmn[i]);
			printf("%d\n", ans);
		}
	}
	return 0;
}

#include <iostream>
#include <ctime>
#include <vector>
#include <algorithm>
using namespace std;
#define BSIZE 447

int N,M,Q;
int a[200000];
int cid[200000];
int pInc[200000],pDec[200000];

bool cmp(int x,int y)
{
	return a[x]<a[y];
}

vector<int> spoints[500];
vector<int> first[500];
vector<int> best[500];
vector<int> last[500];
int tmp[2000];

int dif[200001];

int slow(int l,int r,int x)
{
	int ans = 1000000000;
	for(int i=l;i<=r;i++)
	{
		int cur = 0;
		for(int j=i;j<i+M;j++)
			if(a[j]<x)
				cur++;
		ans = min(ans,cur);
	}
	return ans;
}

int main()
{
	//srand(1451781644);
	ios::sync_with_stdio(0);
	cin >> N >> M;
	for(int i=0;i<N;i++)
	{
		cin >> a[i];
		//a[i] = rand()%1000000;
		//cout << a[i] << ' ';
		cid[i] = i;
	}
	//cout << '\n';
	sort(cid,cid+N,cmp);
	for(int i=0;i<N;i++)
	{
		spoints[i/BSIZE].push_back(max(0,cid[i]-M+1));
		spoints[i/BSIZE].push_back(cid[i]+1);
	}
	for(int i=0;i*BSIZE<N;i++)
	{
		for(int j=0;j<N;j+=BSIZE)
			spoints[i].push_back(j);
		spoints[i].push_back(N);
		sort(spoints[i].begin(),spoints[i].end());
		spoints[i].resize(distance(spoints[i].begin(),unique(spoints[i].begin(),spoints[i].end())));
	}
	for(int i=0;i<N;i++)
	{
		pInc[i] = lower_bound(spoints[i/BSIZE].begin(),spoints[i/BSIZE].end(),max(0,cid[i]-M+1))-spoints[i/BSIZE].begin();
		pDec[i] = lower_bound(spoints[i/BSIZE].begin(),spoints[i/BSIZE].end(),cid[i]+1)-spoints[i/BSIZE].begin();
	}
	int lb = ((N-1)/BSIZE);
	for(int k=0;k<spoints[lb].size();k++)
	{
		best[lb].push_back(0), first[lb].push_back(0), last[lb].push_back(0);
	}
	for(int i=N-1;i>0;i--)
	{
		dif[max(0,cid[i]-M+1)]++, dif[cid[i]+1]--;
		if(i%BSIZE == 0)
		{
			int b = (i/BSIZE)-1;
			int v = 0;
			int j = 0;
			int mx = 0;
			for(int k=0;k<=N;k++)
			{
				v += dif[k];
				if(k == spoints[b][j])
				{
					first[b].push_back(v);
					if(j)
					{
						last[b].push_back(v-dif[k]);
						best[b].push_back(mx);
					}
					mx = v;
					j++;
				}
				else mx = max(mx,v);
			}
/*			for(int k=0;k<spoints[b].size();k++)
				cout << spoints[b][k] << ' ';
			cout << '\n';
			for(int k=0;k<best[b].size();k++)
				cout << best[b][k] << ' ';
			cout << "\n\n";
*/
		}
	}
	cin >> Q;
	int l,r,x;
	int ans;
	for(int i=0;i<Q;i++)
	{
		//l = rand()%N, r = rand()%(N-M), x = rand()%1000000;
		//if(l>r) swap(l,r);
		cin >> l >> r >> x;
		if(i) x ^= ans;
		l--,r--;
		int b = 0;
		while((b+1)*BSIZE<N && a[cid[(b+1)*BSIZE]] < x)
			b++;
		//cout << b << '\n';
		for(int j=0;j<spoints[b].size();j++)
			tmp[j] = 0;
		for(int k=min(N,(b+1)*BSIZE)-1;k>=0 && a[cid[k]] >= x;k--)
			tmp[pInc[k]]++,tmp[pDec[k]]--;
		int lBlock = lower_bound(spoints[b].begin(),spoints[b].end(),l)-spoints[b].begin();
		ans = 0;
		int v = first[b][lBlock];
		for(int j=0;j<=lBlock;j++)
			v += tmp[j];
		for(int j=spoints[b][lBlock]-1;j>=l;j--)
		{
			if(a[j]>=x) v++;
			if(j+M<N && a[j+M]>=x) v--;
			if(j<=r) ans = max(ans,v);
		}
		int rBlock = upper_bound(spoints[b].begin(),spoints[b].end(),r)-spoints[b].begin()-1;
		v = first[b][rBlock];
		for(int j=0;j<=rBlock;j++)
			v += tmp[j];
		for(int j=spoints[b][rBlock];j<=r;j++)
		{
			if(j>=l) ans = max(ans,v);
			if(a[j]>=x) v--;
			if(j+M<N && a[j+M]>=x) v++;
		}
		int ps = 0;
		for(int j=0;j<spoints[b].size();j++)
		{
			ps += tmp[j];
			if(j>=lBlock && j<rBlock)
			{
				ans = max(ans,ps+best[b][j]);
			}
		}
		ans = M-ans;
		cout << ans << '\n';
		/*if(slow(l,r,x)!=ans)
		{
			cout << l << ' ' << r << ' ' << x << '\n';
			cout << ans << '\n';
		}*/
	}
}

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

#define RG register
const int maxN=402000;
const int maxB=510;
const int inf=2000000000;

int n,m,bcnt;
int Seq[maxN],Srt[maxN],B[maxN],Bl[maxN],Br[maxN];
int Label[maxN],Val[maxN];
int Lkey[maxB][maxB],Mn[maxB][maxB],Lmn[maxN],Rmn[maxN];
int Rgmn[maxN],Sm[maxN],Lft[maxN];
pair<int,int> Pr[maxN];

int Query(int l,int r,int x);
int main(){
    //freopen("in","r",stdin);
    RG int i,j,l,r;
    scanf("%d%d",&n,&m);
    for (i=1;i<=n;++i) scanf("%d",&Seq[i]),Pr[i]=make_pair(Seq[i],i);
    sort(&Pr[1],&Pr[n+1]);
    for (i=1;i<=n;++i) Srt[i]=Pr[i].first;Srt[0]=-inf;

    int Block=sqrt(n);
    bcnt=(n-1)/Block+1;
    for (i=1;i<=n;++i){
        B[i]=(i-1)/Block+1;Br[B[i]]=i;
        if (!Bl[B[i]]) Bl[B[i]]=i;
    }

    for (i=1;i<=n;++i){
        l=max(Pr[i].second-m+1,1);r=Pr[i].second;
        if (B[l]==B[r]) for (j=l;j<=r;++j) ++Val[j];
        else{
            for (j=l;j<=Br[B[l]];++j) ++Val[j];
            for (j=Bl[B[r]];j<=r;++j) ++Val[j];
            for (j=B[l]+1;j<B[r];++j) ++Label[j];
        }
        Lmn[i]=inf;for (j=Bl[B[l]];j<=Br[B[l]];++j) Lmn[i]=min(Lmn[i],Val[j]);
        Rmn[i]=inf;for (j=Bl[B[r]];j<=Br[B[r]];++j) Rmn[i]=min(Rmn[i],Val[j]);
        if (B[i]!=B[i+1]){
            int b=B[i];
            for (j=1;j<=n;++j) Val[j]+=Label[B[j]];
            for (j=1;j<=bcnt;++j) Label[j]=0,Mn[b][j]=inf,Lkey[b][j]=Val[Bl[j]];
            for (j=1;j<=n;++j) Mn[b][B[j]]=min(Mn[b][B[j]],Val[j]);
        }
        //for (int j=1;j<=n;j++) cout<<Val[j]+Label[B[j]]<<" ";cout<<endl;
    }

    //for (int i=1;i<=n;i++) cout<<B[i]<<" ";cout<<endl;
    //for (int i=1;i<=n;i++) cout<<Pr[i].second<<" "<<Pr[i].first<<endl;
    int Q,lstans=0;scanf("%d",&Q);
    while (Q--){
        int nx;scanf("%d%d%d",&l,&r,&nx);nx^=lstans;
        int cl=1,cr=n,p=0;
        while (cl<=cr){
            int mid=(cl+cr)>>1;
            if (Pr[mid].first<=nx-1) p=mid,cl=mid+1;
            else cr=mid-1;
        }
        //cout<<"x:"<<x<<" "<<nx<<endl;
        if (p==0){
            printf("%d\n",lstans=0);continue;
        }
        int stb=B[p];
        for (i=1;i<=bcnt;++i) Sm[i]=0,Lft[i]=Lkey[stb-1][i],Rgmn[i]=Mn[stb-1][i];
        for (i=Bl[stb];i<=p;++i){
            int pl=max(Pr[i].second-m+1,1),pr=Pr[i].second;
            //cout<<"Add:"<<i<<" "<<pl<<" "<<pr<<endl;
            Rgmn[B[pl]]=Lmn[i];Rgmn[B[pr]]=Rmn[i];
            if (B[pl]<B[pr]) ++Sm[B[pl]+1],--Sm[B[pr]];
            if (pl<=Bl[B[l]]&&Bl[B[l]]<=pr) ++Lft[B[l]];
            if (pl<=Bl[B[r]]&&Bl[B[r]]<=pr&&B[l]!=B[r]) ++Lft[B[r]];
            //cout<<"L:";for (int j=1;j<=bcnt;j++) cout<<Lft[j]<<" ";cout<<endl;
        }
        for (i=1;i<=bcnt;++i) Sm[i]+=Sm[i-1],Rgmn[i]+=Sm[i];
        if (B[l]==B[r]) lstans=Query(l,r,nx-1);
        else{
            lstans=min(Query(l,Br[B[l]],nx-1),Query(Bl[B[r]],r,nx-1));
            for (int i=B[l]+1;i<B[r];i++) lstans=min(lstans,Rgmn[i]);
        }
        printf("%d\n",lstans);
    }
    return 0;
}
int Query(int l,int r,int x){
    int val=Lft[B[l]];//+Sm[B[l]];
    //cout<<"Q:"<<l<<" "<<r<<" "<<x<<" "<<val<<endl;
    for (int i=Bl[B[l]]+1;i<=l;i++) val+=(Seq[i+m-1]<=x)-(Seq[i-1]<=x);//,cout<<i<<" "<<val<<endl;
    //cout<<val<<endl;
    int ret=val;
    for (int i=l+1;i<=r;i++) val+=(Seq[i+m-1]<=x)-(Seq[i-1]<=x),ret=min(ret,val);//,cout<<i<<" "<<val<<endl;
    return ret;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#define lson a[o].ls
#define rson a[o].rs
#define lmax (l==mid?int(lson):int(a[lson].mx))
#define rmax (mid+1==r?int(rson):int(a[rson].mx))
using namespace std;

typedef unsigned long long ULL;

const int INF=0x3f3f3f3f;

struct Node {
    ULL mx:18,ls:23,rs:23;
}a[8000000];
int tot;

int root[200010];
pair<int,int>repr[200010];

int ql,qr;

int update(int l,int r,int p)
{
    if(l==r)
	{
        return p+1;
    }
    int o=++tot;
    a[o]=a[p];
    if(ql<=l&&r<=qr)
	{
        a[o].mx++;
        return o;
    }
    int mid=(l+r)>>1,al=a[o].mx-max(lmax,rmax);
    if(ql<=mid)
	{
        lson=update(l,mid,a[p].ls);
    }
    if(mid<qr)
	{
        rson=update(mid+1,r,a[p].rs);
    }
    a[o].mx=max(lmax,rmax);
    a[o].mx+=al;
    return o;
}

int query(int o,int l,int r)
{
    if(l==r)
	{
        return o;
    }
    if(ql<=l&&r<=qr)
	{
        return a[o].mx;
    }
    int mid=(l+r)>>1,res=0,al=a[o].mx-max(lmax,rmax);
    if(ql<=mid)
	{
        res=max(res,query(lson,l,mid));
    }
    if(mid<qr)
	{
        res=max(res,query(rson,mid+1,r));
    }
    return res+al;
}

int n,m,q;

int main()
{
//	freopen("E.in","r",stdin),freopen("E.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1,aa;i<=n;i++)
	{
        scanf("%d",&aa);
        repr[i]=make_pair(-aa,i);
    }
    sort(repr+1,repr+n+1);
    for(int i=1;i<=n;i++)
	{
        ql=max(1,repr[i].second-m+1);
        qr=min(repr[i].second,n-m+1);//!!
        root[i]=update(1,n-m+1,root[i-1]);
        //printf("%d\n",tot);
    }
    //return 0;
    scanf("%d",&q);
    int lastans=0;
    while(q--)
	{
        int x;
        scanf("%d%d%d",&ql,&qr,&x);
        x^=lastans;
        int id=upper_bound(repr+1,repr+n+1,make_pair(-x,INF))-repr-1;
        printf("%d\n",lastans=m-query(root[id],1,n-m+1));//n-m+1
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N=200005;
#define ull unsigned long long
struct node{
	ull mx:18,l:23,r:23;
}t[N*40];
int sz;
#define L(x) t[x].l
#define R(x) t[x].r
#define MX(x) t[x].mx
#define UPD(x) max(int(mid>l?MX(L(x)):L(x)),int(r>mid+1?MX(R(x)):R(x)))
#define TAG(x) (MX(x)-UPD(x))
int upd(int k,int l,int r,int x,int y){
	if (l==r) return k+1;
	int nk=++sz; t[nk]=t[k];
	if (l==x&&r==y) return MX(nk)++,nk;
	int mid=(l+r)/2,tg=TAG(k);
	if (y<=mid) L(nk)=upd(L(k),l,mid,x,y);
	else if (x>mid) R(nk)=upd(R(k),mid+1,r,x,y);
	else L(nk)=upd(L(k),l,mid,x,mid),
		 R(nk)=upd(R(k),mid+1,r,mid+1,y);
	MX(nk)=UPD(nk)+tg; return nk;
}
int ask(int k,int l,int r,int x,int y){
	if (l==x&&r==y) return r>l?MX(k):k;
	int mid=(l+r)/2,tg=TAG(k);
	if (y<=mid) return ask(L(k),l,mid,x,y)+tg;
	if (x>mid) return ask(R(k),mid+1,r,x,y)+tg;
	int v1=ask(L(k),l,mid,x,mid);
	int v2=ask(R(k),mid+1,r,mid+1,y);
	return max(v1,v2)+tg;
}
int n,m,q,rt[N];
#define pa pair<int,int>
#define fi first
#define se second
pa v[N];
void init(){
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++){
		scanf("%d",&v[i].fi);
		v[i].fi*=-1; v[i].se=i;
	}
	sort(v+1,v+n+1);
	for (int i=1;i<=n;i++){
		int l=max(1,v[i].se-m+1);
		int r=min(n-m+1,v[i].se);
		rt[i]=upd(rt[i-1],1,n-m+1,l,r);
	}
}
int ans;
void solve(){
	scanf("%d",&q);
	while (q--){
		int l,r,k;
		scanf("%d%d%d",&l,&r,&k); k^=ans;
		k=upper_bound(v+1,v+n+1,pa(-k,1e9))-v-1;
		ans=m-ask(rt[k],1,n-m+1,l,r);
		printf("%d\n",ans);
	}
}
int main(){
	init();
	solve();
}

#include <bits/stdc++.h>
#define il inline
#define RG register
#define ll long long
#define inf (1<<30)
#define N (200005)
#define M (450)

using namespace std;

int mn[M][M],tg[M][M],fi[M][M],tag[M],LL[M],RR[M],bl[N],n,m,Q;
int tmpl[N],tmpr[N],val[N],hsh[N],id[N],a[N],ans,block;

il int gi(){
  RG int x=0,q=1; RG char ch=getchar();
  while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
  if (ch=='-') q=-1,ch=getchar();
  while (ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
  return q*x;
}

il int cmp(const int &x,const int &y){ return a[x]<a[y]; }

il void add(RG int p,RG int k,RG int l,RG int r){
  fi[p][k]+=l==LL[k],mn[p][k]=inf; for (RG int i=l;i<=r;++i) ++val[i];
  for (RG int i=LL[k];i<=RR[k];++i) mn[p][k]=min(mn[p][k],val[i]); return;
}

il int ask(RG int p,RG int l,RG int r,RG int x){
  if (l==LL[p] && r==RR[p]) return mn[0][p]+tg[0][p];
  RG int tmp=fi[0][p]+tg[0][p],res=inf;
  if (l==LL[p]) res=tmp;
  for (RG int i=LL[p]+1;i<=r;++i){
    tmp+=(a[i+m-1]<x)-(a[i-1]<x);
    if (l<=i) res=min(res,tmp);
  }
  return res;
}

int main(){
#ifndef ONLINE_JUDGE
  freopen("music.in","r",stdin);
  freopen("music.out","w",stdout);
#endif
  n=gi(),m=gi(),block=sqrt(n);
  for (RG int i=1;i<=n;++i){
    a[i]=gi(),id[i]=i,bl[i]=(i-1)/block+1;
    if (!LL[bl[i]]) LL[bl[i]]=i; RR[bl[i]]=i;
  }
  sort(id+1,id+n+1,cmp);
  for (RG int i=1;i<=bl[n];++i) mn[0][i]=inf;
  for (RG int i=1;i<=n;++i) hsh[i]=a[id[i]];
  for (RG int i=1,j=0,L,R,BL,BR;i<=n;++i){
    if (bl[i]!=bl[i-1]){
      ++j;
      memcpy(mn[j],mn[j-1],sizeof(int)*(bl[n]+1));
      memcpy(tg[j],tg[j-1],sizeof(int)*(bl[n]+1));
      memcpy(fi[j],fi[j-1],sizeof(int)*(bl[n]+1));
    }
    BL=bl[L=max(1,id[i]-m+1)],BR=bl[R=id[i]];
    if (BL==BR) add(j,BL,L,R),tmpl[i]=mn[j][BL]; else{
      add(j,BL,L,RR[BL]),tmpl[i]=mn[j][BL];
      add(j,BR,LL[BR],R),tmpr[i]=mn[j][BR];
      for (RG int k=BL+1;k<BR;++k) ++tg[j][k];
    }
  }
  Q=gi();
  while (Q--){
    RG int l=gi(),r=gi(),x=gi()^ans;
    RG int y=lower_bound(hsh+1,hsh+n+1,x)-hsh-1;
    if (!y){ ans=0,puts("0"); continue; }
    if (bl[y]!=1){
      memcpy(mn[0],mn[bl[y]-1],sizeof(int)*(bl[n]+1));
      memcpy(tg[0],tg[bl[y]-1],sizeof(int)*(bl[n]+1));
      memcpy(fi[0],fi[bl[y]-1],sizeof(int)*(bl[n]+1));
    } else{
      memset(mn[0],0,sizeof(int)*(bl[n]+1));
      memset(tg[0],0,sizeof(int)*(bl[n]+1));
      memset(fi[0],0,sizeof(int)*(bl[n]+1));
    }
    memset(tag,0,sizeof(int)*(bl[n]+1));
    for (RG int i=LL[bl[y]],L,R,BL,BR;i<=y;++i){
      BL=bl[L=max(1,id[i]-m+1)],BR=bl[R=id[i]],mn[0][BL]=tmpl[i],fi[0][BL]+=L==LL[BL];
      if (BL!=BR) mn[0][BR]=tmpr[i],++fi[0][BR],++tag[BL+1],--tag[BR];
    }
    for (RG int i=1;i<=bl[n];++i) tag[i]+=tag[i-1],tg[0][i]+=tag[i];
    if (bl[l]==bl[r]) ans=ask(bl[l],l,r,x); else{
      ans=min(ask(bl[l],l,RR[bl[l]],x),ask(bl[r],LL[bl[r]],r,x));
      for (RG int i=bl[l]+1;i<bl[r];++i) ans=min(ans,mn[0][i]+tg[0][i]);
    }
    printf("%d\n",ans);
  }
  return 0;
}

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define re register
using namespace std;
inline int read()
{
	int X=0,w=1; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
	while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
	return X*w;
}
const int MAXN=2e5+5;
const int INF=1e9;
struct node {
	unsigned long long mx:18,ls:23,rs:23;
} t[MAXN*40];
int cnt=0,rt[MAXN];
#define lson t[o].ls
#define rson t[o].rs
#define lmax (l==mid?(int)lson:(int)t[lson].mx)
#define rmax (mid+1==r?(int)rson:(int)t[rson].mx)
inline int modify(int p,int l,int r,int ql,int qr)
{
	if (l==r) return p+1;
	int o=++cnt; t[o]=t[p];
	if (ql<=l&&r<=qr) return ++t[o].mx,o;
	int mid=(l+r)>>1,tag=t[o].mx-max(lmax,rmax);
	if (ql<=mid) lson=modify(t[p].ls,l,mid,ql,qr);
	if (qr>mid) rson=modify(t[p].rs,mid+1,r,ql,qr);
	t[o].mx=max(lmax,rmax); t[o].mx+=tag;
	return o;
}
inline int query(int o,int l,int r,int ql,int qr)
{
	if (l==r) return o;
	if (ql<=l&&r<=qr) return t[o].mx;
	int mid=(l+r)>>1,ans=0,tag=t[o].mx-max(lmax,rmax);
	if (ql<=mid) ans=max(ans,query(lson,l,mid,ql,qr));
	if (qr>mid) ans=max(ans,query(rson,mid+1,r,ql,qr));
	return ans+tag;
}
int n,m;
pair<int,int> a[MAXN];
int main()
{
	n=read(),m=read();
	for (re int i=1;i<=n;++i) a[i].first=-read(),a[i].second=i;
	sort(a+1,a+n+1);
	for (re int i=1;i<=n;++i)
	{
		int ql=max(1,a[i].second-m+1);
		int qr=min(a[i].second,n-m+1);
		rt[i]=modify(rt[i-1],1,n-m+1,ql,qr);
	}
	int q=read(),ans=0;
	while (q--)
	{
		int l=read(),r=read(),x=read()^ans;
		int id=upper_bound(a+1,a+n+1,make_pair(-x,INF))-a-1;
		printf("%d\n",ans=m-query(rt[id],1,n-m+1,l,r));
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;

#define TRACE(x)
#define WATCH(x) TRACE(cout << #x" = " << x << endl)
#define WATCHR(a, b) TRACE(for (auto it=a; it!=b;) cout << *(it++) << " "; cout << endl)
#define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end());})

#define all(x) (x).begin(), (x).end()

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vb = vector<bool>;
using vs = vector<string>;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    const int DIM = 500;

    int N, M;
    cin >> N >> M;

    vi qual(N);
    for (int i = 0; i < N; i++) {
        cin >> qual[i];
    }

    N = DIM * DIM - DIM;
    qual.resize(N);

    vi order(N);
    for (int i = 0; i < N; i++) {
        order[i] = i;
    }
    sort(all(order), [&](int i, int j) {
        return qual[i] > qual[j];
    });

    vi ordered_qual = qual;
    sort(all(ordered_qual), greater<int>());

    vvi init(DIM, vi(DIM));
    vvi maxv(DIM, vi(DIM));
    vector<array<int, 2>> iupd(N);
    vector<array<int, 2>> mupd(N);

    vi diff(N + 1);
    for (int _i = 0; _i <= N; _i++) {
        if (_i % DIM == 0) {
            vi curv(DIM * DIM);
            for (int i = 0; i < N; i++) {
                curv[i] = diff[i] + (i ? curv[i-1] : 0);
                if (i % DIM == 0)
                    init[_i/DIM][i/DIM] = curv[i];
                maxv[_i/DIM][i/DIM] = max(curv[i], maxv[_i/DIM][i/DIM]);
            }

            WATCHC(curv);

            for (int ops = 0; ops < DIM && _i + ops < N; ops++) {
                int _j = _i + ops;
                int r = order[_j];
                int l = max(0, r - M + 1);

                iupd[_j] = { (l + DIM - 1) / DIM, r / DIM };
                mupd[_j] = { (l + DIM - 1) / DIM, (r + 1) / DIM - 1 };

                int last = -1;
                bool lres = false;
                auto incr_max = [&](int b) {
                    if (b < 0 || b >= DIM) return false;
                    if (b == last) return lres;
                    last = b;
                    int cmax = 0, nmax = 0;
                    for (int i = b * DIM; i < (b + 1) * DIM; i++) {
                        cmax = max(cmax, curv[i]);
                        if (l <= i && i <= r) curv[i]++;
                        nmax = max(nmax, curv[i]);
                    }
                    return lres = nmax > cmax;
                };

                if (incr_max(mupd[_j][0] - 1)) mupd[_j][0]--;
                if (incr_max(mupd[_j][1] + 1)) mupd[_j][1]++;

                TRACE(
                cout << "Update " << _j << " increments [ " << l << ", " << r << " ]" << endl;
                cout << "It increases init for " << iupd[_j][0] << " through " << iupd[_j][1] << endl;
                cout << "It increases maxv for " << mupd[_j][0] << " through " << mupd[_j][1] << endl;
                )
            }
        }

        if (_i < N) {
            int r = order[_i];
            int l = max(0, r - M + 1);
            diff[l]++;
            diff[r+1]--;
        }
    }

    int S, ans = 0;
    cin >> S;
    for (int d = 0; d < S; d++) {
        int l, r, x;
        cin >> l >> r >> x;

        l--, r--;
        x ^= ans;

        WATCH(x);

        int LIM = upper_bound(all(ordered_qual), x, greater<int>())
                - ordered_qual.begin();

        vi diff_init(DIM + 1), diff_maxv(DIM + 1);
        for (int _i = LIM/DIM*DIM; _i < LIM; _i++) {
            if (iupd[_i][0] <= iupd[_i][1]) {
                diff_init[iupd[_i][0]]++;
                diff_init[iupd[_i][1]+1]--;
            }
            if (mupd[_i][0] <= mupd[_i][1]) {
                diff_maxv[mupd[_i][0]]++;
                diff_maxv[mupd[_i][1]+1]--;
            }
        }

        WATCHC(diff_init);
        WATCHC(diff_maxv);

        vi spec_init = init[LIM/DIM];
        vi spec_maxv = maxv[LIM/DIM];

        WATCHC(spec_init);
        WATCHC(spec_maxv);

        int cur_diff_init = 0, cur_diff_maxv = 0;
        for (int b = 0; b < DIM; b++) {
            cur_diff_init += diff_init[b];
            cur_diff_maxv += diff_maxv[b];
            spec_init[b] += cur_diff_init;
            spec_maxv[b] += cur_diff_maxv;
        }

        WATCHC(spec_init);
        WATCHC(spec_maxv);

        int res = 0;
        for (int b = 0; b < DIM; b++) {
            int bl = b * DIM, br = bl + DIM - 1;
            if (l <= bl && br <= r) {
                res = max(res, spec_maxv[b]);
            } else if (max(l, bl) <= min(r, br)) {
                int val = spec_init[b];
                for (int i = bl; i <= br; i++) {
                    if (l <= i && i <= r) res = max(res, val);
                    if (qual[i] >= x) val--;
                    if (i+M<N && qual[i+M] >= x) val++;
                }
            }
        }

        ans = M - res;
        cout << ans << "\n";
    }

    return 0;
}

// ====================================
//   author: M_sea
//   website: https://m-sea-blog.com/
// ====================================
#include <bits/stdc++.h>
#define file(x) freopen(#x".in","r",stdin); freopen(#x".out","w",stdout)
using namespace std;
typedef long long ll;

int read() {
	int X=0,w=1; char c=getchar();
	while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
	while (c>='0'&&c<='9') X=X*10+c-'0',c=getchar();
	return X*w;
}

const int N=200000+10,M=447+10;

int n,m,Q,a[N],id[N];
bool cmp(int x,int y) { return a[x]>a[y]; }

int B,bl[N],L[M],R[M],w[N],addv[M],maxv[M];
int omaxv[M][M],lw[M][M],ql[N],qr[N],qlmax[N],qrmax[N];

int query(int l,int r,int w,int x) {
	int res=0;
	for (int i=L[bl[l]];i<l;++i) w+=(a[i+m]>=x)-(a[i]>=x);
	for (int i=l;i<=r;++i) res=max(res,w),w+=(a[i+m]>=x)-(a[i]>=x);
	return res;
}

int main() {
	n=read(),m=read(); B=sqrt(n);
	for (int i=1;i<=n;++i) a[i]=read(),id[i]=i;
	sort(id+1,id+n+1,cmp);
	for (int i=1;i<=n;++i) bl[i]=(i-1)/B+1;
	for (int i=1;i<=bl[n];++i) L[i]=(i-1)*B+1,R[i]=min(i*B,n);
	for (int i=1;i<=n;++i) {
		int l=ql[i]=max(1,id[i]-m+1),r=qr[i]=id[i];
		if (bl[i]!=bl[i-1]) {
			for (int j=1;j<=n;++j) w[j]+=addv[bl[j]];
			for (int j=1;j<=bl[n];++j)
				omaxv[bl[i]][j]=maxv[j],lw[bl[i]][j]=w[L[j]],addv[j]=0;
		}
		if (bl[l]==bl[r]) {
			for (int j=l;j<=r;++j)
				++w[j],maxv[bl[j]]=max(maxv[bl[j]],w[j]+addv[bl[j]]);
			for (int j=L[bl[l]];j<=R[bl[l]];++j) qlmax[i]=max(qlmax[i],w[j]);
		} else {
			for (int j=l;j<=R[bl[l]];++j)
				++w[j],maxv[bl[j]]=max(maxv[bl[j]],w[j]+addv[bl[j]]);
			for (int j=L[bl[r]];j<=r;++j)
				++w[j],maxv[bl[j]]=max(maxv[bl[j]],w[j]+addv[bl[j]]);
			for (int j=bl[l]+1;j<=bl[r]-1;++j) ++addv[j],++maxv[j];
			for (int j=L[bl[l]];j<=R[bl[l]];++j) qlmax[i]=max(qlmax[i],w[j]);
			for (int j=L[bl[r]];j<=R[bl[r]];++j) qrmax[i]=max(qrmax[i],w[j]);
		}
	}
	Q=read(); int lastans=0;
	while (Q--) {
		int l=read(),r=read(),x=read()^lastans;
		a[0]=x; int p=upper_bound(id+1,id+n+1,0,cmp)-id-1;
		for (int i=1;i<=bl[n];++i) addv[i]=0,maxv[i]=omaxv[bl[p]][i];
		int llw=lw[bl[p]][bl[l]],lrw=lw[bl[p]][bl[r]];
		for (int i=L[bl[p]];i<=p;++i) {
			maxv[bl[ql[i]]]=qlmax[i];
			if (bl[ql[i]]!=bl[qr[i]])
				maxv[bl[qr[i]]]=qrmax[i],++addv[bl[ql[i]]+1],--addv[bl[qr[i]]];
			if (ql[i]<=L[bl[l]]&&L[bl[l]]<=qr[i]) ++llw;
			if (ql[i]<=L[bl[r]]&&L[bl[r]]<=qr[i]) ++lrw;
		}
		for (int i=1;i<=bl[n];++i) addv[i]+=addv[i-1];
		if (bl[l]==bl[r]) printf("%d\n",lastans=m-query(l,r,llw,x));
		else {
			int ans=max(query(l,R[bl[l]],llw,x),query(L[bl[r]],r,lrw,x));
			for (int i=bl[l]+1;i<=bl[r]-1;++i) ans=max(ans,maxv[i]+addv[i]);
			printf("%d\n",lastans=m-ans);
		}
	}
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
#define dbg(x) cerr << (#x) << " --> " << (x) << endl

#define foreach(i,x) for(type(x)i=x.begin();i!=x.end();i++)
#define FOR(ii,aa,bb) for(int ii=aa;ii<=bb;ii++)
#define ROF(ii,aa,bb) for(int ii=aa;ii>=bb;ii--)

#define type(x) __typeof(x.begin())

#define orta (bas + son >> 1)
#define sag (k + k + 1)
#define sol (k + k)

#define pb push_back
#define mp make_pair

#define nd second
#define st first

#define endl '\n'

typedef pair < int ,int > pii;

typedef long long ll;

const long long linf = 1e18+5;
const int mod = 1e9+7;
const int logN = 18;
const int inf = 1e9;
const int N = 4e5;
const int M = 500;

int curv[M], dp[M], start[M], end[M], belong[N],  b[N], Lmin[N], L , R, l , r, sq, d[N];
int Rmin[N], v[M][M], F[M][M], ST[N << 2], lazy[N << 2], x, y, n, m, ans, last, S, val[N];

pii a[N];

int naive(int tt, int l, int r, int val) {

    int ans = inf;

    FOR(i,tt,end[belong[tt]]) {

        if(l <= i && i <= r)

            ans = min(ans, val);

        if(b[i] < x) val--;

        if(b[i+m] < x) val++;

   }

   return ans;

}

int take(int x) {

    return *min_element(val+start[x],val+end[x]+1);

}

int main() {

    scanf("%d %d",&n,&m);

    FOR(i,1,n) {

        scanf("%d",&a[i].st);

        a[i].nd = i;

        b[i] = a[i].st;

    }

    sort(a+1,a+n+1);

    sq = sqrt(n); last = 1;

    FOR(i,1,n)

        if(i == n || i % sq == 0) {

            start[++S] = last;

            end[S] = i;

            FOR(j,last,i)

                belong[j] = S;

            last = i + 1;

         }

    memset(Lmin, 15, sizeof Lmin);

    memset(Rmin, 15, sizeof Rmin);

    memset(v, 15, sizeof v);

    FOR(i,1,S) {

        FOR(j,start[i],end[i]) {

            int l = max(1, a[j].nd - m + 1), r = a[j].nd;

            if(belong[l] == belong[r]) {

                FOR(k, l, r) ++val[k];

                Lmin[j] = Rmin[j] = take(belong[l]);

            }

            else {

                 FOR(k, l, end[belong[l]]) ++val[k];

                 FOR(k, start[belong[r]], r) ++val[k];

                 Lmin[j] = take(belong[l]);

                 Rmin[j] = take(belong[r]);

             }

         }

         FOR(j, start[i], end[i]) {

             int l = max(1, a[j].nd - m + 1), r = a[j].nd;

             d[l]++; d[r + 1]--;

         }

         FOR(j, 1, n) val[j] = d[j] + val[j - 1];

         FOR(j, 1, S) {

             FOR(k, start[j], end[j]) v[i][j] = min(v[i][j], val[k]);

             F[i][j] = val[start[j]];

         }

   }

   int Q;

   scanf("%d",&Q);

   FOR(i, 1, Q) {

       scanf("%d %d %d", &L, &R, &x);

       x ^= ans;

       int val = lower_bound(a + 1, a + n + 1, mp(x, 0)) - a - 1, tt = start[belong[L]], tt2 = start[belong[R]];

       if(val <= 0) {

           printf("%d\n",ans = 0);

           continue;

       }

       int gr = belong[val];

       int val1 = F[gr - 1][belong[L]], val2 = F[gr - 1][belong[R]];

       FOR(j, 1, S) curv[j] = v[gr - 1][j], dp[j] = 0;

       FOR(j, start[gr], val) {

           l = max(1, a[j].nd - m + 1); r = a[j].nd;

           if(l <= tt && tt <= r) val1++;

           if(l <= tt2 && tt2 <= r) val2++;

           curv[belong[l]] = Lmin[j];

           curv[belong[r]] = Rmin[j];

           if(belong[l] + 1 > belong[r]) continue;

           dp[belong[l] + 1]++; dp[belong[r]]--;

       }

       FOR(j, 1, S) dp[j] += dp[j - 1];

       ans = inf;

       FOR(j, belong[L] + 1, belong[R] - 1) ans = min(ans, curv[j] + dp[j]);

       ans = min(ans, min(naive(tt, L, R, val1), naive(tt2, L, R, val2)));

       printf("%d\n", ans);

   }

   return 0;

}

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#define inf 0x3f3f3f3f
#define N 200005
#define M 450
using namespace std;
int gi()
{
    int res=0,s=1; char ch;
    for(ch=getchar();(ch<'0'||ch>'9')&&ch!='-';ch=getchar());
    if(ch=='-') s=-1,ch=getchar();
    for(;ch>='0'&&ch<='9';ch=getchar()) res=res*10+ch-48;
    return res*s;
}

int n,m,val[N],map[N],id[N],siz,lastans;
int bel[N],lef[M],rig[M],mn[M][M],laz[M][M],fir[M][M],value[N],tmpl[N],tmpr[N],lazy[M];

int cmp(int x,int y) {return val[x]<val[y];}
void add(int now,int p,int l,int r) {
    if(lef[p]>=l) fir[now][p]++;
    for(int k=l;k<=r;++k) value[k]++;
    mn[now][p]=inf;
    for(int k=lef[p];k<=rig[p];++k)
	mn[now][p]=min(mn[now][p],value[k]);
}
int ask(int p,int l,int r,int v) {
    int tmp=fir[0][p]+laz[0][p],res=inf;
    if(lef[p]>=l) res=tmp;
    for(int i=lef[p]+1;i<=r;++i) {
	tmp+=(val[i+m-1]<v)-(val[i-1]<v);
	if(i>=l) res=min(res,tmp);
    }
    return res;
}
int main()
{
    n=gi(),m=gi(); siz=sqrt(n);
    for(int i=1;i<=n;++i) {
	val[i]=gi(),id[i]=i;
	bel[i]=(i-1)/siz+1;
	if(bel[i]!=bel[i-1]) lef[bel[i]]=i,rig[bel[i-1]]=i-1;
    } rig[bel[n]]=n;
    sort(id+1,id+n+1,cmp);
    for(int i=1;i<=n;++i) map[i]=val[id[i]];
    for(int i=1,j=0,L,R;i<=n;++i) {
	if(bel[i]!=bel[i-1]) {   //new store point
	    ++j;
	    for(int k=1;k<=bel[n];++k)
		mn[j][k]=mn[j-1][k],laz[j][k]=laz[j-1][k],fir[j][k]=fir[j-1][k];
	}
	L=max(1,id[i]-m+1),R=id[i];
	if(bel[L]==bel[R])
	    add(j,bel[L],L,R),tmpl[i]=mn[j][bel[L]];
	else {
	    add(j,bel[L],L,rig[bel[L]]);
	    add(j,bel[R],lef[bel[R]],R);
	    tmpl[i]=mn[j][bel[L]],tmpr[i]=mn[j][bel[R]];
	    for(int i=bel[L]+1;i<bel[R];++i) laz[j][i]++;
	}
    }
    for(int q=gi(),l,r,x,X;q;--q) {
	l=gi(),r=gi(),x=gi()^lastans; X=x;
	x=lower_bound(map+1,map+n+1,x)-map-1;
	if(!x) {lastans=0;puts("0");continue;}
	for(int k=1;k<=bel[n];++k) {
	    mn[0][k]=laz[0][k]=fir[0][k]=lazy[k]=0;
	    mn[0][k]=mn[bel[x]-1][k],laz[0][k]=laz[bel[x]-1][k],fir[0][k]=fir[bel[x]-1][k];
	}
	for(int i=lef[bel[x]],L,R;i<=x;++i) {
	    L=max(1,id[i]-m+1),R=id[i];
	    if(bel[L]==bel[R]) {
		if(lef[bel[L]]>=L) fir[0][bel[L]]++;
		mn[0][bel[L]]=tmpl[i];
	    }
	    else {
		mn[0][bel[L]]=tmpl[i],mn[0][bel[R]]=tmpr[i];
		if(lef[bel[L]]>=L) fir[0][bel[L]]++;
		fir[0][bel[R]]++;
		lazy[bel[L]+1]++,lazy[bel[R]]--;
	    }
	}
	for(int i=1;i<=bel[n];++i)
	    lazy[i]+=lazy[i-1],laz[0][i]+=lazy[i];
	if(bel[l]==bel[r]) lastans=ask(bel[l],l,r,X);
	else {
	    lastans=min(ask(bel[l],l,rig[bel[l]],X),ask(bel[r],lef[bel[r]],r,X));
	    for(int i=bel[l]+1;i<bel[r];++i)
		lastans=min(lastans,mn[0][i]+laz[0][i]);
	}
	printf("%d\n",lastans);
    }
    return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2e5 + 5, inf = 1e9;
int read() {
	int x = 0, f = 1; char ch;
	while(! isdigit(ch = getchar())) (ch == '-') && (f = -f);
	for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 3) + (x << 1) + (ch ^ 48));
	return x * f;
}
template <class T> T Max(T a, T b) { return a > b ? a : b; }
template <class T> T Min(T a, T b) { return a < b ? a : b; }
#define lmax (l == mid ? int(t[rt].ls) : int(t[t[rt].ls].max))
#define rmax (mid + 1 == r ? int(t[rt].rs) : int(t[t[rt].rs].max))
struct node { unsigned long long max : 18, ls : 23, rs : 23; } t[N * 40];
pair <int, int> a[N];
int n, m, q, cnt, rt[N];
int change(int p, int l, int r, int x, int y) {
	if(l == r) return p + 1;
	int rt = ++ cnt; t[rt] = t[p];
	if(x <= l && r <= y) return ++ t[rt].max, rt;
	int mid = (l + r) >> 1, tag = t[rt].max - max(lmax, rmax);
	if(x <= mid) t[rt].ls = change(t[p].ls, l, mid, x, y);
	if(y > mid) t[rt].rs = change(t[p].rs, mid + 1, r, x, y);
	t[rt].max = max(lmax, rmax) + tag;
	return rt;
}
int query(int rt, int l, int r, int x, int y) {
	if(l == r) return rt;
	if(x <= l && r <= y) return t[rt].max;
	int mid = (l + r) >> 1, ans = 0, tag = t[rt].max - max(lmax, rmax);
	if(x <= mid) ans = max(ans, query(t[rt].ls, l, mid, x, y));
	if(y > mid) ans = max(ans, query(t[rt].rs, mid + 1, r, x, y));
	return ans + tag;
}
int main() {
//	freopen("seq2.in", "r", stdin);
//	freopen("seq2.out", "w", stdout);
	n = read(); m = read();
	for(int i = 1; i <= n; ++ i) a[i] = make_pair(-read(), i);
	sort(a + 1, a + n + 1);
	for(int i = 1, x, y; i <= n; ++ i) {
		x = max(1, a[i].second - m + 1); y = min(a[i].second, n - m + 1);
		rt[i] = change(rt[i - 1], 1, n - m + 1, x, y);
	}
	q = read();
	for(int i = 1, l, r, x, id, ans = 0; i <= q; ++ i) {
		l = read(); r = read(), x = read() ^ ans;
		id = upper_bound(a + 1, a + n + 1, make_pair(- x, inf)) - a - 1;
		printf("%d\n", ans = m - query(rt[id], 1, n - m + 1, l, r));
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}

#include<cstdio>
#include<algorithm>
#include<vector>
#define rep(i,x,y) for(int i=x;i<=y;++i)
#define dep(i,x,y) for(int i=x;i>=y;--i)
using namespace std;
const int S=700;
const int E=470;
const int N=200005;
const int inf=0x3f3f3f3f;

vector<int> smn[E],sv[E],sinc[E],sdec[E],inc[N],dec[N];
int n,m,a[N],c[N],q,l,r,x,las;

inline bool cmp1(const int &i,const int &j){
	return a[i]<a[j];
}
inline int belong(int x){
	return (x-1)/S+1;
}

inline int lower(vector<int> *u){
	if(!u->size()) return 0;
	if(*(u->end()-1)<x) return u->size();
	return lower_bound(u->begin(),u->end(),x)-u->begin();
}

int main(){
	scanf("%d%d",&n,&m);
	rep(i,1,n) scanf("%d",a+i),c[i]=i;
	sort(c+1,c+1+n,cmp1);
	dep(i,belong(n),1) smn[i].push_back(0),sv[i].push_back(-1);
	rep(v,1,n){
		int i=c[v];
		int j=max(1,i-m+1);
		int be=belong(j);
		int tot=0,mn=inf;
		inc[j].push_back(a[i]);
		sinc[be].push_back(a[i]);
		for(int k=(be-1)*S+1;k<=be*S;k++){
			tot+=inc[k].size();
			tot-=dec[k].size();
			if(tot<mn) mn=tot;
		}
		if(mn!=smn[be][smn[be].size()-1]){
			sv[be].push_back(a[i]);
			smn[be].push_back(mn);
		}
		if(i==n) continue;
		j=i+1;
		be=belong(j);
		tot=0,mn=inf;
		dec[j].push_back(a[i]);
		sdec[be].push_back(a[i]);
		for(int k=(be-1)*S+1;k<=be*S;k++){
			tot+=inc[k].size();
			tot-=dec[k].size();
			if(tot<mn) mn=tot;
		}
		if(mn!=smn[be][smn[be].size()-1]){
			sv[be].push_back(a[i]);
			smn[be].push_back(mn);
		}
	}
	scanf("%d",&q);
	while(q--){
		scanf("%d%d%d",&l,&r,&x);
		x^=las;
		int tot=0,mn=inf;
		int be=belong(l);
		rep(i,1,be-1){
			tot+=lower(sinc+i);
			tot-=lower(sdec+i);
		}
		if(r-l+1<=S){
			rep(i,(be-1)*S+1,l-1){
				tot+=lower(inc+i);
				tot-=lower(dec+i);
			}
			rep(i,l,r){
				tot+=lower(inc+i);
				tot-=lower(dec+i);
				if(mn>tot) mn=tot;
			}
		}
		else{
			int tmp=tot;
			tot+=lower(sinc+be);
			tot-=lower(sdec+be);
			int ce=belong(r);
			int v;
			rep(i,be+1,ce-1){
				v=smn[i][lower(sv+i)-1];
				if(mn>v+tot) mn=v+tot;
				tot+=lower(sinc+i);
				tot-=lower(sdec+i);
			}
			if(mn>tot+smn[ce][lower(sv+ce)-1])
			rep(i,(ce-1)*S+1,r){
				tot+=lower(inc+i);
				tot-=lower(dec+i);
				if(mn>tot) mn=tot;
			}
			tot=tmp;
			if(mn>tot+smn[be][lower(sv+be)-1]){
				rep(i,(be-1)*S+1,l-1){
					tot+=lower(inc+i);
					tot-=lower(dec+i);
				}
				rep(i,l,be*S){
					tot+=lower(inc+i);
					tot-=lower(dec+i);
					if(mn>tot) mn=tot;
				}
			}
		}
		las=mn;
		printf("%d\n",mn);
	}
}

#include <iostream>
#include <ctime>
#include <vector>
#include <algorithm>
using namespace std;
#define BSIZE 447

int N,M,Q;
int a[200000];
int cid[200000];
int pInc[200000],pDec[200000];

bool cmp(int x,int y)
{
	return a[x]<a[y];
}

vector<int> spoints[500];
vector<int> first[500];
vector<int> best[500];
int tmp[2000];

int dif[200001];

int slow(int l,int r,int x)
{
	int ans = 1000000000;
	for(int i=l;i<=r;i++)
	{
		int cur = 0;
		for(int j=i;j<i+M;j++)
			if(a[j]<x)
				cur++;
		ans = min(ans,cur);
	}
	return ans;
}

int main()
{
	//srand(1451781644);
	ios::sync_with_stdio(0);
	cin >> N >> M;
	for(int i=0;i<N;i++)
	{
		cin >> a[i];
		//a[i] = rand()%1000000;
		//cout << a[i] << ' ';
		cid[i] = i;
	}
	//cout << '\n';
	sort(cid,cid+N,cmp);
	for(int i=0;i<N;i++)
	{
		spoints[i/BSIZE].push_back(max(0,cid[i]-M+1));
		spoints[i/BSIZE].push_back(cid[i]+1);
	}
	for(int i=0;i*BSIZE<N;i++)
	{
		for(int j=0;j<N;j+=BSIZE)
			spoints[i].push_back(j);
		spoints[i].push_back(N);
		sort(spoints[i].begin(),spoints[i].end());
		spoints[i].resize(distance(spoints[i].begin(),unique(spoints[i].begin(),spoints[i].end())));
	}
	for(int i=0;i<N;i++)
	{
		pInc[i] = lower_bound(spoints[i/BSIZE].begin(),spoints[i/BSIZE].end(),max(0,cid[i]-M+1))-spoints[i/BSIZE].begin();
		pDec[i] = lower_bound(spoints[i/BSIZE].begin(),spoints[i/BSIZE].end(),cid[i]+1)-spoints[i/BSIZE].begin();
	}
	int lb = ((N-1)/BSIZE);
	for(int k=0;k<spoints[lb].size();k++)
	{
		best[lb].push_back(0), first[lb].push_back(0);
	}
	for(int i=N-1;i>0;i--)
	{
		dif[max(0,cid[i]-M+1)]++, dif[cid[i]+1]--;
		if(i%BSIZE == 0)
		{
			int b = (i/BSIZE)-1;
			int v = 0;
			int j = 0;
			int mx = 0;
			for(int k=0;k<=N;k++)
			{
				v += dif[k];
				if(k == spoints[b][j])
				{
					first[b].push_back(v);
					if(j)
					{
						best[b].push_back(mx);
					}
					mx = v;
					j++;
				}
				else mx = max(mx,v);
			}
/*			for(int k=0;k<spoints[b].size();k++)
				cout << spoints[b][k] << ' ';
			cout << '\n';
			for(int k=0;k<best[b].size();k++)
				cout << best[b][k] << ' ';
			cout << "\n\n";
*/
		}
	}
	cin >> Q;
	int l,r,x;
	int ans;
	for(int i=0;i<Q;i++)
	{
		//l = rand()%N, r = rand()%(N-M), x = rand()%1000000;
		//if(l>r) swap(l,r);
		cin >> l >> r >> x;
		if(i) x ^= ans;
		l--,r--;
		int b = 0;
		while((b+1)*BSIZE<N && a[cid[(b+1)*BSIZE]] < x)
			b++;
		//cout << b << '\n';
		for(int j=0;j<spoints[b].size();j++)
			tmp[j] = 0;
		for(int k=min(N,(b+1)*BSIZE)-1;k>=0 && a[cid[k]] >= x;k--)
			tmp[pInc[k]]++,tmp[pDec[k]]--;
		int lBlock = lower_bound(spoints[b].begin(),spoints[b].end(),l)-spoints[b].begin();
		ans = 0;
		int v = first[b][lBlock];
		int ps = 0;
		for(int j=0;j<lBlock;j++)
			ps += tmp[j];
		v += ps+tmp[lBlock];
		for(int j=spoints[b][lBlock]-1;j>=l;j--)
		{
			if(a[j]>=x) v++;
			if(j+M<N && a[j+M]>=x) v--;
			if(j<=r) ans = max(ans,v);
		}
		int rBlock = upper_bound(spoints[b].begin(),spoints[b].end(),r)-spoints[b].begin()-1;
		v = first[b][rBlock]+ps;
		for(int j=lBlock;j<=rBlock;j++)
			v += tmp[j];
		for(int j=spoints[b][rBlock];j<=r;j++)
		{
			if(j>=l) ans = max(ans,v);
			if(a[j]>=x) v--;
			if(j+M<N && a[j+M]>=x) v++;
		}
		for(int j=lBlock;j<rBlock;j++)
		{
			ps += tmp[j];
			ans = max(ans,ps+best[b][j]);
		}
		ans = M-ans;
		cout << ans << '\n';
		/*if(slow(l,r,x)!=ans)
		{
			cout << l << ' ' << r << ' ' << x << '\n';
			cout << ans << '\n';
		}*/
	}
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
#include <unordered_map>
#include <ctime>
typedef long long ll;
#define MAX 450
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
bool debug = 1;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
ll ln, lk, lm;

int a[200002], S;
vector<int> v, val[MAX];
int mn[MAX][MAX*2], sum[MAX][MAX*2];
int cal(int l, int r, int x) {
    int ans = inf;
    int now = lower_bound(v.begin(), v.end(), x) - v.begin();
    for(int i=0; i*S < n-m+1 && i*S<r; i++) {
        int st = i*S;
        int ed = min(st+S, n-m+1);
        k = lower_bound(val[i].begin(), val[i].end(), x) - val[i].begin()-1;
        if (k<0) {
            if (max(l,st) < min(r,ed)) ans = min(ans, now);
            continue;
        }
        if (l<=st && ed<=r) {
            ans = min(ans, now+mn[i][k]); now += sum[i][k];
        }
        else {
            if (max(l,st) < min(r,ed)) {
                for(int j=st;j<ed;j++) {
                    if(j>0 && a[j-1]<=val[i][k]) now--;
                    if (a[j-1+m]<=val[i][k]) now++;
                    if (l<=j && j<r) ans = min(ans, now);
                }
            } else now += sum[i][k];
        }

    }
    return ans;
}
int main() {
    scanf("%d%d", &n, &m);
    for(int i=0;i<n;i++) {
        scanf("%d", a+i);
    }
    REP(i,m-1) v.push_back(a[i]);
    sort(v.begin(), v.end());
    S = sqrt(n);
    for(int i=0;i*S <n-m+1; i++) {
        int st = i*S;
        int ed = min(st+S, n-m+1);
        for(int j=st;j<ed;j++) {
            if(j>0) val[i].push_back(a[j-1]);
            val[i].push_back(a[j+m-1]);
        }
        sort(val[i].begin(), val[i].end());
        val[i].resize(unique(val[i].begin(), val[i].end()) - val[i].begin());
        REP(k, val[i].size()) {
            mn[i][k] = inf;
            for(int j=st;j<ed;j++) {
                if (j>0 && a[j-1] <= val[i][k]) sum[i][k]--;
                if (a[j-1 + m] <= val[i][k]) sum[i][k]++;
                mn[i][k]=min(mn[i][k], sum[i][k]);
            }
        }
    }
    int q; scanf("%d", &q);
    for(int l,r,x,i=0,ans=0;i<q;i++) {
        scanf("%d%d%d", &l, &r, &x);
        x ^= ans;
        ans = cal(l-1,r,x);
        printf("%d\n", ans);
    }
    return 0;
}

#include <cstdio>
#include <ctime>
#include <climits>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXN = 200003;
const int NBLK = 454;
const int SZBLK = MAXN / NBLK + 3;
int n, m, a[MAXN];
int n0;
vector<int> all[NBLK], mpos[NBLK], sum[NBLK];
int size[NBLK];
void makeblk(int l, int r, vector<int> &all, vector<int> &mpos, vector<int> &sum, int &size)
{
    all.push_back(0);
    for (int i = l; i < r; ++i)
    {
        int mn = a[i], mx = a[i + m];
        if (mn > mx)
            swap(mn, mx);
        all.push_back(mn + 1);
        all.push_back(mx + 1);
    }
    sort(all.begin(), all.end());
    all.erase(unique(all.begin(), all.end()), all.end());
    size = all.size();
    mpos.resize(size);
    sum.resize(size);
    for (int i = 0; i < size; ++i)
    {
        int mp = 0, sm = 0;
        int alli = all[i];
        for (int j = l; j < r; ++j)
            if (a[j] < alli && alli <= a[j + m])
            {
                --sm;
                mp = min(mp, sm);
            }
            else if (a[j + m] < alli && alli <= a[j])
                ++sm;
        sum[i] = sm;
        mpos[i] = mp;
    }
}
int init[MAXN];
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i)
        scanf("%d", &a[i]);
    for (int i = 0; i < m; ++i)
        init[i] = a[i];
    sort(init, init + m);
    n0 = n - m + 1;
    for (int i = 0, j = 0; i < n0; i += SZBLK, ++j)
        makeblk(i, min(i + SZBLK, n0), all[j], mpos[j], sum[j], size[j]);
    int Q;
    scanf("%d", &Q);
    int lastans = 0;
    while (Q--)
    {
        int l, r, x;
        scanf("%d%d%d", &l, &r, &x);
        x ^= lastans;
        --l;
        int bl = l / SZBLK, br = (r - 1) / SZBLK;
        int sm = lower_bound(init, init + m, x) - init;
        for (int i = 0; i < bl; ++i)
        {
            int pos = upper_bound(all[i].begin(), all[i].end(), x) - all[i].begin() - 1;
            sm += sum[i][pos];
        }
        int ans = INT_MAX;

        if (bl == br)
            for (int i = bl * SZBLK; i < r; ++i)
            {
                if (i >= l)
                    ans = min(ans, sm);
                if (a[i] < x && x <= a[i + m])
                    --sm;
                else if (a[i + m] < x && x <= a[i])
                    ++sm;
            }
        else
        {
            //static long long t1 = 0, t2 = 0, t3 = 0;
            //clock_t cl = clock(), cl2;
            for (int i = bl * SZBLK; i < (bl + 1) * SZBLK; ++i)
            {
                if (i >= l)
                    ans = min(ans, sm);
                if (a[i] < x && x <= a[i + m])
                    --sm;
                else if (a[i + m] < x && x <= a[i])
                    ++sm;
            }
            //cl2 = clock();
            //t1 += cl2 - cl, cl = cl2;
            for (int i = bl + 1; i < br; ++i)
            {
                int pos = upper_bound(all[i].begin(), all[i].end(), x) - all[i].begin() - 1;
                ans = min(ans, sm + mpos[i][pos]);
                sm += sum[i][pos];
            }
            //cl2 = clock();
            //t2 += cl2 - cl, cl = cl2;
            for (int i = br * SZBLK; i < r; ++i)
            {
                ans = min(ans, sm);
                if (a[i] < x && x <= a[i + m])
                    --sm;
                else if (a[i + m] < x && x <= a[i])
                    ++sm;
            }
            //cl2 = clock();
            //t3 += cl2 - cl, cl = cl2;
            //fprintf(stderr, "%d    \r", SZBLK + br - bl + r - br * SZBLK);
            //fprintf(stderr, "%.3f %.3f %.3f\r", (double)t1/CLOCKS_PER_SEC, (double)t2/CLOCKS_PER_SEC, (double)t3/CLOCKS_PER_SEC);
        }
        //fprintf(stderr, "%g    \r", (double)Q/MAXN);
        printf("%d\n", lastans = ans);
    }
    return 0;
}

#include <algorithm>
#include <iostream>
#include <sstream>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
#include <map>
using namespace std;

#define _ (size_t)
#define all(a) a.begin(), a.end()
#define forint(i, a, b) for(int i=int(a); i<=int(b); ++i)
#define forintdown(i, a, b) for(int i=int(a); i>=int(b); --i)
#define forn(i, n) forint(i, 0, (n)-1)
#define forndown(i, n) forintdown(i, (n)-1, 0)
#define fillchar(a, x) memset(a, x, sizeof(a))
#define fi first
#define se second
#define PB push_back
#define MP make_pair
typedef long long LL;
typedef pair<LL,LL> PLL;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
typedef vector<LL> VL;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<string> VS;
template<class T> void setmax(T &a, T b) { if(b>a) a=b; }
template<class T> void setmin(T &a, T b) { if(b<a) a=b; }
template<class T> T gcd(T a, T b) { return b==0?a:gcd(b,a%b); }

const int MAXN=200010;
const int MAXNodes1=5900000;
const int MAXNodes2=8300000/3;
const int MAXNodes=MAXNodes1+MAXNodes2;

PII a[MAXN];
int n, m;

typedef unsigned long long ULL;
typedef unsigned short US;

struct FunctionalSegmentTree {
    ULL tr1[MAXNodes1];
    US tr2[MAXNodes2*3];
    int num1, num2;

    int roots[MAXN];
    int curtime;

    // return timestamp
    int init() {
        curtime=0;
        roots[0]=0;
        num1=num2=0;
        tr1[0]=0;
        return 0;
    }

    int make_delta(int x, int v) {
        assert(num2+5<MAXNodes2*3);
        num2+=3; int y=num2;
        ULL tmp=ULL(x)*MAXN+ULL(v);
        tr2[y]=US(tmp>>32), tr2[y+1]=US((tmp>>16)&((1<<16)-1)), tr2[y+2]=US(tmp&((1<<16)-1));
        return MAXNodes1+y/3;
    }
    void get_delta(int x, int &y, int &v) {
        x-=MAXNodes1; x*=3;
        ULL tmp=(ULL(tr2[x])<<32)+(ULL(tr2[x+1])<<16)+ULL(tr2[x+2]);
        y=int(tmp/MAXN), v=int(tmp%MAXN);
    }
    void get_node(int x, int &l, int &r, int &v) {
        int delta=0;
        if(x>=MAXNodes1)
            get_delta(x, x, delta);
        l=int(tr1[x]/MAXN/MAXNodes), r=int(tr1[x]/MAXN%MAXNodes);
        v=int(tr1[x]%MAXN)+delta;
    }
    void set_node(int x, int l, int r, int v) {
        tr1[x]=ULL(l)*MAXN*MAXNodes+ULL(r)*MAXN+v;
    }
    int minv(int x) {
        int delta=0;
        if(x>=MAXNodes1) {
            get_delta(x, x, delta);
            //printf("got %d %d\n",x,delta);
        }
        return int(tr1[x]%MAXN)+delta;
    }

    int add(int x, int s, int t, int st, int en, int val) {
        //printf("add %d [%d,%d] %d\n",x,s,t,val);fflush(stdout);
        if(st<=s && t<=en) {
            if(x>=MAXNodes1) {
                int delta;
                get_delta(x, x, delta);
                val+=delta;
            }
            x=make_delta(x, val);
            return x;
        }
        int l, r, v; get_node(x, l, r, v);
        //printf("   l=%d r=%d v=%d\n",l,r,v);fflush(stdout);
        x=++num1;
        assert(x+5<MAXNodes1);

        //printf("!_!\n"); fflush(stdout);
        int dv=v-min(minv(l), minv(r));
        //printf("!!\n"); fflush(stdout);

        int mid=(s+t)/2;
        if(st<=mid)
            l=add(l, s, mid, st, en, val);
        if(mid<en)
            r=add(r, mid+1, t, st, en, val);
        //printf("!.!\n"); fflush(stdout);

        v=min(minv(l), minv(r))+dv;
        set_node(x, l, r, v);
        return x;
    }
    int getmin(int x, int s, int t, int st, int en) {
        if(x==0)
            return 0;

        int l, r, v; get_node(x, l, r, v);
        int dv=v-min(minv(l), minv(r));
        if(st<=s && t<=en)
            return v;

        int mid=(s+t)/2, ans=1<<30;
        if(st<=mid)
            setmin(ans, getmin(l, s, mid, st, en));
        if(mid<en)
            setmin(ans, getmin(r, mid+1, t, st, en));
        ans+=dv;
        return ans;
    }

    // return timestamp
    int add(int l, int r, int v) {
        ++curtime;
        roots[curtime]=add(roots[curtime-1], 1, n, l, r, v);
        return curtime;
    }

    int getmin(int timestamp, int l, int r) {
        assert(0<=timestamp && timestamp<=curtime);
        int ans=getmin(roots[timestamp], 1, n, l, r);
        return ans;
    }
} tree;

int m1=0, m2=0;

void preprocess() {
    sort(a+1, a+n+1);

    int t0=tree.init();
    assert(t0==0);
    forint(i, 1, n) {
        int pos=a[i].se;
        int l=max(pos-m+1, 1), r=pos;
        //printf("p=%d l=%d r=%d\n",pos,l,r);
        int timestamp=tree.add(l, r, 1);
        assert(timestamp==i);
    }

    /*
    printf("  %d %d\n", tree.num1, tree.num2);
    if(m1<tree.num1 || m2<tree.num2) {
    setmax(m1, tree.num1);
    setmax(m2, tree.num2);
    printf("%d (%d) %d (%d)\n", m1, MAXNodes1,m2,MAXNodes2*3);
    printf(" -> %d %d\n", m1/MAXN+1, m2/3/MAXN+1);
    }
    */
}

int query(int l, int r, int q) {
    int i=int(lower_bound(a+1, a+n+1, MP(q,0))-a);
    assert(i>=1 && i<=n+1);
    //printf("q=%d i=%d\n",q,i);
    int ans=tree.getmin(i-1, l, r);
    return ans;
}

int main() {
    //*
    scanf("%d%d", &n,&m);
    forint(i, 1, n) scanf("%d", &a[i].fi), a[i].se=i;
    preprocess();
    /*/
    printf("%.4lf\n", (sizeof(tree)+sizeof(a))/1024.);
    printf("%.4lf\n", (sizeof(tree)+sizeof(a))/1000.);
    while(1) {
    n=200000; m=(rand()%10000*10000+rand())%(n+1);;
    forint(i,1, n) a[i].fi=rand(), a[i].se=i;

    preprocess();
    }
    //*/

    int cs; scanf("%d", &cs);
    int preans=0;
    while(cs--) {
        int l, r, x;
        scanf("%d%d%d", &l,&r,&x);
        int q=x^preans;

        int ans=query(l, r, q);
        printf("%d\n", ans);
        preans=ans;
    }

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+5;
const int mult=9;
const int blsize=(1<<mult);
const int INF=(1<<30);
pair<int,int> music[maxn];
int Maxval[blsize][blsize],front[blsize][blsize],update[blsize][2*blsize]; // first: time , second: pos
int arr[maxn],curval[maxn];
int curMax[blsize],curtag[blsize];
int n,m;

inline bool is_first(int idx) { return (idx & (blsize - 1)) == 0;}
inline int bl_idx(int idx) {return (idx >> mult) + ( idx >= n ? 1 : 0 );}
inline int first_idx(int blid) {return blid << mult;}
inline int last_idx(int blid) {return min(n - 1, first_idx(blid) + blsize - 1);}

int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++)
    {
        scanf("%d",arr+i);
        music[i].first=-arr[i];
        music[i].second=i;
    }
    sort(music,music+n);

    int totbl= (n+blsize-1)/blsize;
    for(int i=0;i<n;i++)
    {
        if(is_first(i))
        {
            int curtime=bl_idx(i);
            for(int j=0;j<n;j++) curval[j]+=curtag[bl_idx(j)];
            for(int j=0;j<totbl;j++)
            {
                curMax[j]+=curtag[j];
                Maxval[curtime][j]=curMax[j];
                front[curtime][j]=curval[first_idx(j)];
                curtag[j]=0;
            }
        }
        int R=music[i].second,L=max(0,R-m+1),sizeb=0,tochange[]={totbl,totbl};
        for(int j=L;j<=R;j++)
        {
            int curbl=bl_idx(j);
            if(is_first(j) && R>=last_idx(curbl)) // jump whole block
                curtag[curbl]++,j=last_idx(curbl);
            else
            {
                curval[j]++;
                curMax[curbl]=max(curMax[curbl],curval[j]);
                if(sizeb==0 || tochange[sizeb-1]!=curbl) tochange[sizeb++]=curbl;
            }
        }
        int curtime=bl_idx(i),idx=i-first_idx(curtime);
        for(int j=0;j<2;j++) update[curtime][2*idx+j]=(curMax[tochange[j]]<<mult)+tochange[j];
    }
    int query,ans=0;
    scanf("%d",&query);
    while(query--)
    {
        int qL,qR,qv;
        scanf("%d%d%d",&qL,&qR,&qv);qL--,qR--,qv^=ans;
        int idx=upper_bound(music,music+n,make_pair(-qv,INF))-music-1;
        if(idx<0){printf("%d\n",ans=m);continue;}

        int curtime=bl_idx(idx);
        for(int i=0;i<totbl;i++) curMax[i]=Maxval[curtime][i],curtag[i]=0;

        int st=first_idx(curtime);
        for(int i=st;i<=idx;i++)
        {
            int R=music[i].second,L=max(0,R-m+1);
            if(L!=first_idx(bl_idx(L))) L=last_idx(bl_idx(L))+1;
            if(R==last_idx(bl_idx(R))) R++;
            if(bl_idx(L)<=bl_idx(R)) curtag[bl_idx(L)]++,curtag[bl_idx(R)]--;
            for(int j=0;j<2;j++)
            {
                int xx=update[curtime][2*(i-st)+j];
                curMax[xx&(blsize-1)]=xx>>mult;
            }
        }
        for(int i=1;i<totbl;i++) curtag[i]+=curtag[i-1];

        ans=0;
        int val=-1;
        for(int i=qL;i<=qR;i++)
        {
            int curbl=bl_idx(i);
            if(is_first(i) && qR>=last_idx(curbl))
                ans=max(ans,curMax[curbl]+curtag[curbl]),i=last_idx(curbl),val=-1;
            else
            {
                if(val==-1)
                {
                    val=front[curtime][curbl];
                    int ftp=first_idx(curbl);
                    for(int j=st;j<=idx;j++)
                        if(ftp<=music[j].second && ftp>=max(0,music[j].second-m+1)) val++;
                    while(ftp!=i)
                    {
                        val-=(arr[ftp]>=qv),val+=(arr[ftp+m]>=qv);
                        ftp++;
                    }
                }
                ans=max(ans,val);
                val-=(arr[i]>=qv);val+=(arr[i+m]>=qv);
            }
        }
        printf("%d\n",ans=m-ans);
    }
}

#include <bits/stdc++.h>
typedef long long LL;
typedef unsigned long long uLL;
using namespace std;
#define rep(i,n) for(int i = 0;i < n;i ++)

const int N = 200000 + 1;
const int T = 8000000;

struct Data {
	uLL mx:18, ls:23, rs:23;
}tr[T];

pair<int, int> A[N];
int root[N];

int tot;

int calc(int l, int r, int o) {
	int mid = l + r >> 1;
	int lval = tr[o].ls;
	if(mid != l) {
		lval = tr[lval].mx;
	}
	int rval = tr[o].rs;
	if(mid+1 != r) {
		rval = tr[rval].mx;
	}
	return max(lval, rval);
}

int update(const int &last, const int &l, const int &r, const int &ql, const int &qr) {
	if(l == r) {
		return last + 1;
	}
	int o = ++ tot;
	tr[o] = tr[last];
	if(ql <= l && r <= qr) {
		tr[o].mx ++;
		return o;
	}
	int mid = l + r >> 1;
	int add = tr[last].mx - calc(l, r, last);
	if(ql <= mid) {
		tr[o].ls = update(tr[last].ls, l, mid, ql, qr);
	}
	if(qr > mid) {
		tr[o].rs = update(tr[last].rs, mid+1, r, ql, qr);
	}
	tr[o].mx = calc(l, r, o) + add;
	return o;
}

int query(const int &o, const int &l, const int &r, const int &ql, const int &qr) {
	if(l == r)	return o;
	if(ql <= l && r <= qr) {
		return tr[o].mx;
	}
	int mid = l + r >> 1;
	int add = tr[o].mx - calc(l, r, o);
	int res = 0;
	if(ql <= mid) {
		res = max(res, query(tr[o].ls, l, mid, ql, qr));
	}
	if(qr > mid) {
		res = max(res, query(tr[o].rs, mid+1, r, ql, qr));
	}
	res += add;
	return res;
}

int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	rep(i, n) {
		scanf("%d", &A[i].first);
		A[i].second = i;
	}
	sort(A, A + n, greater<pair<int, int>>());
	rep(i, n) {
		root[i+1] = update(root[i], 0, n-m, max(0, A[i].second-m+1), min(A[i].second, n-m));
	}
	int ans = 0, ql, qr, x, q;
	scanf("%d", &q);
	rep(i, q) {
		scanf("%d%d%d", &ql, &qr, &x);
		ql --;
		qr --;
		x ^= ans;
		if(x > A[0].first) {
			ans = m;
			printf("%d\n", ans);
			continue;
		}
		int lo = 0, hi = n - 1;
		while(lo < hi) {
			int mid = lo + hi + 1 >> 1;
			if(A[mid].first >= x) {
				lo = mid;
			} else {
				hi = mid - 1;
			}
		}
		ans = query(root[lo+1], 0, n-m, ql, qr);
	//	printf("%d %d %d %d\n", ql, qr, x, lo+1);
		ans = m - ans;
		printf("%d\n", ans);
	}
	return 0;
}

#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <ctime>
#include <cmath>
#define fi first
#define se second
#define PA pair<int,int>
#define VI vector<int>
#define VP vector<PA >
#define mk(x,y) make_pair(x,y)
#define int64 long long
#define N 200010
#define M 500
#define For(i,x,y) for (i=x;i<=y;i++)
using namespace std;
int i,j,k,n,m,q,an;
int a[N],ma[N][2],zhi[M][M],tma[M],ge[N],id[N],cov[M],L[N],R[N],fu[M][M]
	,yi[M][M];
inline bool cc1(const int &A,const int &B) {
	return a[A]<a[B];
}
inline int get(int x) {
	return x/M;
}
inline int xia(int x) {
	return (x/M+1)*M-1;
}
inline int qian(int x) {
	return x/M*M;
}
inline void pre() {
	For(i,0,n) id[i]=i;
	sort(id,id+n+1,cc1);
	For(i,0,n) {
		int A=id[i];
		int wei=get(i);
		if (i%M==0) {
			For(j,0,M-1) {
				zhi[wei][j]=tma[j];
				fu[wei][j]=cov[j];
				yi[wei][j]=ge[j*M]+cov[j];
			}
		}
		L[i]=R[i]=-1;
		for (j=max(0,A-m+1);j<=A;) {
			int _=get(j);
			if (j%M==0&&j+M-1<=A) {
				cov[_]++;
				if (L[i]<0) L[i]=_;
				R[i]=_;
				j+=M;
			} else {
				int f=_==get(A)?1:0;
				ge[j]++;
				tma[_]=max(tma[_],ge[j]);
				ma[i][f]=max(ma[i][f],ge[j]);
				j++;
			}
		}
	}
}
inline int find(int x) {
	int l=0,r=n,mid;
	for (;l<=r;) {
		mid=(l+r)/2;
		if (a[id[mid]]<=x) l=mid+1;
		else r=mid-1;
	}
	return l-1;
}
inline void work(int l,int r,int x) {
	int shu=find(x),i,j;
	int wei=get(shu);
	int zuo=get(l)+1,you=get(r)-1;
	an=0;
	For(i,0,M-1) tma[i]=0;
	For(i,qian(shu),shu) {
		if (L[i]<0) continue;
		tma[L[i]]++,tma[R[i]+1]--;
	}
	For(i,1,M-1) tma[i]+=tma[i-1];
	For(i,0,M-1) tma[i]+=fu[wei][i];
	For(i,qian(shu),shu) {
		int A=id[i];
		int le=max(0,A-m+1),ri=A;
		if (get(le)>=zuo&&get(le)<=you) an=max(an,ma[i][0]+tma[get(le)]);
		if (get(ri)>=zuo&&get(ri)<=you) an=max(an,ma[i][1]+tma[get(ri)]);
	}
	For(i,zuo,you) an=max(an,tma[i]+zhi[wei][i]);
	{
		int s; j=qian(l);
		s=yi[wei][get(j)];
		For(i,qian(shu),shu) {
			int A=id[i];
			int le=max(0,A-m+1),ri=A;
			if (le<=j&&j<=ri) s++;
		}
		j+=m;
		For(i,qian(l),xia(l)) {
			for (;j<=n&&i+m-1>=j;j++) s+=a[j]<=x;
			if (i>=l&&i<=r) an=max(an,s);
			s-=a[i]<=x;
		}
	}
	{
		int s; j=qian(r);
		s=yi[wei][get(j)];
		For(i,qian(shu),shu) {
			int A=id[i];
			int le=max(0,A-m+1),ri=A;
			if (le<=j&&j<=ri) s++;
		}
		j+=m;
		For(i,qian(r),xia(r)) {
			for (;j<=n&&i+m-1>=j;j++) s+=a[j]<=x;
			if (i>=l&&i<=r) an=max(an,s);
			s-=a[i]<=x;
		}
	}
	an=m-an;
	printf("%d\n",an);
}
int main() {
	scanf("%d%d",&n,&m); n--;
	For(i,0,n) scanf("%d",&a[i]),a[i]*=-1;
	pre();
	scanf("%d",&q);
	For(i,1,q) {
		int l,r,x;
		scanf("%d%d%d",&l,&r,&x);
		x^=an; l--,r--;
		work(l,r,-x);
	}
	//for(;;);
	return 0;
}

//
// Created by dream_maker on 2019-06-08.
//

#include <bits/stdc++.h>

using namespace std;

typedef pair<int, int> pii;
typedef unsigned long long ull;

const int MAXN = 200000;
const int N = 8000000 + 10;
const int T = MAXN + 3;

int n, m, rt[MAXN + 10], Q;
pii a[MAXN + 10];

int ncnt;
ull t[N];

void calc_s(const bool &nl, const bool &nr, ull x, int &ls, int &rs, int &tag) {
  int s = x % T;
  ls = x / T / N;
  rs = x / T % N;
  tag = s - min((nl ? ls : t[ls] % T), (nr ? rs : t[rs] % T));
}

ull calc(const int &ls, const int &rs, const int &val) {
  return (1ull * ls * N + rs) * T + val;
}

void ins(const int &last, int &u, const int &L, const int &R, const int &l, const int &r) {
  if (L == R) {
    u = last + 1;
    return;
  }
  t[u = ++ncnt] = t[last];
  if (l <= L && R <= r) {
    t[u]++;
    return;
  }
  int mid = (L + R) >> 1, ls, rs, val, lson = 0, rson = 0;
  calc_s((L == mid), (R == mid + 1), t[u], ls, rs, val);
  if (l <= mid) ins(ls, lson, L, mid, l, r);
  if (r > mid) ins(rs, rson, mid + 1, R, l, r);
  if (!lson) lson = ls;
  if (!rson) rson = rs;
  val += min(((L == mid) ? lson : t[lson] % T), ((R == mid + 1) ? rson : t[rson] % T));
  t[u] = calc(lson, rson, val);
}

int query(const int &u, const int &L, const int &R, const int &l, const int &r) {
  if (L == R) return u;
  if (!u) return 0;
  if (l <= L && R <= r) return t[u] % T;
  int mid = (L + R) >> 1, ls, rs, val, ret = N;
  calc_s((L == mid), (mid + 1 == R), t[u], ls, rs, val);
  if (l <= mid) ret = min(ret, query(ls, L, mid, l, r));
  if (r > mid) ret = min(ret, query(rs, mid + 1, R, l, r));
  return ret + val;
}

inline void build() {
  for (int i = 1; i <= n; i++)
    ins(rt[i - 1], rt[i], 1, n - m + 1, max(1, a[i].second - m + 1), a[i].second);
}

int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i].first), a[i].second = i;
  sort(a + 1, a + 1 + n);
  build();
  scanf("%d", &Q);
  int ans = 0;
  for (int i = 1; i <= Q; i++) {
    int x, l, r;
    scanf("%d%d%d", &l, &r, &x);
    x ^= ans;
    x = lower_bound(a + 1, a + 1 + n, make_pair(x, 0)) - a;
    if (x == 1) ans = 0;
    else ans = query(rt[x - 1], 1, n - m + 1, l, r);
    printf("%d\n", ans);
  }
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
int n,m,a[222222],b[222222],c[222222];
int ib[222222],bl[1111],br[1111],mn[1111][1111],st[1111][1111],w[222222],ad[1111],cnt;
int pre[222222],suf[222222],q,l,r,x,ans,W[1111],bt,ls,rs,lp,rp;
const int magic=444;
bool cmp(int i,int j)
{
	return a[i]<a[j];
}
void update(int k,int l,int r)
{
	pre[k]=suf[k]=1e9;
	if (ib[l]==ib[r])
	{
		for (int i=l;i<=r;i++) w[i]++;
		for (int i=bl[ib[l]];i<=br[ib[l]];i++) pre[k]=min(pre[k],w[i]);
		return;
	}
	int il=ib[l],ir=ib[r];
	for (int i=il;i<ir;i++) ad[i]++;
	for (int i=l;i<=br[il];i++) w[i]++;
	for (int i=bl[il];i<=br[il];i++) pre[k]=min(pre[k],w[i]);
	for (int i=bl[ir];i<=r;i++) w[i]++;
	for (int i=bl[ir];i<=br[ir];i++) suf[k]=min(suf[k],w[i]);
}
void restore(int x)
{
	for (int i=1;i<=n;i++)
	{
		w[i]+=ad[ib[i]];
	}
	memset(ad,0,sizeof(ad));
	for (int i=1;i<=cnt;i++) mn[x][i]=1e9;
	for (int i=1;i<=n;i++)
	{
		if (ib[i]!=ib[i-1]) st[x][ib[i]]=w[i];
		mn[x][ib[i]]=min(mn[x][ib[i]],w[i]);
	}
}
int query(int l,int r,int id,int s,int x)
{
	int now=s,ans=1e9;
	for (int i=bl[id];i<l;i++) now+=((a[i+m]<x)-(a[i]<x));
	for (int i=l;i<=r;i++)
	{
		ans=min(ans,now);
		now+=((a[i+m]<x)-(a[i]<x));
	}
	return ans;
}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	for (int i=1;i<=n;i++) b[i]=i;
	sort(b+1,b+n+1,cmp);
	for (int i=1;i<=n;i++) c[i]=a[b[i]];
	for (int i=0;i<=n;i++)
	{
		if (i*magic+1>n) break;
		bl[i]=i*magic+1;br[i]=min(n,(i+1)*magic);
		for (int j=bl[i];j<=br[i];j++) ib[j]=i;
	}
	cnt=ib[n];
	for (int i=1;i<=n;i++)
	{
		update(i,max(1,b[i]-m+1),b[i]);
		if (ib[i]!=ib[i+1]) restore(ib[i]);
	}
	scanf("%d",&q);
	while(q--)
	{
		scanf("%d%d%d",&l,&r,&x);
		x^=ans;
		int pos=lower_bound(c+1,c+n+1,x)-c;
		if (pos>n || c[pos]>=x) pos--;
		memset(ad,0,sizeof(ad));
		bt=ib[pos];
		for (int i=1;i<=cnt;i++)
		{
			W[i]=mn[bt-1][i];
		}
		lp=bl[ib[l]];rp=bl[ib[r]];ls=st[bt-1][ib[l]];rs=st[bt-1][ib[r]];
		for (int i=bl[bt];i<=br[bt] && i<=pos;i++)
		{
			int L=max(1,b[i]-m+1),R=b[i],il=ib[L],ir=ib[R];
			if (il==ir)
			{
				W[il]=pre[i];
			}
			else
			{
				W[il]=pre[i];W[ir]=suf[i];
				ad[il+1]++;ad[ir]--;
			}
			if (L<=lp && lp<=R) ls++;
			if (L<=rp && rp<=R) rs++;
		}
		for (int i=1;i<=cnt;i++) ad[i]+=ad[i-1];
		if (ib[l]==ib[r])
		{
			ans=query(l,r,ib[l],ls,x);
		}
		else
		{
			ans=min(query(l,br[ib[l]],ib[l],ls,x),query(bl[ib[r]],r,ib[r],rs,x));
			for (int i=ib[l]+1;i<ib[r];i++) ans=min(ans,W[i]+ad[i]);
		}
		printf("%d\n",ans);
	}
	return 0;
}

#include <stdio.h>
#include <algorithm>

using std :: pair;
using std :: sort;

template < class T > void read(T &x) { char c, mi = 0; while(c = getchar(), c <= 32); if(c == '-') mi = 1, x = 0; else if(c < 48 || c > 57) return void(x = c); else x = c - 48; while(c = getchar(), c > 32) x = 10 * x + c - 48; if(mi == 1) x = -x; }
template < class T > void read(T &x, T &y) { read(x); read(y); }
template < class T > void read(T &x, T &y, T &z) { read(x, y); read(z); }
template < class T > void reada(T *a, int n) { for(int i = 0; i < n; ++i) read(a[i]); }
template < class T > void write(T x) { static char s[20]; char pt = 0, mi = (x < 0); if(mi == 1) x = -x; while(!pt || x > 0) { s[++pt] = (char)(x % 10 + '0'); x /= 10; } if(mi == 1) putchar('-'); while(pt > 0) putchar(s[pt--]); }
template < class T > void write(T x, T y) { write(x); putchar(' '); write(y); }
template < class T > void write(T x, T y, T z) { write(x, y); putchar(' '); write(z); }
template < class T > void writeln(T x) { write(x); puts(""); }
template < class T > void writea(T *a, int n) { for(int i = 0; i < n; ++i) { write(a[i]); putchar(i + 1 == n ? '\n' : ' '); } }

#define nmax 200010
#define fs first
#define sd second

int pt = 0;
int roots[nmax];
int n, m, q;
int ans, l, r, x;
pair < int, int > p[nmax];

int mi(const int &a, const int &b) {
	return a < b ? a : b;
}

int ma(const int &a, const int &b) {
	return a > b ? a : b;
}

struct Node {
	unsigned long long mi : 18, l : 23, r : 23;
} t[7777777];

int findAdd(int ret, int xl, char dl, int xr, char dr) {
	return t[ret].mi - mi(dl == 0 ? t[xl].mi : xl, dr == 0 ? t[xr].mi : xr);
}

void f(int ret, int xl, char dl, int xr, char dr, int add = 0) {
	t[ret].mi = add + mi(dl == 0 ? t[xl].mi : xl, dr == 0 ? t[xr].mi : xr);
	t[ret].l = xl;
	t[ret].r = xr;
}

int upd(int v, int L, int R, int l, int r) {
	if(L == R) {
		return v + 1;
	}
	int ret = ++pt;
	if(L == l && r == R) {
		t[ret].mi = t[v].mi + 1;
		t[ret].l = t[v].l;
		t[ret].r = t[v].r;
		return ret;
	}
	int M = (L + R) / 2;
	if(r <= M) {
		t[ret].l = upd(t[v].l, L, M, l, r);
		t[ret].r = t[v].r;
	} else if(l > M) {
		t[ret].r = upd(t[v].r, M + 1, R, l, r);
		t[ret].l = t[v].l;
	} else {
		t[ret].l = upd(t[v].l, L, M, l, M);
		t[ret].r = upd(t[v].r, M + 1, R, M + 1, r);
	}
	f(ret, t[ret].l, L == M, t[ret].r, M + 1 == R,
	findAdd(v, t[v].l, L == M, t[v].r, M + 1 == R));
	return ret;
}

int get(int v, int L, int R, int l, int r) {
	if(v == 0) {
		return 0;
	}
	if(L == R) {
		return v;
	}
	if(L == l && r == R) {
		return t[v].mi;
	}
	int M = (L + R) / 2;
	int z = findAdd(v, t[v].l, L == M, t[v].r, M + 1 == R);
	if(r <= M) {
		return z + get(t[v].l, L, M, l, r);
	}
	if(l > M) {
		return z + get(t[v].r, M + 1, R, l, r);
	}
	return z + mi(get(t[v].l, L, M, l, M),
		get(t[v].r, M + 1, R, M + 1, r));
}

int main() {
//	freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
//	freopen("elevator.in", "r", stdin); freopen("elevator.out", "w", stdout);
//	printf("%d\n", sizeof(t));
	read(n, m);
	for(int i = 1; i <= n; ++i) {
		read(p[i].fs);
		p[i].sd = i;
	}
	roots[0] = 0;
	sort(p + 1, p + 1 + n);
	for(int i = 1; i <= n; ++i) {
		roots[i] = upd(roots[i - 1], 1, n - m + 1, ma(p[i].sd - m + 1, 1), mi(p[i].sd, n - m + 1));
	}
	read(q);
	while(q --> 0) {
		read(l, r, x);
		x ^= ans;
		int id = upper_bound(p + 1, p + 1 + n, std :: make_pair(x, -1)) - (p + 1);
		ans = get(roots[id], 1, n - m + 1, l, r);
		writeln(ans);
	}
	getchar(); getchar();
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

namespace TYC
{
	typedef long long ll;
	typedef pair<int, int> pii;

	const int N = 2e5 + 5, M = 450, INF = 0x3f3f3f3f;

	int n, m, tot, block, A[N], BV[M][M], FV[M][M], Tag[M], Mx[M], W[N], Bel[N], Begin[M], End[M];
	pii V[N], EX[N][2];

	inline int read()
	{
		int x = 0, f = 0, ch = getchar();
		while (!isdigit(ch))
			f |= (ch == '-'), ch = getchar();
		while (isdigit(ch))
			x = x * 10 + ch - '0', ch = getchar();
		return f ? -x : x;
	}

	void init()
	{
		block = sqrt(n);
		for (int i = 1; i <= n; i++)
			Bel[i] = (i - 1) / block + 1;
		tot = Bel[n];
		for (int i = 1; i <= tot; i++)
		{
			Begin[i] = End[i - 1] + 1;
			End[i] = min(n, End[i - 1] + block);
		}

		sort(V + 1, V + 1 + n, greater<pii>());
		for (int i = 1; i <= n; i++)
		{
			if (Begin[Bel[i]] == i)
			{
				for (int j = 1; j <= n; j++)
					W[j] += Tag[Bel[j]];
				for (int j = 1, p = Bel[i]; j <= tot; j++)
				{
					Mx[j] += Tag[j];
					Tag[j] = 0;
					BV[p][j] = Mx[j];
					FV[p][j] = W[Begin[j]];
				}
			}
			int r = V[i].second, l = max(1, r - m + 1), c = 0;
			for (int j = l; j <= r; j++)
			{
				int p = Bel[j];
				if (j == Begin[p] && End[p] <= r)
					Tag[p]++, j = End[p];
				else
				{
					if (!c || EX[i][c - 1].first != p)
						EX[i][c++].first = p;
					Mx[p] = max(Mx[p], ++W[j]);
				}
			}
			for (int j = 0; j < c; j++)
			   EX[i][j].second = Mx[EX[i][j].first];
		}
	}

	void work()
	{
		n = read(), m = read();
		for (int i = 1; i <= n; i++)
			V[i] = pii(A[i] = read(), i);
		init();
		int q = read(), vl, vr, x, real_val, last_ans = 0;
		while (q--)
		{
			vl = read(), vr = read(), real_val = read() ^ last_ans;
			x = upper_bound(V + 1, V + 1 + n, pii(real_val, -INF), greater<pii>()) - V - 1;
			if (!x)
			{
				printf("%d\n", last_ans = m);
				continue;
			}
			int k = Bel[x];
			memset(Tag, 0, sizeof(int[tot + 1]));
			memcpy(Mx, BV[k], sizeof(int[tot + 1]));
			for (int i = Begin[k]; i <= x; i++)
			{
				int r = V[i].second, l = max(1, r - m + 1);
				if (Bel[l] != Bel[r])
				{
					if (l != Begin[Bel[l]])
						l = End[Bel[l]] + 1;
					Tag[Bel[l]]++;
					Tag[Bel[r + 1]]--;
				}
				for (int j = 0; j < 2; j++)
					if (EX[i][j].first)
						Mx[EX[i][j].first] = EX[i][j].second;
			}
			for (int i = 2; i <= tot; i++)
				Tag[i] += Tag[i - 1];
			int ans = 0, val = -1;
			for (int i = vl; i <= vr; i++)
			{
				int p = Bel[i];
				if (i == Begin[p] && End[p] <= vr)
				{
					ans = max(ans, Mx[p]);
					val = -1;
					i = End[p];
				}
				else
				{
					if (val == -1)
					{
						val = FV[k][p];
						int id = Begin[p];
						for (int j = Begin[k]; j <= x; j++)
							val += max(1, V[j].second - m + 1) <= id && id <= V[j].second;
						while (id != i)
						{
							val = val - (A[id] >= real_val) + (A[id + m] >= real_val);
							id++;
						}
					}
					ans = max(ans, val);
					val = val - (A[i] >= real_val) + (A[i + m] >= real_val);
				}
			}
			printf("%d\n", last_ans = m - ans);
		}
	}
}

int main()
{
	TYC::work();
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//INSERT ABOVE HERE
const int BS = 512;
const int MAX = 1<<18;

int dat[MAX]={}; // state
int val[MAX/BS]={}; // max
int add[MAX/BS]={}; // add
int frt[MAX/BS]={}; // front

int bf[MAX/BS][MAX/BS]; // front
int bm[MAX/BS][MAX/BS]; // max

int cp[3][MAX]={};
int cv[3][MAX]={};

signed main(){
  int n,m;
  cin>>n>>m;
  vector<int> as(n+n,0);
  for(int i=0;i<n;i++) cin>>as[i];

  using P = pair<int, int>;
  vector<P> vp;
  for(int i=0;i<n;i++) vp.emplace_back(-as[i],i);
  sort(vp.begin(),vp.end());

  auto eval=
    [&](int i){
      int l=(i/BS)*BS;
      if(add[l/BS]==0) return;
      for(int j=l;j<l+BS;j++) dat[j]+=add[j/BS];
      add[l/BS]=0;
    };

  for(int i=0;i<n;i++){
    //cout<<i<<" "<<vp[i].second<<" "<<as[vp[i].second]<<endl;
    if(i%BS==0){
      for(int j=0;j<MAX;j+=BS){
        val[j/BS]+=add[j/BS];
        eval(j);
        bf[i/BS][j/BS]=dat[j];
        bm[i/BS][j/BS]=val[j/BS];
      }
    }
    // [l, r)
    int r=vp[i].second+1;
    int l=max(0,r-m);

    int idx=1;
    for(int j=l;j<r;j++){
      if(j%BS==0 && j+BS<=r){
        add[j/BS]+=1;
        j+=BS-1;
      }else{
        dat[j]+=1;
        chmax(val[j/BS],dat[j]+add[j/BS]);
        if(cp[idx-1][i]!=j/BS){
          cp[idx][i]=j/BS;
          cv[idx][i]=val[j/BS];
          idx++;
        }
      }
    }
    while(idx<=2){
      cp[idx][i]=MAX/BS-1;
      cv[idx][i]=0;
      idx++;
    }
  }

  auto calc=
    [&](int l,int r,int v)->int{
      int idx=upper_bound(vp.begin(),vp.end(),P(-v,n))-vp.begin();

      int k=idx/BS*BS;
      for(int j=0;j<MAX;j+=BS){
        val[j/BS]=bm[k/BS][j/BS];
        add[j/BS]=0;
        frt[j/BS]=bf[k/BS][j/BS];
      }

      for(int i=k;i<idx;i++){
        int q=vp[i].second+1;
        int p=max(0,q-m);
        if(p%BS) p=(p/BS+1)*BS;
        add[p/BS]+=1;
        add[q/BS]-=1;

        val[cp[1][i]]=cv[1][i];
        val[cp[2][i]]=cv[2][i];

        if(q%BS) frt[q/BS]+=1;
      }

      for(int j=BS;j<MAX;j+=BS)
        add[j/BS]+=add[j/BS-1];

      int res=0,flg=0,tmp=0;
      for(int j=l;j<r;j++){
        if(j%BS==0 && j+BS<=r){
          chmax(res,val[j/BS]+add[j/BS]);
          j+=BS-1;
          flg=0;
        }else{
          if(flg==0){
            flg=1;
            tmp=frt[j/BS]+add[j/BS];
            int p=j/BS*BS;
            while(p<j){
              tmp-=as[p]>=v;
              tmp+=as[p+m]>=v;
              p++;
            }
          }
          chmax(res,tmp);
          tmp-=as[j]>=v;
          tmp+=as[j+m]>=v;
        }
      }
      return m-res;
    };

  int q;
  cin>>q;
  int pre_ans=0;
  for(int i=0;i<q;i++){
    int l,r,x;
    cin>>l>>r>>x;
    l--;
    int ans=calc(l,r,x^pre_ans);
    cout<<ans<<"\n";
    pre_ans=ans;
  }
  cout<<flush;
  return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#define lson tree[o].ls
#define rson tree[o].rs
using namespace std;

typedef unsigned long long ULL;

const int INF=0x3f3f3f3f;

struct Node {
    ULL mx:18,ls:23,rs:23;
}tree[8000000];
int tot;

int root[200010];
pair<int,int>repr[200010];

int ql,qr;

int update(int l,int r,int p) {
    int o=++tot;
    if(tot==8000000) {
        puts("MLE");
        exit(0);
    }
    tree[o]=tree[p];
    if(ql<=l&&r<=qr) {
        tree[o].mx++;
        return o;
    }
    int mid=(l+r)>>1,a=tree[o].mx-max(tree[lson].mx,tree[rson].mx);
    if(ql<=mid) {
        lson=update(l,mid,tree[p].ls);
    }
    if(mid<qr) {
        rson=update(mid+1,r,tree[p].rs);
    }
    tree[o].mx=max(tree[lson].mx,tree[rson].mx);
    tree[o].mx+=a;
    return o;
}

int query(int o,int l,int r) {
    if(ql<=l&&r<=qr) {
        return tree[o].mx;
    }
    int mid=(l+r)>>1,res=0,a=tree[o].mx-max(tree[lson].mx,tree[rson].mx);
    if(ql<=mid) {
        res=max(res,query(lson,l,mid));
    }
    if(mid<qr) {
        res=max(res,query(rson,mid+1,r));
    }
    return res+a;
}

int n,m,q;

int main() {
    scanf("%d%d",&n,&m);
    for(int i=1,a;i<=n;i++) {
        scanf("%d",&a);
        repr[i]=make_pair(-a,i);
    }
    sort(repr+1,repr+n+1);
    for(int i=1;i<=n;i++) {
        ql=max(1,repr[i].second-m+1);
        qr=repr[i].second;
        root[i]=update(1,n,root[i-1]);
    }
    scanf("%d",&q);
    int lastans=0;
    while(q--) {
        int x;
        scanf("%d%d%d",&ql,&qr,&x);
        x^=lastans;
        int id=upper_bound(repr+1,repr+n+1,make_pair(-x,INF))-repr-1;
        if(id<0) {
            puts("invalid access");
            exit(0);
        }
        printf("%d\n",lastans=m-query(root[id],1,n));
    }
    return 0;
}

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
unsigned long long t[8000005];
struct node {int x,y;} tt[200005];
int ans,i,A,B,C,n,m,Q,cnt,root[200005],L,R,mid;
int cmp(node i,node j) {return i.x>j.x;}
void work(unsigned long long A,int &B,int &C)
{
    A/=200001;
    C=A%8000001;
    A/=8000001;
    B=A%8000001;
}
unsigned long long WORK(unsigned long long A,unsigned long long B,unsigned long long C)
{
    A*=8000001;
    A*=200001;
    B*=200001;
    return A+B+C;
}
void Update(int &A,int &B,int l,int r,int L,int R)
{
    if (l==r) {B=1; return;}
    if (!B) B=++cnt;
    //if (n==200000 && cnt==8000000) {cout<<i<<' '<<cnt<<endl; exit (0);}
    //cout<<A<<' '<<B<<' '<<l<<' '<<r<<' '<<L<<' '<<R<<endl;
    int mid=(l+r)/2,llB,rrB,llA,rrA;
    work(t[B],llB,rrB); work(t[A],llA,rrA);
    if (l==L && r==R)
    {
        if (!llB) llB=llA; if (!rrB) rrB=rrA;
        int D=t[A]%200001-max(llA==1&&mid==l?1:t[llA]%200001,rrA==1&&mid+1==r?1:t[rrA]%200001);
        t[B]=WORK(llB,rrB,max(llB==1&&mid==l?1:t[llB]%200001,rrB==1&&mid+1==r?1:t[rrB]%200001)+D+1);
        return;
    }
    int D=t[A]%200001-max(llA==1&&mid==l?1:t[llA]%200001,rrA==1&&mid+1==r?1:t[rrA]%200001);
    if (L<=mid) Update(llA,llB,l,mid,L,min(mid,R));
    if (R>mid) Update(rrA,rrB,mid+1,r,max(mid+1,L),R);
    if (!llB) llB=llA; if (!rrB) rrB=rrA;
    t[B]=WORK(llB,rrB,max(llB==1&&mid==l?1:t[llB]%200001,rrB==1&&mid+1==r?1:t[rrB]%200001)+D);
}
int work2(int A,int l,int r,int L,int R)
{
    if (l==L && r==R) return A==1&&l==r?1:t[A]%200001;
    int mid=(l+r)/2,llA,rrA,p=0,q=0;
    work(t[A],llA,rrA);
    //cout<<A<<endl;
    int D=t[A]%200001-max(llA==1&&mid==l?1:t[llA]%200001,rrA==1&&mid+1==r?1:t[rrA]%200001);
    if (L<=mid) p=work2(llA,l,mid,L,min(mid,R));
    if (R>mid) q=work2(rrA,mid+1,r,max(mid+1,L),R);
   // cout<<A<<' '<<l<<' '<<r<<' '<<L<<' '<<R<<' '<<D<<' '<<p<<' '<<q<<' '<<t[A]%200001<<endl;
    return max(p,q)+D;
}
int main()
{
    scanf("%d%d",&n,&m);
    for (i=1; i<=n; i++)
    {
        scanf("%d",&tt[i].x);
        tt[i].y=i;
    }
    sort(tt+1,tt+n+1,cmp);
    for (i=1; i<=n; i++) Update(root[i-1],root[i],1,n,max(1,tt[i].y-m+1),tt[i].y);
    scanf("%d",&Q);
    while (Q--)
    {
        scanf("%d%d%d",&A,&B,&C);
        C^=ans;
        L=1; R=n; mid=(L+R)/2;
        while (L<=R)
        {
            if (tt[mid].x<C) {R=mid-1; mid=(L+R)/2;} else
            {
                L=mid+1;
                mid=(L+R)/2;
            }
        }
        if (!R) {ans=m; printf("%d\n",ans); continue;}
        ans=m-work2(root[R],1,n,A,B);
        printf("%d\n",ans);
    }
    return 0;
}

#include <bits/stdc++.h>

#define fs first
#define sc second

using namespace std;

const int INF = 1e9;
const int N = 2e5 + 100;
const int MAGIC1 = 400;
const int MAGIC2 = 400;

int tree[N * 4], tree_add[N * 4];
int n, m, s;
int songs[N];
vector<pair<int, int> > sorted;
vector<vector<int> > places, first, mn;
pair<int, int> add[N];

int get(int v, int vl, int vr, int l, int r) {
    if (vl >= r || vr <= l) {
        return INF;
    }
    if (vl >= l && vr <= r) {
        return tree[v] + tree_add[v];
    }
    int mid = (vl + vr) / 2;
    return min(get(v * 2 + 1, vl, mid, l, r), get(v * 2 + 2, mid, vr, l, r)) + tree_add[v];
}

void upd(int v, int vl, int vr, int l, int r, int x) {
    if (vl >= r || vr <= l) {
        return;
    }
    if (vl >= l && vr <= r) {
        tree_add[v] += x;
    } else {
        int mid = (vl + vr) / 2;
        upd(v * 2 + 1, vl, mid, l, r, x);
        upd(v * 2 + 2, mid, vr, l, r, x);
        tree[v] = min(tree[v * 2 + 1] + tree_add[v * 2 + 1], tree[v * 2 + 2] + tree_add[v * 2 + 2]);
    }
}

inline int get(int l, int r) {
    return get(0, 1, n + 1, l, r);
}
inline void upd(int l, int r, int x) {
    upd(0, 1, n + 1, l, r, x);
}

int main()
{
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
        scanf("%d", songs + i);
        sorted.emplace_back(songs[i], i);
    }
    sort(sorted.begin(), sorted.end());
    for (int i = 0; i < n; i += MAGIC1) {
        places.push_back(vector<int>());
        first.push_back(vector<int>());
        mn.push_back(vector<int>());

        for (int j = 1; j <= n; j += MAGIC2) {
            places.back().push_back(j);
        }
        places.back().push_back(n + 1);
        for (int j = i; j < i + MAGIC1 && j < n; j++) {
            places.back().push_back(max(1, sorted[j].sc - m + 1));
            places.back().push_back(sorted[j].sc + 1);
        }
        sort(places.back().begin(), places.back().end());
        places.back().resize(distance(places.back().begin(), unique(places.back().begin(), places.back().end())));
        for (int j = 0; j < (int) places.back().size() - 1; j++) {
            first.back().push_back(get(places.back()[j], places.back()[j] + 1));
            mn.back().push_back(get(places.back()[j], places.back()[j + 1]));
        }

        for (int j = i; j < i + MAGIC1 && j < n; j++) {
            upd(max(1, sorted[j].sc - m + 1), sorted[j].sc + 1, 1);
            add[j].fs = (int) (lower_bound(places.back().begin(), places.back().end(), max(0, sorted[j].sc - m + 1)) - places.back().begin());
            add[j].sc = (int) (lower_bound(places.back().begin(), places.back().end(), sorted[j].sc + 1) - places.back().begin());
        }
    }

    scanf("%d", &s);
    int pre = 0;
    for (int i = 0; i < s; i++) {
        int l, r, q;
        scanf("%d%d%d", &l, &r, &q);
        r++;
        q ^= pre;
        int t = (int) (lower_bound(sorted.begin(), sorted.end(), make_pair(q, 0)) - sorted.begin());
        int cur = t / MAGIC1;
        if (cur == (int) places.size()) {
            cur--;
        }
        vector<int> dp(places[cur].size());
        for (int j = cur * MAGIC1; j < t; j++) {
            dp[add[j].fs]++;
            dp[add[j].sc]--;
        }
        for (int j = 1; j < (int) dp.size(); j++) {
            dp[j] += dp[j - 1];
        }
        int ans = INF;
        int ll = INF, rr = -1;
        for (int j = 0; j < (int) places[cur].size() - 1; j++) {
            if (places[cur][j] >= l && places[cur][j + 1] <= r) {
                ans = min(ans, mn[cur][j] + dp[j]);
                ll = min(ll, j);
                rr = j + 1;
            }
        }
        cout << "geger: " << places[cur].size() << ' ' << first[cur].size() << endl;
        if (ll != INF) {
            for (int j = places[cur][ll] - 1, z = first[cur][ll] + dp[ll]; j >= l; j--) {
                if (songs[j] < q) {
                    z++;
                }
                if (j + m <= n && songs[j + m] < q) {
                    z--;
                }
                ans = min(ans, z);
            }
            if (places[cur][rr] < n + 1) {
                for (int j = places[cur][rr], z = first[cur][rr] + dp[rr]; j < r; j++) {
                    ans = min(ans, z);
                    if (songs[j] < q) {
                        z++;
                    }
                    if (j + m <= n && songs[j + m] < q) {
                        z--;
                    }
                }
            }
        } else {
            int ll = 0;
            while (places[cur][ll + 1] <= l) {
                ll++;
            }
            int z = first[cur][ll] + dp[ll];
            for (int j = places[cur][ll]; j < r; j++) {
                if (j >= l) {
                    ans = min(ans, cur);
                }
                if (songs[j] < q) {
                    z++;
                }
                if (j + m <= n && songs[j + m] < q) {
                    z--;
                }
            }
        }
        printf("%d\n", ans);
        pre = ans;
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;

#define TRACE(x)
#define WATCH(x) TRACE(cout << #x" = " << x << endl)
#define WATCHR(a, b) TRACE(for (auto it=a; it!=b;) cout << *(it++) << " "; cout << endl)
#define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end());})

#define all(x) (x).begin(), (x).end()

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vb = vector<bool>;
using vs = vector<string>;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    const int DIM = 500;

    int N, M;
    cin >> N >> M;

    vi qual(N);
    for (int i = 0; i < N; i++) {
        cin >> qual[i];
    }

    qual.resize(N);

    vi order(N);
    for (int i = 0; i < N; i++) {
        order[i] = i;
    }
    sort(all(order), [&](int i, int j) {
        return qual[i] > qual[j];
    });

    vi ordered_qual = qual;
    sort(all(ordered_qual), greater<int>());

    vvi init(DIM, vi(DIM));
    vvi maxv(DIM, vi(DIM));
    vector<array<int, 2>> iupd(N);
    vector<array<int, 2>> mupd(N);

    vi diff(N + 1);
    for (int _i = 0; _i <= N; _i++) {
        if (_i % DIM == 0) {
            vi curv(DIM * DIM);
            for (int i = 0; i < N; i++) {
                curv[i] = diff[i] + (i ? curv[i-1] : 0);
                if (i % DIM == 0)
                    init[_i/DIM][i/DIM] = curv[i];
                maxv[_i/DIM][i/DIM] = max(curv[i], maxv[_i/DIM][i/DIM]);
            }

            WATCHC(curv);

            for (int ops = 0; ops < DIM && _i + ops < N; ops++) {
                int _j = _i + ops;
                int r = order[_j];
                int l = max(0, r - M + 1);

                iupd[_j] = { (l + DIM - 1) / DIM, r / DIM };
                mupd[_j] = { (l + DIM - 1) / DIM, r / DIM - 1 };

                auto incr_max = [&](int b) {
                    if (b < 0 || b >= DIM) return false;
                    int cmax = 0, nmax = 0;
                    for (int i = b * DIM; i < (b + 1) * DIM; i++) {
                        cmax = max(cmax, curv[i]);
                        if (l <= i && i <= r) curv[i]++;
                        nmax = max(nmax, curv[i]);
                    }
                    return nmax > cmax;
                };

                if (incr_max(mupd[_j][0] - 1)) mupd[_j][0]--;
                if (incr_max(mupd[_j][1] + 1)) mupd[_j][1]++;

                TRACE(
                cout << "Update " << _j << " increments [ " << l << ", " << r << " ]" << endl;
                cout << "It increases init for " << iupd[_j][0] << " through " << iupd[_j][1] << endl;
                cout << "It increases maxv for " << mupd[_j][0] << " through " << mupd[_j][1] << endl;
                )
            }
        }

        if (_i < N) {
            int r = order[_i];
            int l = max(0, r - M + 1);
            diff[l]++;
            diff[r+1]--;
        }
    }

    int S, ans = 0;
    cin >> S;
    for (int d = 0; d < S; d++) {
        int l, r, x;
        cin >> l >> r >> x;

        l--, r--;
        x ^= ans;

        WATCH(x);

        int LIM = upper_bound(all(ordered_qual), x, greater<int>())
                - ordered_qual.begin();

        vi diff_init(DIM + 1), diff_maxv(DIM + 1);
        for (int _i = LIM/DIM*DIM; _i < LIM; _i++) {
            if (iupd[_i][0] <= iupd[_i][1]) {
                diff_init[iupd[_i][0]]++;
                diff_init[iupd[_i][1]+1]--;
            }
            if (mupd[_i][0] <= mupd[_i][1]) {
                diff_maxv[mupd[_i][0]]++;
                diff_maxv[mupd[_i][1]+1]--;
            }
        }

        WATCHC(diff_init);
        WATCHC(diff_maxv);

        vi spec_init = init[LIM/DIM];
        vi spec_maxv = maxv[LIM/DIM];

        WATCHC(spec_init);
        WATCHC(spec_maxv);

        int cur_diff_init = 0, cur_diff_maxv = 0;
        for (int b = 0; b < DIM; b++) {
            cur_diff_init += diff_init[b];
            cur_diff_maxv += diff_maxv[b];
            spec_init[b] += cur_diff_init;
            spec_maxv[b] += cur_diff_maxv;
        }

        WATCHC(spec_init);
        WATCHC(spec_maxv);

        int res = 0;
        for (int b = 0; b < DIM; b++) {
            int bl = b * DIM, br = bl + DIM - 1;
            if (l <= bl && br <= r) {
                res = max(res, spec_maxv[b]);
            } else if (max(l, bl) <= min(r, br)) {
                int val = spec_init[b];
                for (int i = bl; i <= br; i++) {
                    if (l <= i && i <= r) res = max(res, val);
                    if (qual[i] >= x) val--;
                    if (i+M<N && qual[i+M] >= x) val++;
                }
            }
        }

        ans = M - res;
        cout << ans << "\n";
    }

    return 0;
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
#include <unordered_map>
#include <ctime>
typedef long long ll;
#define MAX 105
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
bool debug = 1;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
ll ln, lk, lm;

int a[200005], S;
vector<int> v, val[450];
int mn[450][900], sum[450][900];
int cal(int l, int r, int x) {
    int ans = inf;
    int now = lower_bound(v.begin(), v.end(), x) - v.begin();
    for(int i=0; i*S < n-m+1 && i*S<r; i++) {
        int st = i*S;
        int ed = min(st+S, n-m+1);
        k = lower_bound(val[i].begin(), val[i].end(), x) - val[i].begin()-1;
        if (k<0) {
            if (max(l,st) < min(r,ed)) ans = min(ans, now);
            continue;
        }
        if (l<=st && ed<=r) {
            ans = min(ans, now+mn[i][k]); now += sum[i][k];
        }
        else {
            if (max(l,st) < min(r,ed)) {
                for(int j=st;j<ed;j++) {
                    if(j>0 && a[j-1]<=x) now--;
                    if (a[j-1+m]<=x) now++;
                    if (l<=j && j<r) ans = min(ans, now);
                }
            }
        }
    }
    return ans;
}
int main() {
    scanf("%d%d", &n, &m);
    for(int i=0;i<n;i++) {
        scanf("%d", a+i);
    }
    REP(i,m-1) v.push_back(a[i]);
    sort(v.begin(), v.end());
    S = sqrt(n);
    for(int i=0;i*S <n-m+1; i++) {
        int st = i*S;
        int ed = min(st+S, n-m+1);
        for(int j=st;j<ed;j++) {
            if(j>0) val[i].push_back(a[j-1]);
            val[i].push_back(a[j+m-1]);
        }
        sort(val[i].begin(), val[i].end());
        val[i].resize(unique(val[i].begin(), val[i].end()) - val[i].begin());
        REP(k, val[i].size()) {
            mn[i][k] = inf;
            for(int j=st;j<ed;j++) {
                if (j>0 && a[j-1] <= val[i][k]) sum[i][k]--;
                if (a[j-1 + m] <= val[i][k]) sum[i][k]++;
                mn[i][k]=min(mn[i][k], sum[i][k]);
            }
        }
    }
    int q; scanf("%d", &q);
    for(int l,r,x,i=0,ans=0;i<q;i++) {
        scanf("%d%d%d", &l, &r, &x);
        x ^= ans;
        ans = cal(l-1,r,x);
        printf("%d\n", ans);
    }
    return 0;
}

#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <iostream>
using namespace std;

const int N = 2e5 + 7;
const int LOG = 19;

int n, m;
int a[N];
int global_i;

class Node {
public:
	int lazy, max_v;
	Node *left, *right;
	Node(Node* left, Node *right): left(left), right(right) {}
	Node(Node* o) {
		*this = *o;
	}
	Node() {}
	void push();
	Node* update();
};

Node pool[N * LOG];
int total = 0;
Node *root[N];

Node* Node::update() {
	this->max_v = max(left->max_v, right->max_v) + this->lazy;
	return this;
}

Node* insert(Node *root, int l, int r, int ql, int qr) {
	if (qr < l || r < ql) return root;
	Node* ret = new(pool + total++) Node(root);
	if (total >= N * LOG) {
		printf("%d\n", global_i);
		exit(0);
	}
	if (ql <= l && r <= qr) {
		ret->lazy++;
		ret->max_v++;
		return ret;
	}

	int mid = l + r >> 1;
	ret->left = insert(ret->left, l, mid, ql, qr);
	ret->right = insert(ret->right, mid + 1, r, ql, qr);
	return ret->update();
}

int ask(Node *root, int l, int r, int ql, int qr, int delta = 0) {
	if (qr < l || r < ql) return 0;
	if (ql <= l && r <= qr) {
		return root->max_v + delta;
	}
	int mid = l + r >> 1, retv = -1;
	retv = max(retv, ask(root->left, l, mid, ql, qr, delta + root->lazy));
	retv = max(retv, ask(root->right, mid + 1, r, ql, qr, delta + root->lazy));
	root->update();
	return retv;
}

Node* build(int l, int r) {
	Node* ret = new(pool + total++) Node(NULL, NULL);
	ret->lazy = ret->max_v = 0;
	if (l == r) return ret;
	int mid = l + r >> 1;
	ret->left = build(l, mid);
	if (r - mid == mid - l + 1) ret->right = ret->left;
	else ret->right = build(mid + 1, r);
	return ret;
}

int main() {
	vector<pair<int, int> > pairs;
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		pairs.push_back(make_pair(a[i], i));
	}
	sort(pairs.begin(), pairs.end());
 	root[n] = build(1, n);
	for (int i = n - 1; i >= 0; i--) {
		global_i = i;
		root[i] = insert(root[i + 1], 1, n, max(pairs[i].second - m + 1, 1), pairs[i].second);
	}
	int q, ans = 0; scanf("%d", &q);
	for (int i = 1; i <= q; i++) {
		int l, r, x;
		scanf("%d %d %d", &l, &r, &x);
		x ^= ans;
		int t = lower_bound(pairs.begin(), pairs.end(), make_pair(x, -1)) - pairs.begin();
		printf("%d\n", ans = (m - ask(root[t], 1, n, l, r)));
	}
	return 0;
}

#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>

using namespace std;

#define type(x) __typeof((x).begin())
#define foreach(i, x) for(type(x) i = (x).begin(); i != (x).end(); i++)

typedef long long ll;
typedef pair < int, int > ii;

const int inf = 1e9 + 333;
const ll linf = 1e18 + 333;

const int N = 2e5 + 5;
const int SIZE = 8000000;

class tree{ public:
    unsigned long long min : 18, l : 23, r : 23;
    tree() {
        min = l = r = 0;
    }
};

int n, m, q, cnt, x1, x2, t[N];
ii a[N];
tree p[SIZE];

int insert(int t, int l, int r) {

    if(l == r)
        return t + 1;

    int x = ++cnt;

    if(x1 <= l and r <= x2) {
        p[x] = p[t];
        p[x].min++;
        return x;
    }

    int m = l + r >> 1;

    p[x] = p[t];

    int add = p[x].min - min((l == m ? p[x].l : p[p[x].l].min), (m + 1 == r ? p[x].r : p[p[x].r].min));

    if(x1 <= m)
        p[x].l = insert(p[t].l, l, m);
    if(x2 > m)
        p[x].r = insert(p[t].r, m + 1, r);

    p[x].min = min((l == m ? p[x].l : p[p[x].l].min), (m + 1 == r ? p[x].r : p[p[x].r].min)) + add;

    return x;

}

int query(int x, int l, int r) {

    if(x2 < l or r < x1)
        return inf;

    if(l == r)
        return x;

    if(x1 <= l and r <= x2)
        return p[x].min;

    int m = l + r >> 1;

    int add = p[x].min - min((l == m ? p[x].l : p[p[x].l].min), (m + 1 == r ? p[x].r : p[p[x].r].min));

    return min(query(p[x].l, l, m), query(p[x].r, m + 1, r)) + add;

}

int main () {

    scanf("%d %d", &n, &m);

    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i].first);
        a[i].second = i;
    }

    sort(a + 1, a + n + 1);

    t[0] = ++cnt;

    for(int i = 1; i <= n; i++) {
        x1 = max(1, a[i].second - m + 1);
        x2 = min(a[i].second, n - m + 1);
        if(x1 <= x2)
            t[i] = insert(t[i - 1], 1, n - m + 1);
        else
            t[i] = t[i - 1];
    }

    scanf("%d", &q);

    int ans = 0, x;

    for(int qq = 0; qq < q; qq++) {
        scanf("%d %d %d", &x1, &x2, &x);
        x = lower_bound(a + 1, a + n + 1, ii(x ^ ans, 0)) - a - 1;
        ans = query(t[x], 1, n - m + 1);
        printf("%d\n", ans);
    }

    return 0;

}

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>

using namespace std;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define foreach(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); ++e)
typedef long long LL;
typedef pair<int, int> PII;

struct Node {
    unsigned x, y;
    unsigned char z;
    Node(unsigned val = 0, unsigned p = 0, unsigned le = 222222, unsigned ri = 222222) {
        x = val;
        x |= (p & 16383) << 18;
        y = (p >> 14);
        y |= le << 4;
        y |= (ri & 1023) << 22;
        z = ri >> 10;
    }
};

inline void getNodeValues(const Node &node, int &val, int &p, int &le, int &ri) {
    val = node.x & ((1 << 18) - 1);
    p = node.x >> 18;
    p |= (node.y & 15) << 14;
    le = node.y >> 4;
    le &= 262143;
    ri = node.y >> 22;
    ri |= (unsigned)node.z << 10;
}

Node nodes[4500000];
int st[200001];
int nc = 0;

int stBuild(int L, int R) {
    if (L == R) {
        nodes[nc] = Node();
        return nc++;
    }
    int mid = (L + R) >> 1;
    int le = stBuild(L, mid);
    int ri = stBuild(mid + 1, R);
    nodes[nc] = Node(0, 0, le, ri);
    return nc++;
}

int puu(int v, int pu, bool eq) {
    int val, p, le, ri;
    getNodeValues(nodes[v], val, p, le, ri);
    val += pu;
    if (!eq) p += pu;
    nodes[nc] = Node(val, p, le, ri);
    return nc++;
}

int stInc(int v, int L, int R, int l, int r, int pu = 0) {
    int val, p, le, ri;
    getNodeValues(nodes[v], val, p, le, ri);
    val += pu;
    if (L != R) p += pu;
    if (l == L && r == R) {
        if (L != R) ++p;
        ++val;
        nodes[nc] = Node(val, p, le, ri);
        return nc++;
    }
    int mid = (L + R) >> 1;
    if (l <= mid) le = stInc(le, L, mid, l, min(r, mid), p);
    else if (p) le = puu(le, p, L == mid);
    if (r > mid) ri = stInc(ri, mid + 1, R, max(l, mid + 1), r, p);
    else if (p) ri = puu(ri, p, L == mid);
    nodes[nc] = Node(min(nodes[le].x & ((1 << 18) - 1), nodes[ri].x & ((1 << 18) - 1)), 0, le, ri);
    return nc++;
}

int stGet(int v, int L, int R, int l, int r, int pu = 0) {
    int val, p, le, ri;
    getNodeValues(nodes[v], val, p, le, ri);
    val += pu;
    if (L != R) p += pu;
    if (l == L && r == R) {
        return val;
    }
    int mid = (L + R) >> 1;
    int best = 222222;
    if (l <= mid) best = stGet(le, L, mid, l, min(r, mid), p);
    if (r > mid) best = min(best, stGet(ri, mid + 1, R, max(l, mid + 1), r, p));
    return best;
}

int n, m;
int a[200000], ord[200000];

bool cmp(const int &lhs, const int &rhs) {
    return a[lhs] > a[rhs];
}

int main() {
    scanf("%d%d", &n, &m);
    st[n] = stBuild(0, n - m);
    REP(i, n) scanf("%d", a + i);
    REP(i, n) ord[i] = i;
    sort(ord, ord + n, cmp);
    for (int i = n - 1; i >= 0; --i) {
        int pos = ord[i];
        st[i] = stInc(st[i + 1], 0, n - m, max(0, pos - m + 1), min(n - m, pos));
    }
    int ans = 0, qcnt;
    scanf("%d", &qcnt);
    REP(query, qcnt) {
        int from, to, qual;
        scanf("%d%d%d", &from, &to, &qual), --from, --to, qual ^= ans;
        int lo = 0, hi = n, mid;
        while (lo < hi) {
            mid = (lo + hi) >> 1;
            if (a[ord[mid]] < qual)
                hi = mid;
            else
                lo = mid + 1;
        }
        ans = stGet(st[lo], 0, n - m, from, to);
        printf("%d\n", ans);
    }
    return 0;
}

#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <string>
#include <math.h>
#include <iostream>
using namespace std;
typedef unsigned long long llu;
const int MAX=8000000;
struct MU
{
    int a,pos;
}mu[200001];
bool cmp(MU a,MU b)
{
    return a.a<b.a;
}

llu st[MAX];
//rs[MAX],mx[MAX],cv[MAX];
int T[200001];
int tot;
llu merge(llu ls,llu rs,llu mx,llu cv)
{
    llu BASE=200001;
    llu BASE1=8000000;
    rs=ls*BASE1+rs;
    return (rs*BASE+mx)*BASE+cv;
}
void build(int l,int r,int tag)
{
    //ls[tag]=tot++;
    //rs[tag]=tot++;
    llu ls=tot++;
    llu rs=tot++;
    llu BASE=200001;
    if(tot>=MAX)while(1);
    //mx[tag]=0;
    //cv[tag]=0;
    llu mx=0;
    llu cv=0;

    st[tag]=merge(ls,rs,mx,cv);
    if(1+l==r)return ;
    int mid=l+r>>1;
    build(l,mid,st[tag]/BASE/BASE/8000000);
    build(mid,r,st[tag]/BASE/BASE%8000000);
}
void split(llu s,llu &ls,llu &rs,llu &mx,llu &cv)
{
    llu BASE=200001;
    cv=s%BASE;
    s/=BASE;
    mx=s%BASE;
    s/=BASE;
    rs=s%8000000;
    ls=s/8000000;
}
void update(int tag,int l,int r)
{
    if(l+1==r)return ;
    //printf("tag=%d ls=%d rs=%d\n",tag,ls[tag],rs[tag]);
    llu BASE=200001;
    llu ls,rs,mx,cv;
    //mx[tag]=mx[ls[tag]];
    split(st[tag],ls,rs,mx,cv);

    mx=st[ls]/BASE%BASE;

    llu rmx=st[rs]/BASE%BASE;

    if(rmx<mx)mx=rmx;

    //printf("mx[tag]=%d\n",mx[tag]);

    mx+=cv;

    st[tag]=merge(ls,rs,mx,cv);
}
void ins(int pr,int cr,int il,int ir,int l,int r)
{
    //ls[cr]=ls[pr];
    //rs[cr]=rs[pr];
    //mx[cr]=mx[pr];
    //cv[cr]=cv[pr];
    st[cr]=st[pr];
    llu BASE=200001;
    if(l<=il&&ir<=r)
    {
        //cv[cr]++;
        //mx[cr]++;
        st[cr]++;
        st[cr]+=BASE;
        return ;
    }
    int mid=il+ir>>1;

    if(r<=mid)
    {
        //ls[cr]=tot++;
        st[cr]=tot*8000000LL*BASE*BASE+st[cr]%(BASE*BASE*8000000);
        tot++;
        if(tot>=MAX)while(1);
        ins(st[pr]/8000000/BASE/BASE,tot-1,il,mid,l,r);
    }
    else if(l>=mid)
    {
        //rs[cr]=tot++;
        llu ls=st[cr]/8000000/BASE/BASE;
        st[cr]=tot*BASE*BASE+st[cr]%(BASE*BASE)+ls*8000000*BASE*BASE;
        tot++;
        if(tot>=MAX)while(1);
        ins(st[pr]/BASE/BASE%8000000,tot-1,mid,ir,l,r);
    }
    else
    {
        //ls[cr]=tot++;
        st[cr]=tot*8000000*BASE*BASE+st[cr]%(8000000*BASE*BASE);
        tot++;
        if(tot>=MAX)while(1);
        ins(st[pr]/8000000/BASE/BASE,tot-1,il,mid,l,mid);

        //rs[cr]=tot++;
        llu ls=st[cr]/8000000/BASE/BASE;
        st[cr]=tot*BASE*BASE+st[cr]%(BASE*BASE)+ls*8000000*BASE*BASE;
        tot++;
        if(tot>=MAX)while(1);
        ins(st[pr]/BASE/BASE%8000000,tot-1,mid,ir,mid,r);
    }

    update(cr,il,ir);
}
int bin(int low,int high,MU mu[],int aim)
{
    int ret=-1;
    if(aim<=mu[low].a)return -1;
    while(low<=high)
    {
        int mid=low+high>>1;
        if(mu[mid].a<aim)
        {
            ret=mid;
            low=mid+1;
        }
        else high=mid-1;
    }
    return ret;
}

int query(int tag,int il,int ir,int l,int r)
{
    //printf("tag=%d il=%d ir=%d l=%d r=%d min=%d cover=%d\n",tag,il,ir,l,r,mx[tag],cv[tag]);
    llu BASE=200001;
    if(l<=il&&ir<=r)return st[tag]/BASE%BASE; //mx[tag];
    int mid=il+ir>>1;
    int ret=0;
    if(r<=mid)ret=query(st[tag]/8000000/BASE/BASE,il,mid,l,r);
    else if(l>=mid)ret=query(st[tag]/BASE/BASE%8000000,mid,ir,l,r);
    else
    {
        ret=query(st[tag]/8000000/BASE/BASE,il,mid,l,mid);
      int b=query(st[tag]/BASE/BASE%8000000,mid,ir,mid,r);
        if(b<ret)ret=b;
    }
    return ret+st[tag]%BASE;
}
/*
void DFS(int l,int r,int tag)
{
    printf("tag=%d l=%d r=%d min=%d cover=%d ls=%d rs=%d\n",tag,l,r,mx[tag],cv[tag],ls[tag],rs[tag]);
    if(l+1==r)return ;
    int mid=l+r>>1;
    DFS(l,mid,ls[tag]);
    DFS(mid,r,rs[tag]);
}
*/
int main()
{
    int n,m;
    int i,j;
    int ioflag=0;
    //freopen("25.in","r",stdin);
    //freopen("25.out","w",stdout);
    while(cin>>n>>m)
    {
        for(i=1;i<=n;i++)
        {
            mu[i].pos=i;
            scanf("%d" ,&mu[i].a);
        }

        sort(mu+1,mu+1+n,cmp);
        //for(i=1;i<=n;i++)        {            printf("a=%d pos=%d\n",mu[i].a,mu[i].pos);        }
        tot=1;
        T[0]=0;
        build(1,n-m+2,T[0]);

        //puts("i=0");        DFS(1,n+1,T[0]);

        int l,r,p;
        for(i=1;i<=n;i++)
        {
            l=mu[i].pos-m+1;
            r=mu[i].pos+1;

            if(l<1)l=1;

            T[i]=tot++;

            ins(T[i-1],T[i],1,n+1-m+1,l,r);

            //cout<<"i="<<i<<endl;            DFS(1,n+1,T[i]);

        }

        int ans=0;
        int Q;
        cin>>Q;
        while(Q--)
        {
            scanf("%d%d%d",&l,&r,&p);
            p^=ans;

            //cout<<"op="<<p<<endl;

            p=bin(1,n,mu,p);

            //cout<<"p="<<p<<endl;            cout<<mx[p]<<endl;

            if(p==-1)ans=0;
            else ans=query(T[p],1,n+1-m+1,l,r+1);

            printf("%d\n",ans);
        }
    }
	return 0;
}

/*
7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3

*/

#include "iostream"
#include "algorithm"
#include "vector"
#include "set"
#include "map"
#include "cstring"
#include "string"
#include "vector"
#include "cassert"
#include "queue"
#include "cstdio"
#include "cstdlib"
#include "ctime"
#include "cmath"
#include "bitset"

using namespace std;

typedef long long ll;
typedef pair < int, int > ii;

const int N = 2e5 + 5;
const int K = 450;
const int T = N / K + 5;

int n, m, q;
int b[N], arr[N], add[N], p[N], addSmall[T + 2 * K];
ii a[N];
vector < int > v[T], mx[T], start[T];

int main () {

    scanf("%d %d", &n, &m);

    for(int i = 0; i < n; i++) {
        scanf("%d", &a[i].first);
        a[i].second = i;
        b[i] = a[i].first;
    }

    sort(a, a + n);

    for(int it = 0; it * K < n; it++) {

        int x = it * K, y = min((it + 1) * K - 1, n - 1);

        for(int i = 0; i < n; i += K)
            v[it].push_back(i);

        memset(add, 0, sizeof(add));

        for(int j = x; j <= y; j++) {
            int i = a[j].second;
            if(i - m + 1 > 0) {
                add[i - m]--;
                v[it].push_back(i - m + 1);
            }
            add[i]++;
            v[it].push_back(i + 1);
        }

        for(int i = n - 1; i >= 0; i--) {
            add[i] += add[i + 1];
            arr[i] += add[i];
        }

        v[it].push_back(0);
        v[it].push_back(n);

        sort(v[it].begin(), v[it].end());
        v[it].resize(unique(v[it].begin(), v[it].end()) - v[it].begin());

        mx[it].resize(v[it].size());
        start[it].resize(v[it].size());

        for(int i = 0; i + 1 < v[it].size(); i++) {
            for(int j = v[it][i]; j < v[it][i + 1]; j++) {
                //printf("i = %d j = %d val = %d\n", i, j, arr[j]);
                mx[it][i] = max(mx[it][i], arr[j]);
            }
            start[it][i] = arr[v[it][i]];
        }

    }

    int ans = 0;

    scanf("%d", &q);

    while(q--) {

        int l, r, x;

        scanf("%d %d %d", &l, &r, &x);

        l--, r--, x ^= ans;

        int id = lower_bound(a, a + n, ii(x, 0)) - a - 1;

        //printf("id = %d\n", id);

        if(n != 5)
            printf("l = %d r = %d x = %d id = %d\n", l, r, x, id);

        if(id < 0) {
            ans = 0;
            puts("0");
            continue;
        }

        vector < int > &ind = v[id / K], &first = start[id / K], vs = mx[id / K];

        for(int it = 0; it < ind.size(); it++)
            p[ind[it]] = it;

        memset(addSmall, 0, sizeof(addSmall));

        int j = min(n - 1, (id / K + 1) * K - 1);

        while(j > id) {
            int i = a[j].second;
            //printf("p[...] = %d\n", p[max(0, i - m + 1)]);
            addSmall[p[max(0, i - m + 1)]]--;
            if(p[i + 1] < vs.size()) {
                //printf("p[i + 1] = %d\n", p[i + 1]);
                addSmall[p[i + 1]]++;
            }
            j--;
        }

        for(int i = 0; i < vs.size(); i++) {
            addSmall[i] += i ? addSmall[i - 1] : 0;
            vs[i] += addSmall[i];
            //printf("vs[%d] = %d ind = %d\n", i, vs[i], ind[i]);
        }

        ans = 1e9;

        for(int it = 0; it + 1 < ind.size(); it++) {
            int L = ind[it], R = ind[it + 1] - 1;
            if(R < l or L > r)
                continue;
            if(l <= L and R <= r) {
                ans = min(ans, vs[it]);
                continue;
            }
            int cur = first[it] + addSmall[it];
            for(int i = L; i <= R; i++) {
                if(l <= i and i <= r)
                    ans = min(ans, cur);
                cur -= b[i] < x;
                cur += b[i + m] < x;
            }
        }

        printf("%d\n", ans);

    }

    return 0;

}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
using namespace std;

const int N = 200010;
#define x first
#define y second
unsigned long long a[N * 40];
const int b18 = (1 << 18) - 1;
const int b23 = (1 << 23) - 1;
int cnt;

void update(int &x, int y, int l, int r, int L, int R) {
	x = ++cnt;
	while (x >= N * 40) {
		exit(0);
	}
	int rc = a[y] & b23;
	int lc = (a[y] >> 23) & b23;
	unsigned long long mx = a[y] >> 46;
	if (l == L && r == R) {
		mx++;
		a[x] = (mx << 46) | ((unsigned long long)lc << 23) | rc;
	    return;
	}

    int mid = (l + r) >> 1;
    int lazy = mx - max(a[lc] >> 46, a[rc] >> 46);
	if (R <= mid) update(lc, lc, l, mid, L, R);
	else if (L > mid) update(rc, rc, mid + 1, r, L, R);
	else {
		update(lc, lc, l, mid, L, mid);
		update(rc, rc, mid + 1, r, mid + 1, R);
	}

	mx = max(a[lc] >> 46, a[rc] >> 46);
	mx += lazy;
	a[x] = (mx << 46) | ((unsigned long long)lc << 23) | (unsigned long long)rc;
}

int query(int x, int l, int r, int L, int R) {
	int rc = a[x] & b23;
	int lc = (a[x] >> 23) & b23;
	int mx = a[x] >> 46;
	if (l == L && R == r) {
		return mx;
	}

	int mid = (l + r) >> 1;
	int lazy = mx - max(a[lc] >> 46, a[rc] >> 46);
	if (R <= mid) return lazy + query(lc, l, mid, L, R);
	if (L > mid) return lazy + query(rc, mid + 1, r, L, R);
	return lazy + max(query(lc, l, mid, L, mid), query(rc, mid + 1, r, mid + 1, R));
}

pair<int, int> pi[N];
int root[N];

int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
	    scanf("%d", &pi[i].x);
		pi[i].y = i;
	}

	sort(pi + 1, pi + n + 1);
	for (int i = n; i >= 1; i--) {
		int l = max(1, pi[i].y - m + 1);
		int r = min(n - m + 1, pi[i].y);
		update(root[i], root[i + 1], 1, n - m + 1, l, r);
	}

	int q;
	scanf("%d", &q);
	int ans = 0;
	while (q--) {
		int l, r, x;
		scanf("%d%d%d", &l, &r, &x);
		x ^= ans;
		if (x <= pi[1].x) {
			ans = 0;
		} else if (x > pi[n].x) {
			ans = m;
		} else {
			int lt = 1, rt = n, mid, pos;
			while (lt <= rt) {
				mid = (lt + rt) >> 1;
				if (pi[mid].x >= x) {
					pos = mid;
					rt = mid - 1;
				} else lt = mid + 1;
			}

			ans = m - query(root[pos], 1, n - m + 1, l, r);
		}
		printf("%d\n", ans);
	}

	return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <bitset>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define FI first
#define SE second
using namespace std;
typedef long long LL;
const int M = 500;
const int N = 200005;
pair <int, int> a[N];
int mi[M][M], fi[M][M];
int b[N], v[N], tv[N], vl[N], vr[N];
int tmi[M], tfi[M];
int n, m;

inline int start(int x) {
    return x * M;
}

inline int end(int x) {
    return min(n, (x + 1) * M) - 1;
}

void update(int x) {
    int l = max(0, a[x].SE - m + 1);
    int r = a[x].SE;
    int L = l / M, R = r / M;
    ++tfi[L + (l != start(L))];
    --tfi[R + 1];
    ++L; --R;
    if(L > R) return;
    ++tmi[L]; --tmi[R + 1];
}

int brute(int l, int r, int q) {
    int val = tfi[l / M], ret = INF;
    for(int i = start(l / M); i <= r; ++i) {
        if(i >= l) ret = min(ret, val);
        if(b[i] < q) --val;
        if(b[i + m] < q) ++val;
    }
    return ret;
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 0; i < n; ++i) {
        scanf("%d", b + i);
        a[i] = make_pair(b[i], i);
    }
    sort(a, a + n);
    int bkcnt = (n - 1) / M;
    for(int i = 0; i <= bkcnt; ++i) {
        for(int j = start(i); j <= end(i); ++j) {
            ++v[min(0, a[j].SE - m + 1)];
            ++v[a[j].SE + 1];
        }
        for(int j = 0; j < n; ++j) tv[j] = v[j];
        for(int j = 1; j < n; ++j) tv[j] += tv[j - 1];
        for(int j = 0; j <= bkcnt; ++j) {
            fi[i][j] = tv[start(j)];
            mi[i][j] = *min_element(tv + start(j), tv + end(j) + 1);
        }
    }
    for(int i = 0; i <= bkcnt; ++i) {
        int add = 0;
        for(int j = start(i); j <= end(i); ++j) v[j] = 0;
        for(int j = 0; j < n; ++j) {
            int l = min(0, a[j].SE - m + 1), r = a[j].SE;
            int L = l / M, R = r / M;
            if(L < i && i < R) ++add;
            if(L == R && L == i) {
                for(int k = l; k <= r; ++k) ++v[k];
                vl[j] = vr[j] = *min_element(v + start(i), v + end(i) + 1) + add;
                continue;
            }
            if(L == i) {
                for(int k = l; k <= end(i); ++k) ++v[k];
                vl[j] = *min_element(v + start(i), v + end(i) + 1) + add;
            }
            if(R == i) {
                for(int k = start(i); k <= r; ++k) ++v[k];
                vr[j] = *min_element(v + start(i), v + end(i) + 1) + add;
            }
        }
    }
    int q, ans = 0;
    scanf("%d", &q);
    while(q--) {
        int l, r, x;
        scanf("%d%d%d", &l, &r, &x);
        x ^= ans; --l; --r;
        int p = lower_bound(a, a + n, make_pair(x, -1)) - a - 1;
        if(p < 0) {
            printf("%d\n", ans = 0);
            continue;
        }
        int idx = p / M;
        memset(tmi, 0, sizeof(tmi));
        memset(tfi, 0, sizeof(tfi));
        for(int i = idx * M; i <= p; ++i) update(i);
        for(int i = 1; i <= bkcnt; ++i) {
            tmi[i] += tmi[i - 1];
            tfi[i] += tfi[i - 1];
        }
        if(idx) {
            for(int i = 0; i <= bkcnt; ++i) {
                tmi[i] += mi[idx - 1][i];
                tfi[i] += fi[idx - 1][i];
            }
        }
        for(int i = idx * M; i <= p; ++i) {
            int L = min(0, a[i].SE - m + 1) / M;
            int R = a[i].SE / M;
            tmi[L] = vl[i]; tmi[R] = vr[i];
        }
        ans = INF;
        int L = l / M, R = r / M;
        for(int i = L + 1; i < R; ++i) ans = min(ans, tmi[i]);
        ans = min(ans, brute(l, min(r, end(L)), x));
        if(L ^ R) ans = min(ans, brute(start(R), r, x));
        printf("%d\n", ans);
    }
    return 0;
}

#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <ctime>
#include <cmath>
#define fi first
#define se second
#define PA pair<int,int>
#define VI vector<int>
#define VP vector<PA >
#define mk(x,y) make_pair(x,y)
#define int64 long long
#define N 200010
#define M 500
#define For(i,x,y) for (i=x;i<=y;i++)
using namespace std;
int i,j,k,n,m,q,an;
int a[N],ma[N][2],zhi[M][M],tma[M],ge[N],id[N],cov[M],L[N],R[N],fu[M][M]
	,yi[M][M];
inline bool cc1(const int &A,const int &B) {
	return a[A]<a[B];
}
inline int get(int x) {
	return x/M+1;
}
inline int xia(int x) {
	return (x/M+1)*M-1;
}
inline int qian(int x) {
	return x/M*M;
}
inline void pre() {
	For(i,0,n) id[i]=i;
	sort(id,id+n+1,cc1);
	For(i,0,n) {
		int A=id[i];
		int wei=get(i);
		if (i%M==0) {
			For(j,0,M-1) {
				zhi[wei][j]=tma[j];
				fu[wei][j]=cov[j];
				yi[wei][j]=ge[j*M];
			}
		}
		L[i]=R[i]=0;
		int f=0;
		for (j=max(0,A-m+1);j<=A;) {
			int _=get(j);
			if (j%M==0&&j+M-1<=A) {
				cov[_]++;
				f=1;
				if (!L[i]) L[i]=_;
				R[i]=_;
				j+=M;
			} else {
				ge[j]++;
				tma[_]=max(tma[_],ge[j]);
				ma[i][f]=max(ma[i][f],ge[j]);
				j++;
			}
		}
	}
}
inline int find(int x) {
	int l=0,r=n,mid;
	for (;l<=r;) {
		mid=(l+r)/2;
		if (a[id[mid]]<=x) l=mid+1;
		else r=mid-1;
	}
	return l-1;
}
inline void work(int l,int r,int x) {
	int shu=find(x),i,j;
	int wei=get(shu);
	int zuo=get(l)+1,you=get(r)-1;
	an=0;
	For(i,0,M-1) tma[i]=0;
	For(i,qian(shu),shu) {
		if (!L[i]) continue;
		tma[L[i]]++,tma[R[i]+1]--;
	}
	For(i,1,M-1) tma[i]+=tma[i-1];
	For(i,0,M-1) tma[i]+=fu[wei][i];
	For(i,qian(shu),shu) {
		int A=id[i];
		int le=max(0,A-m+1),ri=A;
		if (get(le)>=zuo&&get(le)<=you) an=max(an,ma[i][0]+tma[i]);
		if (get(ri)>=zuo&&get(ri)<=you) an=max(an,ma[i][1]+tma[i]);
	}
	For(i,zuo,you) an=max(an,tma[i]+zhi[wei][i]);
	{
		int s; j=qian(l);
		s=yi[wei][get(j)]+tma[get(j)];
		For(i,qian(shu),shu) {
			int A=id[i];
			int le=max(0,A-m+1),ri=A;
			if (le<=j&&j<=ri) s++;
		}
		j+=m;
		For(i,qian(l),xia(l)) {
			for (;j<=n&&i+m-1>=j;j++) s+=a[j]<=x;
			if (i>=l&&i<=r) an=max(an,s);
			s-=a[i]<=x;
		}
	}
	{
		int s; j=qian(r);
		s=yi[wei][get(j)]+tma[get(j)];
		For(i,qian(shu),shu) {
			int A=id[i];
			int le=max(0,A-m+1),ri=A;
			if (le<=j&&j<=ri) s++;
		}
		j+=m;
		For(i,qian(r),xia(r)) {
			for (;j<=n&&i+m-1>=j;j++) s+=a[j]<=x;
			if (i>=l&&i<=r) an=max(an,s);
			s-=a[i]<=x;
		}
	}
	an=m-an;
	printf("%d\n",an);
}
int main() {
	scanf("%d%d",&n,&m); n--;
	For(i,0,n) scanf("%d",&a[i]),a[i]*=-1;
	pre();
	scanf("%d",&q);
	For(i,1,q) {
		int l,r,x;
		scanf("%d%d%d",&l,&r,&x);
		x^=an; l--,r--;
		work(l,r,-x);
	}
	//for(;;);
	return 0;
}

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

#define RG register
const int maxN=402000;
const int maxB=510;
const int inf=2000000000;

int n,m,bcnt;
int Seq[maxN],Srt[maxN],B[maxN],Bl[maxN],Br[maxN];
int Label[maxN],Val[maxN];
int Lkey[maxB][maxB],Mn[maxB][maxB],Lmn[maxN],Rmn[maxN];
int Rgmn[maxN],Sm[maxN],Lft[maxN];
pair<int,int> Pr[maxN];

int Query(int l,int r,int x);
int main(){
    //freopen("in","r",stdin);
    RG int i,j,l,r;
    scanf("%d%d",&n,&m);
    for (i=1;i<=n;++i) scanf("%d",&Seq[i]),Pr[i]=make_pair(Seq[i],i);
    sort(&Pr[1],&Pr[n+1]);
    for (i=1;i<=n;++i) Srt[i]=Pr[i].first;Srt[0]=-inf;

    int Block=sqrt(n);
    bcnt=(n-1)/Block+1;
    for (i=1;i<=n;++i){
        B[i]=(i-1)/Block+1;Br[B[i]]=i;
        if (!Bl[B[i]]) Bl[B[i]]=i;
    }

    for (i=1;i<=n;++i){
        l=max(Pr[i].second-m+1,1);r=Pr[i].second;
        if (B[l]==B[r]) for (j=l;j<=r;++j) ++Val[j];
        else{
            for (j=l;j<=Br[B[l]];++j) ++Val[j];
            for (j=Bl[B[r]];j<=r;++j) ++Val[j];
            for (j=B[l]+1;j<B[r];++j) ++Label[j];
        }
        Lmn[i]=inf;for (j=Bl[B[l]];j<=Br[B[l]];++j) Lmn[i]=min(Lmn[i],Val[j]);
        Rmn[i]=inf;for (j=Bl[B[r]];j<=Br[B[r]];++j) Rmn[i]=min(Rmn[i],Val[j]);
        if (B[i]!=B[i+1]){
            int b=B[i];
            for (j=1;j<=n;++j) Val[j]+=Label[B[j]];
            for (j=1;j<=bcnt;++j) Label[j]=0,Mn[b][j]=inf,Lkey[b][j]=Val[Bl[j]];
            for (j=1;j<=n;++j) Mn[b][B[j]]=min(Mn[b][B[j]],Val[j]);
        }
        //for (int j=1;j<=n;j++) cout<<Val[j]+Label[B[j]]<<" ";cout<<endl;
    }

    //for (int i=1;i<=n;i++) cout<<B[i]<<" ";cout<<endl;
    //for (int i=1;i<=n;i++) cout<<Pr[i].second<<" "<<Pr[i].first<<endl;
    int Q,lstans=0;scanf("%d",&Q);
    while (Q--){
        int nx;scanf("%d%d%d",&l,&r,&nx);nx^=lstans;
        int cl=1,cr=n,p=0;
        while (cl<=cr){
            int mid=(cl+cr)>>1;
            if (Pr[mid].first<=nx-1) p=mid,cl=mid+1;
            else cr=mid-1;
        }
        //cout<<"x:"<<x<<" "<<nx<<endl;
        if (p==0){
            printf("%d\n",lstans=0);continue;
        }
        int stb=B[p];
        for (i=1;i<=bcnt;++i) Sm[i]=0,Lft[i]=Lkey[stb-1][i],Rgmn[i]=Mn[stb-1][i];
        for (i=Bl[stb];i<=p;++i){
            int pl=max(Pr[i].second-m+1,1),pr=Pr[i].second;
            //cout<<"Add:"<<i<<" "<<pl<<" "<<pr<<endl;
            Rgmn[B[pl]]=Lmn[i];Rgmn[B[pr]]=Rmn[i];
            if (B[pl]<B[pr]) ++Sm[B[pl]+1],--Sm[B[pr]];
            if (pl<=Bl[B[l]]&&Bl[B[l]]<=pr) ++Lft[B[l]];
            if (pl<=Bl[B[r]]&&Bl[B[r]]<=pr&&B[l]!=B[r]) ++Lft[B[r]];
            //cout<<"L:";for (int j=1;j<=bcnt;j++) cout<<Lft[j]<<" ";cout<<endl;
        }
        for (i=1;i<=bcnt;++i) Sm[i]+=Sm[i-1],Rgmn[i]+=Sm[i],Lft[i]+=Sm[i];
        if (B[l]==B[r]) lstans=Query(l,r,nx-1);
        else{
            lstans=min(Query(l,Br[B[l]],nx-1),Query(Bl[B[r]],r,nx-1));
            for (int i=B[l]+1;i<B[r];i++) lstans=min(lstans,Rgmn[i]);
        }
        printf("%d\n",lstans);
    }
    return 0;
}
int Query(int l,int r,int x){
    int val=Lft[B[l]];//+Sm[B[l]];
    //cout<<"Q:"<<l<<" "<<r<<" "<<x<<" "<<val<<endl;
    for (int i=Bl[B[l]]+1;i<=l;i++) val+=(Seq[i+m-1]<=x)-(Seq[i-1]<=x);//,cout<<i<<" "<<val<<endl;
    //cout<<val<<endl;
    int ret=val;
    for (int i=l+1;i<=r;i++) val+=(Seq[i+m-1]<=x)-(Seq[i-1]<=x),ret=min(ret,val);//,cout<<i<<" "<<val<<endl;
    return ret;
}

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define BSIZE 500

int N,M,Q;
int a[200000];
int cid[200000];
int pInc[200000],pDec[200000];

bool cmp(int x,int y)
{
	return a[x]<a[y];
}

vector<int> spoints[500];
vector<int> first[500];
vector<int> best[500];
vector<int> last[500];
int tmp[2000];

int dif[200001];

void slow(int l,int r,int x)
{
	int ans = 1000000000;
	for(int i=l;i<=r;i++)
	{
		int cur = 0;
		for(int j=i;j<i+M;j++)
			if(a[j]<x)
				cur++;
		ans = min(ans,cur);
	}
	cout << ans << '\n';
}

int main()
{
	ios::sync_with_stdio(0);
	cin >> N >> M;
	for(int i=0;i<N;i++)
	{
		cin >> a[i];
		cid[i] = i;
	}
	sort(cid,cid+N,cmp);
	for(int i=0;i<N;i++)
	{
		spoints[i/BSIZE].push_back(max(0,cid[i]-M+1));
		spoints[i/BSIZE].push_back(cid[i]+1);
	}
	for(int i=0;i*BSIZE<N;i++)
	{
		for(int j=0;j<N;j+=BSIZE)
			spoints[i].push_back(j);
		spoints[i].push_back(N);
		sort(spoints[i].begin(),spoints[i].end());
		spoints[i].resize(distance(spoints[i].begin(),unique(spoints[i].begin(),spoints[i].end())));
	}
	for(int i=0;i<N;i++)
	{
		pInc[i] = lower_bound(spoints[i/BSIZE].begin(),spoints[i/BSIZE].end(),max(0,cid[i]-M+1))-spoints[i/BSIZE].begin();
		pDec[i] = lower_bound(spoints[i/BSIZE].begin(),spoints[i/BSIZE].end(),cid[i]+1)-spoints[i/BSIZE].begin();
	}
	int lb = ((N-1)/BSIZE);
	for(int k=0;k<spoints[lb].size();k++)
	{
		best[lb].push_back(0), first[lb].push_back(0), last[lb].push_back(0);
	}
	for(int i=N-1;i>0;i--)
	{
		dif[max(0,cid[i]-M+1)]++, dif[cid[i]+1]--;
		if(i%BSIZE == 0)
		{
			int b = (i/BSIZE)-1;
			int v = 0;
			int j = 0;
			int mx = 0;
			for(int k=0;k<=N;k++)
			{
				if(k<N)
					v += dif[k];
				if(k == spoints[b][j])
				{
					first[b].push_back(v);
					if(j)
					{
						last[b].push_back(v-dif[k]);
						best[b].push_back(mx);
					}
					mx = v;
					j++;
				}
				else mx = max(mx,v);
			}
/*			for(int k=0;k<spoints[b].size();k++)
				cout << spoints[b][k] << ' ';
			cout << '\n';
			for(int k=0;k<best[b].size();k++)
				cout << best[b][k] << ' ';
			cout << "\n\n";
*/
		}
	}
	cin >> Q;
	int l,r,x;
	int ans;
	for(int i=0;i<Q;i++)
	{
		cin >> l >> r >> x;
		if(i) x ^= ans;
		l--,r--;
		int b = 0;
		while((b+1)*BSIZE<N && a[cid[(b+1)*BSIZE]] <= x)
			b++;
		//cout << b << '\n';
		for(int j=0;j<spoints[b].size();j++)
			tmp[j] = 0;
		for(int k=min(N,(b+1)*BSIZE)-1;k>=0 && a[cid[k]] >= x;k--)
			tmp[pInc[k]]++,tmp[pDec[k]]--;
		int lBlock = lower_bound(spoints[b].begin(),spoints[b].end(),l)-spoints[b].begin();
		ans = 0;
		int v = first[b][lBlock];
		for(int j=0;j<=lBlock;j++)
			v += tmp[j];
		for(int j=spoints[b][lBlock]-1;j>=l;j--)
		{
			if(a[j]>=x) v++;
			if(j+M<N && a[j+M]>=x) v--;
			ans = max(ans,v);
		}
		int rBlock = upper_bound(spoints[b].begin(),spoints[b].end(),r)-spoints[b].begin()-1;
		v = first[b][rBlock];
		for(int j=0;j<=rBlock;j++)
			v += tmp[j];
		for(int j=spoints[b][rBlock];j<=r;j++)
		{
			ans = max(ans,v);
			if(a[j]>=x) v--;
			if(j+M<N && a[j+M]>=x) v++;
		}
		int ps = 0;
		for(int j=0;j<spoints[b].size();j++)
		{
			ps += tmp[j];
			if(j>=lBlock && j<rBlock)
				ans = max(ans,ps+best[b][j]);
		}
		ans = M-ans;
		cout << ans << '\n';
		//slow(l,r,x);
	}
}

#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
#include <unordered_map>
#include <ctime>
typedef long long ll;
#define MAX 200002
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1<<30)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
bool debug = 1;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
ll ln, lk, lm;

#define ull unsigned long long
struct uz {
    ull ma:18,lc:23,rc:23;
};
const int MN = 200200*40;
uz tree[MN];
int cnt;
int getm(int rt) {
    return max(tree[tree[rt].lc].ma, tree[tree[rt].rc].ma);
}
int upt(int rt, int l, int r, int L, int R) {
    if (r<=L || R<=l) {
        return rt;
    }
    int cur = ++cnt;
    if(cur >= MN) {
        cout<<cur<<endl; exit(0);
    }
    if (L<=l && r<=R) {
        tree[cur] = tree[rt];
        tree[cur].ma++;
        return cur;
    }
    int mid = (l+r)>>1;
    int tg = tree[rt].ma - getm(rt);
    tree[cur].lc = upt(tree[rt].lc, l, mid, L, R);
    tree[cur].rc = upt(tree[rt].rc, mid, r, L, R);
    tree[cur].ma = getm(cur) + tg;
    return cur;
}
int qy(int cur, int l, int r, int L, int R) {
    if (R<=l || r<=L) return 0;
    if (L<=l && r<=R) {
        return tree[cur].ma;
    }
    int mid = (l+r)>>1;
    int tg = tree[cur].ma - getm(cur);
    int x = qy(tree[cur].lc, l, mid, L, R);
    int y = qy(tree[cur].rc, mid, r, L, R);
    return max(x,y) + tg;
}

pair<int, int> va[MAX];
int vv[MAX];
void cal() {
    int q, ans=0; scanf("%d", &q);
    for(int i=0,l,r,x;i<q;i++) {
        scanf("%d%d%d", &l, &r, &x);
        x = ans ^ x;
        auto it = upper_bound(va, va+n, make_pair(x,-1)) - va;
        if (it == n) ans = 0;
        else {
            int u = vv[n-it];
            ans = qy(u, 0, n, l-1, r);
        }
        ans = m-ans;
        printf("%d\n", ans);
    }
}
int main() {
    scanf("%d%d", &n, &m);
    for(int i=0;i<n;i++) {
        scanf("%d", &va[i].first); va[i].second = i+1;
    }
    sort(va, va+n);
    for(int i=n-1,j=1;i>=0;i--,j++) {
        int r = va[i].second;
        int l = max(r-(m-1), 1);
        vv[j] = upt(vv[j-1], 0, n, l-1, r);
    }
    cal();
    return 0;
}

#include<cstdio>
#include<algorithm>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define fi first
#define se second
#define ls t[v].lc,l,mid
#define ls2 t[u].lc,l,mid
#define rs t[v].rc,mid+1,r
#define rs2 t[u].rc,mid+1,r
typedef unsigned long long ull;typedef std::pair<int,int> pii;const int N=200005,M=N*40;
template<typename T>T min(const T&x,const T&y){return x<y?x:y;}
template<typename T>T max(const T&x,const T&y){return x>y?x:y;}
int n,m,q,ans,tot,rt[N];pii a[N];struct node{ull lc:23,rc:23,mx:18;}t[M];
int mdf(int v,int l,int r,int lx,int rx){
	int u=++tot;t[u]=t[v];if(l==lx&&r==rx)return++t[u].mx,u;int mid=l+r>>1,tag=t[u].mx-max(t[t[u].lc].mx,t[t[u].rc].mx);
	if(rx<=mid)t[u].lc=mdf(ls,lx,rx);else if(mid<lx)t[u].rc=mdf(rs,lx,rx);else t[u].lc=mdf(ls,lx,mid),t[u].rc=mdf(rs,mid+1,rx);
	t[u].mx=max(t[t[u].lc].mx,t[t[u].rc].mx)+tag;return u;
}
int qry(int u,int l,int r,int lx,int rx){
	if(l==lx&&r==rx)return t[u].mx;int mid=l+r>>1,tag=t[u].mx-max(t[t[u].lc].mx,t[t[u].rc].mx),res=0;
	if(rx<=mid)res=qry(ls2,lx,rx);else if(mid<lx)res=qry(rs2,lx,rx);else res=max(qry(ls2,lx,mid),qry(rs2,mid+1,rx));return res+tag;
}
int main(){
	scanf("%d%d",&n,&m);rep(i,1,n)scanf("%d",&a[i].fi),a[i].fi=-a[i].fi,a[i].se=i;std::sort(a+1,a+1+n);
	for(int i=1;i<=n;++i)rt[i]=mdf(rt[i-1],1,n-m+1,max(1,a[i].se-m+1),min(a[i].se,n-m+1));scanf("%d",&q);
	for(int i=1;i<=q;++i){
		int l,r,x;scanf("%d%d%d",&l,&r,&x);x^=ans;
		int pos=std::upper_bound(a+1,a+1+n,std::make_pair(-x,n+1))-a-1;
		printf("%d\n",ans=m-qry(rt[pos],1,n-m+1,l,r));
	}
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <bitset>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define FI first
#define SE second
using namespace std;
typedef long long LL;
const int M = 500;
const int N = 200005;
pair <int, int> a[N];
int mi[M][M], fi[M][M];
int b[N], v[N], tv[N], vl[N], vr[N];
int tmi[M], tfi[M];
int n, m;

inline int start(int x) {
    return x * M;
}

inline int end(int x) {
    return min(n, (x + 1) * M) - 1;
}

void update(int x) {
    int l = max(0, a[x].SE - m + 1);
    int r = a[x].SE;
    int L = l / M, R = r / M;
    ++tfi[L + (l != start(L))];
    --tfi[R + 1];
    ++L; --R;
    if(L > R) return;
    ++tmi[L]; --tmi[R + 1];
}

int brute(int l, int r, int q) {
    int val = tfi[l / M], ret = INF;
    for(int i = start(l / M); i <= r; ++i) {
        if(i >= l) ret = min(ret, val);
        if(b[i] < q) --val;
        if(b[i + m] < q) ++val;
    }
    return ret;
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 0; i < n; ++i) {
        scanf("%d", b + i);
        a[i] = make_pair(b[i], i);
    }
    sort(a, a + n);
    int bkcnt = (n - 1) / M;
    for(int i = 0; i <= bkcnt; ++i) {
        for(int j = start(i); j <= end(i); ++j) {
            ++v[max(0, a[j].SE - m + 1)];
            --v[a[j].SE + 1];
        }
        for(int j = 0; j < n; ++j) tv[j] = v[j];
        for(int j = 1; j < n; ++j) tv[j] += tv[j - 1];
        for(int j = 0; j <= bkcnt; ++j) {
            fi[i][j] = tv[start(j)];
            mi[i][j] = *min_element(tv + start(j), tv + end(j) + 1);
        }
    }
    for(int i = 0; i <= bkcnt; ++i) {
        int add = 0;
        for(int j = start(i); j <= end(i); ++j) v[j] = 0;
        for(int j = 0; j < n; ++j) {
            int l = max(0, a[j].SE - m + 1), r = a[j].SE;
            int L = l / M, R = r / M;
            if(L < i && i < R) ++add;
            if(L == R && L == i) {
                for(int k = l; k <= r; ++k) ++v[k];
                vl[j] = vr[j] = *min_element(v + start(i), v + end(i) + 1) + add;
                continue;
            }
            if(L == i) {
                for(int k = l; k <= end(i); ++k) ++v[k];
                vl[j] = *min_element(v + start(i), v + end(i) + 1) + add;
            }
            if(R == i) {
                for(int k = start(i); k <= r; ++k) ++v[k];
                vr[j] = *min_element(v + start(i), v + end(i) + 1) + add;
            }
        }
    }
    int q, ans = 0;
    scanf("%d", &q);
    while(q--) {
        int l, r, x;
        scanf("%d%d%d", &l, &r, &x);
        x ^= ans; --l; --r;
        int p = lower_bound(a, a + n, make_pair(x, -1)) - a - 1;
        if(p < 0) {
            printf("%d\n", ans = 0);
            continue;
        }
        int idx = p / M;
        memset(tmi, 0, sizeof(tmi));
        memset(tfi, 0, sizeof(tfi));
        for(int i = idx * M; i <= p; ++i) update(i);
        for(int i = 1; i <= bkcnt; ++i) {
            tmi[i] += tmi[i - 1];
            tfi[i] += tfi[i - 1];
        }
        if(idx) {
            for(int i = 0; i <= bkcnt; ++i) {
                tmi[i] += mi[idx - 1][i];
                tfi[i] += fi[idx - 1][i];
            }
        }
        for(int i = idx * M; i <= p; ++i) {
            int L = max(0, a[i].SE - m + 1) / M;
            int R = a[i].SE / M;
            tmi[L] = vl[i]; tmi[R] = vr[i];
        }
        ans = INF;
        int L = l / M, R = r / M;
        for(int i = L + 1; i < R; ++i) ans = min(ans, tmi[i]);
        ans = min(ans, brute(l, min(r, end(L)), x));
        if(L ^ R) ans = min(ans, brute(start(R), r, x));
        printf("%d\n", ans);
    }
    return 0;
}

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<utility>
#define M 19
#define N 200010
#define B (N*2)
#define inf 1023456789
using namespace std;
//sum*(2N)^2+min*(2N)+lc
//offset=N
long long tr[M][N*2];
int buffer[2][N*2],a[N],p,mlen;
int nxt[N];
char single[N];
const long long zero=1LL*N*B*B+1LL*N*B+N;
bool cmp(int i,int j){
	return a[abs(i)]<a[abs(j)];
}
void build(int n,int m){
	int i,l,r,md,nid,sum,mn,lsum,rsum,lmn,rmn,lc,rc,lv=0;
	bool final;
	for(i=0;i<n-m;i++){
		buffer[p][i*2]=i;
		buffer[p][i*2+1]=-(i+m);
	}
	sort(buffer[p],buffer[p]+(n-m)*2,cmp);
	nxt[0]=n-m;
	while(1){
		final=true;
		for(l=0;l<n-m;l=r){
			r=nxt[l];
			if(l+1==r){
				single[l]++;
				if(buffer[p][l*2]>=0){
					tr[lv][l*2]=zero-1LL*B*B-B;
					tr[lv][l*2+1]=zero;
				}
				else{
					tr[lv][l*2]=zero+1LL*B*B;
					tr[lv][l*2+1]=zero;
				}
				continue;
			}
			final=false;
			md=(l+r)/2;
			nxt[l]=md;
			nxt[md]=r;
			lc=l*2;
			rc=md*2;
			for(i=l*2;i<r*2;i++){
				if(buffer[p][i]>=0){
					nid=buffer[p][i];
				}
				else{
					nid=-buffer[p][i]-m;
				}
				if(nid<md){
					buffer[p^1][lc++]=buffer[p][i];
				}
				else{
					buffer[p^1][rc++]=buffer[p][i];
				}
				tr[lv][i]=zero+lc-1;
			}
		}
		if(final) break;
		p^=1;
		lv++;
	}
	if(n>=200000){
		printf("first part done\n");
		exit(0);
	}
	lv--;
	while(lv>=0){
		for(l=0;l<n-m;l=nxt[l]){
			if(single[l]>1){
				single[l]--;
				continue;
			}
			md=nxt[l];
			nxt[l]=r=nxt[md];
			if(l+1==r){
				continue;
			}
			for(i=l*2;i<r*2;i++){
				lc=tr[lv][i]%B-N;
				rc=md*2+(i-lc)-1;
				if(lc<l*2){
					lsum=lmn=0;
				}
				else{
					lsum=tr[lv+1][lc]/B/B-N;
					lmn=tr[lv+1][lc]/B%B-N;
				}
				if(i==lc){
					rsum=rmn=0;
				}
				else{
					rsum=tr[lv+1][rc]/B/B-N;
					rmn=tr[lv+1][rc]/B%B-N;
				}
				sum=lsum+rsum;
				mn=min(lsum+rmn,lmn);
				tr[lv][i]+=1LL*sum*B*B+1LL*mn*B;
			}
		}
		lv--;
	}
	memcpy(buffer[0],a,sizeof(int)*m);
	sort(buffer[0],buffer[0]+m);
	memcpy(buffer[1],a,sizeof(int)*(n-m));
	memcpy(buffer[1]+n-m,a+m,sizeof(int)*(n-m));
	sort(buffer[1],buffer[1]+(n-m)*2);
}
int qpsum(int lv,int l,int r,int qr,int i){
	if(i<l*2) return 0;
	if(lv>=M||i<0||i>=N*2){
		fprintf(stderr,"GGqsum\n");
		exit(0);
	}
	if(qr==r) return tr[lv][i]/B/B-N;
	int md=(l+r)/2,lc=tr[lv][i]%B-N,rc=md*2+i-lc-1;
	if(qr<=md) return qpsum(lv+1,l,md,qr,lc);
	else return qpsum(lv+1,l,md,md,lc) + qpsum(lv+1,md,r,qr,rc);
}
//first:sum second:min
//return value: (sum+N)*B+(min+N)
long long qry(int lv,int l,int r,int ql,int qr,int i){
	if(i<l*2) return zero/B;
	if(lv>=M||i<0||i>=N*2){
		fprintf(stderr,"GGqry\n");
		exit(0);
	}
	if(l==ql&&r==qr){
		return tr[lv][i]/B;
	}
	int md=(l+r)/2,lc=tr[lv][i]%B-N,rc=md*2+i-lc-1;
	if(qr<=md){
		return qry(lv+1,l,md,ql,qr,lc);
	}
	else if(ql>=md){
		return qry(lv+1,md,r,ql,qr,rc);
	}
	else{
		long long lres=qry(lv+1,l,md,ql,md,lc);
		long long rres=qry(lv+1,md,r,md,qr,rc);
		return (lres/B+rres/B-N)*B+min(lres%B,lres/B+rres%B-N);
	}
}
int cal(int l,int r,int n,int m,int q){
	l--;
	r--;
	int base=lower_bound(buffer[0],buffer[0]+m,q)-buffer[0];
	int i=(upper_bound(buffer[1],buffer[1]+(n-m)*2,q-1)-buffer[1])-1;
	return base+(l?qpsum(0,0,n-m,l,i):0)+(l<r?qry(0,0,n-m,l,r,i)%B-N:0);
}
int main(){
	int n,m,q,ans=0,l,r,x,i;
	scanf("%d%d",&n,&m);
	for(i=0;i<n;i++) scanf("%d",&a[i]);
	build(n,m);
	if(n>=200000){
		printf("build done\n");
		exit(0);
	}
	scanf("%d",&q);
	while(q--){
		scanf("%d%d%d",&l,&r,&x);
		x^=ans;
		ans=cal(l,r,n,m,x);
		printf("%d\n",ans);
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

namespace TYC
{
	typedef long long ll;
	typedef pair<int, int> pii;

	const int N = 2e5 + 5, M = 450, INF = 0x3f3f3f3f;

	int n, m, tot, block, A[N], BV[M][M], FV[M][M], Tag[M], Mx[M], W[N], Bel[N], Begin[M], End[M];
	pii V[N], EX[N][2];

	inline int read()
	{
		int x = 0, f = 0, ch = getchar();
		while (!isdigit(ch))
			f |= (ch == '-'), ch = getchar();
		while (isdigit(ch))
			x = x * 10 + ch - '0', ch = getchar();
		return f ? -x : x;
	}

	void init()
	{
		block = sqrt(n);
		for (int i = 1; i <= n; i++)
			Bel[i] = (i - 1) / block + 1;
		tot = Bel[n];
		for (int i = 1; i <= tot; i++)
		{
			Begin[i] = End[i - 1] + 1;
			End[i] = min(n, End[i - 1] + block);
		}

		sort(V + 1, V + 1 + n, greater<pii>());
		for (int i = 1; i <= n; i++)
		{
			if (Begin[Bel[i]] == i)
			{
				for (int j = 1; j <= n; j++)
					W[j] += Tag[Bel[j]];
				for (int j = 1, p = Bel[i]; j <= tot; j++)
				{
					Mx[j] += Tag[j];
					Tag[j] = 0;
					BV[p][j] = Mx[j];
					FV[p][j] = W[Begin[j]];
				}
			}
			int r = V[i].second, l = max(1, r - m + 1), c = 0;
			for (int j = l; j <= r; j++)
			{
				int p = Bel[j];
				if (j == Begin[p] && End[p] <= r)
					Tag[p]++, j = End[p];
				else
				{
					if (!c || EX[i][c - 1].first != p)
						EX[i][c++].first = p;
					Mx[p] = max(Mx[p], ++W[j]);
				}
			}
			for (int j = 0; j < c; j++)
			   EX[i][j].second = Mx[EX[i][j].first];
		}
	}

	void work()
	{
		n = read(), m = read();
		for (int i = 1; i <= n; i++)
			V[i] = pii(A[i] = read(), i);
		init();
		int q = read(), vl, vr, x, real_val, last_ans = 0;
		while (q--)
		{
			vl = read(), vr = read(), real_val = read() ^ last_ans;
			x = upper_bound(V + 1, V + 1 + n, pii(real_val, -INF), greater<pii>()) - V - 1;
			if (!x)
			{
				printf("%d\n", last_ans = m);
				continue;
			}
			int k = Bel[x];
			memset(Tag, 0, sizeof(int[tot + 1]));
			memcpy(Mx, BV[k], sizeof(int[tot + 1]));
			for (int i = Begin[k]; i <= x; i++)
			{
				int r = V[i].second, l = max(1, r - m + 1);
				if (Bel[l] != Bel[r])
				{
					if (l != Begin[Bel[l]])
						l = End[Bel[l]] + 1;
					Tag[Bel[l]]++;
					Tag[Bel[r + 1]]--;
				}
				for (int j = 0; j < 2; j++)
					if (EX[i][j].first)
						Mx[EX[i][j].first] = EX[i][j].second;
			}
			for (int i = 2; i <= tot; i++)
				Tag[i] += Tag[i - 1];
			int ans = 0, val = -1;
			for (int i = vl; i <= vr; i++)
			{
				int p = Bel[i];
				if (i == Begin[p] && End[p] <= vr)
				{
					ans = max(ans, Mx[p]);
					val = -1;
					i = End[p];
				}
				else
				{
					if (val == -1)
					{
						val = FV[k][p];
						int id = Begin[p];
						for (int j = Begin[k]; j <= x; j++)
							val += max(1, V[j].second - m + 1) <= id && id <= V[j].second;
						while (id != i)
						{
							val = val - (A[id] >= real_val) + (A[id + m] >= real_val);
							id++;
						}
					}
					ans = max(ans, val);
					val = val - (A[i] >= real_val) + (A[i + m] >= real_val);
				}
			}
			printf("%d\n", last_ans = m - ans);
		}
	}
}

int main()
{
	TYC::work();
	return 0;
}

#include<bits/stdc++.h>
#define maxn 200009
#define maxm 502
using namespace std;
struct data{
  int w,p;
}t[maxn];
struct Blo{
  int a[maxm],fr[maxm],tag[maxm];
}B[maxm];
int a[maxn],bl[maxn],L[maxn],R[maxn],w[maxn],fr[maxn],tag[maxn],zx[maxn],f[maxn][2],blo,ans=0,n,m,qes;
inline bool cmp(const data &A,const data &B){return A.w<B.w;}
inline int Find(int x){
  int l=1,r=n,p=0;
  while(l<=r){
    int mid=(l+r)>>1;
    if(t[mid].w<=x) l=mid+1,p=mid;
    else r=mid-1;
  }
  return p;
}
inline void work(int l,int r,int x){
  ans=1e9;int p=Find(x-1);
  if(!p){puts("0"),ans=0;return;}
  for(int i=1;i<=bl[n];i++) tag[i]=0,zx[i]=B[bl[p]-1].a[i],fr[i]=B[bl[p]-1].fr[i];
  for(int i=L[bl[p]];i<=p;i++){
    int hl=max(1,t[i].p+1-m),hr=t[i].p;
    if(bl[hl]!=bl[hr]) tag[bl[hl]+1]++,tag[bl[hr]]--;
    zx[bl[hl]]=f[i][0],zx[bl[hr]]=f[i][1];
    if(hl==L[bl[hl]]) ++fr[bl[hl]];
    if(bl[hl]!=bl[hr]) ++fr[bl[hr]];
  }
  for(int i=1;i<=bl[n];i++) tag[i]+=tag[i-1];
  for(int i=1;i<=bl[n];i++) tag[i]+=B[bl[p]-1].tag[i];
  int sum=fr[bl[l]]+tag[bl[l]];
  for(int i=L[bl[l]];i<=R[bl[l]];i++){
    if(i>=l && i<=r) ans=min(ans,sum);
    if(a[i]<x) --sum;if(a[i+m]<x) ++sum;
  }
  for(int i=bl[l]+1;i<bl[r];i++) ans=min(ans,tag[i]+zx[i]);
  sum=fr[bl[r]]+tag[bl[r]];
  for(int i=L[bl[r]];i<=R[bl[r]];i++){
    if(i>=l && i<=r) ans=min(ans,sum);
    if(a[i]<x) --sum;if(a[i+m]<x) ++sum;
  }
  printf("%d\n",ans);
}
inline void rebuild(int x){
  zx[x]=1e9,fr[x]=w[L[x]];
  for(int i=L[x];i<=R[x];i++)
    zx[x]=min(zx[x],w[i]);
}
int main(){
  scanf("%d%d",&n,&m);blo=sqrt(n);
  for(int i=1;i<=n;i++)
  scanf("%d",a+i),t[i].p=i,t[i].w=a[i];
  sort(t+1,t+n+1,cmp);
  for(int i=1;i<=n;i++) bl[i]=(i-1)/blo+1,L[i]=(i-1)*blo+1,R[i]=min(i*blo,n);
  for(int i=1;i<=bl[n];i++){
    for(int j=L[i];j<=R[i];j++){
      int p=t[j].p,l=max(1,p+1-m),r=p;
      for(int k=l;k<=R[bl[l]];k++) ++w[k];
      if(bl[l]!=bl[r])
	for(int k=L[bl[r]];k<=r;k++) ++w[k];
      for(int k=bl[l]+1;k<bl[r];k++) tag[k]++;
      rebuild(bl[l]),rebuild(bl[r]);
      f[j][0]=zx[bl[l]],f[j][1]=zx[bl[r]];
    }
    for(int j=1;j<=bl[n];j++)
      B[i].a[j]=zx[j],B[i].fr[j]=fr[j],B[i].tag[j]=tag[j];
  }
  scanf("%d",&qes);
  for(int i=1,l,r,x;i<=qes;i++)
    scanf("%d%d%d",&l,&r,&x),work(l,r,x^ans);
  return 0;
}

#include<bits/stdc++.h>

#define x first
#define y second
#define pb push_back

using namespace std;
typedef long long INT;
typedef vector<int> VI;
typedef pair<int, int> pii;

const int NN=200500, L=500;
int n, m, a[NN];
VI v, val[L];
int st[L], ed[L];
int sum[NN/L][L<<1], mn[NN/L][L<<1];

int calc(int l, int r, int x){
	int ans=1e9;
	int now=lower_bound(v.begin(), v.end(), x)-v.begin();
	for(int i=0; i*L<n-m+1 && i<r; i++){
		int k=lower_bound(val[i].begin(), val[i].end(), x)-val[i].begin()-1;
		if(k<0){
			if(max(l, st[i])<min(r, ed[i])) ans=min(ans, now);
			continue;
		}

		if(l<=st[i] && ed[i]<=r) ans=min(ans, now+mn[i][k]);
		else if(max(l, st[i])<min(r, ed[i])){
			int cur=now;
			for(int j=st[i]; j<ed[i]; j++){
				if(j && a[j-1]<x) cur--;
				if(j+m<=n && a[j+m-1]<x) cur++;
				if(l<=j && j<r) ans=min(ans, cur);
			}
		}
		now+=sum[i][k];
	}
	return ans;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	cin.tie(0), cout.tie(0);

	cin >> n >> m;
	for(int i=0; i<n; i++) cin >> a[i];
	for(int i=0; i<m-1; i++) v.pb(a[i]);

	for(int i=0; i*L<n-m+1; i++){
		st[i]=i*L;
		ed[i]=min(st[i]+L, n-m+1);

		for(int j=st[i]; j<ed[i]; j++){
			if(j) val[i].pb(a[j-1]);
			if(j+m<=n) val[i].pb(a[j+m-1]);
		}

		sort(val[i].begin(), val[i].end());
		val[i].resize(unique(val[i].begin(), val[i].end())-val[i].begin());

		for(int k=0; k<val[i].size(); k++){
			mn[i][k]=1e9;
			for(int j=st[i]; j<ed[i]; j++){
				if(j && a[j-1]<=val[i][k]) sum[i][k]--;
				if(j+m<=n && a[j+m-1]<=val[i][k]) sum[i][k]++;
				mn[i][k]=min(mn[i][k], sum[i][k]);
			}
		}
	}

	int q, l, r, x, ans=0;
	for(cin >> q; q--; ){
		cin >> l >> r >> x;
		x^=ans;
		cout << (ans=calc(l-1, r, x)) << endl;
	}

	return 0;
}

#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
using ll = long long;
using db = long double;
using ull = unsigned long long;

const int N = 2e5 + 5, MOD = 1e9 + 7;

struct Node{
  ull mx:18,ls:23,rs:23;
} t[8 * 1000 * 1000];

int ptr, rt[2 * 1000 * 100 + 5];
pair<int, int> go[2 * 1000 * 100 + 5];
int l, r;
int upd(int node, int s, int e){
  if(s==e)
    return node+1;
  int ret = ++ptr;
  t[ret] = t[node];
  if(l <= s && e <= r){
    t[ret].mx++;
    return ret;
  }
  int md = (s + e) >> 1;
  int lMx = t[ret].ls, rMx = t[ret].rs;
  if(s != md)
    lMx = t[lMx].mx;
  if(e != md + 1)
    rMx = t[rMx].mx;
  int flr = t[ret].mx - max(lMx,rMx);
  if(l <= md){
    t[ret].ls = upd(t[node].ls,s,md);
  }
  if(md < r){
    t[ret].rs = upd(t[node].rs,md+1,e);
  }
  lMx = t[ret].ls, rMx = t[ret].rs;
  if(s != md)
    lMx = t[lMx].mx;
  if(e != md + 1)
    rMx = t[rMx].mx;

  t[ret].mx = max(lMx,rMx) + flr;
  return ret;
}
int qry(int node, int s, int e){
  if(s==e)
    return 0;
  if(l<=s&&e<=r)
    return t[node].mx;
  int md = (s+e)>>1;
  int lMx = t[node].ls, rMx = t[node].rs;
  if(s != md)
    lMx = t[lMx].mx;
  if(e != md + 1)
    rMx = t[rMx].mx;

  int ret = 0, flr = t[node].mx - max(lMx,rMx);
  if(l <= md){
    ret = max(ret, qry(t[node].ls,s,md));
  }
  if(md < r){
    ret = max(ret, qry(t[node].rs,md+1,e));
  }
  return ret + flr;
}

int main(){
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(0);cin.tie(0);
#endif // ONLINE_JUDGE
  int n, m, q;
  cin >> n >> m;
  vector<int> ord;
  for(int i = 1; i <= n; i++){
      cin >> go[i].first; go[i].second = i;
      go[i].first *= -1;
  }

  sort(go+1,go+n+1);

  for(int i = 1; i <= n; i++){
    l = max(1, go[i].second - m + 1);
    r = min(n-m+1,go[i].second);
    rt[i] = upd(rt[i-1], 1, n - m + 1);
  }
  cin >> q;
  int ans = 0, v;
  while(q--){
    cin >> l >> r >> v;
    v ^= ans;
    int idx = upper_bound(go + 1, go+ n + 1, make_pair(-v,INT_MAX)) - go - 1;
    cout << (ans = (m-qry(rt[idx], 1, n - m + 1))) << '\n';
  }

  return 0;
}

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define R register
#define MN 200000
#define MB 200
#define SIZE 1000
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int n,m,N,a[MN+5],L[MN+5],num,block[MN+5],mn[MB+5][SIZE*2+5],val[SIZE*2+5],rt[MN+5],cnt=0;
struct disc
{
	int*s,num;
	disc(int size=SIZE<<1){s=new int[size];num=0;}
	void Add(int x){s[++num]=x;}
	void Build(){sort(s+1,s+num+1);num=unique(s+1,s+num+1)-s-1;}
	int Calc(int x){return upper_bound(s+1,s+num+1,x)-s-1;}
}lb[MB+5],LB(MN+5);
struct Tree{int l,r,x;}T[MN*18+5];

void Init_Mn(int x)
{
	int l=(x-1)*SIZE+1,r=min(x*SIZE,n-m+1);
	memset(val,0,sizeof(val));
	for(R int i=l;i<=r;++i) lb[x].Add(a[i]),lb[x].Add(a[i+m-1]);
	lb[x].Build();int tot=lb[x].num;
	for(R int i=l;i<r;++i)
	{
		int A=lb[x].Calc(a[i]),B=lb[x].Calc(a[i+m-1]);
		for(R int i=A;i<=tot;++i) --val[i];
		for(R int i=B;i<=tot;++i) ++val[i];
		for(R int i=min(A,B);i<=tot;++i) mn[x][i]=min(mn[x][i],val[i]);
	}
}
int Modify(int x,int l,int r,int k,int v)
{
	int y=++cnt,mid=l+r>>1;T[y]=T[x];T[y].x+=v;
	if(l==r) return y;
	if(k<=mid) T[y].l=Modify(T[y].l,l,mid,k,v);
	else T[y].r=Modify(T[y].r,mid+1,r,k,v);
	return y;
}
int Query(int x,int l,int r,int k)
{
	if(k<l||!x) return 0;
	if(l==r) return T[x].x;
	int mid=l+r>>1;
	if(k<=mid) return Query(T[x].l,l,mid,k);
	else return T[T[x].l].x+Query(T[x].r,mid+1,r,k);
}
int Solve(int x,int v,int V)
{
	int l=(x-1)*SIZE+1,r=min(x*SIZE,n);
	int num=Query(rt[l+m-1],1,LB.num,v)-Query(rt[l-1],1,LB.num,v);
	return num+mn[x][lb[x].Calc(V)];
}
int main()
{
	n=read();m=read();N=n-m+1;
	for(R int i=1;i<=n;++i)a[i]=read(),LB.Add(i);
	for(R int i=1;i<=N;++i) block[i]=(i-1)/SIZE+1;
	for(R int i=1;i<=block[N];++i) Init_Mn(i);
	LB.Build();int tot=LB.num;
	for(R int i=1;i<=n;++i) rt[i]=Modify(rt[i-1],1,tot,LB.Calc(a[i]),1);
	for(int Q=read(),ans=0;Q;--Q)
	{
		int l=read(),r=read(),x=(read()^ans)-1;
		int nx=LB.Calc(x);ans=1e9;
		for(int i=block[l]+1;i<block[r];++i) ans=min(ans,Solve(i,nx,x));
		int L=min(r,block[l]*SIZE),lnum=Query(rt[L+m-1],1,tot,nx)-Query(rt[L-1],1,tot,nx);
		ans=min(ans,lnum);
		for(R int i=L-1;i>=l;--i) lnum-=(a[i+m]<=x),lnum+=(a[i]<=x),ans=min(ans,lnum);
		if(block[l]!=block[r])
		{
			int RR=(block[r]-1)*SIZE+1,rnum=Query(rt[RR+m-1],1,tot,nx)-Query(rt[RR-1],1,tot,nx);
			ans=min(ans,rnum);
			for(int j=RR+1;j<=r;++j) rnum-=(a[j-1]<=x),rnum+=(a[j+m-1]<=x),ans=min(ans,rnum);
		}
		printf("%d\n",ans);
	}
	return 0;
}

#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <iostream>
using namespace std;

const int N = 2e5 + 7;
const int LOG = 19;

int n, m;
int a[N];
int global_i;

class Node {
public:
	int lazy, max_v;
	Node *left, *right;
	Node(Node* left, Node *right): left(left), right(right) {}
	Node(Node* o) {
		*this = *o;
	}
	Node() {}
	void push();
	Node* update();
};

Node pool[N * LOG];
int total = 0;
Node *root[N];

Node* Node::update() {
	this->max_v = max(left->max_v, right->max_v) + this->lazy;
	return this;
}

Node* insert(Node *root, int l, int r, int ql, int qr) {
	if (qr < l || r < ql) return root;
	Node* ret = new(pool + total++) Node(root);
	if (total >= N * LOG) {
		printf("%d\n", global_i);
		exit(0);
	}
	if (ql <= l && r <= qr) {
		ret->lazy++;
		ret->max_v++;
		return ret;
	}

	int mid = l + r >> 1;
	ret->left = insert(ret->left, l, mid, ql, qr);
	ret->right = insert(ret->right, mid + 1, r, ql, qr);
	return ret->update();
}

int ask(Node *root, int l, int r, int ql, int qr, int delta = 0) {
	if (qr < l || r < ql) return 0;
	if (ql <= l && r <= qr) {
		return root->max_v + delta;
	}
	int mid = l + r >> 1, retv = -1;
	retv = max(retv, ask(root->left, l, mid, ql, qr, delta + root->lazy));
	retv = max(retv, ask(root->right, mid + 1, r, ql, qr, delta + root->lazy));
	root->update();
	return retv;
}

Node* build(int l, int r) {
	Node* ret = new(pool + total++) Node(NULL, NULL);
	ret->lazy = ret->max_v = 0;
	if (l == r) return ret;
	int mid = l + r >> 1;
	ret->left = build(l, mid);
	if (r - mid == mid - l + 1) ret->right = ret->left;
	else ret->right = build(mid + 1, r);
	return ret;
}

int main() {
	vector<pair<int, int> > pairs;
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		pairs.push_back(make_pair(a[i], i));
	}
	sort(pairs.begin(), pairs.end());
 	root[n] = build(1, n);
	for (int i = n - 1; i >= 0; i--) {
		global_i = i;
		root[i] = insert(root[i + 1], 1, n, max(pairs[i].second - m + 1, 1), pairs[i].second);
	}
	int q, ans = 0; scanf("%d", &q);
	for (int i = 1; i <= q; i++) {
		int l, r, x;
		scanf("%d %d %d", &l, &r, &x);
		x ^= ans;
		int t = lower_bound(pairs.begin(), pairs.end(), make_pair(x, -1)) - pairs.begin();
		printf("%d\n", ans = (m - ask(root[t], 1, n, l, r)));
	}
	return 0;
}

//
//  main.cpp
//  Codeforces Round #302 (Div. 1) E
//
//  Created by Scholar on 6/10/15.
//  Copyright (c) 2015 Scholar. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

#define lson t[o].ls
#define rson t[o].rs
#define lmax (l==mid?int(lson):int(t[lson].mx))
#define rmax (mid+1==r?int(rson):int(t[rson].mx))

using namespace std;

typedef unsigned long long ULL;

const int Maxn=2e5+10,INF=0x3f3f3f3f;
int n,m,s,ql,qr,tot,root[Maxn];
struct treenode{
    ULL ls:18,rs:23,mx:23;
}t[40*Maxn];
pair<int,int>a[Maxn];

int update(int l,int r,int p){
    if(l==r)return p+1;
    int o=++tot;
    t[o]=t[p];
    if(ql<=l&&r<=qr){
        t[o].mx++;
        return o;
    }
    int mid=(l+r)>>1,tmp=t[o].mx-max(lmax,rmax);
    if(ql<=mid)lson=update(l,mid,t[p].ls);
    if(mid<qr)rson=update(mid+1,r,t[p].rs);
    t[o].mx=max(lmax,rmax);
    t[o].mx+=tmp;
    return o;
}

int query(int o,int l,int r){
    if(l==r)return o;
    if(ql<=l&&r<=qr)return t[o].mx;
    int mid=(l+r)>>1,tmx=0,tmp=t[o].mx-max(lmax,rmax);
    if(ql<=mid)tmx=max(tmx,query(lson,l,mid));
    if(mid<qr)tmx=max(tmx,query(rson,mid+1,r));
    return tmx+tmp;
}

int main(int argc, const char * argv[]) {
    ios::sync_with_stdio(0);
    int i;
    cin>>n>>m;
    for(i=1;i<=n;i++){
        cin>>a[i].first;
        a[i].first*=-1;
        a[i].second=i;
    }
    sort(a+1,a+n+1);
    for(i=1;i<=n;i++){
        ql=max(1,a[i].second-m+1);
        qr=min(n-m+1,a[i].second);
        root[i]=update(1,n-m+1,root[i-1]);
    }
    cin>>s;
    int tmp=0;
    while(s--){
        int q;
        cin>>ql>>qr>>q;
        q^=tmp;
        int id=upper_bound(a+1,a+n+1,make_pair(-q,INF))-a-1;
        printf("%d\n",tmp=m-query(root[id],1,n-m+1));
    }
    return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <bitset>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define FI first
#define SE second
using namespace std;
typedef long long LL;
const int M = 500;
const int N = 200005;
pair <int, int> a[N];
int mi[M][M], fi[M][M];
int b[N], v[N], tv[N], vl[N], vr[N];
int tmi[M], tfi[M];
int n, m;

inline int start(int x) {
    return x * M;
}

inline int end(int x) {
    return min(n, (x + 1) * M) - 1;
}

void update(int x) {
    int l = max(0, a[x].SE - m + 1);
    int r = a[x].SE;
    int L = l / M, R = r / M;
    ++tfi[L + (l != start(L))];
    --tfi[R + 1];
    ++L; --R;
    if(L > R) return;
    ++tmi[L]; --tmi[R + 1];
}

int brute(int l, int r, int q) {
    int val = tfi[l / M], ret = INF;
    for(int i = start(l / M); i <= r; ++i) {
        if(i >= l) ret = min(ret, val);
        if(b[i] < q) --val;
        if(b[i + m] < q) ++val;
    }
    return ret;
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 0; i < n; ++i) {
        scanf("%d", b + i);
        a[i] = make_pair(b[i], i);
    }
    sort(a, a + n);
    int bkcnt = (n - 1) / M;
    for(int i = 0; i <= bkcnt; ++i) {
        for(int j = start(i); j <= end(i); ++j) {
            ++v[max(0, a[j].SE - m + 1)];
            --v[a[j].SE + 1];
        }
        for(int j = 0; j < n; ++j) tv[j] = v[j];
        for(int j = 1; j < n; ++j) tv[j] += tv[j - 1];
        for(int j = 0; j <= bkcnt; ++j) {
            fi[i][j] = tv[start(j)];
            mi[i][j] = *min_element(tv + start(j), tv + end(j) + 1);
        }
    }
    for(int i = 0; i <= bkcnt; ++i) {
        for(int j = start(i); j <= end(i); ++j) v[j] = 0;
        for(int j = 0; j < n; ++j) {
            int l = max(0, a[j].SE - m + 1), r = a[j].SE;
            int L = l / M, R = r / M;
            if(L == R && L == i) {
                for(int k = l; k <= r; ++k) ++v[k];
                vl[j] = vr[j] = *min_element(v + start(i), v + end(i) + 1);
                continue;
            }
            if(L == i) {
                for(int k = l; k <= end(i); ++k) ++v[k];
                vl[j] = *min_element(v + start(i), v + end(i) + 1);
            }
            if(R == i) {
                for(int k = start(i); k <= r; ++k) ++v[k];
                vr[j] = *min_element(v + start(i), v + end(i) + 1);
            }
        }
    }
    int q, ans = 0;
    scanf("%d", &q);
    while(q--) {
        int l, r, x;
        scanf("%d%d%d", &l, &r, &x);
        --l; --r; x ^= ans;
        int p = lower_bound(a, a + n, make_pair(x, -1)) - a - 1;
        if(p < 0) {
            printf("%d\n", ans = 0);
            continue;
        }
        int idx = p / M;
        memset(tmi, 0, sizeof(tmi));
        memset(tfi, 0, sizeof(tfi));
        for(int i = start(idx); i <= p; ++i) update(i);
        for(int i = 1; i <= bkcnt; ++i) {
            tmi[i] += tmi[i - 1];
            tfi[i] += tfi[i - 1];
        }
        if(idx) {
            for(int i = 0; i <= bkcnt; ++i) {
                tmi[i] += mi[idx - 1][i];
                tfi[i] += fi[idx - 1][i];
            }
        }
        for(int i = start(idx); i <= p; ++i) {
            int L = max(0, a[i].SE - m + 1) / M;
            int R = a[i].SE / M;
            tmi[L] += vl[i]; tmi[R] += vr[i];
        }
        ans = INF;
        int L = l / M, R = r / M;
        for(int i = L + 1; i < R; ++i) ans = min(ans, tmi[i]);
        ans = min(ans, brute(l, min(r, end(L)), x));
        if(L ^ R) ans = min(ans, brute(start(R), r, x));
        printf("%d\n", ans);
    }
    return 0;
}

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<set>
#include<map>
#include<vector>
#define N 230005
#define size 650
//#define N 1005
//#define size 3
#define mo 1000000007
#define inf 100000000
#define eps 0.00000001
#define Long long long
#define uint unsigned int
#define lowbit(x) (x&(-x))
#define For(i,l,r) for(int i=l;i<=r;i++)
#define Dor(i,r,l) for(int i=r;i>=l;i--)
#define Rep(i,t) for(int i=son[t];i;i=Next[i]) if(ed[i]!=f)
#define y1 Y1
#define next Next
#define link Link
using namespace std;
struct rec{
	int x,key;
}F[N/size][(size+5)*2];
int Ans,n;
int G[N/size][(size+5)*2];
int S[N/size][(size+5)*2],SR[3*N];
int V[N/size][(size+5)*2],VR[3*N];
int L[N/size],Tmp[N],be[N];
int ml[N/size],mr[N/size],num[N/size];
int Doit(int t,int num){
	rec *Ft=F[t];
	For(i,1,num){
		rec Now=Ft[i];
		Tmp[Now.x]+=Now.key;
	}
	int Ret=inf,now=0;
	For(i,ml[t],mr[t]){
		now+=Tmp[i]; Tmp[i]=0;
		if(now<Ret) Ret=now;
	}
/*	For(i,ml[t],mr[t]) Tmp[i]+=Tmp[i-1];
	For(i,ml[t],mr[t]) if(Tmp[i]<Ret) Ret=Tmp[i];
	For(i,ml[t],mr[t]) Tmp[i]=0;*/
	return Ret;
}
int Find(int key){
	int l=1,r=num[0],ret=0;
	while(l<=r){
		int mid=(l+r)>>1;
		if(VR[mid]<key) l=mid+1,ret=mid;
		else r=mid-1;
	}
	if(!ret) return 0;
	return SR[ret];
}
int Find(int t,int key){
	int l=1,r=num[t],ret=0;
	while(l<=r){
		int mid=(l+r)>>1;
		if(V[t][mid]<key) l=mid+1,ret=mid;
		else r=mid-1;
	}
	if(!ret) return 0;
	return S[t][ret];
}
int Cnt;
void Query(int t,int key){
	int l=1,r=num[t],ret=0;
	while(l<=r){
		int mid=(l+r)>>1;
		if(V[t][mid]<key) l=mid+1,ret=mid;
		else r=mid-1;
	}
	if(!ret) return;
	Ans=min(Ans,G[t][ret]+L[t-1]);
}
void Query(int t,int key,int ll,int rr){
	rec *Ft=F[t]; int *Vt=V[t];
//	For(i,1,num[t])
	For(i,1,num[t]){
		if(Vt[i]>=key) break;
		rec Now=Ft[i];
		Tmp[Now.x]+=Now.key;
	}

//	printf("Query t %d key %d ll rr %d %d --> Num %d\n",t,key,ll,rr,Num);
//	For(i,1,Num) printf("Add (%d -> %d)\n",F[t][i].x,F[t][i].key);
	int Ret=inf,now=0;
	For(i,ml[t],mr[t]){
		now+=Tmp[i]; Tmp[i]=0;
		if(ll<=i && i<=rr)
		if(now<Ret) Ret=now;
	}
//	For(i,ml[t],mr[t]) Tmp[i]+=Tmp[i-1];
//	For(i,ll,rr) if(Tmp[i]<Ret) Ret=Tmp[i];
//	For(i,ml[t],mr[t]) Tmp[i]=0;

//	printf("---> Ret %d L %d Ans %d\n",Ret,L[t-1],Ans);

	Ans=min(Ans,Ret+L[t-1]);
}
void Add(int x,int key,int val){
	if(x>n) return;
	if(x<1){
		int t=0; ++num[t];
		SR[num[t]]=SR[num[t]-1]+key;
		VR[num[t]]=val;
		return;
	}
	int t=be[x]; ++num[t];
	F[t][num[t]]=(rec){x,key};
	G[t][num[t]]=Doit(t,num[t]);
	S[t][num[t]]=S[t][num[t]-1]+key;
	V[t][num[t]]=val;
}
struct opt{
	int val,id;
}E[N];
bool cmp(const opt &i,const opt &j){return i.val<j.val;}
int main(){
	int m; scanf("%d%d",&n,&m); int sum=(n+1)/size+1;
	For(i,1,n) scanf("%d",&E[i].val),E[i].id=i;
	sort(E+1,E+1+n,cmp);
	For(i,1,sum) ml[i]=max(1,i*size-size);
	For(i,1,sum) mr[i]=min(n,i*size-1);
	For(i,1,sum) For(j,ml[i],mr[i]) be[j]=i;

//	printf("sum %d n %d size %d\n",sum,n,size);
//	For(i,1,sum) printf("(%d,%d) ",ml[i],mr[i]); printf("seg\n");

//	cerr<<"Time -> "<<clock()<<endl;

	For(i,1,n){
//		ll=E[i].id-m+1; rr=E[i].id;

//		printf("Add %d -> key %d val %d\n",E[i].id-m+1,1,E[i].val);
//		printf("Add %d -> key %d val %d\n",E[i].id+1,-1,E[i].val);

		Add(E[i].id-m+1,1,E[i].val);
		Add(E[i].id+1,-1,E[i].val);
	}

//	For(i,0,sum) printf("%d ",num[i]); printf("num[]\n");

//	cerr<<"Time -> "<<clock()<<endl;

	int Q_Q; scanf("%d",&Q_Q);
	while(Q_Q--){
		int t,l,r,key; scanf("%d%d%d",&l,&r,&key);
	//	key^=Ans;
		Ans=inf;
		L[0]=Find(key); For(t,1,be[r]) L[t]=L[t-1]+Find(t,key);
		if(be[l]==be[r]){
			t=be[l]; Query(t,key,l,r);
		}else{
			t=be[l]; Query(t,key,l,mr[t]);
			t=be[r]; Query(t,key,ml[t],r);
			For(d,be[l]+1,be[r]-1) Query(d,key);
		}
		printf("%d\n",Ans);
	}

}

#include<cstdio>
#include<algorithm>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define fi first
#define se second
#define ls t[v].lc,l,mid
#define ls2 t[u].lc,l,mid
#define rs t[v].rc,mid+1,r
#define rs2 t[u].rc,mid+1,r
#define lm l==mid?t[u].lc:t[t[u].lc].mx
#define rm mid+1==r?t[u].rc:t[t[u].rc].mx
typedef unsigned long long ull;typedef std::pair<int,int> pii;const int N=200005,M=N*40;
template<typename T>T min(const T&x,const T&y){return x<y?x:y;}
template<typename T>T max(const T&x,const T&y){return x>y?x:y;}
int n,m,q,ans,tot,rt[N];pii a[N];struct node{ull lc:23,rc:23,mx:18;}t[M];
int mdf(int v,int l,int r,int lx,int rx){
	if(l==r)return v+1;
	int u=++tot;t[u]=t[v];
	if(l==lx&&r==rx)return++t[u].mx,u;int mid=l+r>>1,tag=t[u].mx-max(lm,rm);
	if(rx<=mid)t[u].lc=mdf(ls,lx,rx);else if(mid<lx)t[u].rc=mdf(rs,lx,rx);else t[u].lc=mdf(ls,lx,mid),t[u].rc=mdf(rs,mid+1,rx);
	t[u].mx=max(lm,rm)+tag;return u;
}
int qry(int u,int l,int r,int lx,int rx){
	if(l==lx&&r==rx)return t[u].mx;int mid=l+r>>1,tag=t[u].mx-max(lm,rm),res=0;
	if(rx<=mid)res=qry(ls2,lx,rx);else if(mid<lx)res=qry(rs2,lx,rx);else res=max(qry(ls2,lx,mid),qry(rs2,mid+1,rx));return res+tag;
}
int main(){
	scanf("%d%d",&n,&m);rep(i,1,n)scanf("%d",&a[i].fi),a[i].fi=-a[i].fi,a[i].se=i;std::sort(a+1,a+1+n);
	for(int i=1;i<=n;++i)rt[i]=mdf(rt[i-1],1,n-m+1,max(1,a[i].se-m+1),min(a[i].se,n-m+1));scanf("%d",&q);
	for(int i=1;i<=q;++i){
		int l,r,x;scanf("%d%d%d",&l,&r,&x);x^=ans;
		int pos=std::upper_bound(a+1,a+1+n,std::make_pair(-x,n+1))-a-1;
		printf("%d\n",ans=m-qry(rt[pos],1,n-m+1,l,r));
	}
	return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<ctime>
int tms=clock();
using namespace std;
inline int read()
{
	int x;char c;
	while((c=getchar())<'0'||c>'9');
	for(x=c-'0';(c=getchar())>='0'&&c<='9';)x=x*10+c-'0';
	return x;
}
#define MN 200000
#define K 50
#define KK 1000
#define mp make_pair
struct node{int l,r,s;}t[MN*19+5];
int a[MN+5],c[MN+5],p[MN+5],tn,rt[MN+5],s[MN/K+5],sc[MN/KK+5][MN/K+5],lp[MN+5],rp[MN+5],f[MN/K+5],fc[MN/KK+5][MN/K+5];
vector<int> v[MN/K+5],z[MN/K+5];
int add(int k,int l,int r,int x)
{
	int p=++tn;t[p].s=t[k].s+1;
	if(l==r)return p;
	int mid=l+r>>1;
	if(x<=mid)t[p].l=add(t[k].l,l,mid,x),t[p].r=t[k].r;
	else t[p].l=t[k].l,t[p].r=add(t[k].r,mid+1,r,x);
	return p;
}
#define L (k<<1)
#define R (k<<1|1)
struct tree{int mx,mk;}T[10000];
void build(int k,int l,int r)
{
	if(T[k].mx=T[k].mk=0,l==r)return;
	int mid=l+r>>1;build(L,l,mid);build(R,mid+1,r);
}
inline void up(int k){T[k].mx=min(T[L].mx,T[R].mx);}
inline void add(int k,int x){T[k].mx+=x;T[k].mk+=x;}
inline void down(int k){if(T[k].mk)add(L,T[k].mk),add(R,T[k].mk),T[k].mk=0;}
void add(int k,int l,int r,int ql,int qr)
{
	if(ql<=l&&qr>=r){add(k,1);return;}
	int mid=l+r>>1;down(k);
	if(qr<=mid)add(L,l,mid,ql,qr);
	else if(ql>mid)add(R,mid+1,r,ql,qr);
	else add(L,l,mid,ql,mid),add(R,mid+1,r,mid+1,qr);
	up(k);
}
int query(int k,int l,int r,int x)
{
	if(!k)return 0;
	if(r<=x)return t[k].s;
	int mid=l+r>>1;
	if(x<=mid)return query(t[k].l,l,mid,x);
	return t[t[k].l].s+query(t[k].r,mid+1,r,x);
}
bool CMP(int x,int y){return a[x]<a[y];}
int main()
{//freopen("0.in","r",stdin);
	int n,m,q,i,j,l,r,lk,rk,ans=0;
	n=read();m=read();
	for(i=1;i<=n;++i)a[i]=read(),c[i]=i;
	sort(c+1,c+n+1,CMP);sort(a+1,a+n+1);
	for(i=1;i<=n;++i)p[c[i]]=i;
	for(i=1;i<=n;++i)
	{
		rt[i]=add(rt[i-1],1,n,p[i]);
		l=max(1,i-m+1);r=i;
		lp[i]=(lk=(l-1)/K)+1;rp[i]=(rk=(r-1)/K)-1;
		if(lk==rk)v[lk].push_back(p[i]);
		else v[lk].push_back(p[i]),
			 v[rk].push_back(p[i]);
	}
	for(i=1;i<=n;++i)
	{
		++f[lp[c[i]]-1];++f[rp[c[i]]+1];
		for(j=lp[c[i]];j<=rp[c[i]];++j)++s[j];
		if(i%KK==0)for(j=0;j<=(n-1)/K;++j)sc[i/KK][j]=s[j],fc[i/KK][j]=f[j];
	}
	for(i=0;i<=(n-1)/K;++i)
	{
		sort(v[i].begin(),v[i].end());
		build(1,i*K+1,min(i*K+K,n));
		for(j=0;j<v[i].size();++j)
		{
			add(1,i*K+1,min(i*K+K,n),c[v[i][j]]-m+1,c[v[i][j]]);
			z[i].push_back(T[1].mx);
		}
	}
//	printf("%d\n",clock()-tms);
	for(q=read();q--;)
	{
//		if(q%10000==0)printf("%d\n",clock()-tms);
		l=read();r=read();a[0]=lower_bound(a+1,a+n+1,ans^read())-a-1;ans=MN;
		if(!a[0]){ans=0;puts("0");continue;}
		lk=(l-1)/K;rk=(r-1)/K;
		for(i=l;i<=r&&i<=lk*K+K;++i)
			ans=min(ans,query(rt[i+m-1],1,n,a[0])-query(rt[i-1],1,n,a[0]));
		if(lk!=rk)for(i=r;i>rk*K;--i)
			ans=min(ans,query(rt[i+m-1],1,n,a[0])-query(rt[i-1],1,n,a[0]));
		memset(s,0,sizeof(s));
		for(i=lk;++i<rk;)f[i]=fc[(a[0]-1)/KK][i];
		for(i=(a[0]-1)/KK*KK;++i<=a[0];)
		{
			if(lp[c[i]]<=rp[c[i]])++s[lp[c[i]]],--s[rp[c[i]]+1];
			++f[lp[c[i]]-1];++f[rp[c[i]]+1];
		}
		for(i=1;i<rk;++i)s[i]+=s[i-1];
		for(i=lk;++i<rk;)ans=min(ans,(f[i]?z[i][f[i]-1]:0)+sc[(a[0]-1)/KK][i]+s[i]);
		printf("%d\n",ans);
	}
//	printf("%d\n",clock()-tms);
}

#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<stdio.h>
#include<map>
#include<vector>
#include<cassert>
using namespace std;
const int inf=1e9;
const int bound=400;
struct pp
{
	int vl,id,min_value,min_id,value,sum;
	int flag;
	bool operator <(const pp &temp)const
	{
		return vl<temp.vl;
	}
};
pp bl[210000/bound][2*bound],now;
int num[210000/bound],num_bl,cnt_list,np[2*bound];
int m,n,a[220000],num_q,ans,nw;
int lower_bound(int left,int right,pp bl[],int x)
{
	int mid;
	while(left<=right)
	{
		mid=(left+right)>>1;
		if(bl[mid].vl<x)
		   left=mid+1;
        else
           right=mid-1;
	}
	return right;
}
int main()
{
	int i,j,s,p,q,ip,id,l,r,x,fir;
	scanf("%d%d",&n,&m);
	for(i=0;i<n;i++)
	    scanf("%d",&a[i]);
    memset(num,0,sizeof(num));
    num_bl=0;
	for(i=0;i<n;i++)
    {
    	if(i%bound==0)
    	{
	    	for(j=i;j<min(n,i+bound);j++)
	    	{
	    		now.id=j;
	    		now.vl=a[j];
	    	    bl[num_bl][num[num_bl]++]=now;
	    	    if(j+m<n)
	    	    {
    	    		now.id=j+m;
    	    		now.vl=a[j+m];
    	    		bl[num_bl][num[num_bl]++]=now;
    	    	}
			}
			num_bl++;
	    }
    }
    for(i=0;i<num_bl;i++)
    {
        sort(bl[i],bl[i]+num[i]);
        for(j=i*bound;j<min((i+1)*bound,n);j++)
             np[j-i*bound]=0;
        for(j=0;j<num[i];j++)
        {
       	    if(bl[i][j].id<(i+1)*bound)
       	    {
       	       bl[i][j].value=-1;
       	       bl[i][j].sum=1;
       	       ip=bl[i][j].id;
       	       np[ip-i*bound]--;
		    }
			else
			{
	           bl[i][j].value=1;
			   bl[i][j].sum=0;
			   ip=bl[i][j].id-m;
			   np[ip-i*bound]++;
			}
			if(j)
			{
               bl[i][j].value+=bl[i][j-1].value;
			   bl[i][j].sum+=bl[i][j-1].sum;
			}
			bl[i][j].min_value=0;
			nw=0;
			for(s=i*bound;s<min((i+1)*bound,n);s++)
			{
				nw+=np[s-i*bound];
				if(bl[i][j].min_value>nw)
				   bl[i][j].min_value=nw;
			}
        }
	}
	scanf("%d",&num_q);
	ans=0;
	while(num_q--)
	{
		scanf("%d%d%d",&l,&r,&x);
		l--;
		r--;
		x^=ans;
		ans=0;
		id=l/bound;
		nw=fir=0;
		for(i=0;i<num[id];i++)
		{
			if(bl[id][i].id>=l&&bl[id][i].id<l+m)
			{
			    if(bl[id][i].vl<x)
    				fir++;
			}
		}
		if(id!=(l+m)/bound)
		{
			id=(l+m)/bound;
			for(i=0;i<num[id];i++)
			{
				if(bl[id][i].id>=l&&bl[id][i].id<l+m)
			    {
			       if(bl[id][i].vl<x)
    		     	  fir++;
			    }
			}
			for(i=l/bound+1;i<(l+m)/bound;i++)
			{
				 id=lower_bound(0,num[i]-1,bl[i],x);
				 if(id>=0)
				    fir+=bl[i][id].sum;
			}
		}
        for(i=l;i<=min(r,(l/bound+1)*bound-1);i++)
	    {
    	     if(a[i]<x&&i!=l)
     	    	nw--;
     	     if(i+m<n&&a[i+m]<x)
     	        nw++;
  	         if(ans>nw)
  	            ans=nw;
	    }
		if(r>=(l/bound+1)*bound)
		{
			id=r/bound;
			for(i=l/bound+1;i<r/bound;i++)
			{
				id=lower_bound(0,num[i]-1,bl[i],x);
				if(id>=0)
				{
				   if(nw+bl[i][id].min_value<ans)
				      ans=nw+bl[i][id].min_value;
				    nw+=bl[i][id].value;
				}
			}
			for(i=r/bound;i<=r;i++)
			{
				if(a[i]<x)
				   nw--;
	            if(i+m<n&&a[i]<x)
	               nw++;
                if(ans>nw)
                    ans=nw;
			}
		}
	//	printf("ans=%d,fir=%d\n",ans,fir);
        ans+=fir;
        printf("%d\n",ans);
	}
	return 0;
}

/* --- author: dxm --- */
#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;

#define INF 1e9
#define Inf 1e18
#define rep(i,n) for(int (i)=0;(i)<n;i++)
#define REP(i,n) for(int (i)=1;(i)<=n;i++)
#define mp make_pair
#define pb push_back
#define fst first
#define snd second
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;

void qread(int &x){
	int neg=1;x=0;
	char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')neg=-1;c=getchar();}
	while(c>='0'&&c<='9')x=10*x+c-'0',c=getchar();
	x*=neg;
}

const int maxn=200005;
const int mlog=40;
int n,m,q,ans;
pii a[maxn];
int rt[maxn];

namespace Segtree{
	int tot;
	ull t[maxn*mlog+60000];
	const ull mask=(1<<22)-1;
	void code(ull &x,int ls,int rs,int mn){
		x=(ull(mn)<<44)+(ull(ls)<<22)+(ull(rs));
	}
	void decode(ull x,int &ls,int &rs,int &mn){
		rs=int(x&mask);ls=int((x>>22)&mask);mn=int(x>>44);
	}
	int getmn(int v){
		if(!v)return 0;
		int ls,rs,mn;
		decode(t[v],ls,rs,mn);
		return mn;
	}
	void update(int &v,int old,int l,int r,int vl,int vr,int val){
		v=++tot;
		t[v]=t[old];
		int ls,rs,mn;
		decode(t[v],ls,rs,mn);
		int tag=mn-min(getmn(ls),getmn(rs));
		if(l>=vl&&r<=vr){
			mn+=val;
			code(t[v],ls,rs,mn);
			return;
		}
		int mid=(l+r)>>1;
		if(mid>=vl)update(ls,ls,l,mid,vl,vr,val);
		if(mid<vr)update(rs,rs,mid+1,r,vl,vr,val);
		mn=min(getmn(ls),getmn(rs))+tag;
		code(t[v],ls,rs,mn);
	}
	int query(int v,int l,int r,int vl,int vr,int val){
		if(!v)return val;
		int ls,rs,mn;
		decode(t[v],ls,rs,mn);
		int tag=mn-min(getmn(ls),getmn(rs));
		if(l>=vl&&r<=vr)return mn+val;
		int mid=(l+r)>>1;
		val+=tag;
		int sl=mid>=vl?query(ls,l,mid,vl,vr,val):INF;
		int sr=mid<vr?query(rs,mid+1,r,vl,vr,val):INF;
		return min(sl,sr);
	}
}

int main(){
#ifdef FILE
	freopen("E.in","r",stdin);
	freopen("E.out","w",stdout);
#endif
	scanf("%d%d",&n,&m);
	REP(i,n)scanf("%d",&a[i].fst),a[i].snd=i;
	sort(a+1,a+n+1);
	REP(i,n){
		Segtree::update(rt[i],rt[i-1],1,n-m+1,max(1,a[i].snd-m+1),min(a[i].snd,n-m+1),1);
	}
	scanf("%d",&q);
	while(q--){
		int l,r,x;
		scanf("%d%d%d",&l,&r,&x);
		x^=ans;
		int pos=lower_bound(a+1,a+n+1,mp(x,-1))-a;
		ans=Segtree::query(rt[pos-1],1,n-m+1,l,r,0);
		printf("%d\n",ans);
	}
#ifdef TIME
	printf("Running Time = %d ms\n",int(clock()*1000.0/CLOCKS_PER_SEC));
#endif
	return 0;
}

/*
Input:
-----------------
Output:
*/

#include<cstdio>
#include<algorithm>

#define REP(i,m) for(int i=0;i<(m);++i)
#define REPN(i,m,in) for(int i=(in);i<(m);++i)
#define ALL(t) (t).begin(),(t).end()
#define CLR(a) memset((a),0,sizeof(a))
#define pb push_back
#define mp make_pair
#define fr first
#define sc second

using namespace std;

#ifndef ONLINE_JUDGE
#include<iostream>
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
#else
#define dump(x) ;
#define prl ;
template<class T> void debug(T a,T b){ ;}
#endif
template<class T> void chmin(T& a,const T& b) { a=min(a,b);}
typedef unsigned int uint;
const int INF=(1<<18)-1;

typedef pair<int,int> pi;
int cnt=1;

#define mnL(i) (md-l==1?i:pool[i].mini)
#define mnR(i) (r-md==1?i:pool[i].mini)
struct node{
  unsigned long long lch:23,rch:23,mini:18;
  int copy();
  int add(int,int,int,int);
  int query(int,int,int,int);
};
node pool[7829000];

int node::add(int a,int b,int l,int r){
  if(r-l==1) return (this-pool)+1;

  int id=cnt++;
  pool[id]=*this;
  if(a<=l && r<=b){
    ++pool[id].mini;
    return id;
  }
  int md=(l+r+1)>>1,base=mini-min(mnL(lch),mnR(rch));
  if(a<md){
    pool[id].lch=pool[lch].add(a,b,l,md);
  }
  if(md<b){
    pool[id].rch=pool[rch].add(a,b,md,r);
  }
  pool[id].mini=base+min(mnL(pool[id].lch),mnR(pool[id].rch));
  return id;
}
int node::query(int a,int b,int l,int r){
  if(r-l==1) return (this-pool);
  if(a<=l && r<=b) return mini;
  int md=(l+r+1)>>1;
  int res=INF,base=mini-min(mnL(lch),mnR(rch));
  if(a<md){
    res=pool[lch].query(a,b,l,md);
  }
  if(md<b){
    chmin(res,pool[rch].query(a,b,md,r));
  }
  return res+base;
}

int root[200005];

int n,m;

pi ar2[200005];

int main(){
  srand(114514);

  const int OFF=1;
  scanf("%d%d",&n,&m);
  REP(i,n){
    int a;scanf("%d",&a);
    ar2[i]=mp(a,i);
  }
  sort(ar2,ar2+n);
  root[0]=cnt++;

  int len=n-m+1;
  int MX=0;
  REP(i,n){
    int p=ar2[i].sc;
    int L=max(0,p-m+1),R=min(p+1,len);
    int prev=cnt;
    L+=OFF;R+=OFF;
    root[i+1]=pool[root[i]].add(L,R,0,len);
    MX=max(MX,cnt-prev);
    dump(MX);
  }
  dump(MX);

  int q;
  scanf("%d",&q);

  int prev=0;
  REP(_,q){
    int l,r,x;
    scanf("%d%d%d",&l,&r,&x);
    --l;
    x^=prev;

    l+=OFF;r+=OFF;

    int lb=lower_bound(ar2,ar2+n,mp(x,-1))-ar2;
    int res=pool[root[lb]].query(l,r,0,len);

    prev=res;
    printf("%d\n",res);
  }
  return 0;
}

#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
using ll = long long;
using db = long double;

const int N = 2e5 + 5, MOD = 1e9 + 7;
vector<int> go[N];
int n, m, q, a[N];
struct Node{
  Node * L, * R;
  int val, lzL, lzR;  ///let lz identify those that are under it
  node(){
    L = R = NULL;
    val = lzL = lzR = 0;
  }
} * trees[N];
Node * upd(Node * root, int s, int e, int l, int r){
  if(l <= s && e <= r){
    Node * ret = new Node();
    if(s != e){
      ret->lzL = root->lzL + 1;
      ret->lzR = root->lzR + 1;
    }
    ret->val = root->val + 1;
    ret->L = root->L, ret->R = root->R;
    return ret;
  }
  int md = (s + e) >> 1;
  Node * ret = new Node();
  ret->L = root->L, ret->R = root->R;
  if(l <= md){  /// i need the left child
    if(!root->L)root->L = new Node();
    root->L->val += root->lzL;
    root->L->lzL += root->lzL;
    root->L->lzR += root->lzL;
    root->lzL = 0;
    ret->L = upd(root->L,s,md,l,r);
  }
  if(r > md){ /// i need the right child
    if(!root->R)root->R = new Node();
    root->R->val += root->lzR;
    root->R->lzL += root->lzR;
    root->R->lzR += root->lzR;
    root->lzR = 0;
    ret->R = upd(root->R,md+1,e,l,r);
  }
  ret->lzL = root->lzL, ret->lzR = root->lzR;
  int x, y;
  if(ret->L)x = ret->L->val + ret->lzL;
  else x = ret->lzL;
  if(ret->R)y = ret->R->val + ret->lzR;
  else y = ret->lzR;
  ret->val = min(x,y);
  return ret;
}
int qry(Node * root, int s, int e, int l, int r){
  if(l <= s && e <= r)
    return root->val;
  int md = (s + e) >> 1, ret = INT_MAX;
  if(l <= md){  /// i need the left child
    if(!root->L)ret = min(ret, root->lzL);
    else {
      root->L->val += root->lzL;
      root->L->lzL += root->lzL;
      root->L->lzR += root->lzL;
      root->lzL = 0;
      ret = min(ret, qry(root->L,s,md,l,r));
    }
  }
  if(r > md){ /// i need the right child
    if(!root->R)ret = min(ret, root->lzR);
    else {
      root->R->val += root->lzR;
      root->R->lzL += root->lzR;
      root->R->lzR += root->lzR;
      root->lzR = 0;
      ret = min(ret, qry(root->R,md+1,e,l,r));
    }
  }
  return ret;
}
int main(){
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(0);cin.tie(0);
#endif // ONLINE_JUDGE

  cin >> n >> m;
  vector<int> ord;
  for(int i = 0; i < n; i++)
    cin >> a[i], ord.pb(a[i]);
  ord.pb(-1);
  ord.pb(INT_MAX);
  sort(ord.begin(), ord.end());
  ord.resize(unique(ord.begin(), ord.end()) - ord.begin());

  for(int i = 0; i < n; i++){
    a[i] = lower_bound(ord.begin(),ord.end(), a[i]) - ord.begin();
    go[a[i]].pb(i);
  }

  trees[0] = new Node();

  for(int i = 1; i < ord.size(); i++){
    trees[i] = trees[i-1];
    for(auto x : go[i]){
      ///check those from x-m+1 to x
      int l = max(0,x-m+1), r = min(x,n-m);
      trees[i] = upd(trees[i], 0, n - m, l, r);
//      cout << ord[i] << ' ' << l << ' ' << r << '\n';
    }
  }

  cin >> q;
  int ans = 0;
  while(q--){
    int l, r, v;
    cin >> l >> r >> v;
    v ^= ans;
    --l, --r, --v;
    ///find the biggest number less than or equal to v
    int idx = upper_bound(ord.begin(), ord.end(), v) - ord.begin(); --idx;
//    cout << "H" << ord[idx] << '\n';
    cout << (ans = qry(trees[idx], 0, n - m, l, r)) << '\n';
  }

  return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<ctime>
int tms=clock();
using namespace std;
inline int read()
{
	int x;char c;
	while((c=getchar())<'0'||c>'9');
	for(x=c-'0';(c=getchar())>='0'&&c<='9';)x=x*10+c-'0';
	return x;
}
#define MN 200000
#define K 50
#define KK 1000
#define mp make_pair
struct node{int l,r,s;}t[MN*19+5];
int a[MN+5],c[MN+5],p[MN+5],tn,rt[MN+5],s[MN/K+5],sc[MN/KK+5][MN/K+5],lp[MN+5],rp[MN+5],f[MN/K+5],fc[MN/KK+5][MN/K+5];
vector<int> v[MN/K+5],z[MN/K+5];
int add(int k,int l,int r,int x)
{
	int p=++tn;t[p].s=t[k].s+1;
	if(l==r)return p;
	int mid=l+r>>1;
	if(x<=mid)t[p].l=add(t[k].l,l,mid,x),t[p].r=t[k].r;
	else t[p].l=t[k].l,t[p].r=add(t[k].r,mid+1,r,x);
	return p;
}
#define L (k<<1)
#define R (k<<1|1)
struct tree{int mx,mk;}T[1000];
void build(int k,int l,int r)
{
	if(T[k].mx=T[k].mk=0,l==r)return;
	int mid=l+r>>1;build(L,l,mid);build(R,mid+1,r);
}
inline void up(int k){T[k].mx=min(T[L].mx,T[R].mx);}
inline void add(int k,int x){T[k].mx+=x;T[k].mk+=x;}
inline void down(int k){if(T[k].mk)add(L,T[k].mk),add(R,T[k].mk),T[k].mk=0;}
int query(int k1,int k2,int l,int r,int x)
{
	for(int res=0,mid;;)
	{
		if(r<=x)return res+t[k1].s-t[k2].s;
		mid=l+r>>1;
		if(x<=mid)k1=t[k1].l,k2=t[k2].l,r=mid;
		else res+=t[t[k1].l].s-t[t[k2].l].s,k1=t[k1].r,k2=t[k2].r,l=mid+1;
	}
}
void add(int k,int l,int r,int ql,int qr)
{
	if(ql<=l&&qr>=r){add(k,1);return;}
	int mid=l+r>>1;down(k);
	if(qr<=mid)add(L,l,mid,ql,qr);
	else if(ql>mid)add(R,mid+1,r,ql,qr);
	else add(L,l,mid,ql,mid),add(R,mid+1,r,mid+1,qr);
	up(k);
}
bool CMP(int x,int y){return a[x]<a[y];}
int main()
{//freopen("0.in","r",stdin);
	int n,m,q,i,j,l,r,lk,rk,ans=0;
	n=read();m=read();
	for(i=1;i<=n;++i)a[c[i]=i]=read();
	sort(c+1,c+n+1,CMP);sort(a+1,a+n+1);
	for(i=1;i<=n;++i)p[c[i]]=i;
	for(i=1;i<=n;++i)
	{
		rt[i]=add(rt[i-1],1,n,p[i]);
		l=max(1,i-m+1);r=i;
		lp[i]=(lk=(l-1)/K)+1;rp[i]=(rk=(r-1)/K)-1;
		if(lk==rk)v[lk].push_back(p[i]);
		else v[lk].push_back(p[i]),
			 v[rk].push_back(p[i]);
	}
	for(i=1;i<=n;++i)
	{
		++f[lp[c[i]]-1];++f[rp[c[i]]+1];
		for(j=lp[c[i]];j<=rp[c[i]];++j)++s[j];
		if(i%KK==0)for(j=0;j<=(n-1)/K;++j)sc[i/KK][j]=s[j],fc[i/KK][j]=f[j];
	}
	for(i=0;i<=(n-1)/K;++i)
	{
		sort(v[i].begin(),v[i].end());
		build(1,i*K+1,min(i*K+K,n));
		for(j=0;j<v[i].size();++j)
		{
			add(1,i*K+1,min(i*K+K,n),c[v[i][j]]-m+1,c[v[i][j]]);
			z[i].push_back(T[1].mx);
		}
	}
//	printf("%d\n",clock()-tms);
	for(q=read();q--;)
	{
//		if(q%10000==0)printf("%d\n",clock()-tms);
		l=read();r=read();a[0]=lower_bound(a+1,a+n+1,ans^read())-a-1;ans=MN;
		if(!a[0]){ans=0;puts("0");continue;}
		lk=(l-1)/K;rk=(r-1)/K;
		for(i=l;i<=r&&i<=lk*K+K;++i)ans=min(ans,query(rt[i+m-1],rt[i-1],1,n,a[0]));
		if(lk!=rk)for(i=r;i>rk*K;--i)ans=min(ans,query(rt[i+m-1],rt[i-1],1,n,a[0]));
		memset(s,0,sizeof(s));
		for(i=lk;++i<rk;)f[i]=fc[(a[0]-1)/KK][i];
		for(i=(a[0]-1)/KK*KK;++i<=a[0];)
		{
			if(lp[c[i]]<=rp[c[i]])++s[lp[c[i]]],--s[rp[c[i]]+1];
			++f[lp[c[i]]-1];++f[rp[c[i]]+1];
		}
		for(i=1;i<rk;++i)s[i]+=s[i-1];
		for(i=lk;++i<rk;)ans=min(ans,(f[i]?z[i][f[i]-1]:0)+sc[(a[0]-1)/KK][i]+s[i]);
		printf("%d\n",ans);
	}
//	printf("%d\n",clock()-tms);
}

#include <stdio.h>
#include <algorithm>

using std :: pair;
using std :: sort;

template < class T > void read(T &x) { char c, mi = 0; while(c = getchar(), c <= 32); if(c == '-') mi = 1, x = 0; else if(c < 48 || c > 57) return void(x = c); else x = c - 48; while(c = getchar(), c > 32) x = 10 * x + c - 48; if(mi == 1) x = -x; }
template < class T > void read(T &x, T &y) { read(x); read(y); }
template < class T > void read(T &x, T &y, T &z) { read(x, y); read(z); }
template < class T > void reada(T *a, int n) { for(int i = 0; i < n; ++i) read(a[i]); }
template < class T > void write(T x) { static char s[20]; char pt = 0, mi = (x < 0); if(mi == 1) x = -x; while(!pt || x > 0) { s[++pt] = (char)(x % 10 + '0'); x /= 10; } if(mi == 1) putchar('-'); while(pt > 0) putchar(s[pt--]); }
template < class T > void write(T x, T y) { write(x); putchar(' '); write(y); }
template < class T > void write(T x, T y, T z) { write(x, y); putchar(' '); write(z); }
template < class T > void writeln(T x) { write(x); puts(""); }
template < class T > void writea(T *a, int n) { for(int i = 0; i < n; ++i) { write(a[i]); putchar(i + 1 == n ? '\n' : ' '); } }

#define nmax 200010
#define fs first
#define sd second

int pt = 0;
int roots[nmax];
int n, m, q;
int ans, l, r, x;
pair < int, int > p[nmax];

int mi(const int &a, const int &b) {
	return a < b ? a : b;
}

int ma(const int &a, const int &b) {
	return a > b ? a : b;
}

struct Node {
	unsigned long long mi : 18, l : 23, r : 23;
} t[8088888];

int findAdd(int ret, int xl, char dl, int xr, char dr) {
	return t[ret].mi - mi(dl == 0 ? t[xl].mi : xl, dr == 0 ? t[xr].mi : xr);
}

void f(int ret, int xl, char dl, int xr, char dr, int add = 0) {
	t[ret].mi = add + mi(dl == 0 ? t[xl].mi : xl, dr == 0 ? t[xr].mi : xr);
	t[ret].l = xl;
	t[ret].r = xr;
}

int upd(int v, int L, int R, int l, int r) {
	if(L == R) {
		return v + 1;
	}
	int ret = ++pt;
	if(pt == 8088888) exit(1);
	if(L == l && r == R) {
		t[ret].mi = t[v].mi + 1;
		t[ret].l = t[v].l;
		t[ret].r = t[v].r;
		return ret;
	}
	int M = (L + R) / 2;
	if(r <= M) {
		t[ret].l = upd(t[v].l, L, M, l, r);
		t[ret].r = t[v].r;
	} else if(l > M) {
		t[ret].r = upd(t[v].r, M + 1, R, l, r);
		t[ret].l = t[v].l;
	} else {
		t[ret].l = upd(t[v].l, L, M, l, M);
		t[ret].r = upd(t[v].r, M + 1, R, M + 1, r);
	}
	f(ret, t[ret].l, M == L, t[ret].r, M + 1 == R, findAdd(v, t[v].l, L == M, t[v].r, M + 1 == R));
	return ret;
}

int get(int v, int L, int R, int l, int r) {
	if(v == 0) {
		return 0;
	}
	if(L == R) {
		return v;
	}
	if(L == l && r == R) {
		return t[v].mi;
	}
	int M = (L + R) / 2;
	int z = findAdd(v, t[v].l, L == M, t[v].r, M + 1 == R);
	if(r <= M) {
		return z + get(t[v].l, L, M, l, r);
	}
	if(l > M) {
		return z + get(t[v].r, M + 1, R, l, r);
	}
	return z + mi(get(t[v].l, L, M, l, M),
		get(t[v].r, M + 1, R, M + 1, r));
}

int main() {
//	freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
//	freopen("elevator.in", "r", stdin); freopen("elevator.out", "w", stdout);
//	printf("%d\n", sizeof(t));
	read(n, m);
	for(int i = 1; i <= n; ++i) {
		read(p[i].fs);
		p[i].sd = i;
	}
	roots[0] = ++pt;
	sort(p + 1, p + 1 + n);
	for(int i = 1; i <= n; ++i) {
		roots[i] = upd(roots[i - 1], 1, n - m + 1, ma(p[i].sd - m + 1, 1), mi(p[i].sd, n - m + 1));
	}
	read(q);
	while(q --> 0) {
		read(l, r, x);
		x ^= ans;
		int id = upper_bound(p + 1, p + 1 + n, std :: make_pair(x, -1)) - (p + 1);
		ans = get(roots[id], 1, n - m + 1, l, r);
		writeln(ans);
	}
	getchar(); getchar();
	return 0;
}

#include "iostream"
#include "algorithm"
#include "vector"
#include "set"
#include "map"
#include "cstring"
#include "string"
#include "vector"
#include "cassert"
#include "queue"
#include "cstdio"
#include "cstdlib"
#include "ctime"
#include "cmath"
#include "bitset"

using namespace std;

typedef long long ll;
typedef pair < int, int > ii;

const int N = 2e5 + 5;
const int K = 450;
const int T = N / K + 5;

int n, m, q;
int b[N], arr[N], add[N], p[N], addSmall[T + 2 * K];
ii a[N];
vector < int > v[T], mx[T], start[T];

int main () {

    scanf("%d %d", &n, &m);

    for(int i = 0; i < n; i++) {
        scanf("%d", &a[i].first);
        a[i].second = i;
        b[i] = a[i].first;
    }

    sort(a, a + n);

    for(int it = 0; it * K < n; it++) {

        int x = it * K, y = min((it + 1) * K - 1, n - 1);

        for(int i = 0; i < n; i += K)
            v[it].push_back(i);

        memset(add, 0, sizeof(add));

        for(int j = x; j <= y; j++) {
            int i = a[j].second;
            if(i - m + 1 > 0) {
                add[i - m]--;
                v[it].push_back(i - m + 1);
            }
            add[i]++;
            v[it].push_back(i + 1);
        }

        for(int i = n - 1; i >= 0; i--) {
            add[i] += add[i + 1];
            arr[i] += add[i];
        }

        v[it].push_back(0);
        v[it].push_back(n);

        sort(v[it].begin(), v[it].end());
        v[it].resize(unique(v[it].begin(), v[it].end()) - v[it].begin());

        mx[it].resize(v[it].size());
        start[it].resize(v[it].size());

        for(int i = 0; i + 1 < v[it].size(); i++) {
            for(int j = v[it][i]; j < v[it][i + 1]; j++) {
                //printf("i = %d j = %d val = %d\n", i, j, arr[j]);
                mx[it][i] = max(mx[it][i], arr[j]);
            }
            start[it][i] = arr[v[it][i]];
        }

    }

    int ans = 0;

    scanf("%d", &q);

    while(q--) {

        int l, r, x;

        scanf("%d %d %d", &l, &r, &x);

        l--, r--, x ^= ans;

        int id = lower_bound(a, a + n, ii(x, 0)) - a - 1;

        //printf("id = %d\n", id);

        if(n != 5)
            printf("l = %d r = %d x = %d id = %d\n", l, r, x, id);

        if(id < 0) {
            ans = 0;
            puts("0");
            continue;
        }

        vector < int > &ind = v[id / K], &first = start[id / K], vs = mx[id / K];

        for(int it = 0; it < ind.size(); it++)
            p[ind[it]] = it;

        memset(addSmall, 0, sizeof(addSmall));

        int j = min(n - 1, (id / K + 1) * K - 1);

        while(j > id) {
            int i = a[j].second;
            //printf("p[...] = %d\n", p[max(0, i - m + 1)]);
            addSmall[p[max(0, i - m + 1)]]--;
            if(p[i + 1] < vs.size()) {
                //printf("p[i + 1] = %d\n", p[i + 1]);
                addSmall[p[i + 1]]++;
            }
            j--;
        }

        for(int i = 0; i < vs.size(); i++) {
            addSmall[i] += i ? addSmall[i - 1] : 0;
            vs[i] += addSmall[i];
            //printf("vs[%d] = %d ind = %d\n", i, vs[i], ind[i]);
        }

        ans = 1e9;

        for(int it = 0; it + 1 < ind.size(); it++) {
            int L = ind[it], R = ind[it + 1] - 1;
            if(R < l or L > r)
                continue;
            if(l <= L and R <= r) {
                ans = min(ans, vs[it]);
                continue;
            }
            int cur = first[it] + addSmall[it];
            for(int i = L; i <= R and n == 5; i++) {
                if(l <= i and i <= r)
                    ans = min(ans, cur);
                cur -= b[i] < x;
                cur += b[i + m] < x;
            }
        }

        printf("%d\n", ans);

    }

    return 0;

}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
using namespace std;

const int N = 200010;
#define x first
#define y second
unsigned long long a[N * 40];
const int b18 = (1 << 18) - 1;
const int b23 = (1 << 23) - 1;
int cnt;

void update(int &x, int y, int l, int r, int L, int R) {
	x = ++cnt;
	while (x >= cnt * 40) {
		exit(0);
	}
	int rc = a[y] & b23;
	int lc = (a[y] >> 23) & b23;
	unsigned long long mx = a[y] >> 46;
	if (l == L && r == R) {
		mx++;
		a[x] = (mx << 46) | ((unsigned long long)lc << 23) | rc;
	    return;
	}

    int mid = (l + r) >> 1;
    int lazy = mx - max(a[lc] >> 46, a[rc] >> 46);
	if (R <= mid) update(lc, lc, l, mid, L, R);
	else if (L > mid) update(rc, rc, mid + 1, r, L, R);
	else {
		update(lc, lc, l, mid, L, mid);
		update(rc, rc, mid + 1, r, mid + 1, R);
	}

	mx = max(a[lc] >> 46, a[rc] >> 46);
	mx += lazy;
	a[x] = (mx << 46) | ((unsigned long long)lc << 23) | (unsigned long long)rc;
}

int query(int x, int l, int r, int L, int R) {
	int rc = a[x] & b23;
	int lc = (a[x] >> 23) & b23;
	int mx = a[x] >> 46;
	if (l == L && R == r) {
		return mx;
	}

	int mid = (l + r) >> 1;
	int lazy = mx - max(a[lc] >> 46, a[rc] >> 46);
	if (R <= mid) return lazy + query(lc, l, mid, L, R);
	if (L > mid) return lazy + query(rc, mid + 1, r, L, R);
	return lazy + max(query(lc, l, mid, L, mid), query(rc, mid + 1, r, mid + 1, R));
}

pair<int, int> pi[N];
int root[N];

int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
	    scanf("%d", &pi[i].x);
		pi[i].y = i;
	}

	sort(pi + 1, pi + n + 1);
	for (int i = n; i >= 1; i--) {
		int l = max(1, pi[i].y - m + 1);
		int r = min(n - m + 1, pi[i].y);
		update(root[i], root[i + 1], 1, n - m + 1, l, r);
	}

	exit(0);
	int q;
	scanf("%d", &q);
	int ans = 0;
	while (q--) {
		int l, r, x;
		scanf("%d%d%d", &l, &r, &x);
		x ^= ans;
		if (x <= pi[1].x) {
			ans = 0;
		} else if (x > pi[n].x) {
			ans = m;
		} else {
			int lt = 1, rt = n, mid, pos;
			while (lt <= rt) {
				mid = (lt + rt) >> 1;
				if (pi[mid].x >= x) {
					pos = mid;
					rt = mid - 1;
				} else lt = mid + 1;
			}

			ans = m - query(root[pos], 1, n - m + 1, l, r);
		}
		printf("%d\n", ans);
	}

	return 0;
}

#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
using ll = long long;
using db = long double;

const int N = 2e5 + 5, MOD = 1e9 + 7;
vector<int> go[N];
int n, m, q, a[N];
struct Node{
  Node * L, * R;
  int val, lzL, lzR;  ///let lz identify those that are under it
  node(){
    L = R = NULL;
    val = lzL = lzR = 0;
  }
} * trees[N];
Node * upd(Node * root, int s, int e, int l, int r){
//  cout << s << ' ' << e << '\n';
  ///add 1 to all elements between l and r
  ///this part is pretty trick
  ///if this is out of range forget it
  if(r<s||e<l)
    return root;
  if(l <= s && e <= r){
    Node * ret = new Node();
    ///remember the lz isn't for you it's for your children
    if(s != e){
      ret->lzL = root->lzL + 1;
      ret->lzR = root->lzR + 1;
    }
    ret->val = root->val + 1;
    ret->L = root->L, ret->R = root->R;
//    cout << "F " << ret->val << '\n';
    return ret;
  }
  int md = (s + e) >> 1;
  ///remember to push this one before doing anything
  Node * ret = new Node();
  ret->L = root->L, ret->R = root->R;
  if(l <= md){  /// i need the left child
    if(!root->L)root->L = new Node();
    root->L->val += root->lzL;
    root->L->lzL += root->lzL;
    root->L->lzR += root->lzL;
    root->lzL = 0;
    ret->L = upd(root->L,s,md,l,r);
  }
  if(r > md){ /// i need the right child
    if(!root->R)root->R = new Node();
    root->R->val += root->lzR;
    root->R->lzL += root->lzR;
    root->R->lzR += root->lzR;
    root->lzR = 0;
    ret->R = upd(root->R,md+1,e,l,r);
  }
  ret->lzL = root->lzL, ret->lzR = root->lzR;
  int x, y;
  if(ret->L)x = ret->L->val + ret->lzL;
  else x = ret->lzL;
  if(ret->R)y = ret->R->val + ret->lzR;
  else y = ret->lzR;
  ret->val = min(x,y);
  return ret;
}
int qry(Node * root, int s, int e, int l, int r){
//  cout << root->val << ' ' << s << ' ' << e << ' ' << l << ' ' << r << ' ' << root->lzL << ' ' << root->lzR << '\n';
  if(r < s || e < l)
    return INT_MAX;
  if(!root)return 0;
  if(l <= s && e <= r){
    ///fokk mn el lazy dh lel children ana 3yz just el value
    return root->val;
  }
  int md = (s + e) >> 1, ret = INT_MAX;
  if(l <= md){  /// i need the left child
    if(!root->L)root->L = new Node();
    root->L->val += root->lzL;
    root->lzL = 0;
    ret = min(ret, qry(root->L,s,md,l,r));
  }
  if(r > md){ /// i need the right child
    if(!root->R)root->R = new Node();
    root->R->val += root->lzR;
    root->lzR = 0;
    ret = min(ret, qry(root->R,md+1,e,l,r));
  }
  return ret;
}
int main(){
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(0);cin.tie(0);
#endif // ONLINE_JUDGE

  cin >> n >> m;
  vector<int> ord;
  for(int i = 0; i < n; i++)
    cin >> a[i], ord.pb(a[i]);
  ord.pb(-1);
  sort(ord.begin(), ord.end());
  ord.resize(unique(ord.begin(), ord.end()) - ord.begin());

  for(int i = 0; i < n; i++){
    a[i] = lower_bound(ord.begin(),ord.end(), a[i]) - ord.begin();
    go[a[i]].pb(i);
  }

  trees[0] = new Node();

  for(int i = 1; i < ord.size(); i++){
    trees[i] = trees[i-1];
    for(auto x : go[i]){
      ///check those from x-m+1 to x
      int l = max(0,x-m+1), r = min(x,n-m);
      trees[i] = upd(trees[i], 0, n - m, l, r);
//      cout << ord[i] << ' ' << l << ' ' << r << '\n';
    }
  }

  cin >> q;
  int ans = 0;
  while(q--){
    int l, r, v;
    cin >> l >> r >> v;
    v ^= ans;
    --l, --r, --v;
    ///find the biggest number less than or equal to v
    int idx = upper_bound(ord.begin(), ord.end(), v) - ord.begin(); --idx;
//    cout << "H" << ord[idx] << '\n';
    cout << (ans = qry(trees[idx], 0, n - m, l, r)) << '\n';
  }

  return 0;
}

#include<cstdio>
#include<algorithm>
using namespace std;
inline int read()
{
	char c=getchar();
	int out=0;
	while(!(c>='0'&&c<='9'))c=getchar();
	while(c>='0'&&c<='9')out=out*10+c-'0',c=getchar();
	return out;
}
struct node
{
	unsigned long long mx:18,lc:23,rc:23;
}t[8000010];
int nodecnt;
#define lmax (l==mid)?int(t[rt].lc):int(t[t[rt].lc].mx)
#define rmax (mid+1==r)?int(t[rt].rc):int(t[t[rt].rc].mx)
int modify(int p,int l,int r,int ml,int mr)
{
	if(l==r)return p+1;
	int rt=++nodecnt;
	t[rt]=t[p];
	if(ml<=l&&r<=mr)
	{
		t[rt].mx++;
		return rt;
	}
	int mid=(l+r)>>1,tag=t[p].mx-max(lmax,rmax);
	if(ml<=mid)t[rt].lc=modify(t[p].lc,l,mid,ml,mr);
	if(mr>mid)t[rt].rc=modify(t[p].rc,mid+1,r,ml,mr);
	t[rt].mx=max(lmax,rmax)+tag;
	return rt;
}
int query(int rt,int l,int r,int ql,int qr)
{
	if(!rt)return 0;
	if(ql<=l&&r<=qr)return t[rt].mx;
	int mid=(l+r)>>1;
	int tag=t[rt].mx-max(lmax,rmax),ret=0;
	if(ql<=mid)ret=query(t[rt].lc,l,mid,ql,qr);
	if(qr>mid)ret=max(ret,query(t[rt].rc,mid+1,r,ql,qr));
	return ret+tag;
}
int n,m,Q,lastans,rt[200010];
pair<int,int> a[200010];
int main()
{
	n=read();m=read();
	for(int i=1;i<=n;i++)a[i].first=-read(),a[i].second=i;
	sort(&a[1],&a[n+1]);
	for(int i=1;i<=n;i++)
	{
		int ql=max(1,a[i].second-m+1);
		int qr=min(n-m+1,a[i].second);
		rt[i]=modify(rt[i-1],1,n-m+1,ql,qr);
	}
	scanf("%d",&Q);
	while(Q--)
	{
		int l,r,x;
		l=read();r=read();x=read();
		x^=lastans;
		int id=upper_bound(&a[1],&a[n+1],make_pair(-x,0x3f3f3f3f))-a-1;
		printf("%d\n",lastans=m-query(rt[id],1,n-m+1,l,r));
	}
	return 0;
}

#include <bits/stdc++.h>

#define x first
#define y second
#define pb push_back
using namespace std;
using pii = pair<int, int>;

using VI = vector<int>;
const int NN = 200002;
int n, m;
int root[NN];
pii a[NN];

struct node {
	unsigned long long mn : 18, ls : 23, rs : 23;
} mem[7088800];

#define mid (st+ed>>1)
int cur;

inline void push_up(int u) {
	mem[u].mn += min(mem[mem[u].ls].mn, mem[mem[u].rs].mn);
}

int update(int u, int st, int ed, int l, int r) {
	if(l >= ed or r <= st) return u;
	int ans = cur++;
	mem[ans] = mem[u];
	if(l <= st and ed <= r) {
		mem[ans].mn++;
		return ans;
	}
	mem[ans].mn -= min(mem[mem[ans].ls].mn, mem[mem[ans].rs].mn);
	mem[ans].ls = update(mem[ans].ls, st, mid, l, r);
	mem[ans].rs = update(mem[ans].rs, mid, ed, l, r);
	mem[ans].mn += min(mem[mem[ans].ls].mn, mem[mem[ans].rs].mn);
	return ans;
}

int calc(int u, int st, int ed, int l, int r, int f=0) {
	if(l >= ed or r <= st) return 0x3f3f3f3f;
	f += mem[u].mn;
	if(l <= st and ed <= r) return f;
	f -= min(mem[mem[u].ls].mn, mem[mem[u].rs].mn);
	return min(calc(mem[u].ls, st, mid, l, r, f), calc(mem[u].rs, mid, ed, l, r, f));
}

int solve() {
	cin >> n >> m;
	for(int i=0; i<n; i++) scanf("%d", &a[i].x), a[i].y = i+1;
	sort(a, a+n);
	root[0] = cur++;
	for(int i=1; i<=n; i++) root[i] = update(root[i-1], 0, n, max(0, a[i-1].y-m), a[i-1].y);
	int q;
	cin >> q;
	int ans = 0;
	int l, r, x;
	while(q--) {
		scanf("%d %d %d", &l, &r, &x);
		x ^= ans;
		x = lower_bound(a, a+n, pii(x, 0)) - a;
		ans = calc(root[x], 0, n, l-1, r);
		printf("%d\n", ans);
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif // ONLINE_JUDGE
	solve();
}

#include <cmath>
#include <map>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <set>
#include <bitset>
#include <memory.h>
#include <functional>
#include <queue>
#include <fstream>
#include <ctime>
#include <deque>
#include <utility>
#include <stack>
#include <sstream>
#include <list>
#include <cctype>
#include <numeric>
#include <iomanip>
#include <assert.h>
using namespace std;
#define sqr(_) ((_)*(_))

typedef long long LL;
typedef long double ld;

typedef pair<int, int> Pii;
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()

const int MAXN = (int)2e5 + 10, L = 500;

vector<int> v, w[MAXN / L + 10];
int n, m, a[MAXN], b[MAXN / L + 10][L * 2], c[MAXN / L + 10][L * 2];

int pos(int x, int k)
{
	if (k == -1)
		return x - 1;
	return x - 1 + m;
}
int Query(int l, int r, int y)
{
//	puts("******");
	int pres = lower_bound(ALL(v), y) - v.begin(), Tohka = (int)1e9;
	for (int i = 0; i * L < n - m + 1 && i * L <= r; ++i)
	{
		int p = lower_bound(ALL(w[i]), y) - w[i].begin() - 1;
		if (p < 0)
		{
			if ((i * L <= l && l <= i * L + L - 1) ||
				(i * L <= r && r <= i * L + L - 1))
				Tohka = min(Tohka, pres);
			continue ;
		}
		if (l <= i * L && i + L - 1 <= r)
			Tohka = min(Tohka, pres + c[i][p]);
		else
		if ((i * L <= l && l <= i * L + L - 1) ||
			(i * L <= r && r <= i * L + L - 1))
		{
			for (int j = i * L, s = pres; j < min(n - m + 1, (i + 1) * L); ++j)
			{
				for (int k = -1; k <= 1; k += 2)
				{
					int x = pos(j, k);
					if (!(0 <= x && x < n) || a[x] >= y) continue ;
					s += k;
				}
				if (l <= j && j <= r)
					Tohka = min(Tohka, s);
			}
		}
		pres += b[i][p];
	}
	return Tohka;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif

	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; ++i)
		scanf("%d", &a[i]);
	for (int i = 0; i < m - 1; ++i)
		v.pb(a[i]);
	sort(ALL(v));
	for (int i = 0; i * L < n - m + 1; ++i)
		for (int j = i * L; j < min(n - m + 1, (i + 1) * L); ++j)
			for (int k = -1; k <= 1; k += 2)
			{
				int x = pos(j, k);
				if (!(0 <= x && x < n)) continue ;
				w[i].pb(a[x]);
			}
	for (int i = 0; i * L < n - m + 1; ++i)
	{
		sort(ALL(w[i]));
		w[i].erase(unique(ALL(w[i])), w[i].end());
		for (int p = 0; p < SZ(w[i]); ++p)
		{
			c[i][p] = (int)1e9;
			for (int j = i * L; j < min(n - m + 1, (i + 1) * L); ++j)
			{
				for (int k = -1; k <= 1; k += 2)
				{
					int x = pos(j, k);
					if (!(0 <= x && x < n) || a[x] > w[i][p]) continue ;
					b[i][p] += k;
				}
				c[i][p] = min(c[i][p], b[i][p]);
			}
		}
	}
	int q, Tohka = 0;
	scanf("%d", &q);
	for (int i = 1; i <= q; ++i)
	{
		int l, r, x;
		scanf("%d%d%d", &l, &r, &x);
		x ^= Tohka;
//		cout << l << " " << r << " " << x << endl;
		printf("%d\n", Tohka = Query(l - 1, r - 1, x));
	}

	fclose(stdin);
	fclose(stdout);
	return 0;
}

// {{{ by shik
#include <bits/stdc++.h>
#include <unistd.h>
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x),end(x)
#define REP(i,n) for ( int i=0; i<int(n); i++ )
#define REP1(i,a,b) for ( int i=(a); i<=int(b); i++ )
#define FOR(it,c) for ( auto it=(c).begin(); it!=(c).end(); it++ )
#define MP make_pair
#define PB push_back
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

#ifdef SHIK
template<typename T>
void _dump( const char* s, T&& head ) { cerr<<s<<"="<<head<<endl; }

template<typename T, typename... Args>
void _dump( const char* s, T&& head, Args&&... tail ) {
    int c=0;
    while ( *s!=',' || c!=0 ) {
        if ( *s=='(' || *s=='[' || *s=='{' ) c++;
        if ( *s==')' || *s==']' || *s=='}' ) c--;
        cerr<<*s++;
    }
    cerr<<"="<<head<<", ";
    _dump(s+1,tail...);
}

#define dump(...) do { \
    fprintf(stderr, "%s:%d - ", __PRETTY_FUNCTION__, __LINE__); \
    _dump(#__VA_ARGS__, __VA_ARGS__); \
} while (0);

template<typename Iter>
ostream& _out( ostream &s, Iter b, Iter e ) {
    s<<"[";
    for ( auto it=b; it!=e; it++ ) s<<(it==b?"":" ")<<*it;
    s<<"]";
    return s;
}

template<typename A, typename B>
ostream& operator <<( ostream &s, const pair<A,B> &p ) { return s<<"("<<p.first<<","<<p.second<<")"; }
template<typename T>
ostream& operator <<( ostream &s, const vector<T> &c ) { return _out(s,ALL(c)); }
template<typename T>
ostream& operator <<( ostream &s, const set<T> &c ) { return _out(s,ALL(c)); }
template<typename A, typename B>
ostream& operator <<( ostream &s, const map<A,B> &c ) { return _out(s,ALL(c)); }
#else
#define dump(...)
#endif

void RI() {}

template<typename... T>
void RI( int& head, T&... tail ) {
    scanf("%d",&head);
    RI(tail...);
}
// }}}

const int N=2e5+10;

struct Seg {
    static Seg mem[39 * N], *pmem;
    uint64_t lc:23, rc:23, mx:18;
    Seg() {}
    Seg(int l, int r) : mx(0) {
        if (l == r) return;
        int m = (l + r) / 2;
        lc = new (pmem++) Seg(l, m) - mem;
        rc = new (pmem++) Seg(m + 1, r) - mem;
    }
    Seg* _lc() { return &mem[lc]; }
    Seg* _rc() { return &mem[rc]; }
    int c_max() { return max(_lc()->mx, _rc()->mx); }
    Seg* ins(int l, int r, int ql, int qr) {
        Seg *t = new (pmem++) Seg(*this);
        if (ql <= l && r <= qr) {
            t->mx++;
        } else {
            int add = t->mx - t->c_max();
            t->_lc()->mx += add;
            t->_rc()->mx += add;
            int m = (l + r) / 2;
            if (ql <= m) t->lc = t->_lc()->ins(l, m, ql, qr) - mem;
            if (qr > m) t->rc = t->_rc()->ins(m + 1, r, ql, qr) - mem;
            t->mx = t->c_max();
        }
        return t;
    }
    int get_max(int l, int r, int ql, int qr) {
        if (ql <= l && r <= qr) {
            return mx;
        } else {
            int m = (l + r) / 2;
            int ret = 0;
            if (ql <= m) ret = max(ret, _lc()->get_max(l, m, ql, qr));
            if (qr > m) ret = max(ret, _rc()->get_max(m + 1, r, ql, qr));
            return ret + (mx - c_max());
        }
    }
} Seg::mem[39 * N], *Seg::pmem = mem;

int n,m,a[N];
PII _a[N];
Seg *seg[N];
int main() {
    RI(n,m);
    REP1(i,1,n) RI(a[i]);
    REP1(i,1,n) _a[i]=MP(a[i],i);
    sort(_a+1,_a+n+1);
    seg[n+1] = new (Seg::pmem++) Seg(1, n);
    for ( int i=n; i>=1; i-- ) {
        int r=_a[i].second;
        int l=max(1,r-m+1);
        seg[i]=seg[i+1]->ins(1,n,l,r);
    }
    int s,ans=0;
    RI(s);
    while ( s-- ) {
        int l,r,x;
        RI(l,r,x);
        x^=ans;
        int idx=lower_bound(_a+1,_a+n+1,MP(x,0))-_a;
        ans=m-seg[idx]->get_max(1,n,l,r);
        printf("%d\n",ans);
    }
    return 0;
}

#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
const int maxn = 200001, maxm = 451, maxc = 1351;
int n, m, q, a[maxn], ord[maxn];
int sz, tot, cnt[maxm], pos[maxm][maxc];
int qL[maxn], qR[maxn], mx[maxm][maxc], fir[maxm][maxc];
int seq[maxn], dt[maxc], ans;
bool cmp(int const &x, int const &y)
{
	return a[x] > a[y];
}
int main()
{
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; ++i)
	{
		scanf("%d", a + i);
		ord[i] = i;
	}
	std::sort(ord + 1, ord + n + 1, cmp);
	sz = (int)ceil(sqrt(n));
	for(int i = 1; i <= n; i += sz, ++tot)
	{
		int L = i, R = std::min(i + sz - 1, n);
		cnt[tot] = 0;
		for(int j = 1; j <= n; j += sz)
			pos[tot][cnt[tot]++] = j;
		pos[tot][cnt[tot]++] = n + 1;
		for(int j = L; j <= R; ++j)
		{
			pos[tot][cnt[tot]++] = (qL[j] = std::max(1, ord[j] - m + 1));
			pos[tot][cnt[tot]++] = (qR[j] = ord[j] + 1);
		}
		std::sort(pos[tot], pos[tot] + cnt[tot]);
		cnt[tot] = std::unique(pos[tot], pos[tot] + cnt[tot]) - pos[tot];
		memset(dt, 0, cnt[tot] * sizeof(int));
		for(int j = L; j <= R; ++j)
		{
			qL[j] = std::lower_bound(pos[tot], pos[tot] + cnt[tot], qL[j]) - pos[tot];
			qR[j] = std::lower_bound(pos[tot], pos[tot] + cnt[tot], qR[j]) - pos[tot];
			++dt[qL[j]];
			--dt[qR[j]];
		}
		for(int j = 0; j + 1 < cnt[tot]; ++j)
		{
			int pL = pos[tot][j], pR = pos[tot][j + 1];
			for(fir[tot][j] = mx[tot][j] = seq[pL++]; pL < pR; ++pL)
				if(mx[tot][j] < seq[pL])
					mx[tot][j] = seq[pL];
			for(pL = pos[tot][j]; pL < pR; ++pL)
				seq[pL] += dt[j];
			dt[j + 1] += dt[j];
		}
	}
	scanf("%d", &q);
	while(q--)
	{
		int L, R, idx;
		scanf("%d%d%d", &L, &R, a + 0);
		a[0] ^= ans;
		idx = std::upper_bound(ord + 1, ord + n + 1, 0, cmp) - ord - 1;
		int blk = std::min(idx / sz, tot - 1);
		memset(dt, 0, cnt[blk] * sizeof(int));
		for(int i = blk * sz + 1; i <= idx; ++i)
		{
			++dt[qL[i]];
			--dt[qR[i]];
		}
		ans = 0;
		for(int i = 0; i + 1 < cnt[blk]; ++i)
		{
			dt[i + 1] += dt[i];
			if(L <= pos[blk][i] && pos[blk][i + 1] <= R + 1)
				ans = std::max(ans, mx[blk][i] + dt[i]);
			else if(pos[blk][i] <= R && L < pos[blk][i + 1])
				for(int j = pos[blk][i], val = fir[blk][i] + dt[i]; j < pos[blk][i + 1]; ++j)
				{
					if(j >= L)
						ans = std::max(ans, val);
					else if(j > R)
						break;
					val -= (a[j] >= a[0]);
					val += (a[j + m] >= a[0]);
				}
		}
		printf("%d\n", ans = m - ans);
	}
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
using namespace std;

const int N = 200010;
#define x first
#define y second
unsigned long long a[N * 40];
const int b18 = (1 << 18) - 1;
const int b23 = (1 << 23) - 1;
int cnt;

void update(int &x, int y, int l, int r, int L, int R) {
	x = ++cnt;
	while (x >= N * 40) {
		exit(0);
	}
	int rc = a[y] & b23;
	int lc = (a[y] >> 23) & b23;
	unsigned long long mx = a[y] >> 46;
	if (l == L && r == R) {
		mx++;
		a[x] = (mx << 46) | ((unsigned long long)lc << 23) | rc;
	    return;
	}

    int mid = (l + r) >> 1;
    int lazy = mx - max(a[lc] >> 46, a[rc] >> 46);
	if (R <= mid) update(lc, lc, l, mid, L, R);
	else if (L > mid) update(rc, rc, mid + 1, r, L, R);
	else {
		update(lc, lc, l, mid, L, mid);
		update(rc, rc, mid + 1, r, mid + 1, R);
	}

	mx = max(a[lc] >> 46, a[rc] >> 46);
	mx += lazy;
	a[x] = (mx << 46) | ((unsigned long long)lc << 23) | (unsigned long long)rc;
}

int query(int x, int l, int r, int L, int R) {
	int rc = a[x] & b23;
	int lc = (a[x] >> 23) & b23;
	int mx = a[x] >> 46;
	if (l == L && R == r) {
		return mx;
	}

	int mid = (l + r) >> 1;
	int lazy = mx - max(a[lc] >> 46, a[rc] >> 46);
	if (R <= mid) return lazy + query(lc, l, mid, L, R);
	if (L > mid) return lazy + query(rc, mid + 1, r, L, R);
	return lazy + max(query(lc, l, mid, L, mid), query(rc, mid + 1, r, mid + 1, R));
}

pair<int, int> pi[N];
int root[N];

int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
	    scanf("%d", &pi[i].x);
		pi[i].y = i;
	}

	sort(pi + 1, pi + n + 1);
	for (int i = n; i >= 1; i--) {
		int l = max(1, pi[i].y - m + 1);
		int r = min(n - m + 1, pi[i].y);
		update(root[i], root[i + 1], 1, n - m + 1, l, r);
	}

	int q;
	scanf("%d", &q);
	int ans = 0;
	while (q--) {
		int l, r, x;
		scanf("%d%d%d", &l, &r, &x);
		x ^= ans;
		if (x <= pi[1].x) {
			ans = 0;
		} else if (x > pi[n].x) {
			ans = m;
		} else {
			int lt = 1, rt = n, mid, pos;
			while (lt <= rt) {
				mid = (lt + rt) >> 1;
				if (pi[mid].x >= x) {
					pos = mid;
					rt = mid - 1;
				} else lt = mid + 1;
			}

			ans = m - query(root[pos], 1, n - m + 1, l, r);
		}
		printf("%d\n", ans);
	}

	return 0;
}

#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>

using namespace std;

#define type(x) __typeof((x).begin())
#define foreach(i, x) for(type(x) i = (x).begin(); i != (x).end(); i++)

typedef long long ll;
typedef pair < int, int > ii;

const int inf = 1e9 + 333;
const ll linf = 1e18 + 333;

const int N = 2e5 + 5;
const int SIZE = 8000000;

class tree{ public:
    unsigned long long min : 18, l : 23, r : 23;
    tree() {
        min = l = r = 0;
    }
};

int n, m, q, cnt, x1, x2, t[N];
ii a[N];
tree p[SIZE];

int insert(int t, int l, int r) {

    if(l == r)
        return t + 1;

    int x = ++cnt;

    if(x1 <= l and r <= x2) {
        p[x] = p[t];
        p[x].min++;
        return x;
    }

    int m = l + r >> 1;

    p[x] = p[t];

    int add = p[x].min - min((l == m ? p[x].l : p[p[x].l].min), (m + 1 == r ? p[x].r : p[p[x].r].min));

    if(x1 <= m)
        p[x].l = insert(p[t].l, l, m);
    if(x2 > m)
        p[x].r = insert(p[t].r, m + 1, r);

    p[x].min = min((l == m ? p[x].l : p[p[x].l].min), (m + 1 == r ? p[x].r : p[p[x].r].min)) + add;

    return x;

}

int query(int x, int l, int r) {

    if(x2 < l or r < x1)
        return inf;

    if(l == r)
        return x;

    if(x1 <= l and r <= x2)
        return p[x].min;

    int m = l + r >> 1;

    int add = p[x].min - min((l == m ? p[x].l : p[p[x].l].min), (m + 1 == r ? p[x].r : p[p[x].r].min));

    return min(query(p[x].l, l, m), query(p[x].r, m + 1, r)) + add;

}

int main () {

    scanf("%d %d", &n, &m);

    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i].first);
        a[i].second = i;
    }

    sort(a + 1, a + n + 1);

    t[0] = ++cnt;

    for(int i = 1; i <= n; i++) {
        x1 = max(1, a[i].second - m + 1);
        x2 = min(a[i].second, n - m + 1);
        t[i] = insert(t[i - 1], 1, n - m + 1);
    }

    scanf("%d", &q);

    int ans = 0;

    for(int qq = 0; qq < q; qq++) {
        int x;
        scanf("%d %d %d", &x1, &x2, &x);
        x = lower_bound(a + 1, a + n + 1, ii(x ^ ans, 0)) - a - 1;
        ans = query(t[x], 1, n - m + 1);
        printf("%d\n", ans);
    }

    return 0;

}

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<utility>
#define M 19
#define N 200010
#define B (N*2)
#define inf 1023456789
using namespace std;
//sum*(2N)^2+min*(2N)+lc
//offset=N
long long tr[M][N*2];
int buffer[2][N*2],a[N],p,mlen;
int nxt[N];
char single[N];
const long long zero=1LL*N*B*B+1LL*N*B+N;
bool cmp(int i,int j){
	return a[abs(i)]<a[abs(j)];
}
void build(int n,int m){
	int i,l,r,md,nid,sum,mn,lsum,rsum,lmn,rmn,lc,rc,lv=0;
	bool final;
	for(i=0;i<n-m;i++){
		buffer[p][i*2]=i;
		buffer[p][i*2+1]=-(i+m);
	}
	sort(buffer[p],buffer[p]+(n-m)*2,cmp);
	nxt[0]=n-m;
	while(1){
		final=true;
		for(l=0;l<n-m;l=r){
			r=nxt[l];
			if(l+1==r){
				single[l]++;
				if(buffer[p][l*2]>=0){
					tr[lv][l*2]=zero-1LL*B*B-B;
					tr[lv][l*2+1]=zero;
				}
				else{
					tr[lv][l*2]=zero+1LL*B*B;
					tr[lv][l*2+1]=zero;
				}
				continue;
			}
			final=false;
			md=(l+r)/2;
			nxt[l]=md;
			nxt[md]=r;
			lc=l*2;
			rc=md*2;
			for(i=l*2;i<r*2;i++){
				if(buffer[p][i]>=0){
					nid=buffer[p][i];
				}
				else{
					nid=-buffer[p][i]-m;
				}
				if(nid<md){
					buffer[p^1][lc++]=buffer[p][i];
				}
				else{
					buffer[p^1][rc++]=buffer[p][i];
				}
				tr[lv][i]=zero+lc-1;
			}
		}
		if(final) break;
		p^=1;
		lv++;
	}
	lv--;
	while(lv>=0){
		for(l=0;l<n-m;l=nxt[l]){
			if(single[l]>1){
				single[l]--;
				continue;
			}
			md=nxt[l];
			nxt[l]=r=nxt[md];
			if(l+1==r){
				continue;
			}
			for(i=l*2;i<r*2;i++){
				lc=tr[lv][i]%B-N;
				rc=md*2+(i-lc)-1;
				if(lc<l*2){
					lsum=lmn=0;
				}
				else{
					if(lc>=md*2){
						printf("WTF\n");
						exit(0);
					}
					lsum=tr[lv+1][lc]/B/B-N;
					lmn=tr[lv+1][lc]/B%B-N;
				}
				if(i==lc){
					rsum=rmn=0;
				}
				else{
					if(rc>=r*2||rc<md*2){
						printf("wtong %d in range(%d,%d)\n",rc,md*2,r*2);
						exit(0);
					}
					rsum=tr[lv+1][rc]/B/B-N;
					rmn=tr[lv+1][rc]/B%B-N;
				}
				sum=lsum+rsum;
				mn=min(lsum+rmn,lmn);
				tr[lv][i]+=1LL*sum*B*B+1LL*mn*B;
			}
		}
		lv--;
	}
	memcpy(buffer[0],a,sizeof(int)*m);
	sort(buffer[0],buffer[0]+m);
	memcpy(buffer[1],a,sizeof(int)*(n-m));
	memcpy(buffer[1]+n-m,a+m,sizeof(int)*(n-m));
	sort(buffer[1],buffer[1]+(n-m)*2);
}
int qpsum(int lv,int l,int r,int qr,int i){
	if(i<l*2) return 0;
	if(lv>=M||i<0||i>=N*2){
		fprintf(stderr,"GGqsum\n");
		exit(0);
	}
	if(qr==r) return tr[lv][i]/B/B-N;
	int md=(l+r)/2,lc=tr[lv][i]%B-N,rc=md*2+i-lc-1;
	if(qr<=md) return qpsum(lv+1,l,md,qr,lc);
	else return qpsum(lv+1,l,md,md,lc) + qpsum(lv+1,md,r,qr,rc);
}
//first:sum second:min
//return value: (sum+N)*B+(min+N)
long long qry(int lv,int l,int r,int ql,int qr,int i){
	if(i<l*2) return zero/B;
	if(lv>=M||i<0||i>=N*2){
		fprintf(stderr,"GGqry\n");
		exit(0);
	}
	if(l==ql&&r==qr){
		return tr[lv][i]/B;
	}
	int md=(l+r)/2,lc=tr[lv][i]%B-N,rc=md*2+i-lc-1;
	if(qr<=md){
		return qry(lv+1,l,md,ql,qr,lc);
	}
	else if(ql>=md){
		return qry(lv+1,md,r,ql,qr,rc);
	}
	else{
		long long lres=qry(lv+1,l,md,ql,md,lc);
		long long rres=qry(lv+1,md,r,md,qr,rc);
		return (lres/B+rres/B-N)*B+min(lres%B,lres/B+rres%B-N);
	}
}
int cal(int l,int r,int n,int m,int q){
	l--;
	r--;
	int base=lower_bound(buffer[0],buffer[0]+m,q)-buffer[0];
	int i=(upper_bound(buffer[1],buffer[1]+(n-m)*2,q-1)-buffer[1])-1;
	return base+(l?qpsum(0,0,n-m,l,i):0)+(l<r?qry(0,0,n-m,l,r,i)%B-N:0);
}
int main(){
	int n,m,q,ans=0,l,r,x,i;
	scanf("%d%d",&n,&m);
	for(i=0;i<n;i++) scanf("%d",&a[i]);
	build(n,m);
	if(n>=200000){
		printf("build done\n");
		exit(0);
	}
	scanf("%d",&q);
	while(q--){
		scanf("%d%d%d",&l,&r,&x);
		x^=ans;
		ans=cal(l,r,n,m,x);
		printf("%d\n",ans);
	}
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define REP(i, a, b) for (int i = (a), _end_ = (b); i < _end_; ++i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define EXIT(...) printf(__VA_ARGS__), exit(0)

template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

typedef long long LL;

const int oo = 0x3f3f3f3f;

int now;

struct node;

node *newnode();

struct node
{
	node *c[2];

	LL val;

	node(): val(0) { memset(c, 0, sizeof c); }

	int Min() { return val & 1048575; }
	int label() { return (val >> 20) & 1048575; }
	int t() { return (val >> 40) & 1048575; }

	void update()
	{
		int ret = oo;
		REP(i, 0, 2)
			if (c[i]) chkmin(ret, c[i]->Min());
			else ret = 0;
		val >>= 20;
		(val <<= 20) |= ret;
	}

	void flag_label(int _label)
	{
		val += _label * 1048577;
	}

	void push_down()
	{
		int tmp = label();
		if (tmp)
		{
			REP(i, 0, 2)
			{
				if (!c[i]) c[i] = newnode();
				else if (c[i]->t() != now)
				{
					node *tmp = newnode();
					*tmp = *c[i];
					c[i] = tmp;
					c[i]->val &= (1LL << 40) - 1;
					c[i]->val |= (LL)now << 40;
				}
				c[i]->flag_label(tmp);
			}
			val ^= (LL)tmp << 20;
		}
	}

	inline int get_Min() { return this ? Min() : 0; }

};

const int max0 = 100000;

node *nd_pool;
int nd_res = 0;

node *newnode()
{
	if (!nd_res) nd_pool = new node[max0], nd_res = max0;
	return nd_pool + (--nd_res);
}

const int maxn = 200000;

int n, m;
int a[maxn + 5];

node *rt[maxn + 5];

int seg_x, seg_y, seg_z;

void add(node *&rt, int l, int r)
{
	if (!rt) rt = newnode();
	else if (rt->t() != now)
	{
		node *tmp = newnode();
		*tmp = *rt;
		rt = tmp;
		rt->val &= (1LL << 40) - 1;
		rt->val |= (LL)now << 40;
	}
	if (seg_x <= l && r <= seg_y)
	{
		rt->flag_label(seg_z);
		return;
	}
	int mid = (l + r) >> 1;
	rt->push_down();
	if (seg_x < mid) add(rt->c[0], l, mid);
	if (seg_y > mid) add(rt->c[1], mid, r);
	rt->update();
}

inline bool seg_add(node *&rt, int x, int y, int z)
{
	if (x >= y) return 0;
	seg_x = x, seg_y = y, seg_z = z;
	add(rt, 0, n);
	return 1;
}

void query(node *rt, int l, int r, int sum = 0)
{
	if (!rt)
	{
		chkmin(seg_z, sum);
		return;
	}
	if (seg_x <= l && r <= seg_y)
	{
		chkmin(seg_z, sum + rt->Min());
		return;
	}
	sum += rt->label();
	int mid = (l + r) >> 1;
	if (seg_x < mid) query(rt->c[0], l, mid, sum);
	if (seg_y > mid) query(rt->c[1], mid, r, sum);
}

inline int seg_query(node *rt, int x, int y)
{
	if (x >= y) return oo;
	seg_x = x, seg_y = y, seg_z = oo;
	query(rt, 0, n);
	return seg_z;
}

inline bool cmp(int x, int y) { return a[x] < a[y]; }

int main()
{
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	scanf("%d%d", &n, &m);
	static int pos[maxn + 5];
	REP(i, 0, n) scanf("%d", a + i), pos[i] = i;
	sort(pos, pos + n, cmp);
	vector<int> X;
	REP(i, 0, n) X.pb(a[pos[i]]);
	rt[0] = NULL;
	REP(i, 0, n)
	{
		now = i + 1;
		rt[i + 1] = rt[i];
		seg_add(rt[i + 1], max(pos[i] - m + 1, 0), pos[i] + 1, 1);
	}
	int qn;
	scanf("%d", &qn);
	int lastans = 0;
	REP(i, 0, qn)
	{
		int x, y, z;
		scanf("%d%d%d", &x, &y, &z), --x;
		z ^= lastans;
		int tmp = lower_bound(ALL(X), z) - X.begin();
		printf("%d\n", lastans = seg_query(rt[tmp], x, y));
	}
	return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define BSIZE 500

int N,M,Q;
int a[200000];
int cid[200000];
int pInc[200000],pDec[200000];

bool cmp(int x,int y)
{
	return a[x]<a[y];
}

vector<int> spoints[500];
vector<int> first[500];
vector<int> best[500];
vector<int> last[500];
int tmp[2000];

int dif[200001];

void slow(int l,int r,int x)
{
	int ans = 1000000000;
	for(int i=l;i<=r;i++)
	{
		int cur = 0;
		for(int j=i;j<i+M;j++)
			if(a[j]<x)
				cur++;
		ans = min(ans,cur);
	}
	cout << ans << '\n';
}

int main()
{
	ios::sync_with_stdio(0);
	cin >> N >> M;
	for(int i=0;i<N;i++)
	{
		cin >> a[i];
		cid[i] = i;
	}
	sort(cid,cid+N,cmp);
	for(int i=0;i<N;i++)
	{
		spoints[i/BSIZE].push_back(max(0,cid[i]-M+1));
		spoints[i/BSIZE].push_back(cid[i]+1);
	}
	for(int i=0;i*BSIZE<N;i++)
	{
		for(int j=0;j<N;j+=BSIZE)
			spoints[i].push_back(j);
		spoints[i].push_back(N);
		sort(spoints[i].begin(),spoints[i].end());
		spoints[i].resize(distance(spoints[i].begin(),unique(spoints[i].begin(),spoints[i].end())));
	}
	for(int i=0;i<N;i++)
	{
		pInc[i] = lower_bound(spoints[i/BSIZE].begin(),spoints[i/BSIZE].end(),max(0,cid[i]-M+1))-spoints[i/BSIZE].begin();
		pDec[i] = lower_bound(spoints[i/BSIZE].begin(),spoints[i/BSIZE].end(),cid[i]+1)-spoints[i/BSIZE].begin();
	}
	int lb = ((N-1)/BSIZE);
	for(int k=0;k<spoints[lb].size();k++)
	{
		best[lb].push_back(0), first[lb].push_back(0), last[lb].push_back(0);
	}
	for(int i=N-1;i>0;i--)
	{
		dif[max(0,cid[i]-M+1)]++, dif[cid[i]+1]--;
		if(i%BSIZE == 0)
		{
			int b = (i/BSIZE)-1;
			int v = 0;
			int j = 0;
			int mx = 0;
			for(int k=0;k<=N;k++)
			{
				v += dif[k];
				if(k == spoints[b][j])
				{
					first[b].push_back(v);
					if(j)
					{
						last[b].push_back(v-dif[k]);
						best[b].push_back(mx);
					}
					mx = v;
					j++;
				}
				else mx = max(mx,v);
			}
/*			for(int k=0;k<spoints[b].size();k++)
				cout << spoints[b][k] << ' ';
			cout << '\n';
			for(int k=0;k<best[b].size();k++)
				cout << best[b][k] << ' ';
			cout << "\n\n";
*/
		}
	}
	cin >> Q;
	int l,r,x;
	int ans;
	for(int i=0;i<Q;i++)
	{
		cin >> l >> r >> x;
		if(i) x ^= ans;
		l--,r--;
		int b = 0;
		while((b+1)*BSIZE<N && a[cid[(b+1)*BSIZE]] <= x)
			b++;
		//cout << b << '\n';
		for(int j=0;j<spoints[b].size();j++)
			tmp[j] = 0;
		for(int k=min(N,(b+1)*BSIZE)-1;k>=0 && a[cid[k]] >= x;k--)
			tmp[pInc[k]]++,tmp[pDec[k]]--;
		int lBlock = lower_bound(spoints[b].begin(),spoints[b].end(),l)-spoints[b].begin();
		ans = 0;
		int v = first[b][lBlock];
		for(int j=0;j<=lBlock;j++)
			v += tmp[j];
		for(int j=spoints[b][lBlock]-1;j>=l;j--)
		{
			if(a[j]>=x) v++;
			if(j+M<N && a[j+M]>=x) v--;
			ans = max(ans,v);
		}
		int rBlock = upper_bound(spoints[b].begin(),spoints[b].end(),r)-spoints[b].begin()-1;
		v = first[b][rBlock];
		for(int j=0;j<=rBlock;j++)
			v += tmp[j];
		for(int j=spoints[b][rBlock];j<=r;j++)
		{
			ans = max(ans,v);
			if(a[j]>=x) v--;
			if(j+M<N && a[j+M]>=x) v++;
		}
		int ps = 0;
		for(int j=0;j<spoints[b].size();j++)
		{
			ps += tmp[j];
			if(j>=lBlock && j<rBlock)
				ans = max(ans,ps+best[b][j]);
		}
		ans = M-ans;
		cout << ans << '\n';
		//slow(l,r,x);
	}
}

#include <bits/stdc++.h>

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define nfor(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define fore(i, l, r) for (int i = int(l); i < int(r); i++)
#define correct(x, y, n, m) (0 <= (x) && (x) < (n) && 0 <= (y) && (y) < (m))
#define all(a) (a).begin(), (a).end()
#define sz(a) int((a).size())
#define pb(a) push_back(a)
#define mp(x, y) make_pair((x), (y))
#define x first
#define y second

using namespace std;

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

template<typename X> inline X abs(const X& a) { return a < 0? -a: a; }
template<typename X> inline X sqr(const X& a) { return a * a; }

template<typename A, typename B> inline ostream& operator<< (ostream& out, const pair<A, B>& p) { return out << "(" << p.x << ", " << p.y << ")"; }
template<typename T> inline ostream& operator<< (ostream& out, const vector<T>& a) { out << "["; forn(i, sz(a)) { if (i) out << ','; out << ' ' << a[i]; } return out << " ]"; }
template<typename T> inline ostream& operator<< (ostream& out, const set<T>& a) { return out << vector<T>(all(a)); }
template<typename T> inline ostream& operator<< (ostream& out, pair<T*, int> a) { return out << vector<T>(a.x, a.x + a.y); }

inline ld gett() { return ld(clock()) / CLOCKS_PER_SEC; }

const int INF = int(1e9);
const li INF64 = li(1e18);
const ld EPS = 1e-9, PI = 3.1415926535897932384626433832795;

const int N = 200200;

int n, m, k;
int a[N];
int l[N], r[N], x[N];

bool read() {
	if (!(cin >> n >> m)) return false;
	forn(i, n) assert(scanf("%d", &a[i]) == 1);
	assert(cin >> k);
	forn(i, k) {
		assert(scanf("%d%d%d", &l[i], &r[i], &x[i]) == 3);
		l[i]--;
	}
	return true;
}

#ifdef SU1
#define LOG
#endif

int blen;

int calc(int lf, int rg, int p, int* s) {
#ifdef LOG
	//cerr << "lf=" << lf << " rg=" << rg << " p=" << p << endl;
#endif

	int bnum = lf / blen;
	pt seg(INF, -INF);

	int blfnum = (lf + m - 1) / blen;
	int mm = m;
	vector<int> cur;
	fore(i, lf, min(n, rg + m - 1)) {
		int cbnum = i / blen;
		if (i % blen == 0 && bnum < cbnum && cbnum < blfnum) {
			seg.x = min(seg.x, cbnum);
			seg.y = max(seg.y, cbnum);
			mm -= blen;
			i += blen - 1;
		}
		else cur.pb(a[i] < p);
	}

#ifdef LOG
	//cerr << "seg=" << seg << endl;
	//cerr << cur << endl;
#endif

	forn(i, sz(cur) - 1) cur[i + 1] += cur[i];

	int ans = INF;
	fore(i, lf, rg) {
		int l = i - lf, r = l + mm;
		int val = (r ? cur[r - 1] : 0) - (l ? cur[l - 1] : 0);
		//cerr << "val=" << val << " l=" << l << " r=" << r << endl;
		if (seg.x <= seg.y) val += s[seg.y + 1] - s[seg.x];
#ifdef LOG
		//cerr << "i=" << i << " val=" << val << endl;
#endif
		ans = min(ans, val);

	}
	return ans;
}

int b[N];
pt seg[N];
vector<pt> z[N];

void solve() {
	blen = int(sqrt(n));
	forn(i, n) b[i] = a[i];
	for (int i = 0; i < n; i += blen) {
		int j = min(n, i + blen);
		sort(b + i, b + j);

		if (i + m <= n) {
			int bnum = i / blen;
			seg[bnum] = pt(INF, -INF);

			int binum = (i + m - 1) / blen;
			int mm = m;
			vector<int> cur;
			fore(k, i, min(n, j + m - 1)) {
				int cbnum = k / blen;
				if (k % blen == 0 && bnum < cbnum && cbnum < binum) {
					seg[bnum].x = min(seg[bnum].x, cbnum);
					seg[bnum].y = max(seg[bnum].y, cbnum);
					mm -= blen;
					k += blen - 1;
				} else cur.pb(a[k]);
			}
			fore(k, i, j) assert((k + m <= n) == (k - i + mm <= sz(cur)));

			z[bnum].clear();
			forn(k, sz(cur)) z[bnum].pb(mp(cur[k], INF));
			z[bnum].pb(pt(INT_MAX, INF));
			sort(all(z[bnum]));
			z[bnum].erase(unique(all(z[bnum])), z[bnum].end());

			static int t[N];
			forn(q, sz(cur)) t[q] = 0;
			auto inc = [&](int i, int val) { for ( ; i < sz(cur); i |= i + 1) t[i] += val; };
			auto sum1 = [&](int i) { int ans = 0; for ( ; i >= 0; i = (i & (i + 1)) - 1) ans += t[i]; return ans; };
			auto sum = [&](int i, int j) { return sum1(j) - sum1(i - 1); };

			int ptr = 0;
			vector<pt> pts; forn(k, sz(cur)) pts.pb(pt(cur[k], k));
			sort(all(pts));
			forn(k, sz(z[bnum])) {
				int p = z[bnum][k].x;
				int& ansv = z[bnum][k].y;
				while (ptr < sz(pts) && pts[ptr].x < p) inc(pts[ptr++].y, +1);
				forn(q, sz(cur) - mm + 1) {
					assert(q + mm <= sz(cur));
					int curv = sum(q, q + mm - 1);
					ansv = min(ansv, curv);
				}
			}
			assert(ptr == sz(pts));
		}
	}

	int ans = 0;
	forn(i, k) {
		int p = x[i] ^ ans;

		static int s[N];
		for (int j = 0; j < n; j += blen) {
			int k = min(n, j + blen);
			int bnum = j / blen;
			int cnt = int(lower_bound(b + j, b + k, p) - (b + j));
			//cerr << "j=" << j << " cnt=" << cnt << endl;
			s[bnum + 1] = s[bnum] + cnt;
		}
		//cerr << mp(s, (n + blen - 1) / blen + 1) << endl;

		ans = INF;
		int bl = l[i] / blen, br = (r[i] - 1) / blen;
		fore(bnum, bl + 1, br) {
			int cur = lower_bound(all(z[bnum]), pt(p, -INF))->y;
			pt p = seg[bnum];
			if (p.x < p.y) cur += s[p.y + 1] - s[p.x];
			ans = min(ans, cur);
		}
		if (bl == br)
			ans = min(ans, calc(l[i], r[i], p, s));
		else {
			ans = min(ans, calc(l[i], (bl + 1) * blen, p, s));
			ans = min(ans, calc(br * blen, r[i], p, s));
		}
		printf("%d\n", ans);
	}
}

int main() {
#ifdef SU1
    assert(freopen("input.txt", "rt", stdin));
    //assert(freopen("output.txt", "wt", stdout));
#endif

    cout << setprecision(10) << fixed;
    cerr << setprecision(5) << fixed;

    while (read()) {
		ld stime = gett();
		solve();
		cerr << "Time: " << gett() - stime << endl;
		//break;
	}

    return 0;
}

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cmath>
using namespace std;

namespace zyt
{
	template<typename T>
	inline bool read(T &x)
	{
		char c;
		bool f = false;
		x = 0;
		do
			c = getchar();
		while (c != EOF && c != '-' && !isdigit(c));
		if (c == EOF)
			return false;
		if (c == '-')
			f = true, c = getchar();
		do
			x = x * 10 + c - '0', c = getchar();
		while (isdigit(c));
		if (f)
			x = -x;
		return true;
	}
	template<typename T>
	inline void write(T x)
	{
		static char buf[20];
		char *pos = buf;
		if (x < 0)
			putchar('-'), x = -x;
		do
			*pos++ = x % 10 + '0';
		while (x /= 10);
		while (pos > buf)
			putchar(*--pos);
	}
	typedef pair<int, int> pii;
	const int N = 2e5 + 10, B = 500, INF = 0x3f3f3f3f;
	int n, m, arr[N], block, blnum, begin[B], belong[N];
	int first[B][B], mn[B][B], buf[N];
	pii mdf[N], upd[N][2];
	void get(const int x, int v, const int pos, int *const tmp)
	{
		for (int i = begin[pos]; i < begin[pos + 1]; i++)
		{
			tmp[i] = v;
			v -= (arr[i] < x);
			if (i + m <= n)
				v += (arr[i + m] < x);
		}
	}
	int work()
	{
		read(n), read(m);
		block = sqrt(n);
		blnum = ceil((double)n / block);
		for (int i = 1; i <= n; i++)
			read(arr[i]), mdf[i] = pii(arr[i], i), belong[i] = (i - 1) / block + 1;
		for (int i = 1; i <= blnum; i++)
			begin[i] = (i - 1) * block + 1;
		begin[blnum + 1] = n + 1;
		sort(mdf + 1, mdf + n + 1);
		for (int i = 1; i <= n; i++)
			arr[mdf[i].second] = i;
		for (int i = 1; i <= n; i++)
		{
			int pos = belong[i];
			if (i == begin[pos])
			{
				memcpy(first[pos], first[pos - 1], sizeof(int[blnum + 1]));
				memcpy(mn[pos], mn[pos - 1], sizeof(int[blnum + 1]));
			}
			int r = mdf[i].second, l = max(1, r - m + 1);
			for (int j = belong[l] + 1; j < belong[r]; j++)
				++mn[pos][j], ++first[pos][j];
			if (l == begin[belong[l]])
				++first[pos][belong[l]];
			get(i, first[pos][belong[l]], belong[l], buf);
			for (int j = l; j < min(r + 1, begin[belong[l] + 1]); j++)
				++buf[j];
			mn[pos][belong[l]] = INF;
			for (int j = begin[belong[l]]; j < begin[belong[l] + 1]; j++)
				mn[pos][belong[l]] = min(mn[pos][belong[l]], buf[j]);
			upd[i][0] = pii(belong[l], mn[pos][belong[l]]);
			if (belong[l] < belong[r])
			{
				++first[pos][belong[r]];
				get(i, first[pos][belong[r]], belong[r], buf);
				for (int j = begin[belong[r]]; j <= r; j++)
					++buf[j];
				mn[pos][belong[r]] = INF;
				for (int j = begin[belong[r]]; j < begin[belong[r] + 1]; j++)
					mn[pos][belong[r]] = min(mn[pos][belong[r]], buf[j]);
				upd[i][1] = pii(belong[r], mn[pos][belong[r]]);
			}
		}
		int q, lastans = 0;
		read(q);
		while (q--)
		{
			int l, r, x;
			read(l), read(r), read(x);
			x ^= lastans;
			int t = lower_bound(mdf + 1, mdf + n + 1, pii(x, 0)) - mdf - 1;
			if (!t)
			{
				lastans = 0;
				puts("0");
				continue;
			}
			static int fst[B], mm[B];
			memcpy(fst, first[belong[t] - 1], sizeof(int[blnum + 1]));
			memcpy(mm, mn[belong[t] - 1], sizeof(int[blnum + 1]));
			for (int i = begin[belong[t]]; i <= t; i++)
			{
				int r = mdf[i].second, l = max(1, r - m + 1);
				for (int j = belong[l] + 1; j < belong[r]; j++)
					++fst[j], ++mm[j];
				if (l == begin[belong[l]])
					++fst[belong[l]];
				if (belong[l] != belong[r])
					++fst[belong[r]];
				for (int j = 0; j < 2; j++)
					mm[upd[i][j].first] = upd[i][j].second;
			}
			int ans = INF;
			for (int i = belong[l] + 1; i < belong[r]; i++)
				ans = min(ans, mm[i]);
			get(t, fst[belong[l]], belong[l], buf);
			for (int i = l; i < min(r + 1, begin[belong[l] + 1]); i++)
				ans = min(ans, buf[i]);
			if (belong[l] != belong[r])
			{
				get(t, fst[belong[r]], belong[r], buf);
				for (int i = begin[belong[r]]; i <= r; i++)
					ans = min(ans, buf[i]);
			}
			write(lastans = ans), putchar('\n');
		}
		return 0;
	}
}
int main()
{
	return zyt::work();
}

#include<bits/stdc++.h>
using namespace std;

const int maxN=200005,LEN=450;
int N,M,Q,len,cntb; //len 块长，cntb 块数
int bg[LEN],ed[LEN]; //bg 块开头编号，ed 块结尾编号
int inb[maxN]; //inb 元素处在的块的编号
int A[maxN],id[maxN];
bool cmp(int a,int b){
	return A[a]>A[b];
}
int Lpos[maxN],Rpos[maxN]; //修改操作的左右端点编号
int Linb[maxN],Rinb[maxN]; //修改操作的左右端点所在块编号
int Lans[maxN],Rans[maxN]; //修改操作的左右端点所在块的答案

int ans[LEN],val[maxN],lzy[LEN];
int ANS[LEN][LEN],bgval[LEN][LEN];

void init(){
	scanf("%d%d",&N,&M);
//	N=200000,M=200;
	len=sqrt(N);
	for(int i=1;i<=N;i++) inb[i]=(i-1)/len+1,bg[inb[i]]=(bg[inb[i]]?bg[inb[i]]:i),ed[inb[i]]=i;cntb=inb[N];

	for(int i=1;i<=N;i++) scanf("%d",&A[i]),id[i]=i;
//	for(int i=1;i<=N;i++) A[i]=i,id[i]=i;
	sort(id+1,id+N+1,cmp);

	for(int i=1;i<=N;i++){
		Lpos[i]=max(1,id[i]-M+1),Rpos[i]=id[i];
		Linb[i]=inb[Lpos[i]],Rinb[i]=inb[Rpos[i]];
		if(inb[i]!=inb[i-1]){ //存储版本，pushdown tag，保证 LansRans 有完整修改的 tag 而没有边角修改的 tag
			for(int j=1;j<=N;j++) val[j]+=lzy[inb[j]];
			for(int j=1;j<=cntb;j++)
				ANS[inb[i]][j]=ans[j],bgval[inb[i]][j]=val[bg[j]],lzy[j]=0;
		}
		//修改
		if(Linb[i]==Rinb[i]){
			int debug_cnt=0;
			for(int j=Lpos[i];j<=Rpos[i];j++) val[j]++,ans[inb[j]]=max(ans[inb[j]],val[j]+lzy[inb[j]]),debug_cnt++;
			for(int j=bg[Linb[i]];j<=ed[Linb[i]];j++) Lans[i]=max(Lans[i],val[j]),debug_cnt++;
		}
		else{
			for(int j=Lpos[i];j<=ed[Linb[i]];j++) val[j]++,ans[inb[j]]=max(ans[inb[j]],val[j]+lzy[inb[j]]);
			for(int j=Linb[i]+1;j<=Rinb[i]-1;j++) lzy[j]++,ans[j]++;
			for(int j=bg[Rinb[i]];j<=Rpos[i];j++) val[j]++,ans[inb[j]]=max(ans[inb[j]],val[j]+lzy[inb[j]]);
			for(int j=bg[Linb[i]];j<=ed[Linb[i]];j++) Lans[i]=max(Lans[i],val[j]);
			for(int j=bg[Rinb[i]];j<=ed[Rinb[i]];j++) Rans[i]=max(Rans[i],val[j]);
		}
	}
}

int lstans;
int brute_query(int L,int R,int bid,int tmp,int q){
	int ANS=0;
	for(int i=bg[bid];i<L;i++) tmp+=(A[i+M]>=q)-(A[i]>=q);
	for(int i=L;i<=R;i++) ANS=max(ANS,tmp),tmp+=(A[i+M]>=q)-(A[i]>=q);
	return ANS;
}

int main(){
	init();
	printf("check\n");
	scanf("%d",&Q);
	while(Q--){
		int QL,QR,QQ,QX;scanf("%d%d%d",&QL,&QR,&QQ);QQ^=lstans;
		A[N+1]=QQ;QX=upper_bound(id+1,id+N+1,N+1,cmp)-id-1;
		memset(lzy,0,sizeof(lzy));
		for(int i=1;i<=cntb;i++) ans[i]=ANS[inb[QX]][i];
		int bgvalL=bgval[inb[QX]][inb[QL]],bgvalR=bgval[inb[QX]][inb[QR]];
		for(int i=bg[inb[QX]];i<=QX;i++){
			ans[Linb[i]]=Lans[i];
			if(Linb[i]!=Rinb[i]) ans[Rinb[i]]=Rans[i],lzy[Linb[i]+1]++,lzy[Rinb[i]]--;
			if(Lpos[i]<=bg[inb[QL]]&&bg[inb[QL]]<=Rpos[i]) bgvalL++;
			if(Lpos[i]<=bg[inb[QR]]&&bg[inb[QR]]<=Rpos[i]) bgvalR++;
		}
		for(int i=1;i<=cntb;i++) lzy[i]+=lzy[i-1];
		int ANS=0;
		if(inb[QL]==inb[QR]) ANS=brute_query(QL,QR,inb[QL],bgvalL,QQ);
		else{
			ANS=max(ANS,brute_query(QL,ed[inb[QL]],inb[QL],bgvalL,QQ));
			ANS=max(ANS,brute_query(bg[inb[QR]],QR,inb[QR],bgvalR,QQ));
			for(int i=inb[QL]+1;i<=inb[QR]-1;i++)
				ANS=max(ANS,ans[i]+lzy[i]);
		}
		printf("%d\n",lstans=M-ANS);
	}

	return 0;
}

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>

using namespace std;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define foreach(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); ++e)
typedef long long LL;
typedef pair<int, int> PII;

const int MX = 200005, BITS = 9, S = 1 << BITS, MASK = S - 1, CNT = (MX + S - 1) / S;

struct Upd {
    int pos, val, t;
    Upd(int pos, int val, int t) : pos(pos), val(val), t(t) {}
    Upd() {}
};

int n, m, q;
int a[MX], ord[MX];
int val[MX], whole[CNT], mx[CNT][CNT], wh[CNT][CNT];
vector<Upd> upd[CNT];
vector<vector<int> > save[CNT];
int bFrom[MX], bTo[MX], bInd[2], bMx[2], nbMx[2], uFrom[MX], uTo[MX];

bool cmp(const int &lhs, const int &rhs) {
    return a[lhs] > a[rhs];
}

int calcMx(int block) {
    int ret = 0;
    int ffrom = block << BITS;
    int tto = min(n - 1, (block << BITS) | MASK);
    for (int k = ffrom; k <= tto; ++k)
        ret = max(ret, val[k] + whole[k >> BITS]);
    return ret;
}

int main() {
    scanf("%d%d", &n, &m);
    REP(i, n) scanf("%d", a + i);
    REP(i, n) ord[i] = i;
    sort(ord, ord + n, cmp);
    for (int i = 0; ; ++i) {
        if (!(i & MASK)) {
            int cur = i >> BITS;
            REP(j, n) {
                mx[cur][j >> BITS] = max(mx[cur][j >> BITS], val[j] + whole[j >> BITS]);
            }
            REP(j, CNT) wh[cur][j] = whole[j];
        }
        if (i == n) break;
        int from = uFrom[i] = max(0, ord[i] - m + 1);
        int to = uTo[i] = min(n - m, ord[i]);
        bInd[0] = from >> BITS, bInd[1] = to >> BITS;
        REP(j, 2) bMx[j] = calcMx(bInd[j]);
        for (int j = from; j <= to; ) {
            if (!(j & MASK) && j + S - 1 <= to) {
                ++whole[j >> BITS];
                j += S;
            } else {
                ++val[j];
                ++j;
            }
        }
        REP(j, 2) nbMx[j] = calcMx(bInd[j]);
        bFrom[i] = from >> BITS;
        bTo[i] = to >> BITS;
        if (((from & MASK) || to - from < MASK) && nbMx[0] == bMx[0]) ++bFrom[i];
        if (((to & MASK) != MASK || to - from < MASK) && nbMx[1] == bMx[1]) --bTo[i];
        if ((from & MASK) || to - from < MASK) upd[from >> BITS].pb(Upd(from & MASK, +1, i));
        if ((to & MASK) != MASK || to - from < MASK) {
            if ((from >> BITS) != (to >> BITS)) upd[to >> BITS].pb(Upd(0, +1, i));
            upd[to >> BITS].pb(Upd((to & MASK) + 1, -1, i));
        }
    }
    REP(i, CNT) if ((int)upd[i].size() >= S) {
        vector<int> tmp(S + 1, 0);
        REP(j, upd[i].size()) {
            tmp[upd[i][j].pos] += upd[i][j].val;
            if ((j & MASK) == MASK)
                save[i].pb(tmp);
        }
    }
    scanf("%d", &q);
    int ans = 0;
    REP(query, q) {
        int from, to, qual;
        scanf("%d%d%d", &from, &to, &qual), --from, --to, qual ^= ans;
        int lo = -1, hi = n - 1, mid;
        while (lo < hi) {
            mid = (lo + hi + 1) >> 1;
            if (a[ord[mid]] < qual)
                hi = mid - 1;
            else
                lo = mid;
        }
        if (lo == -1) {
            printf("%d\n", m);
            ans = m;
            continue;
        }
        qual = lo;
        bInd[0] = from >> BITS, bInd[1] = to >> BITS;
        ans = 0;
        REP(i, 2) {
            if (i == 1 && bInd[0] == bInd[1]) break;
            int cur = bInd[i];
            lo = 0, hi = (int)upd[cur].size();
            while (lo < hi) {
                mid = (lo + hi) >> 1;
                if (upd[cur][mid].t > qual)
                    hi = mid;
                else
                    lo = mid + 1;
            }
            vector<int> tmp;
            if ((lo >> BITS) > 0) tmp = save[cur][(lo >> BITS) - 1];
            else tmp.assign(S + 1, 0);
            for (int j = lo & (~MASK); j < lo; ++j)
                tmp[upd[cur][j].pos] += upd[cur][j].val;
            int va = wh[qual >> BITS][cur];
            for (int i = qual & (~MASK); i <= qual; ++i) if (uFrom[i] <= (cur << BITS) && uTo[i] >= (cur << BITS) + MASK)
                ++va;
            REP(j, S) {
                va += tmp[j];
                if (((cur << BITS) | j) >= from && ((cur << BITS) | j) <= to)
                    ans = max(ans, va);
            }
        }
        int wfrom = from >> BITS;
        int wto = to >> BITS;
        if ((from & MASK) || to - from < S) ++wfrom;
        if ((to & MASK) != MASK || to - from < S) --wto;
        vector<int> tmp(CNT + 1, 0);
        int wqual = qual >> BITS;
        REP(i, CNT) tmp[i] = mx[wqual][i];
        for (int i = CNT - 1; i >= 1; --i) tmp[i] -= tmp[i - 1];
        for (int i = wqual << BITS; i <= qual; ++i) {
            ++tmp[bFrom[i]];
            --tmp[bTo[i] + 1];
        }
        int cur = 0;
        REP(i, CNT) {
            cur += tmp[i];
            if (i >= wfrom && i <= wto)
                ans = max(ans, cur);
        }
        ans = m - ans;
        printf("%d\n", ans);
    }
    return 0;
}

#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<stdio.h>
#include<map>
#include<vector>
#include<cassert>
using namespace std;
const int inf=1e9;
const int bound=400;
struct pp
{
	int vl,id,min_value,min_id,value,sum;
	int flag;
	bool operator <(const pp &temp)const
	{
		return vl<temp.vl;
	}
};
pp bl[210000/bound][2*bound],now;
int num[210000/bound],num_bl,cnt_list,np[2*bound];
int m,n,a[220000],num_q,ans,nw;
int lower_bound(int left,int right,pp bl[],int x)
{
	int mid;
	while(left<=right)
	{
		mid=(left+right)>>1;
		if(bl[mid].vl<x)
		   left=mid+1;
        else
           right=mid-1;
	}
	return right;
}
int main()
{
	int i,j,s,p,q,ip,id,l,r,x,fir;
	scanf("%d%d",&n,&m);
	for(i=0;i<n;i++)
	    scanf("%d",&a[i]);
    memset(num,0,sizeof(num));
    num_bl=0;
	for(i=0;i<n;i++)
    {
    	if(i%bound==0)
    	{
	    	for(j=i;j<min(n,i+bound);j++)
	    	{
	    		now.id=j;
	    		now.vl=a[j];
	    	    bl[num_bl][num[num_bl]++]=now;
	    	    if(j+m<n)
	    	    {
    	    		now.id=j+m;
    	    		now.vl=a[j+m];
    	    		bl[num_bl][num[num_bl]++]=now;
    	    	}
			}
			num_bl++;
	    }
    }
    for(i=0;i<num_bl;i++)
    {
        sort(bl[i],bl[i]+num[i]);
        for(j=i*bound;j<min((i+1)*bound,n);j++)
             np[j-i*bound]=0;
        for(j=0;j<num[i];j++)
        {
       	    if(bl[i][j].id<(i+1)*bound)
       	    {
       	       bl[i][j].value=-1;
       	       bl[i][j].sum=1;
       	       ip=bl[i][j].id;
       	       np[ip-i*bound]--;
		    }
			else
			{
	           bl[i][j].value=1;
			   bl[i][j].sum=0;
			   ip=bl[i][j].id-m;
			   np[ip-i*bound]++;
			}
			if(j)
			{
               bl[i][j].value+=bl[i][j-1].value;
			   bl[i][j].sum+=bl[i][j-1].sum;
			}
			bl[i][j].min_value=0;
			nw=0;
			for(s=i*bound;s<min((i+1)*bound,n);s++)
			{
				nw+=np[s-i*bound];
				if(bl[i][j].min_value>nw)
				   bl[i][j].min_value=nw;
			}
        }
	}
	scanf("%d",&num_q);
	ans=0;
	while(num_q--)
	{
		scanf("%d%d%d",&l,&r,&x);
		l--;
		r--;
		x^=ans;
		ans=0;
		id=l/bound;
		nw=fir=0;
		for(i=0;i<num[id];i++)
		{
			if(bl[id][i].id>=l&&bl[id][i].id<l+m)
			{
			    if(bl[id][i].vl<x)
    				fir++;
			}
		}
		if(id!=(l+m)/bound)
		{
			id=(l+m)/bound;
			for(i=0;i<num[id];i++)
			{
				if(bl[id][i].id>=l&&bl[id][i].id<l+m)
			    {
			       if(bl[id][i].vl<x)
    		     	  fir++;
			    }
			}
			for(i=l/bound+1;i<(l+m)/bound;i++)
			{
				 id=lower_bound(0,num[i]-1,bl[i],x);
				 if(id>=0)
				    fir+=bl[i][id].sum;
			}
		}
        for(i=l;i<=min(r,(l/bound+1)*bound-1);i++)
	    {
    	     if(a[i]<x&&i!=l)
     	    	nw--;
     	     if(i+m<n&&a[i+m]<x)
     	        nw++;
  	         if(ans>nw)
  	            ans=nw;
	    }
		if(r>=(l/bound+1)*bound)
		{
			id=r/bound;
			for(i=l/bound+1;i<r/bound;i++)
			{
				id=lower_bound(0,num[i]-1,bl[i],x);
				if(id>=0)
				{
				   if(nw+bl[i][id].min_value<ans)
				      ans=nw+bl[i][id].min_value;
				    nw+=bl[i][id].value;
				}
			}
			for(i=r/bound;i<=r;i++)
			{
				if(a[i]<x)
				   nw--;
	            if(i+m<n&&a[i]<x)
	               nw++;
                if(ans>nw)
                    ans=nw;
			}
		}
	//	printf("ans=%d,fir=%d\n",ans,fir);
        ans+=fir;
        printf("%d\n",ans);
	}
	return 0;
}

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>

using namespace std;

const int limite=1000000;
const int base=450;
//const int base=4;

struct segmento {
  int pos,sol,solmin,anyade,acumulado;
};

struct accion {
  int val,le,ri;
  accion() {
  }
  accion(int inval,int inle,int inri) {
    val=inval;le=inle;ri=inri;
  }
};

int a[limite];

int n,m;

int calcula(int q,int le,int ri,vector<segmento> &vseg,vector<accion> &vacc)
{
  //cout<<"query "<<q<<" "<<le<<" "<<ri<<endl;
  for (int i=0;i<int(vseg.size());i++)
    vseg[i].anyade=0;
  for (int i=0;i<int(vacc.size()) and vacc[i].val<q;i++) {
    vseg[vacc[i].le].anyade--;
    vseg[vacc[i].ri].anyade++;
  }
  vseg[0].acumulado=vseg[0].anyade;
  for (int i=1;i<int(vseg.size());i++)
    vseg[i].acumulado=vseg[i-1].acumulado+vseg[i].anyade;
  /*
  cout<<"acumulado:"<<endl;
  for (int i=0;i<int(vseg.size());i++)
    cout<<vseg[i].acumulado<<",";
  cout<<endl;
  */
  int sol=0;
  for (int i=0;i<int(vseg.size())-1;i++) {
    segmento &s=vseg[i];
    int segini=s.pos;
    int segtope=vseg[i+1].pos;
    if (le<=segini and segtope<=ri+1) sol=max(sol,s.solmin+s.acumulado);
    else if (le<segtope and segini<=ri) {
      int lef=max(le,segini);
      int rig=min(ri,segtope-1);
      int solact=s.sol+s.acumulado;
      for (int j=segini;j<lef;j++) {
	solact-=a[j]>=q;
	if (j+m<=n) solact+=a[j+m]>=q;
      }
      for (int j=lef;j<=rig;j++) {
	sol=max(sol,solact);
	solact-=a[j]>=q;
	if (j+m<=n) solact+=a[j+m]>=q;
      }
    }
  }
  return sol;
}

int anyade[limite];
int acumulado[limite];
int nval=0;
int valinicial[limite];
vector<segmento> vseg[limite];
vector<accion> vacc[limite];

int main()
{
  ios::sync_with_stdio(false);
  cin>>n>>m;
  vector<pair<int,int> > w;
  for (int i=1;i<=n;i++) {
    cin>>a[i];
    w.push_back(pair<int,int> (a[i],i));
    anyade[max(1,i-(m-1))]++;
    anyade[i+1]--;
  }
  sort(w.begin(),w.end());
  int iw=0;
  while (iw<int(w.size())) {
    if (nval>0) {
      valinicial[nval]=w[iw].first;
      while (iw<int(w.size()) and w[iw].first==valinicial[nval]) {
	pair<int,int> &p=w[iw];
	int pos=p.second;
	anyade[max(1,pos-(m-1))]--;
	anyade[pos+1]++;
	iw++;
      }
    }
    for (int i=1;i<=n;i++)
      acumulado[i]=acumulado[i-1]+anyade[i];
    int iwini=iw;
    iw=min(int(w.size()),iw+base);
    if (iw<int(w.size())) {
      int val=w[iw].first;
      while (w[iw].first==val) iw--;
      iw++;
    }
    map<int,int> pos2pos;
    for (int i=1;i<=n;i+=base)
      pos2pos[i];
    pos2pos[n+1];
    for (int i=iwini;i<iw;i++) {
      pair<int,int> &p=w[i];
      int pos=p.second;
      pos2pos[max(1,pos-(m-1))];
      pos2pos[pos+1];
    }
    vector<segmento> &vse=vseg[nval];
    vse=vector<segmento> (int(pos2pos.size()));
    {
      int ivse=0;
      for (map<int,int>::iterator it=pos2pos.begin();it!=pos2pos.end();it++,ivse++) {
	vse[ivse].pos=it->first;
	it->second=ivse;
      }
    }
    for (int i=0;i<int(vse.size());i++) {
      segmento &s=vse[i];
      int posini=s.pos;
      int postope=n+1;
      if (i<int(vse.size())-1) postope=vse[i+1].pos;
      s.sol=s.solmin=acumulado[posini];
      for (int pos=posini;pos<postope;pos++)
	s.solmin=min(s.solmin,acumulado[pos]);
    }
    vector<accion> &vac=vacc[nval];
    for (int i=iwini;i<iw;i++) {
      pair<int,int> &p=w[i];
      int val=p.first;
      int pos=p.second;
      vac.push_back(accion(val,pos2pos[max(1,pos-(m-1))],pos2pos[pos+1]));
      anyade[max(1,pos-(m-1))]--;
      anyade[pos+1]++;
    }
    nval++;
  }
  /*
  for (int i=0;i<nval;i++) {
    cout<<"valinicial "<<valinicial[i]<<endl;
    vector<segmento> &vse=vseg[i];
    for (int j=0;j<int(vse.size());j++)
      cout<<"["<<vse[j].pos<<","<<vse[j].sol<<","<<vse[j].solmin<<"]";
    cout<<endl;
    vector<accion> &vac=vacc[i];
    for (int j=0;j<int(vac.size());j++)
      cout<<"("<<vac[j].val<<","<<vac[j].le<<","<<vac[j].ri<<")";
    cout<<endl;
  }
  */
  int casos;
  cin>>casos;
  int sol=0;
  for (int cas=0;cas<casos;cas++) {
    int le,ri,q;
    cin>>le>>ri>>q;
    q^=sol;
    int ival=0;
    while (ival<nval-1 and valinicial[ival+1]<q) ival++;
    sol=m-calcula(q,le,ri,vseg[ival],vacc[ival]);
    cout<<sol<<endl;
  }
}

#include <stdio.h>
#include <algorithm>

using std :: pair;
using std :: sort;

template < class T > void read(T &x) { char c, mi = 0; while(c = getchar(), c <= 32); if(c == '-') mi = 1, x = 0; else if(c < 48 || c > 57) return void(x = c); else x = c - 48; while(c = getchar(), c > 32) x = 10 * x + c - 48; if(mi == 1) x = -x; }
template < class T > void read(T &x, T &y) { read(x); read(y); }
template < class T > void read(T &x, T &y, T &z) { read(x, y); read(z); }
template < class T > void reada(T *a, int n) { for(int i = 0; i < n; ++i) read(a[i]); }
template < class T > void write(T x) { static char s[20]; char pt = 0, mi = (x < 0); if(mi == 1) x = -x; while(!pt || x > 0) { s[++pt] = (char)(x % 10 + '0'); x /= 10; } if(mi == 1) putchar('-'); while(pt > 0) putchar(s[pt--]); }
template < class T > void write(T x, T y) { write(x); putchar(' '); write(y); }
template < class T > void write(T x, T y, T z) { write(x, y); putchar(' '); write(z); }
template < class T > void writeln(T x) { write(x); puts(""); }
template < class T > void writea(T *a, int n) { for(int i = 0; i < n; ++i) { write(a[i]); putchar(i + 1 == n ? '\n' : ' '); } }

#define nmax 200010
#define fs first
#define sd second

int pt = 0;
int roots[nmax];
int n, m, q;
int ans, l, r, x;
pair < int, int > p[nmax];

int mi(const int &a, const int &b) {
	return a < b ? a : b;
}

int ma(const int &a, const int &b) {
	return a > b ? a : b;
}

struct Node {
	unsigned long long mi : 18, l : 23, r : 23;
} t[7877777];

int findAdd(int ret, int xl, char dl, int xr, char dr) {
	return t[ret].mi - mi(dl == 0 ? t[xl].mi : xl, dr == 0 ? t[xr].mi : xr);
}

void f(int ret, int xl, char dl, int xr, char dr, int add = 0) {
	t[ret].mi = add + mi(dl == 0 ? t[xl].mi : xl, dr == 0 ? t[xr].mi : xr);
	t[ret].l = xl;
	t[ret].r = xr;
}

int upd(int v, int L, int R, int l, int r) {
	if(L == R) {
		return v + 1;
	}
	int ret = ++pt;
	if(L == l && r == R) {
		t[ret].mi = t[v].mi + 1;
		t[ret].l = t[v].l;
		t[ret].r = t[v].r;
		return ret;
	}
	int M = (L + R) / 2;
	if(r <= M) {
		t[ret].l = upd(t[v].l, L, M, l, r);
		t[ret].r = t[v].r;
	} else if(l > M) {
		t[ret].r = upd(t[v].r, M + 1, R, l, r);
		t[ret].l = t[v].l;
	} else {
		t[ret].l = upd(t[v].l, L, M, l, M);
		t[ret].r = upd(t[v].r, M + 1, R, M + 1, r);
	}
	f(ret, t[ret].l, L == M, t[ret].r, M + 1 == R,
	findAdd(v, t[v].l, L == M, t[v].r, M + 1 == R));
	return ret;
}

int get(int v, int L, int R, int l, int r) {
	if(v == 0) {
		return 0;
	}
	if(L == R) {
		return v;
	}
	if(L == l && r == R) {
		return t[v].mi;
	}
	int M = (L + R) / 2;
	int z = findAdd(v, t[v].l, L == M, t[v].r, M + 1 == R);
	if(r <= M) {
		return z + get(t[v].l, L, M, l, r);
	}
	if(l > M) {
		return z + get(t[v].r, M + 1, R, l, r);
	}
	return z + mi(get(t[v].l, L, M, l, M),
		get(t[v].r, M + 1, R, M + 1, r));
}

int main() {
//	freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
//	freopen("elevator.in", "r", stdin); freopen("elevator.out", "w", stdout);
//	printf("%d\n", sizeof(t));
	read(n, m);
	for(int i = 1; i <= n; ++i) {
		read(p[i].fs);
		p[i].sd = i;
	}
	roots[0] = 0;
	sort(p + 1, p + 1 + n);
	for(int i = 1; i <= n; ++i) {
		roots[i] = upd(roots[i - 1], 1, n - m + 1, ma(p[i].sd - m + 1, 1), mi(p[i].sd, n - m + 1));
	}
	read(q);
	while(q --> 0) {
		read(l, r, x);
		x ^= ans;
		int id = upper_bound(p + 1, p + 1 + n, std :: make_pair(x, -1)) - (p + 1);
		ans = get(roots[id], 1, n - m + 1, l, r);
		writeln(ans);
	}
	getchar(); getchar();
	return 0;
}

#include <bits/stdc++.h>
#define endl '\n'

using namespace std;
const int MAXN = (int)2e5 + 42;
const int MAXLOG = 10;

int n, m, q;
int a[MAXN];

void read()
{
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        cin >> a[i];
}

struct node
{
	int64_t l:23, r:23, mn:18;
	int add;
};

node tr[MAXN * MAXLOG];
int tot = 0, root[MAXN];

void expand(int idx)
{
    if(!tr[idx].l) tr[idx].l = ++tot;
    if(!tr[idx].r) tr[idx].r = ++tot;
}

int update(int bef, int qL, int qR, int val, int l, int r)
{
    if(qL > r || qR < l)
        return bef;

    int nw = ++tot;
    tr[nw] = tr[bef];

    if(qL <= l && r <= qR)
    {
        tr[nw].add += val;
		tr[nw].mn += val;
        return nw;
    }

    int mid = (l + r) >> 1;
    tr[nw].l = update(tr[nw].l, qL, qR, val, l, mid);
    tr[nw].r = update(tr[nw].r, qL, qR, val, mid + 1, r);

	tr[nw].mn = min(tr[tr[nw].l].mn, tr[tr[nw].r].mn) + tr[nw].add;

	return nw;
}

int query(int idx, int qL, int qR, int l, int r, int add = 0)
{
    if(qL > r || qR < l)
        return MAXN;

	if(idx == 0) return add;

    if(qL <= l && r <= qR)
		return add + tr[idx].mn;

    int mid = (l + r) >> 1;
	return min(query(tr[idx].l, qL, qR, l, mid, add + tr[idx].add), query(tr[idx].r, qL, qR, mid + 1, r, add + tr[idx].add));
}

void solve()
{
    vector<pair<int, int>> vec;
    for(int i = 0; i < n; i++)
        vec.push_back({a[i], i + 1});

    sort(vec.begin(), vec.end());
    for(int i = 1; i <= n; i++)
        root[i] = update(root[i - 1], max(1, vec[i - 1].second - m + 1), vec[i - 1].second, 1, 1, n);

    int last_ans = 0;
    cin >> q;

    for(int i = 0; i < q; i++)
    {
        int l, r, p;
        cin >> l >> r >> p;

        p ^= last_ans;
        p--;

        int pos = upper_bound(vec.begin(), vec.end(), make_pair(p, MAXN)) - vec.begin();
        cout << (last_ans = query(root[pos], l, r, 1, n)) << endl;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    read();
    solve();
    return 0;
}

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define R register
#define MN 200000
#define MB 20
#define SIZE 10000
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int n,m,N,a[MN+5],L[MN+5],num,block[MN+5],mn[MB+5][SIZE*2+5],val[SIZE*2+5],rt[MN+5],cnt=0;
struct disc
{
	int*s,num;
	disc(int size=SIZE<<1){s=new int[size+5];num=0;}
	void Add(int x){s[++num]=x;}
	void Build(){sort(s+1,s+num+1);num=unique(s+1,s+num+1)-s-1;}
	int Calc(int x){return upper_bound(s+1,s+num+1,x)-s-1;}
}lb[MB+5],LB(MN+5);
struct Tree{int l,r,x;}T[MN*18+5];

void Init_Mn(int x)
{
	int l=(x-1)*SIZE+1,r=min(x*SIZE,N);
	memset(val,0,sizeof(val));
	for(R int i=l;i<=r;++i) lb[x].Add(a[i]),lb[x].Add(a[i+m-1]);
	lb[x].Build();int tot=lb[x].num;
	for(R int i=l;i<r;++i)
	{
		int A=lb[x].Calc(a[i]),B=lb[x].Calc(a[i+m-1]);
		for(R int i=A;i<=tot;++i) --val[i];
		for(R int i=B;i<=tot;++i) ++val[i];
		for(R int i=min(A,B);i<=tot;++i) mn[x][i]=min(mn[x][i],val[i]);
	}
}
int Modify(int x,int l,int r,int k,int v)
{
	int y=++cnt,mid=l+r>>1;T[y]=T[x];T[y].x+=v;
	if(l==r) return y;
	if(k<=mid) T[y].l=Modify(T[y].l,l,mid,k,v);
	else T[y].r=Modify(T[y].r,mid+1,r,k,v);
	return y;
}
int Query(int x,int l,int r,int k)
{
	if(k<l||!x) return 0;
	if(l==r) return T[x].x;
	int mid=l+r>>1;
	if(k<=mid) return Query(T[x].l,l,mid,k);
	else return T[T[x].l].x+Query(T[x].r,mid+1,r,k);
}
int Solve(int x,int v,int V)
{
	int l=(x-1)*SIZE+1,r=min(x*SIZE,n);
	int num=Query(rt[l+m-1],1,LB.num,v)-Query(rt[l-1],1,LB.num,v);
	return num+mn[x][lb[x].Calc(V)];
}
int main()
{
	n=read();m=read();N=n-m+1;
	for(R int i=1;i<=n;++i)a[i]=read(),LB.Add(i);
	for(R int i=1;i<=N;++i) block[i]=(i-1)/SIZE+1;
	for(R int i=1;i<=block[N];++i) Init_Mn(i);
	LB.Build();int tot=LB.num;
	for(R int i=1;i<=n;++i) rt[i]=Modify(rt[i-1],1,tot,LB.Calc(a[i]),1);
	for(int Q=read(),ans=0;Q;--Q)
	{
		int l=read(),r=read(),x=(read()^ans)-1;
		int nx=LB.Calc(x);ans=1e9;
		for(int i=block[l]+1;i<block[r];++i) ans=min(ans,Solve(i,nx,x));
		int L=min(r,block[l]*SIZE),lnum=Query(rt[L+m-1],1,tot,nx)-Query(rt[L-1],1,tot,nx);
		ans=min(ans,lnum);
		for(R int i=L-1;i>=l;--i) lnum-=(a[i+m]<=x),lnum+=(a[i]<=x),ans=min(ans,lnum);
		if(block[l]!=block[r])
		{
			int RR=(block[r]-1)*SIZE+1,rnum=Query(rt[RR+m-1],1,tot,nx)-Query(rt[RR-1],1,tot,nx);
			ans=min(ans,rnum);
			for(int j=RR+1;j<=r;++j) rnum-=(a[j-1]<=x),rnum+=(a[j+m-1]<=x),ans=min(ans,rnum);
		}
		printf("%d\n",ans);
	}
	return 0;
}

#include <algorithm>
#include <iostream>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>

using namespace std;

#define type(x) __typeof((x).begin())
#define foreach(i, x) for(type(x) i = (x).begin(); i != (x).end(); i++)

typedef long long ll;
typedef pair < int, int > ii;

const int inf = 1e9 + 333;
const ll linf = 1e18 + 333;

const int N = 2e5 + 5;
const int SIZE = 8000000;

class tree{ public:
    unsigned long long min : 18, l : 23, r : 23;
    tree() {
        min = l = r = 0;
    }
};

int n, m, q, cnt, x1, x2, t[N];
ii a[N];
tree p[SIZE];

int insert(int t, int l, int r) {

    if(l == r)
        return t + 1;

    int x = ++cnt;

    if(x1 <= l and r <= x2) {
        p[x] = p[t];
        p[x].min++;
        return x;
    }

    int m = l + r >> 1;

    p[x] = p[t];

    int add = p[x].min - min((l == m ? p[x].l : p[p[x].l].min), (m + 1 == r ? p[x].r : p[p[x].r].min));

    if(x1 <= m)
        p[x].l = insert(p[t].l, l, m);
    if(x2 > m)
        p[x].r = insert(p[t].r, m + 1, r);

    p[x].min = min((l == m ? p[x].l : p[p[x].l].min), (m + 1 == r ? p[x].r : p[p[x].r].min)) + add;

    return x;

}

int query(int x, int l, int r) {

    if(x2 < l or r < x1)
        return inf;

    if(l == r)
        return x;

    if(x1 <= l and r <= x2)
        return p[x].min;

    int m = l + r >> 1;

    int add = p[x].min - min((l == m ? p[x].l : p[p[x].l].min), (m + 1 == r ? p[x].r : p[p[x].r].min));

    return min(query(p[x].l, l, m), query(p[x].r, m + 1, r)) + add;

}

int main () {

    scanf("%d %d", &n, &m);

    for(int i = 1; i <= n; i++) {
        scanf("%d", &a[i].first);
        a[i].second = i;
    }

    sort(a + 1, a + n + 1);

    t[0] = ++cnt;

    for(int i = 1; i <= n; i++) {
        x1 = max(1, a[i].second - m + 1);
        x2 = min(a[i].second, n - m + 1);
        if(x1 <= x2)
            t[i] = insert(t[i - 1], 1, n - m + 1);
        else
            t[i] = t[i - 1];
    }

    scanf("%d", &q);

    int ans = 0, x;

    for(int qq = 0; qq < q; qq++) {
        scanf("%d %d %d", &x1, &x2, &x);
        x = lower_bound(a + 1, a + n + 1, ii(x ^ ans, 0)) - a - 1;
        ans = query(t[x], 1, n - m + 1);
        printf("%d\n", ans);
    }

    return 0;

}

//
//  main.cpp
//  Codeforces Round #302 (Div. 1) E
//
//  Created by Scholar on 6/10/15.
//  Copyright (c) 2015 Scholar. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>

#define lson t[o].ls
#define rson t[o].rs
#define lmax (l==mid?int(lson):int(t[lson].mx))
#define rmax (mid+1==r?int(rson):int(t[rson].mx))

using namespace std;

typedef unsigned long long ULL;

const int INF=0x3f3f3f3f;
struct treenode{
    ULL mx:18,rs:23,ls:23;
}t[8000000];
int tot;
int root[200010];
pair<int,int>rep[200010];
int ql,qr;

int update(int l,int r,int p){
    if(l==r)return p+1;
    int o=++tot;
    t[o]=t[p];
    if(ql<=l&&r<=qr){
        t[o].mx++;
        return o;
    }
    int mid=(l+r)>>1,a=t[o].mx-max(lmax,rmax);
    if(ql<=mid)lson=update(l,mid,t[p].ls);
    if(mid<qr)rson=update(mid+1,r,t[p].rs);
    t[o].mx=max(lmax,rmax);
    t[o].mx+=a;
    return o;
}

int query(int o,int l,int r){
    if(l==r)return o;
    if(ql<=l&&r<=qr)return t[o].mx;
    int mid=(l+r)>>1,res=0,a=t[o].mx-max(lmax,rmax);
    if(ql<=mid)res=max(res,query(lson,l,mid));
    if(mid<qr)res=max(res,query(rson,mid+1,r));
    return res+a;
}

int n,m,q;

int main() {
    ios::sync_with_stdio(0);
    int i;
    cin>>n>>m;
    int a;
    for(i=1;i<=n;i++){
        cin>>a;
        rep[i]=make_pair(-a,i);
    }
    sort(rep+1,rep+n+1);
    for(i=1;i<=n;i++){
        ql=max(1,rep[i].second-m+1);
        qr=min(n-m+1,rep[i].second);
        root[i]=update(1,n-m+1,root[i-1]);
    }
    cin>>q;
    int last=0;
    while(q--){
        int x;
        cin>>ql>>qr>>x;
        x^=last;
        int id=upper_bound(rep+1,rep+n+1,make_pair(-q,INF))-rep-1;
        last=m-query(root[id],1,n-m+1);
        cout<<last<<endl;
    }
    return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;

const int N=(int)2e5+5,SIZE=512;

int n,m,size,bnum;

int a[N],ord[N];

bool cmpa(const int i,const int j){
	return a[i]>a[j];
}

int val[SIZE],ma[SIZE],delta[SIZE],first[SIZE];

int tma[SIZE][SIZE],tdelta[SIZE][SIZE],tfirst[SIZE][SIZE];

int ttma[N][2];

int main(){
	scanf("%d%d",&n,&m);
	for(size=0;size*size<n;size++);
	bnum=n/size+1;
	for(int i=0;i<n;i++) scanf("%d",&a[i]),ord[i]=i;
	sort(ord,ord+n,cmpa);
	for(int i=0;i<n;i++){
		if(i%size==0){
			int tb=i/size;
			for(int j=0;j<bnum;j++){
				tma[tb][j]=ma[j];
				tdelta[tb][j]=delta[j];
				tfirst[tb][j]=val[j*SIZE]+delta[j];
			}
		}
		int l=max(0,ord[i]-(m-1)),r=ord[i]+1;
		int bl=l/size,br=r/size;
		if(bl==br){
			for(int j=l;j<r;j++){
				ma[bl]=max(ma[bl],++val[j]);
			}
		}
		else{
			for(int j=l;j<(bl+1)*size;j++){
				ma[bl]=max(ma[bl],++val[j]);
			}
			for(int j=bl+1;j<br;j++){
				delta[j]++;
			}
			for(int j=br*size;j<r;j++){
				ma[br]=max(ma[br],++val[j]);
			}
		}
		ttma[i][0]=ma[bl];
		ttma[i][1]=ma[br];
	}
	if(n%size==0){
		int tb=n/size;
		for(int j=0;j<bnum;j++){
			tma[tb][j]=ma[j];
			tdelta[tb][j]=delta[j];
			tfirst[tb][j]=val[j*SIZE]+delta[j];
		}
	}
	int q; scanf("%d",&q);
	int ans=0;
	while(q--){
		int l,r,x; scanf("%d%d%d",&l,&r,&x); l--; x^=ans; ans=0;
		ord[n]=n; a[n]=x;
		int tim=upper_bound(ord,ord+n,n,cmpa)-ord;
		int tb=tim/size;
		for(int j=0;j<bnum;j++){
			delta[j]=0;
			ma[j]=tma[tb][j];
			first[j]=tfirst[tb][j];
		}
		for(int i=tb*size;i<tim;i++){
			int l=max(0,ord[i]-(m-1)),r=ord[i]+1;
			int bl=l/size,br=r/size;
			if(bl<br){
				delta[bl+1]++;
				delta[br]--;
			}
			if(l%size==0)
				first[bl]++;
			if(r%size&&bl!=br)
				first[br]++;
			ma[bl]=ttma[i][0];
			ma[br]=ttma[i][1];
		}
		for(int j=0;j<bnum;j++) delta[j+1]+=delta[j];
		for(int j=0;j<bnum;j++) delta[j]+=tdelta[tb][j];
		int bl=l/size,br=r/size;
		for(int j=bl+1;j<br;j++){
			ans=max(ans,ma[j]+delta[j]);
		}
		{
			int v=first[bl];
			for(int j=bl*size;j<(bl+1)*size;j++){
				if(j>=l&&j<r)
					ans=max(ans,v);
				if(a[j]>=x) v--;
				if(j+m<n&&a[j+m]>=x) v++;
			}
		}
		{
			int v=first[br];
			for(int j=br*size;j<r;j++){
				if(j>=l&&j<r)
					ans=max(ans,v);
				if(a[j]>=x) v--;
				if(j+m<n&&a[j+m]>=x) v++;
			}
		}
		printf("%d\n",ans=m-ans);
	}
}

#include <stdio.h>
#include <algorithm>

using std :: pair;
using std :: sort;
#define x first
#define y second
#define pb push_back
using pii = pair<int, int>;
const int NN = 200002;
int n, m;
int root[NN];
pii a[NN];

template<class T>void gn(T &x) {char c, sg=0; while (c=getchar(), (c>'9' || c<'0') && c!='-');for((c=='-'?sg=1, c=getchar():0), x=0; c>='0' && c<='9'; c=getchar()) x=(x<<1)+(x<<3)+c-'0';if (sg) x=-x;}
template<class T>void print(T x) {if (x<0) {putchar('-');return print(-x);}if (x<10) {putchar('0'+x);return;} print(x/10);putchar(x%10+'0');}
template <class T>  void println(T x) {print(x); putchar('\n');}

int min(const int &u, const int &v) {return u > v ? v : u;}
int max(const int &u, const int &v) {return u < v ? v : u;}

const int MM = 8068700;
struct node {
	unsigned long long mn : 18, ls : 23, rs : 23;
} mem[MM];

#define mid (st+ed>>1)
int cur;
int l, r, x, ans, q;

int update(int u, int st, int ed) {
	if(l >= ed or r <= st) return u;
	int ans = cur++;
	mem[ans] = mem[u];
	if(l <= st and ed <= r) {
		mem[ans].mn++;
		return ans;
	}
	mem[ans].mn -= min(mem[mem[ans].ls].mn, mem[mem[ans].rs].mn);
	if(mid < r) mem[ans].rs = update(mem[ans].rs, mid, ed);
	if(mid > l) mem[ans].ls = update(mem[ans].ls, st, mid);
	mem[ans].mn += min(mem[mem[ans].ls].mn, mem[mem[ans].rs].mn);
	return ans;
}

int calc(int u, int st, int ed, int f=0) {
	f += mem[u].mn;
	if(l <= st and ed <= r) return f;
	f -= min(mem[mem[u].ls].mn, mem[mem[u].rs].mn);
	if(r <= mid) return calc(mem[u].ls, st, mid, f);
	if(l >= mid) return calc(mem[u].rs, mid, ed, f);
	return min(calc(mem[u].ls, st, mid, f), calc(mem[u].rs, mid, ed, f));
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif // ONLINE_JUDGE
	gn(n), gn(m);
	for(int i=0; i<n; i++) gn(a[i].x), a[i].y = i+1;
	sort(a, a+n);
	root[0] = cur++;
	for(int i=1; i<=n; i++) {
		l = max(0, a[i-1].y-m);
		r = a[i-1].y;
		root[i] = update(root[i-1], 0, n);
	}
	gn(q);
	while(q--) {
		gn(l); gn(r); gn(x);
		x ^= ans;
		x = lower_bound(a, a+n, pii(x, 0)) - a;
		l--;
		ans = calc(root[x], 0, n);
		println(ans);
	}
}

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define IT iterator
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
#define debug puts("wzpakking")
#define y1 ysghysgsygsh
using namespace std;
const int N=200005;
struct node{
	ull mx:18,ls:23,rs:23;
}t[N*40];
int n,m,nd,rt[N];
pii v[N];
int upd(int k,int l,int r,int x,int y){
	int nk=++nd; t[nk]=t[k];
	if (x<=l&&r<=y) return ++t[nk].mx,nk;
	int mid=(l+r)/2,tg=t[k].mx-max(t[t[k].ls].mx,t[t[k].rs].mx);
	if (x<=mid) t[nk].ls=upd(t[k].ls,l,mid,x,y);
	if (y>mid) t[nk].rs=upd(t[nk].rs,mid+1,r,x,y);
	t[nk].mx=max(t[t[nk].ls].mx,t[t[nk].rs].mx)+tg;
	return nk;
}
int ask(int k,int l,int r,int x,int y){
	if (x<=l&&r<=y) return t[k].mx;
	int mid=(l+r)/2,tg=t[k].mx-max(t[t[k].ls].mx,t[t[k].rs].mx);
	if (y<=mid) return ask(t[k].ls,l,mid,x,y)+tg;
	if (x>mid) return ask(t[k].rs,mid+1,r,x,y)+tg;
	return max(ask(t[k].ls,l,mid,x,mid),ask(t[k].rs,mid+1,r,mid+1,y))+tg;
}
int main(){
	scanf("%d%d",&n,&m);
	For(i,1,n) scanf("%d",&v[i].fi),v[i].se=i;
	sort(v+1,v+n+1,greater<pii>());
	For(i,1,n){
		int l=max(1,v[i].se-m+1);
		int r=min(n-m+1,v[i].se);
		cout<<v[i].se<<' '<<l<<' '<<r<<endl;
		rt[i]=upd(rt[i-1],1,n-m+1,l,r);
	}
	int las=0,Q;
	scanf("%d",&Q);
	while (Q--){
		int l,r,k;
		scanf("%d%d%d",&l,&r,&k);
		k=lower_bound(v+1,v+n+1,pii(k^las,0),greater<pii>())-v-1;
		//cout<<k<<endl;
		printf("%d\n",las=m-ask(rt[k],1,n-m+1,l,r));
	}
}

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
inline int read()
{
	int x;char c;
	while((c=getchar())<'0'||c>'9');
	for(x=c-'0';(c=getchar())>='0'&&c<='9';)x=x*10+c-'0';
	return x;
}
#define MN 200000
#define K 450
#define mp make_pair
struct node{int l,r,s;}t[MN*19+5];
int a[MN+5],c[MN+5],cn,tn,rt[MN+5];
typedef pair< int,pair<int,int> > data;
vector<data> v[MN/K+5];
vector<int> z[MN/K+5];
bool cmp(const data&a,const data&b){return a.first<b.first;}
int add(int k,int l,int r,int x)
{
	int p=++tn;t[p].s=t[k].s+1;
	if(l==r)return p;
	int mid=l+r>>1;
	if(x<=mid)t[p].l=add(t[k].l,l,mid,x),t[p].r=t[k].r;
	else t[p].l=t[k].l,t[p].r=add(t[k].r,mid+1,r,x);
	return p;
}
#define L (k<<1)
#define R (k<<1|1)
struct tree{int mx,mk;}T[MN*4+5];
void build(int k,int l,int r)
{
	if(T[k].mx=T[k].mk=0,l==r)return;
	int mid=l+r>>1;build(L,l,mid);build(R,mid+1,r);
}
inline void up(int k){T[k].mx=min(T[L].mx,T[R].mx);}
inline void add(int k,int x){T[k].mx+=x;T[k].mk+=x;}
inline void down(int k){if(T[k].mk)add(L,T[k].mk),add(R,T[k].mk),T[k].mk=0;}
void add(int k,int l,int r,int ql,int qr)
{
	if(l==ql&&r==qr){add(k,1);return;}
	int mid=l+r>>1;down(k);
	if(qr<=mid)add(L,l,mid,ql,qr);
	else if(ql>mid)add(R,mid+1,r,ql,qr);
	else add(L,l,mid,ql,mid),add(R,mid+1,r,mid+1,qr);
	up(k);
}
int query(int k,int l,int r,int x)
{
	if(!k)return 0;
	if(l==r)return t[k].s;
	int mid=l+r>>1;
	if(x<=mid)return query(t[k].l,l,mid,x);
	return t[t[k].l].s+query(t[k].r,mid+1,r,x);
}
int main()
{
	int n,m,i,j,k,l,r,lk,rk,ans=0;
	n=read();m=read();k=max(m,K);
	for(i=1;i<=n;++i)a[i]=c[i]=read();
	sort(c+1,c+n+1);cn=unique(c+1,c+n+1)-c-1;
	for(i=1;i<=n;++i)
	{
		rt[i]=add(rt[i-1],1,cn,a[i]=lower_bound(c+1,c+cn+1,a[i])-c);
		l=max(1,i-m+1);r=i;
		lk=(l-1)/k;rk=(r-1)/k;
		if(lk==rk)v[lk].push_back(mp(a[i],mp(l,r)));
		else v[lk].push_back(mp(a[i],mp(l,rk*k))),
			 v[rk].push_back(mp(a[i],mp(rk*k+1,r)));
	}
	for(i=0;i<=(n-1)/k;++i)
	{
		sort(v[i].begin(),v[i].end(),cmp);
		build(1,i*k+1,min(i*k+k,n));
		for(j=0;j<v[i].size();++j)
		{
			add(1,i*k+1,min(i*k+k,n),v[i][j].second.first,v[i][j].second.second);
			z[i].push_back(T[1].mx);
		}
	}
	for(n=read();n--;)
	{
		l=read();r=read();a[0]=lower_bound(c+1,c+cn+1,ans^read())-c-1;ans=MN;
		if(!a[0]){ans=0;puts("0");continue;}
		lk=(l-1)/k;rk=(r-1)/k;
		for(i=l;i<=r&&i<=lk*k+k;++i)
			ans=min(ans,query(rt[i+m-1],1,cn,a[0])-query(rt[i-1],1,cn,a[0]));
		if(lk!=rk)for(i=r;i>rk*k;--i)
			ans=min(ans,query(rt[i+m-1],1,cn,a[0])-query(rt[i-1],1,cn,a[0]));
		for(i=lk;++i<rk;)
		{
			j=upper_bound(v[i].begin(),v[i].end(),mp(a[0],mp(n,n)))-(v[i].begin())-1;
			if(j>=0)ans=min(ans,z[i][j]);
		}
		printf("%d\n",ans);
	}
}

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
unsigned long long t[8000005];
struct node {int x,y;} tt[200005];
int ans,i,A,B,C,n,m,Q,cnt,root[200005],L,R,mid;
int cmp(node i,node j) {return i.x>j.x;}
void work(unsigned long long A,int &B,int &C)
{
    A/=200001;
    C=A%8000001;
    A/=8000001;
    B=A%8000001;
}
unsigned long long WORK(unsigned long long A,unsigned long long B,unsigned long long C)
{
    A*=8000001;
    A*=200001;
    B*=200001;
    return A+B+C;
}
void Update(int &A,int &B,int l,int r,int L,int R)
{
    if (n==200000) {if (l==r) {B=A+1; return;}}
    if (!B) B=++cnt;
    //if (n==200000 && cnt==8000000) {cout<<i<<' '<<cnt<<endl; exit (0);}
    //cout<<A<<' '<<B<<' '<<l<<' '<<r<<' '<<L<<' '<<R<<endl;
    int mid=(l+r)/2,llB,rrB,llA,rrA;
    work(t[B],llB,rrB); work(t[A],llA,rrA);
    if (l==L && r==R)
    {
        if (!llB) llB=llA; if (!rrB) rrB=rrA;
        int D=t[A]%200001-max(mid==l?llA:t[llA]%200001,mid+1==r?rrA:t[rrA]%200001);
        t[B]=WORK(llB,rrB,max(mid==l?llA:t[llB]%200001,mid+1==r?rrA:t[rrB]%200001)+D+1);
        return;
    }
    int D=t[A]%200001-max(mid==l?llA:t[llA]%200001,mid+1==r?rrA:t[rrA]%200001);
    if (L<=mid) Update(llA,llB,l,mid,L,min(mid,R));
    if (R>mid) Update(rrA,rrB,mid+1,r,max(mid+1,L),R);
    if (!llB) llB=llA; if (!rrB) rrB=rrA;
    t[B]=WORK(llB,rrB,max(mid==l?llA:t[llB]%200001,mid+1==r?rrA:t[rrB]%200001)+D);
}
int work2(int A,int l,int r,int L,int R)
{
    if (l==L && r==R) return l==r?A:t[A]%200001;
    int mid=(l+r)/2,llA,rrA,p=0,q=0;
    work(t[A],llA,rrA);
    //cout<<A<<endl;
    int D=t[A]%200001-max(mid==l?llA:t[llA]%200001,mid+1==r?rrA:t[rrA]%200001);
    if (L<=mid) p=work2(llA,l,mid,L,min(mid,R));
    if (R>mid) q=work2(rrA,mid+1,r,max(mid+1,L),R);
   // cout<<A<<' '<<l<<' '<<r<<' '<<L<<' '<<R<<' '<<D<<' '<<p<<' '<<q<<' '<<t[A]%200001<<endl;
    return max(p,q)+D;
}
int main()
{
    scanf("%d%d",&n,&m);
    for (i=1; i<=n; i++)
    {
        scanf("%d",&tt[i].x);
        tt[i].y=i;
    }
    sort(tt+1,tt+n+1,cmp);
    for (i=1; i<=n; i++) Update(root[i-1],root[i],1,n,max(1,tt[i].y-m+1),tt[i].y);
    scanf("%d",&Q);
    while (Q--)
    {
        scanf("%d%d%d",&A,&B,&C);
        C^=ans;
        L=1; R=n; mid=(L+R)/2;
        while (L<=R)
        {
            if (tt[mid].x<C) {R=mid-1; mid=(L+R)/2;} else
            {
                L=mid+1;
                mid=(L+R)/2;
            }
        }
        if (!R) {ans=m; printf("%d\n",ans); continue;}
        ans=m-work2(root[R],1,n,A,B);
        printf("%d\n",ans);
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
const unsigned long long N=8e6,T=200000;
struct hjt
{
	unsigned long long msk[7950000],sum;
	int rt[200002],sz;
	unsigned long long code(int lp,int rp,int val,int lazy)
	{
		return (1ull*lp*N*T+1ull*rp*T+1ull*val+1ull*lazy);
	}
	void decode(unsigned long long mskk,int &lp,int &rp,int &val,int &lazy)
	{
		lp=mskk/N/T;rp=(mskk/T)%N;
		sum=mskk%T;
		val=max(msk[lp]%T,msk[rp]%T);
		lazy=sum-val;
	}
	int build(int lp,int rp)
	{
		if (lp==rp)
		{
			sz++;
			msk[sz]=0;
			return sz;
		}
		int pos=++sz,mid=(lp+rp)>>1,ls,rs;
		ls=build(lp,mid);rs=build(mid+1,rp);
		msk[pos]=code(ls,rs,0,0);
		return pos;
	}
	int update(int pre,int beg,int en,int lp,int rp)
	{
		if (lp>en || rp<beg) return pre;
		int pos=++sz,ls,rs,val,lazy,mid=(lp+rp)>>1;
		if (sz>7948888)
		{
			exit(0);
		}
		decode(msk[pre],ls,rs,val,lazy);
		if (lp>=beg && rp<=en)
		{
			msk[pos]=code(ls,rs,max(msk[ls]%T,msk[rs]%T),lazy+1);
			return pos;
		}
		ls=update(ls,beg,en,lp,mid);rs=update(rs,beg,en,mid+1,rp);
		msk[pos]=code(ls,rs,max(msk[ls]%T,msk[rs]%T),lazy);
		return pos;
	}
	int query(int beg,int en,int lp,int rp,int node)
	{
		if (!node) return 0;
		if (lp>en || rp<beg) return 0;
		if (lp>=beg && rp<=en) return msk[node]%T;
		int mid=(lp+rp)>>1,ls,rs,val,lazy;
		decode(msk[node],ls,rs,val,lazy);
		return max(query(beg,en,lp,mid,ls),query(beg,en,mid+1,rp,rs))+lazy;
	}
}tree;
int n,m,a[200002],b[200002],c[200002],l,r,x,q,ans,L,R;
bool cmp(int i,int j)
{
	return a[i]>a[j];
}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	for (int i=1;i<=n;i++) b[i]=i;
	sort(b+1,b+n+1,cmp);
	for (int i=1;i<=n;i++) c[i]=a[b[i]];
	tree.rt[0]=tree.build(1,n-m+1);
	for (int i=1;i<=n;i++)
	{
		L=max(1,b[i]-m+1);R=b[i];
		tree.rt[i]=tree.update(tree.rt[i-1],L,R,1,n-m+1);
	}
	scanf("%d",&q);
	while(q--)
	{
		scanf("%d%d%d",&l,&r,&x);
		x^=ans;
		L=1;R=n;
		while(L<=R)
		{
			int mid=(L+R)>>1;
			if (c[mid]>=x) L=mid+1;
			else R=mid-1;
		}
		ans=tree.query(l,r,1,n-m+1,tree.rt[R]);
		ans=m-ans;
		printf("%d\n",ans);
	}
	return 0;
}

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<utility>
#define M 19
#define N 200010
#define B (N*2)
#define inf 1023456789
using namespace std;
//sum*(2N)^2+min*(2N)+lc
//offset=N
long long tr[M][N*2];
int buffer[2][N*2],a[N],p,mlen;
int nxt[N];
char single[N];
const long long zero=1LL*N*B*B+1LL*N*B+N;
bool cmp(int i,int j){
	return a[abs(i)]<a[abs(j)];
}
void build(int n,int m){
	int i,l,r,md,nid,sum,mn,lsum,rsum,lmn,rmn,lc,rc,lv=0;
	bool final;
	for(i=0;i<n-m;i++){
		buffer[p][i*2]=i;
		buffer[p][i*2+1]=-(i+m);
	}
	sort(buffer[p],buffer[p]+(n-m)*2,cmp);
	nxt[0]=n-m;
	while(1){
		final=true;
		for(l=0;l<n-m;l=r){
			r=nxt[l];
			if(l+1==r){
				single[l]++;
				if(buffer[p][l*2]>=0){
					tr[lv][l*2]=zero-1LL*B*B-B;
					tr[lv][l*2+1]=zero;
				}
				else{
					tr[lv][l*2]=zero+1LL*B*B;
					tr[lv][l*2+1]=zero;
				}
				continue;
			}
			final=false;
			md=(l+r)/2;
			nxt[l]=md;
			nxt[md]=r;
			lc=l*2;
			rc=md*2;
			for(i=l*2;i<r*2;i++){
				if(buffer[p][i]>=0){
					nid=buffer[p][i];
				}
				else{
					nid=-buffer[p][i]-m;
				}
				if(nid<md){
					buffer[p^1][lc++]=buffer[p][i];
				}
				else{
					buffer[p^1][rc++]=buffer[p][i];
				}
				tr[lv][i]=zero+lc-1;
			}
		}
		if(final) break;
		p^=1;
		lv++;
	}
	lv--;
	while(lv>=0){
		for(l=0;l<n-m;l=nxt[l]){
			if(single[l]>1){
				single[l]--;
				continue;
			}
			md=nxt[l];
			nxt[l]=r=nxt[md];
			if(l+1==r){
				continue;
			}
			for(i=l*2;i<r*2;i++){
				lc=tr[lv][i]%B-N;
				rc=md*2+(i-lc)-1;
				if(lc<l*2){
					if(lc!=l*2-1){
						printf("lc(%d,%d)=%d\n",lv,i,lc);
						exit(0);
					}
					lsum=lmn=0;
				}
				else{
					lsum=tr[lv+1][lc]/B/B-N;
					lmn=tr[lv+1][lc]/B%B-N;
				}
				if(i==lc){
					rsum=rmn=0;
				}
				else{
					rsum=tr[lv+1][rc]/B/B-N;
					rmn=tr[lv+1][rc]/B%B-N;
				}
				sum=lsum+rsum;
				mn=min(lsum+rmn,lmn);
				tr[lv][i]+=1LL*sum*B*B+1LL*mn*B;
			}
		}
		lv--;
	}
	memcpy(buffer[0],a,sizeof(int)*m);
	sort(buffer[0],buffer[0]+m);
	memcpy(buffer[1],a,sizeof(int)*(n-m));
	memcpy(buffer[1]+n-m,a+m,sizeof(int)*(n-m));
	sort(buffer[1],buffer[1]+(n-m)*2);
}
int qpsum(int lv,int l,int r,int qr,int i){
	if(i<l*2) return 0;
	if(lv>=M||i<0||i>=N*2){
		fprintf(stderr,"GGqsum\n");
		exit(0);
	}
	if(qr==r) return tr[lv][i]/B/B-N;
	int md=(l+r)/2,lc=tr[lv][i]%B-N,rc=md*2+i-lc-1;
	if(qr<=md) return qpsum(lv+1,l,md,qr,lc);
	else return qpsum(lv+1,l,md,md,lc) + qpsum(lv+1,md,r,qr,rc);
}
//first:sum second:min
//return value: (sum+N)*B+(min+N)
long long qry(int lv,int l,int r,int ql,int qr,int i){
	if(i<l*2) return zero/B;
	if(lv>=M||i<0||i>=N*2){
		fprintf(stderr,"GGqry\n");
		exit(0);
	}
	if(l==ql&&r==qr){
		return tr[lv][i]/B;
	}
	int md=(l+r)/2,lc=tr[lv][i]%B-N,rc=md*2+i-lc-1;
	if(qr<=md){
		return qry(lv+1,l,md,ql,qr,lc);
	}
	else if(ql>=md){
		return qry(lv+1,md,r,ql,qr,rc);
	}
	else{
		long long lres=qry(lv+1,l,md,ql,md,lc);
		long long rres=qry(lv+1,md,r,md,qr,rc);
		return (lres/B+rres/B-N)*B+min(lres%B,lres/B+rres%B-N);
	}
}
int cal(int l,int r,int n,int m,int q){
	l--;
	r--;
	int base=lower_bound(buffer[0],buffer[0]+m,q)-buffer[0];
	int i=(upper_bound(buffer[1],buffer[1]+(n-m)*2,q-1)-buffer[1])-1;
	return base+(l?qpsum(0,0,n-m,l,i):0)+(l<r?qry(0,0,n-m,l,r,i)%B-N:0);
}
int main(){
	int n,m,q,ans=0,l,r,x,i;
	scanf("%d%d",&n,&m);
	for(i=0;i<n;i++) scanf("%d",&a[i]);
	build(n,m);
	if(n>=200000){
		printf("build done\n");
		exit(0);
	}
	scanf("%d",&q);
	while(q--){
		scanf("%d%d%d",&l,&r,&x);
		x^=ans;
		ans=cal(l,r,n,m,x);
		printf("%d\n",ans);
	}
	return 0;
}

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
unsigned long long t[8000005];
struct node {int x,y;} tt[200005];
int ans,i,A,B,C,n,m,Q,cnt,root[200005],L,R,mid;
int cmp(node i,node j) {return i.x>j.x;}
void work(unsigned long long A,int &B,int &C)
{
    A/=200001;
    C=A%8000001;
    A/=8000001;
    B=A%8000001;
}
unsigned long long WORK(unsigned long long A,unsigned long long B,unsigned long long C)
{
    A*=8000001;
    A*=200001;
    B*=200001;
    return A+B+C;
}
void Update(int &A,int &B,int l,int r,int L,int R)
{
    if (l==r) {B=1; return;}
    if (!B) B=++cnt;
    //if (n==200000 && cnt==8000000) {cout<<i<<' '<<cnt<<endl; exit (0);}
    //cout<<A<<' '<<B<<' '<<l<<' '<<r<<' '<<L<<' '<<R<<endl;
    int mid=(l+r)/2,llB,rrB,llA,rrA;
    work(t[B],llB,rrB); work(t[A],llA,rrA);
    if (l==L && r==R)
    {
        if (!llB) llB=llA; if (!rrB) rrB=rrA;
        int D=t[A]%200001-max(llA==1?1:t[llA]%200001,rrA==1?1:t[rrA]%200001);
        t[B]=WORK(llB,rrB,max(llB==1?1:t[llB]%200001,rrB==1?1:t[rrB]%200001)+D+1);
        return;
    }
    int D=t[A]%200001-max(llA==1?1:t[llA]%200001,rrA==1?1:t[rrA]%200001);
    if (L<=mid) Update(llA,llB,l,mid,L,min(mid,R));
    if (R>mid) Update(rrA,rrB,mid+1,r,max(mid+1,L),R);
    if (!llB) llB=llA; if (!rrB) rrB=rrA;
    t[B]=WORK(llB,rrB,max(llB==1?1:t[llB]%200001,rrB==1?1:t[rrB]%200001)+D);
}
int work2(int A,int l,int r,int L,int R)
{
    if (l==L && r==R) return A==1?1:t[A]%200001;
    int mid=(l+r)/2,llA,rrA,p=0,q=0;
    work(t[A],llA,rrA);
    //cout<<A<<endl;
    int D=t[A]%200001-max(llA==1?1:t[llA]%200001,rrA==1?1:t[rrA]%200001);
    if (L<=mid) p=work2(llA,l,mid,L,min(mid,R));
    if (R>mid) q=work2(rrA,mid+1,r,max(mid+1,L),R);
   // cout<<A<<' '<<l<<' '<<r<<' '<<L<<' '<<R<<' '<<D<<' '<<p<<' '<<q<<' '<<t[A]%200001<<endl;
    return max(p,q)+D;
}
int main()
{
    scanf("%d%d",&n,&m);
    for (i=1; i<=n; i++)
    {
        scanf("%d",&tt[i].x);
        tt[i].y=i;
    }
    sort(tt+1,tt+n+1,cmp);
    for (i=1; i<=n; i++) Update(root[i-1],root[i],1,n,max(1,tt[i].y-m+1),tt[i].y);
    scanf("%d",&Q);
    while (Q--)
    {
        scanf("%d%d%d",&A,&B,&C);
        C^=ans;
        L=1; R=n; mid=(L+R)/2;
        while (L<=R)
        {
            if (tt[mid].x<C) {R=mid-1; mid=(L+R)/2;} else
            {
                L=mid+1;
                mid=(L+R)/2;
            }
        }
        if (!R) {ans=m; printf("%d\n",ans); continue;}
        ans=m-work2(root[R],1,n,A,B);
        printf("%d\n",ans);
    }
    return 0;
}

#include<cstdio>
#include<algorithm>

#define REP(i,m) for(int i=0;i<(m);++i)
#define REPN(i,m,in) for(int i=(in);i<(m);++i)
#define ALL(t) (t).begin(),(t).end()
#define CLR(a) memset((a),0,sizeof(a))
#define pb push_back
#define mp make_pair
#define fr first
#define sc second

using namespace std;

#ifndef ONLINE_JUDGE
#include<iostream>
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
#else
#define dump(x) ;
#define prl ;
template<class T> void debug(T a,T b){ ;}
#endif
template<class T> void chmin(T& a,const T& b) { a=min(a,b);}
typedef unsigned int uint;
const int INF=(1<<18)-1;

typedef pair<int,int> pi;
int cnt=1;

#define mnL(i) (md-l==1?i:pool[i].mini)
#define mnR(i) (r-md==1?i:pool[i].mini)
struct node{
  unsigned long long lch:23,rch:23,mini:18;
  int copy();
  int add(int,int,int,int);
  int query(int,int,int,int);
};
node pool[7829000];

int node::add(int a,int b,int l,int r){
  if(r-l==1) return (this-pool)+1;

  int id=cnt++;
  pool[id]=*this;
  if(a<=l && r<=b){
    ++pool[id].mini;
    return id;
  }
  int md=(l+r+1)>>1,base=mini-min(mnL(lch),mnR(rch));
  if(a<md){
    pool[id].lch=pool[lch].add(a,b,l,md);
  }
  if(md<b){
    pool[id].rch=pool[rch].add(a,b,md,r);
  }
  pool[id].mini=base+min(mnL(pool[id].lch),mnR(pool[id].rch));
  return id;
}
int node::query(int a,int b,int l,int r){
  if(r-l==1) return (this-pool);
  if(a<=l && r<=b) return mini;
  int md=(l+r+1)>>1;
  int res=INF,base=mini-min(mnL(lch),mnR(rch));
  if(a<md){
    res=pool[lch].query(a,b,l,md);
  }
  if(md<b){
    chmin(res,pool[rch].query(a,b,md,r));
  }
  return res+base;
}

int root[200005];

int n,m;

pi ar2[200005];

int main(){
  srand(114514);

  const int OFF=1;
  scanf("%d%d",&n,&m);
  REP(i,n){
    int a;scanf("%d",&a);
    ar2[i]=mp(a,i);
  }
  sort(ar2,ar2+n);
  root[0]=cnt++;

  int len=n-m+1;
  int MX=0;
  REP(i,n){
    int p=ar2[i].sc;
    int L=max(0,p-m+1),R=min(p+1,len);
    int prev=cnt;
    L+=OFF;R+=OFF;
    root[i+1]=pool[root[i]].add(L,R,0,len);
    MX=max(MX,cnt-prev);
    dump(MX);
  }
  dump(MX);

  int q;
  scanf("%d",&q);

  int prev=0;
  REP(_,q){
    int l,r,x;
    scanf("%d%d%d",&l,&r,&x);
    --l;
    x^=prev;

    l+=OFF;r+=OFF;

    int lb=lower_bound(ar2,ar2+n,mp(x,-1))-ar2;
    int res=pool[root[lb]].query(l,r,0,len);

    prev=res;
    printf("%d\n",res);
  }
  return 0;
}

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define R register
#define MN 200000
#define MB 200
#define SIZE 1000
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int n,m,N,a[MN+5],L[MN+5],num,block[MN+5],mn[MB+5][SIZE*2+5],val[SIZE*2+5],rt[MN+5],cnt=0;
struct disc
{
	int*s,num;
	disc(int size=SIZE<<1){s=new int[size+5];num=0;}
	void Add(int x){s[++num]=x;}
	void Build(){sort(s+1,s+num+1);num=unique(s+1,s+num+1)-s-1;}
	int Calc(int x){return upper_bound(s+1,s+num+1,x)-s-1;}
}lb[MB+5],LB(MN+5);
struct Tree{int l,r,x;}T[MN*18+5];

void Init_Mn(int x)
{
	int l=(x-1)*SIZE+1,r=min(x*SIZE,N);
	memset(val,0,sizeof(val));
	for(R int i=l;i<=r;++i) lb[x].Add(a[i]),lb[x].Add(a[i+m-1]);
	lb[x].Build();int tot=lb[x].num;
	for(R int i=l;i<r;++i)
	{
		int A=lb[x].Calc(a[i]),B=lb[x].Calc(a[i+m-1]);
		for(R int i=A;i<=tot;++i) --val[i];
		for(R int i=B;i<=tot;++i) ++val[i];
		for(R int i=min(A,B);i<=tot;++i) mn[x][i]=min(mn[x][i],val[i]);
	}
}
int Modify(int x,int l,int r,int k,int v)
{
	int y=++cnt,mid=l+r>>1;T[y]=T[x];T[y].x+=v;
	if(l==r) return y;
	if(k<=mid) T[y].l=Modify(T[y].l,l,mid,k,v);
	else T[y].r=Modify(T[y].r,mid+1,r,k,v);
	return y;
}
int Query(int x,int l,int r,int k)
{
	if(k<l||!x) return 0;
	if(l==r) return T[x].x;
	int mid=l+r>>1;
	if(k<=mid) return Query(T[x].l,l,mid,k);
	else return T[T[x].l].x+Query(T[x].r,mid+1,r,k);
}
int Solve(int x,int v,int V)
{
	int l=(x-1)*SIZE+1,r=min(x*SIZE,n);
	int num=Query(rt[l+m-1],1,LB.num,v)-Query(rt[l-1],1,LB.num,v);
	return num+mn[x][lb[x].Calc(V)];
}
int main()
{
	n=read();m=read();N=n-m+1;
	for(R int i=1;i<=n;++i)a[i]=read(),LB.Add(i);
	for(R int i=1;i<=N;++i) block[i]=(i-1)/SIZE+1;
	for(R int i=1;i<=block[N];++i) Init_Mn(i);
	LB.Build();int tot=LB.num;
	for(R int i=1;i<=n;++i) rt[i]=Modify(rt[i-1],1,tot,LB.Calc(a[i]),1);
	for(int Q=read(),ans=0;Q;--Q)
	{
		int l=read(),r=read(),x=(read()^ans)-1;
		int nx=LB.Calc(x);ans=1e9;
		for(int i=block[l]+1;i<block[r];++i) ans=min(ans,Solve(i,nx,x));
		int L=min(r,block[l]*SIZE),lnum=Query(rt[L+m-1],1,tot,nx)-Query(rt[L-1],1,tot,nx);
		ans=min(ans,lnum);
		for(R int i=L-1;i>=l;--i) lnum-=(a[i+m]<=x),lnum+=(a[i]<=x),ans=min(ans,lnum);
		if(block[l]!=block[r])
		{
			int RR=(block[r]-1)*SIZE+1,rnum=Query(rt[RR+m-1],1,tot,nx)-Query(rt[RR-1],1,tot,nx);
			ans=min(ans,rnum);
			for(int j=RR+1;j<=r;++j) rnum-=(a[j-1]<=x),rnum+=(a[j+m-1]<=x),ans=min(ans,rnum);
		}
		printf("%d\n",ans);
	}
	return 0;
}

#include <bits/stdc++.h>

#define	st first
#define	nd second
#define	mp make_pair
#define	pb push_back
#define	lli long long int
#define	all( gg )	gg.begin(),gg.end()
#define	foreach( gg,itit )	for( typeof(gg.begin()) itit=gg.begin();itit!=gg.end();itit++ )
#define	FP( ii,aa,bb ) for( int ii=aa;ii<=bb;ii++ )
#define	FM( ii,aa,bb ) for( int ii=aa;ii>=bb;ii-- )
#define	debug(ccc)	cout << #ccc << " = " << ccc << endl;

#define	mod	1000000007LL

#define	N	(1<<18)
#define	LEN	(1<<13)
#define	K	(1<<5)

#define	orta	(bas+son)/2
#define	sol		root*2,bas,orta
#define	sag		root*2+1,orta+1,son

using namespace std;

int	n,m;
int	arr[N];
pair<int,int>	a[N];

int	kd[18][N];
void	Init( int root,int bas,int son,int d=0 ){
	if( bas==son ){
		kd[d][bas] = arr[bas];
		return;
	}
	Init( sol,d+1 );
	Init( sag,d+1 );
	merge( kd[d+1]+bas,kd[d+1]+orta+1,kd[d+1]+orta+1,kd[d+1]+son+1,kd[d]+bas );
}
int	Query( int root,int bas,int son,int x,int y,int val,int d=0 ){
	if( son<x or y<bas )	return	0;
	if( x<=bas and son<=y )	return	lower_bound( kd[d]+bas,kd[d]+son+1,val )-(kd[d]+bas);
	return	Query( sol,x,y,val,d+1 )+Query( sag,x,y,val,d+1 );
}
int	minimal_solver( int l,int r,int v ){
	int	res=Query( 1,0,n-1,l,l+m-1,v );
	int	mini=res;
	FP( i,l,r-1 ){
		res -= (arr[i]<v);
		res += (arr[i+m]<v);
		mini = min( mini,res );
	}
	return	mini;
}

int	MINI[LEN<<1],art[LEN<<1];

int	update( int root,int bas,int son,int x,int y ){
	if( y>x )	return 0;
	if( son<x or y<bas )	return	MINI[root];
	if( x<=bas and son<=y ){
		art[root]++;
		MINI[root]++;
		return	MINI[root];
	}
	return	MINI[root] = min( update( sol,x,y ),update( sag,x,y ) )+art[root];
}
int	query( int root,int bas,int son,int x,int y ){
	if( son<x or y<bas )	return	mod;
	if( x<=bas and son<=y )	return	MINI[root];
	return	min( query( sol,x,y ),query( sag,x,y ) )+art[root];
}

int	s[K][N];
void	generate(){
	FP( g,0,(n-1)/LEN ){
		memset( MINI,30,sizeof MINI );
		memset( art,0,sizeof art );
		FP( i,0,n-1 ){
			update( 1,0,n-1,max(g*K,a[i].nd-m+1),min(g*(K+1)-1,a[i].nd) );
			s[g][i] = MINI[1];
		}
	}
}

int main(){
	ios_base::sync_with_stdio(false);
	cin >> n >> m;
	FP( i,0,n-1 ){
		cin >> arr[i];
		a[i] = mp( arr[i],i );
	}
	sort( a,a+n );
	Init( 1,0,n-1 );
	generate();
	int	q;
	cin >> q;
	int	last=0,ok=0;
	while( q-- ){
		int	l,r,v;
		cin >> l >> r >> v;
		v ^= last;
		l--;
		r--;
		int	res;
		int	t = lower_bound( a,a+n,mp( v,0 ) )-a-1;
		/*if( t==n ){
			res = m;
			last = res;
			cout << res << endl;
			continue;
		}*/
		if( l/LEN==r/LEN ){
			res = minimal_solver( l,r,v );
			last = res;
			cout << res << endl;
			continue;
		}
		res = min( minimal_solver( l,(l/LEN+1)*LEN-1,v ),minimal_solver( r/LEN*LEN,r,v ) );
		FP( i,l/LEN+1,r/LEN-1 )
			res = min( res,s[i][t] );
		last = res;
		cout << res << endl;
	}
}

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define R register
#define MN 200000
#define MB 200
#define SIZE 1000
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int n,m,N,a[MN+5],L[MN+5],num,block[MN+5],mn[MB+5][SIZE*2+5],val[SIZE*2+5],rt[MN+5],cnt=0;
struct disc
{
	int*s,num;
	disc(int size=SIZE<<1){s=new int[size+5];num=0;}
	void Add(int x){s[++num]=x;}
	void Build(){sort(s+1,s+num+1);num=unique(s+1,s+num+1)-s-1;}
	int Calc(int x){return upper_bound(s+1,s+num+1,x)-s-1;}
}lb[MB+5],LB(MN+5);
struct Tree{int l,r,x;}T[MN*18+5];

void Init_Mn(int x)
{
	int l=(x-1)*SIZE+1,r=min(x*SIZE,N);
	memset(val,0,sizeof(val));
	for(R int i=l;i<=r;++i) lb[x].Add(a[i]),lb[x].Add(a[i+m-1]);
	lb[x].Build();int tot=lb[x].num;
	for(R int i=l;i<r;++i)
	{
		int A=lb[x].Calc(a[i]),B=lb[x].Calc(a[i+m-1]);
		for(R int i=A;i<=tot;++i) --val[i];
		for(R int i=B;i<=tot;++i) ++val[i];
		for(R int i=min(A,B);i<=tot;++i) mn[x][i]=min(mn[x][i],val[i]);
	}
}
void Modify(int x,int y,int l,int r,int k,int v)
{
	T[y].x=T[x].x+v;
	if(l==r) return;int mid=l+r>>1;
	if(k<=mid) T[y].r=T[x].r,Modify(T[x].l,T[y].l=++cnt,l,mid,k,v);
	else T[y].l=T[x].l,Modify(T[x].r,T[y].r=++cnt,mid+1,r,k,v);
}
int Query(int x,int l,int r,int k)
{
	if(k<l||!x) return 0;
	if(l==r) return T[x].x;
	int mid=l+r>>1;
	if(k<=mid) return Query(T[x].l,l,mid,k);
	else return T[T[x].l].x+Query(T[x].r,mid+1,r,k);
}
int Solve(int x,int v,int V)
{
	int l=(x-1)*SIZE+1,r=min(x*SIZE,n);
	int num=Query(rt[l+m-1],1,LB.num,v)-Query(rt[l-1],1,LB.num,v);
	return num+mn[x][lb[x].Calc(V)];
}
int main()
{
	n=read();m=read();N=n-m+1;
	for(R int i=1;i<=n;++i)a[i]=read(),LB.Add(a[i]);
	for(R int i=1;i<=N;++i) block[i]=(i-1)/SIZE+1;
	for(R int i=1;i<=block[N];++i) Init_Mn(i);
	LB.Build();int tot=LB.num;
	for(R int i=1;i<=n;++i) Modify(rt[i-1],rt[i]=++cnt,1,tot,LB.Calc(a[i]),1);
	for(int Q=read(),ans=0;Q;--Q)
	{
		int l=read(),r=read(),x=(read())-1;
		int nx=LB.Calc(x);ans=1e9;
		for(int i=block[l]+1;i<block[r];++i) ans=min(ans,Solve(i,nx,x));
		int L=min(r,block[l]*SIZE),lnum=Query(rt[L+m-1],1,tot,nx)-Query(rt[L-1],1,tot,nx);
		ans=min(ans,lnum);
		for(R int i=L-1;i>=l;--i) lnum-=(a[i+m]<=x),lnum+=(a[i]<=x),ans=min(ans,lnum);
		if(block[l]!=block[r])
		{
			int RR=(block[r]-1)*SIZE+1,rnum=Query(rt[RR+m-1],1,tot,nx)-Query(rt[RR-1],1,tot,nx);
			ans=min(ans,rnum);
			for(int j=RR+1;j<=r;++j) rnum-=(a[j-1]<=x),rnum+=(a[j+m-1]<=x),ans=min(ans,rnum);
		}
		printf("%d\n",ans);
	}
	return 0;
}

//
//  main.cpp
//  Codeforces Round #302 (Div. 1) E
//
//  Created by Scholar on 6/10/15.
//  Copyright (c) 2015 Scholar. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

#define lson t[o].ls
#define rson t[o].rs
#define lmax (l==mid?int(lson):int(t[lson].mx))
#define rmax (mid+1==r?int(rson):int(t[rson].mx))

typedef unsigned long long ULL;

using namespace std;

const int Maxn=2e5+100,INF=0x3f3f3f3f;
int n,m,s,ql,qr,l,r,x,tot,root[Maxn];
struct treenode{
    ULL ls:18,rs:23,mx:23;
}t[40*Maxn];
pair<int,int>a[Maxn];

int update(int l,int r,int p){
    if(l==r)return p+1;
    int o=++tot;
    t[o]=t[p];
    if(ql<=l&&r<=qr){
        t[o].mx++;
        return o;
    }
    int mid=(l+r)>>1,tmp=t[o].mx-max(lmax,rmax);
    if(ql<=mid)lson=update(l,mid,t[p].ls);
    if(mid<qr)rson=update(mid+1,r,t[p].rs);
    t[o].mx=max(lmax,rmax);
    t[o].mx+=tmp;
    return o;
}

int query(int o,int l,int r){
    if(l==r)return o;
    if(ql<=l&&r<=qr)return t[o].mx;
    int mid=(l+r)>>1,tmx=0,tmp=t[o].mx-max(lmax,rmax);
    if(ql<=mid)tmx=max(tmx,query(lson,l,mid));
    if(mid<qr)tmx=max(tmx,query(rson,mid+1,r));
    return tmx+tmp;
}

int main(int argc, const char * argv[]) {
    ios::sync_with_stdio(0);
    int i;
    cin>>n>>m;
    for(i=1;i<=n;i++){
        cin>>a[i].first;
        a[i].first*=-1;
        a[i].second=i;
    }
    sort(a+1,a+n+1);
    for(i=1;i<=n;i++){
        ql=max(1,a[i].second-m+1);
        qr=min(n-m+1,a[i].second);
        root[i]=update(1,n-m+1,root[i-1]);
    }
    cin>>s;
    int tmp=0;
    while(s--){
        int q;
        cin>>ql>>qr>>q;
        q^=tmp;
        int id=upper_bound(a+1,a+n+1,make_pair(-q,INF))-a-1;
        printf("%d\n",tmp=m-query(root[id],1,n-m+1));
    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//INSERT ABOVE HERE
const int BS = 512;
const int MAX = 1<<18;

int dat[MAX]={}; // state
int val[MAX/BS]={}; // max
int add[MAX/BS]={}; // add
int frt[MAX/BS]={}; // front

int bf[MAX/BS][MAX/BS]; // front
int bm[MAX/BS][MAX/BS]; // max

int cp[3][MAX]={}; // pos
int cv[3][MAX]={}; // value

signed main(){
  int n,m;
  cin>>n>>m;
  vector<int> as(n+n,0);
  for(int i=0;i<n;i++) cin>>as[i];

  using P = pair<int, int>;
  vector<P> vp;
  for(int i=0;i<n;i++) vp.emplace_back(-as[i],i);
  sort(vp.begin(),vp.end());

  auto eval=
    [&](int i){
      int l=(i/BS)*BS;
      if(add[l/BS]==0) return;
      for(int j=l;j<l+BS;j++) dat[j]+=add[j/BS];
      add[l/BS]=0;
    };

  for(int i=0;i<n;i++){
    //cout<<i<<" "<<vp[i].second<<" "<<as[vp[i].second]<<endl;
    if(i%BS==0){
      for(int j=0;j<MAX;j+=BS){
        val[j/BS]+=add[j/BS];
        eval(j);
        bf[i/BS][j/BS]=dat[j];
        bm[i/BS][j/BS]=val[j/BS];
      }
    }
    // [l, r)
    int r=vp[i].second+1;
    int l=max(0,r-m);

    int idx=1;
    for(int j=l;j<r;j++){
      if(j%BS==0 && j+BS<=r){
        add[j/BS]+=1;
        j+=BS-1;
      }else{
        dat[j]+=1;
        chmax(val[j/BS],dat[j]);
        if(cp[idx-1][i]!=j/BS){
          cp[idx][i]=j/BS;
          cv[idx][i]=val[j/BS];
          idx++;
        }
      }
    }
    while(idx<=2){
      cp[idx][i]=MAX/BS-1;
      cv[idx][i]=0;
      idx++;
    }
  }

  auto calc=
    [&](int l,int r,int v)->int{
      int idx=upper_bound(vp.begin(),vp.end(),P(-v,n))-vp.begin();

      int k=idx/BS*BS;
      for(int j=0;j<MAX;j+=BS){
        val[j/BS]=bm[k/BS][j/BS];
        add[j/BS]=0;
        frt[j/BS]=bf[k/BS][j/BS];
      }

      for(int i=k;i<idx;i++){
        int q=vp[i].second+1;
        int p=max(0,q-m);
        if(p%BS) p=(p/BS+1)*BS;
        if(p<=q){
          add[p/BS]+=1;
          add[q/BS]-=1;
        }
        val[cp[1][i]]=cv[1][i];
        val[cp[2][i]]=cv[2][i];

        if(p<=q && q%BS) frt[q/BS]+=1;
      }

      for(int j=BS;j<MAX;j+=BS)
        add[j/BS]+=add[j/BS-1];

      int res=0,flg=0,tmp=0;
      for(int j=l;j<r;j++){
        if(j%BS==0 && j+BS<=r){
          chmax(res,val[j/BS]+add[j/BS]);
          j+=BS-1;
          flg=0;
        }else{
          if(flg==0){
            flg=1;
            tmp=frt[j/BS]+add[j/BS];
            int p=j/BS*BS;
            while(p<j){
              tmp-=as[p]>=v;
              tmp+=as[p+m]>=v;
              p++;
            }
          }
          chmax(res,tmp);
          tmp-=as[j]>=v;
          tmp+=as[j+m]>=v;
        }
      }
      return m-res;
    };

  int q;
  cin>>q;
  int pre_ans=0;
  for(int i=0;i<q;i++){
    int l,r,x;
    cin>>l>>r>>x;
    l--;
    int ans=calc(l,r,x^pre_ans);
    cout<<ans<<"\n";
    pre_ans=ans;
  }
  cout<<flush;
  return 0;
}

#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
using ll = long long;
using db = long double;
using ull = unsigned long long;

const int N = 2e5 + 5, MOD = 1e9 + 7;

struct Node{
  ull mx:18,ls:23,rs:23;
} t[8 * 1000 * 1000];

int ptr, rt[2 * 1000 * 100 + 5];
pair<int, int> go[2 * 1000 * 100 + 5];
int l, r;
int upd(int node, int s, int e){
  int ret = ++ptr;
  t[ret] = t[node];
  if(l <= s && e <= r){
    t[ret].mx++;
    return ret;
  }
  int md = (s + e) >> 1;
  int lMx = t[t[node].ls].mx, rMx = t[t[node].rs].mx;
  int flr = t[ret].mx - max(lMx,rMx);
  if(l <= md){
    t[ret].ls = upd(t[node].ls,s,md);
  }
  if(md < r){
    t[ret].rs = upd(t[node].rs,md+1,e);
  }
  lMx = t[t[node].ls].mx, rMx = t[t[node].rs].mx;
  t[ret].mx = max(lMx,rMx) + flr;
  return ret;
}
int qry(int node, int s, int e){
  if(l<=s&&e<=r)
    return t[node].mx;
  int md = (s+e)>>1;
  int lMx = t[t[node].ls].mx, rMx = t[t[node].rs].mx;
  int ret = 0, flr = t[node].mx - max(lMx,rMx);
  if(l <= md){
    ret = max(ret, qry(t[node].ls,s,md));
  }
  if(md < r){
    ret = max(ret, qry(t[node].rs,md+1,e));
  }
  return ret + flr;
}

int main(){
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(0);cin.tie(0);
#endif // ONLINE_JUDGE
  int n, m, q;
  cin >> n >> m;
  vector<int> ord;
  for(int i = 1; i <= n; i++){
      cin >> go[i].first; go[i].second = i;
      go[i].first *= -1;
  }

  sort(go+1,go+n+1);

  for(int i = 1; i <= n; i++){
    l = max(1, go[i].second - m + 1);
    r = min(n-m+1,go[i].second);
    rt[i] = upd(rt[i-1], 1, n - m + 1);
  }
  cin >> q;
  int ans = 0, v;
  while(q--){
    cin >> l >> r >> v;
    v ^= ans;
    int idx = upper_bound(go + 1, go+ n + 1, make_pair(-v,INT_MAX)) - go - 1;
    cout << (ans = (m-qry(rt[idx], 1, n - m + 1))) << '\n';
  }

  return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
using namespace std;

const int N = 200010;
#define x first
#define y second
unsigned long long a[N * 40];
const int b18 = (1 << 18) - 1;
const int b23 = (1 << 23) - 1;
int cnt;

void update(int &x, int y, int l, int r, int L, int R) {
	if (l == r) {
		x = y + 1;
		return;
	}

	x = ++cnt;
	while (x >= N * 40) {
		exit(0);
	}
	int rc = a[y] & b23;
	int lc = (a[y] >> 23) & b23;
	unsigned long long mx = a[y] >> 46;
	if (l == L && r == R) {
		mx++;
		a[x] = (mx << 46) | ((unsigned long long)lc << 23) | rc;
	    return;
	}

    int mid = (l + r) >> 1;
    int lazy = mx - max(a[lc] >> 46, a[rc] >> 46);
	if (R <= mid) update(lc, lc, l, mid, L, R);
	else if (L > mid) update(rc, rc, mid + 1, r, L, R);
	else {
		update(lc, lc, l, mid, L, mid);
		update(rc, rc, mid + 1, r, mid + 1, R);
	}

	mx = max(a[lc] >> 46, a[rc] >> 46);
	mx += lazy;
	a[x] = (mx << 46) | ((unsigned long long)lc << 23) | (unsigned long long)rc;
}

int query(int x, int l, int r, int L, int R) {
	int rc = a[x] & b23;
	int lc = (a[x] >> 23) & b23;
	int mx = a[x] >> 46;
	if (l == L && R == r) {
		return mx;
	}

	int mid = (l + r) >> 1;
	int lazy = mx - max(a[lc] >> 46, a[rc] >> 46);
	if (R <= mid) return lazy + query(lc, l, mid, L, R);
	if (L > mid) return lazy + query(rc, mid + 1, r, L, R);
	return lazy + max(query(lc, l, mid, L, mid), query(rc, mid + 1, r, mid + 1, R));
}

pair<int, int> pi[N];
int root[N];

int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) {
	    scanf("%d", &pi[i].x);
		pi[i].y = i;
	}

	sort(pi + 1, pi + n + 1);
	for (int i = n; i >= 1; i--) {
		int l = max(1, pi[i].y - m + 1);
		int r = min(n - m + 1, pi[i].y);
		update(root[i], root[i + 1], 1, n - m + 1, l, r);
	}

	int q;
	scanf("%d", &q);
	int ans = 0;
	while (q--) {
		int l, r, x;
		scanf("%d%d%d", &l, &r, &x);
		x ^= ans;
		if (x <= pi[1].x) {
			ans = 0;
		} else if (x > pi[n].x) {
			ans = m;
		} else {
			int lt = 1, rt = n, mid, pos;
			while (lt <= rt) {
				mid = (lt + rt) >> 1;
				if (pi[mid].x >= x) {
					pos = mid;
					rt = mid - 1;
				} else lt = mid + 1;
			}

			ans = m - query(root[pos], 1, n - m + 1, l, r);
		}
		printf("%d\n", ans);
	}

	return 0;
}

#include <bits/stdc++.h>
#define endl '\n'

using namespace std;
const int MAXN = (int)2e5 + 42;
const int MAXLOG = 3;

int n, m, q;
int a[MAXN];

void read()
{
    cin >> n >> m;
    for(int i = 0; i < n; i++)
        cin >> a[i];
}

struct node
{
	int64_t l:23, r:23, mn:17;
	int add;
};

node tr[MAXN * MAXLOG];
int tot = 0, root[MAXN];

void expand(int idx)
{
    if(!tr[idx].l) tr[idx].l = ++tot;
    if(!tr[idx].r) tr[idx].r = ++tot;
}

int update(int bef, int qL, int qR, int val, int l, int r)
{
    if(qL > r || qR < l)
        return bef;

	expand(bef);

    int nw = ++tot;
    tr[nw] = tr[bef];

    if(qL <= l && r <= qR)
    {
        tr[nw].add += val;
		tr[nw].mn += val;
        return nw;
    }

    int mid = (l + r) >> 1;
    tr[nw].l = update(tr[nw].l, qL, qR, val, l, mid);
    tr[nw].r = update(tr[nw].r, qL, qR, val, mid + 1, r);

	tr[nw].mn = min(tr[tr[nw].l].mn, tr[tr[nw].r].mn) + tr[nw].add;

	return nw;
}

int query(int idx, int qL, int qR, int l, int r, int add = 0)
{
    if(qL > r || qR < l)
        return MAXN;

    if(qL <= l && r <= qR)
		return add + tr[idx].mn;

    int mid = (l + r) >> 1; expand(idx);
	return min(query(tr[idx].l, qL, qR, l, mid, add + tr[idx].add), query(tr[idx].r, qL, qR, mid + 1, r, add + tr[idx].add));
}

void solve()
{
    vector<pair<int, int>> vec;
    for(int i = 0; i < n; i++)
        vec.push_back({a[i], i + 1});

    sort(vec.begin(), vec.end());
    root[0] = ++tot;
    for(int i = 1; i <= n; i++)
        root[i] = update(root[i - 1], max(1, vec[i - 1].second - m + 1), vec[i - 1].second, 1, 1, n);

    int last_ans = 0;
    cin >> q;

    for(int i = 0; i < q; i++)
    {
        int l, r, p;
        cin >> l >> r >> p;

        p ^= last_ans;
        p--;

        int pos = upper_bound(vec.begin(), vec.end(), make_pair(p, MAXN)) - vec.begin();
        cout << (last_ans = query(root[pos], l, r, 1, n)) << endl;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    read();
    solve();
    return 0;
}

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <list>
#include <cassert>
#include <ctime>
#include <climits>
using namespace std;

#define PB push_back
#define MP make_pair
#define SZ(v) ((int)(v).size())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define REPE(i,n) FORE(i,0,n)
#define FORSZ(i,a,v) FOR(i,a,SZ(v))
#define REPSZ(i,v) REP(i,SZ(v))
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); }

const int MAXN=200000;
const int BLOCKSZ=500;
const int MAXBLOCKS=(MAXN+BLOCKSZ-1)/BLOCKSZ;
const int MAXBSTATES=2*BLOCKSZ+1;
const int STEPSZ=500;
const int MAXSTEPS=(MAXN+1)/STEPSZ;
const int MAXSEG=1<<19;

int sval[MAXSEG],smin[MAXSEG],scnt;
void seginit(int x,int l,int r) {
	sval[x]=smin[x]=0;
	if(l==r) return;
	int m=l+(r-l)/2;
	seginit(2*x+1,l,m);
	seginit(2*x+2,m+1,r);
}
void seginit(int _scnt) { scnt=_scnt; seginit(0,0,scnt-1); }
void segadd(int x,int l,int r,int L,int R) {
	if(L<=l&&r<=R) { ++sval[x]; ++smin[x]; return; }
	int m=l+(r-l)/2;
	if(L<=m) segadd(2*x+1,l,m,L,R);
	if(m+1<=R) segadd(2*x+2,m+1,r,L,R);
	smin[x]=sval[x]+min(smin[2*x+1],smin[2*x+2]);
}
void segadd(int L,int R) { segadd(0,0,scnt-1,L,R); }
int segmn(int x,int l,int r,int L,int R) {
	if(L<=l&&r<=R) return smin[x];
	int m=l+(r-l)/2;
	if(R<=m) return sval[x]+segmn(2*x+1,l,m,L,R);
	if(L>=m+1) return sval[x]+segmn(2*x+2,m+1,r,L,R);
	return sval[x]+min(segmn(2*x+1,l,m,L,R),segmn(2*x+2,m+1,r,L,R));
}
int segmn(int L,int R) { return segmn(0,0,scnt-1,L,R); }

int n,nlisten;
typedef struct X { int val,idx; } X;
bool operator<(const X &a,const X &b) { return a.val<b.val; }
X x[MAXN];

int av[MAXN+1],al[MAXN],ar[MAXN],na;
int bcv[MAXBLOCKS][MAXBSTATES],bcl[MAXBLOCKS][MAXBSTATES-1],bcr[MAXBLOCKS][MAXBSTATES-1],bcmn[MAXBLOCKS][MAXBSTATES],nbc[MAXBLOCKS]; // local
int bd[MAXBLOCKS][MAXSTEPS]; // global

int findai(int x) { // finds last ai | av[ai]<x
	int l=0,r=na;
	while(l+1<r) {
		int m=l+(r-l)/2;
		if(av[m]<x) l=m; else r=m;
	}
	return l;
}

int findbi(int i,int x) { // finds last bi | bcv[i][bi]<x
	int l=0,r=nbc[i];
	while(l+1<r) {
		int m=l+(r-l)/2;
		if(bcv[i][m]<x) l=m; else r=m;
	}
	return l;
}

int curglob[MAXBLOCKS+1];

void run() {
	scanf("%d%d",&n,&nlisten);
	REP(i,n) scanf("%d",&x[i].val),x[i].idx=i;
	sort(x,x+n);

	int nblocks=(n+BLOCKSZ-1)/BLOCKSZ;
	seginit(nblocks*BLOCKSZ);
	na=0,av[na++]=-1; REP(i,MAXBLOCKS) nbc[i]=0,bcmn[i][nbc[i]]=0,bcv[i][nbc[i]++]=-1;
	REP(i,n) {
		int l=max(0,x[i].idx-nlisten+1),r=x[i].idx;
		int bl=l/BLOCKSZ,br=r/BLOCKSZ;
		al[na-1]=bl,ar[na-1]=br,av[na++]=x[i].val;
		{
			int cl=l-bl*BLOCKSZ,cr=bl==br?r-br*BLOCKSZ:BLOCKSZ-1;
			segadd(bl*BLOCKSZ+cl,bl*BLOCKSZ+cr);
			bcl[bl][nbc[bl]-1]=cl,bcr[bl][nbc[bl]-1]=cr,bcmn[bl][nbc[bl]-1]=segmn(bl*BLOCKSZ,bl*BLOCKSZ+BLOCKSZ-1),bcv[bl][nbc[bl]++]=x[i].val;
		}
		if(bl!=br) {
			int cl=0,cr=r-br*BLOCKSZ;
			segadd(br*BLOCKSZ+cl,br*BLOCKSZ+cr);
			bcl[br][nbc[br]-1]=cl,bcr[br][nbc[br]-1]=cr,bcmn[br][nbc[br]-1]=segmn(br*BLOCKSZ,br*BLOCKSZ+BLOCKSZ-1),bcv[br][nbc[br]++]=x[i].val;
		}
	}

	seginit(nblocks);
	REP(i,n) {
		if(al[i]!=ar[i]) segadd(al[i]+1,ar[i]-1);
		if(i%STEPSZ==0) REP(j,nblocks) bd[j][i/STEPSZ]=segmn(j,j);
	}

	int nq; scanf("%d",&nq); int pret=0;
	REP(qi,nq) {
		int l,r,x; scanf("%d%d%d",&l,&r,&x); --l,--r; int q=x^pret;
		int bl=l/BLOCKSZ,br=r/BLOCKSZ;

		FORE(i,bl,br) curglob[i]=0;
		int ai=findai(q),astep=ai/STEPSZ;
		FOR(i,astep*STEPSZ,ai) if(al[i]!=ar[i]) {
			curglob[max(bl,al[i]+1)]++;
			curglob[ar[i]]--;
		}
		FORE(i,bl+1,br) curglob[i]+=curglob[i-1];
		FORE(i,bl,br) curglob[i]+=bd[i][astep];

		int ret=INT_MAX;
		FORE(i,bl+1,br-1) {
			int bi=findbi(i,q);
			int cur=curglob[i]+bcmn[i][bi];
			if(cur<ret) ret=cur;
		}
		{
			seginit(BLOCKSZ);
			int bi=findbi(bl,q);
			REP(i,bi) segadd(bcl[bl][i],bcr[bl][i]);
			int cur=curglob[bl]+segmn(l-bl*BLOCKSZ,bl==br?r-br*BLOCKSZ:BLOCKSZ-1);
			if(cur<ret) ret=cur;
		}
		if(bl!=br) {
			seginit(BLOCKSZ);
			int bi=findbi(br,q);
			REP(i,bi) segadd(bcl[br][i],bcr[br][i]);
			int cur=curglob[br]+segmn(0,r-br*BLOCKSZ);
			if(cur<ret) ret=cur;
		}
		printf("%d\n",ret); pret=ret;
	}

}

int main() {
	run();
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

namespace TYC
{
	typedef long long ll;
	typedef pair<int, int> pii;

	const int N = 2e5 + 5, M = 450, INF = 0x3f3f3f3f;

	int n, m, tot, block, A[N], BV[M][M], FV[M][M], Tag[M], Mx[M], W[N], Bel[N], Begin[M], End[M];
	pii V[N], EX[N][2];

	inline int read()
	{
		int x = 0, f = 0, ch = getchar();
		while (!isdigit(ch))
			f |= (ch == '-'), ch = getchar();
		while (isdigit(ch))
			x = x * 10 + ch - '0', ch = getchar();
		return f ? -x : x;
	}

	void init()
	{
		block = sqrt(n);
		for (int i = 1; i <= n; i++)
			Bel[i] = (i - 1) / block + 1;
		tot = Bel[n];
		for (int i = 1; i <= tot; i++)
		{
			Begin[i] = End[i - 1] + 1;
			End[i] = min(n, End[i - 1] + block);
		}

		sort(V + 1, V + 1 + n, greater<pii>());
		for (int i = 1; i <= n; i++)
		{
			if (Begin[Bel[i]] == i)
			{
				for (int j = 1; j <= n; j++)
					W[j] += Tag[Bel[j]];
				for (int j = 1, p = Bel[i]; j <= tot; j++)
				{
					Mx[j] += Tag[j];
					Tag[j] = 0;
					BV[p][j] = Mx[j];
					FV[p][j] = W[Begin[j]];
				}
			}
			int r = V[i].second, l = max(1, r - m + 1), c = 0;
			for (int j = l; j <= r; j++)
			{
				int p = Bel[j];
				if (j == Begin[p] && End[p] <= r)
					Tag[p]++, j = End[p];
				else
				{
					if (!c || EX[i][c - 1].first != p)
						EX[i][c++].first = p;
					Mx[p] = max(Mx[p], ++W[j]);
				}
			}
			for (int j = 0; j < c; j++)
			   EX[i][j].second = Mx[EX[i][j].first];
		}
	}

	void work()
	{
		n = read(), m = read();
		for (int i = 1; i <= n; i++)
			V[i] = pii(A[i] = read(), i);
		init();
		int q = read(), vl, vr, x, real_val, last_ans = 0;
		while (q--)
		{
			vl = read(), vr = min(read(), n - m + 1), real_val = read() ^ last_ans;
			x = upper_bound(V + 1, V + 1 + n, pii(real_val, -INF), greater<pii>()) - V - 1;
			if (!x)
			{
				printf("%d\n", last_ans = m);
				continue;
			}
			int k = Bel[x];
			memset(Tag, 0, sizeof(int[tot + 1]));
			memcpy(Mx, BV[k], sizeof(int[tot + 1]));
			for (int i = Begin[k]; i <= x; i++)
			{
				int r = V[i].second, l = max(1, r - m + 1);
				if (Bel[l] != Bel[r])
				{
					if (l != Begin[Bel[l]])
						l = End[Bel[l]] + 1;
					Tag[Bel[l]]++;
					Tag[Bel[r + 1]]--;
				}
				for (int j = 0; j < 2; j++)
					if (EX[i][j].first)
						Mx[EX[i][j].first] = EX[i][j].second;
			}
			for (int i = 2; i <= tot; i++)
				Tag[i] += Tag[i - 1];
			int ans = 0, val = -1;
			for (int i = vl; i <= vr; i++)
			{
				int p = Bel[i];
				if (i == Begin[p] && End[p] <= vr)
				{
					ans = max(ans, Mx[p] + Tag[p]);
					val = -1;
					i = End[p];
				}
				else
				{
					if (val == -1)
					{
						val = FV[k][p];
						int id = Begin[p];
						for (int j = Begin[k]; j <= x; j++)
							val += max(1, V[j].second - m + 1) <= id && id <= V[j].second;
						while (id != i)
						{
							val = val - (A[id] >= real_val) + (A[id + m] >= real_val);
							id++;
						}
					}
					ans = max(ans, val);
					val = val - (A[i] >= real_val) + (A[i + m] >= real_val);
				}
			}
			printf("%d\n", last_ans = m - ans);
		}
	}
}

int main()
{
	TYC::work();
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N=200010,M=510;
int n,m,lis[N],a[N],bc,b[N],bl[N],br[N],p[N],cnt,v[N];
int f[M][M],la[N],w[N],st[M][M],pre[N],suf[N],ed[N];
inline bool comp(int i,int j){return a[i]>a[j];}
inline void Modify(int k,int l,int r){
	if(b[l]==b[r]){
		for(int i=l;i<=r;i++)w[i]++;
		for(int i=bl[l];i<=br[l];i++)pre[k]=max(pre[k],w[i]);
		return ;
	}
   for(int i=l;i<=br[b[l]];i++)w[i]++;
	for(int i=bl[l];i<=br[l];i++)pre[k]=max(pre[k],w[i]);
	for(int i=b[l]+1;i<b[r];i++)la[i]++;
	for(int i=bl[b[r]];i<=r;i++)w[i]++;
	for(int i=bl[r];i<=br[r];i++)suf[k]=max(suf[k],w[i]);
}
inline void rec(int k){
	for(int i=1;i<=cnt;i++){
		for(int j=bl[i];j<=br[i];j++)
			f[k][i]=max(f[k][i],w[j]+la[i]);
		st[k][i]=w[bl[i]];la[i]=0;
	}
}
inline int qry(int l,int r,int k,int x){
	int now=v[k],ret=0;
	for(int i=bl[k];i<l;i++)now+=(a[i+m]>=x)-(a[i]>=x);
	for(int i=l;i<=r;i++)ret=max(ret,now),now+=(a[i+m]>=x)-(a[i]>=x);
	return ret;
}
int main(){
  scanf("%d%d",&n,&m);bc=sqrt(n);
  for(int i=1;i<=n;i++)scanf("%d",&a[i]),lis[i]=a[i],p[i]=i;
  sort(lis+1,lis+n+1);
  int tp=unique(lis+1,lis+n+1)-lis-1,Q,ans=0;
  for(int i=1;i<=n;i++)a[i]=lower_bound(lis+1,lis+tp+1,a[i])-lis;
  for(int i=1,t;i<=n;i++)t=b[i]=(i-1)/bc+1,bl[t]=bl[t]?bl[t]:i,br[t]=i;
  sort(p+1,p+n+1,comp);cnt=b[n];
  for(int i=1;i<=n;i++){
	  Modify(b[i],max(1,p[i]-m+1),p[i]);
	  if(b[i]!=b[i+1])rec(b[i]),ed[b[i]]=a[p[i]];
  }
  cin>>Q;
  while(Q--){
	  int l,r,x,t=1;
	  scanf("%d%d%d",&l,&r,&x);
	  x=lower_bound(lis+1,lis+tp+1,x^ans)-lis;
	  while(t<cnt && ed[t]>=x)t++;
	  for(int i=1;i<=cnt;i++)w[i]=f[t-1][i],v[i]=st[t-1][i],la[i]=0;
	  for(int i=bl[t];i<=br[t] && a[p[i]]>=x;i++){
		  int L=max(1,p[i]-m+1),R=p[i],A=b[L],B=b[R];
		  if(A==B)w[A]=pre[A];
		  else{
			  w[A]=pre[A];w[B]=suf[B];
			  la[A+1]++;la[B]--;
		  }
		  if(L<=bl[A] && bl[A]<=R)v[A]++;
		  if(A!=B && L<=bl[B] && br[B]<=R)v[B]++;
	  }
	  for(int i=1;i<=cnt;i++)la[i]+=la[i-1];
	  int A=b[l],B=b[r];
	  if(A==B)ans=qry(l,r,A,x);
	  else{
		  ans=max(qry(l,br[A],A,x),qry(bl[B],r,B,x));
		  for(int i=A+1;i<B;i++)ans=max(ans,la[i]+w[i]);
	  }
	  ans=m-ans;
	  printf("%d\n",ans);
  }
  return 0;
}

#include<cstdio>
#include<algorithm>

#define REP(i,m) for(int i=0;i<(m);++i)
#define REPN(i,m,in) for(int i=(in);i<(m);++i)
#define ALL(t) (t).begin(),(t).end()
#define CLR(a) memset((a),0,sizeof(a))
#define pb push_back
#define mp make_pair
#define fr first
#define sc second

using namespace std;

#ifndef ONLINE_JUDGE
#include<iostream>
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
#else
#define dump(x) ;
#define prl ;
template<class T> void debug(T a,T b){ ;}
#endif
template<class T> void chmin(T& a,const T& b) { a=min(a,b);}
typedef unsigned int uint;
const int INF=(1<<18)-1;

typedef pair<int,int> pi;
int cnt=1;

#define mnL(i) (md-l==1?i:pool[i].mini)
#define mnR(i) (r-md==1?i:pool[i].mini)
struct node{
  unsigned long long lch:23,rch:23,mini:18;
  int copy();
  int add(int,int,int,int);
  int query(int,int,int,int);
};
node pool[7829000];

int node::add(int a,int b,int l,int r){
  if(r-l==1) return (this-pool)+1;

  int id=cnt++;
  pool[id]=*this;
  if(a<=l && r<=b){
    ++pool[id].mini;
    return id;
  }
  int md=(l+r+1)>>1,base=mini-min(mnL(lch),mnR(rch));
  if(a<md){
    pool[id].lch=pool[lch].add(a,b,l,md);
  }
  if(md<b){
    pool[id].rch=pool[rch].add(a,b,md,r);
  }
  pool[id].mini=base+min(mnL(pool[id].lch),mnR(pool[id].rch));
  return id;
}
int node::query(int a,int b,int l,int r){
  if(r-l==1) return (this-pool);
  if(a<=l && r<=b) return mini;
  int md=(l+r+1)>>1;
  int res=INF,base=mini-min(mnL(lch),mnR(rch));
  if(a<md){
    res=pool[lch].query(a,b,l,md);
  }
  if(md<b){
    chmin(res,pool[rch].query(a,b,md,r));
  }
  return res+base;
}

int root[200005];

int n,m;

pi ar2[200005];

int main(){
  srand(114514);

  const int OFF=7;
  scanf("%d%d",&n,&m);
  REP(i,n){
    int a;scanf("%d",&a);
    ar2[i]=mp(a,i);
  }
  sort(ar2,ar2+n);
  root[0]=cnt++;

  int len=n-m+1;
  int MX=0;
  REP(i,n){
    int p=ar2[i].sc;
    int L=max(0,p-m+1),R=min(p+1,len);
    int prev=cnt;
    L+=OFF;R+=OFF;
    root[i+1]=pool[root[i]].add(L,R,0,len);
    MX=max(MX,cnt-prev);
    dump(MX);
  }
  dump(MX);

  int q;
  scanf("%d",&q);

  int prev=0;
  REP(_,q){
    int l,r,x;
    scanf("%d%d%d",&l,&r,&x);
    --l;
    x^=prev;

    l+=OFF;r+=OFF;

    int lb=lower_bound(ar2,ar2+n,mp(x,-1))-ar2;
    int res=pool[root[lb]].query(l,r,0,len);

    prev=res;
    printf("%d\n",res);
  }
  return 0;
}

#include<bits/stdc++.h>
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define fi first
#define se second
#define sz(a) int(a.size())
#define clr(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
const int inf=1e9;
const ll Inf=1e18;
const int mod=1;
template<typename T=int>
T gi() {
    T x=0,o=1;char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if(ch=='-') o=-1,ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*o;
}
template<typename T> bool chkmax(T &a,T b) { return a<b?a=b,1:0; };
template<typename T> bool chkmin(T &a,T b) { return a>b?a=b,1:0; };
int add(int a,int b) { return a+b>=mod?a+b-mod:a+b; }
int sub(int a,int b) { return a-b<0?a-b+mod:a-b; }
void inc(int &a,int b) { a=(a+b>=mod?a+b-mod:a+b); }
void dec(int &a,int b) { a=(a-b<0?a-b+mod:a-b); }
string to_string(string s) { return '"'+s+'"'; }
string to_string(const char *s) { return to_string((string)s); }
string to_string(bool b) { return (b?"true":"false"); }
template<typename A,typename B> string to_string(pair<A,B> p) {
    return "("+to_string(p.fi)+","+to_string(p.se)+")";
}
template<typename T> string to_string(T v) {
    int fst=1;string ret="{";
    for(auto x:v) {
        if(!fst) ret+=",";
        fst=0,ret+=to_string(x);
    }
    ret+="}";return ret;
}
void dbg_out() { cerr<<endl; }
template<typename Head,typename... Tail> void dbg_out(Head H,Tail... T) {
    cerr<<" "<<to_string(H);
    dbg_out(T...);
}
#define dbg(...) cerr<<"{"<<#__VA_ARGS__<<"}:",dbg_out(__VA_ARGS__)

template<typename T>
int qpow(int a,T b) {
    int ret=1;
    while(b) {
        if(b&1) ret=1ll*ret*a%mod;
        a=1ll*a*a%mod,b>>=1;
    }
    return ret;
}

const int N=2e5+10;
const int M=450;

int n,m,a[N],bel[N],sz,blo,pre[N],suf[N],val[N],id[N];
int w[N],s[N],sum[N];

struct block {

    int bl,br,tag=0,f[M],s[M];

    void modify(int l,int r) {
	for(int i=l;i<=r;i++) ++val[i];
    }

    void modify() {
	++tag;
    }

    void pushdown() {
	for(int i=bl;i<=br;i++) val[i]+=tag;
	tag=0;
    }

    void save(int tim) {
	pushdown();
	for(int i=bl;i<=br;i++) chkmax(f[tim],val[i]);
	s[tim]=val[bl];
    }

    int max() {
	int ret=0;
	for(int i=bl;i<=br;i++) chkmax(ret,val[i]);
	return ret;
    }

    int query(int l,int r,int x,int s) {
	int sum=s,ans=0;
	for(int i=bl;i<l;i++) sum+=(a[i+m]>=x)-(a[i]>=x);
	for(int i=l;i<=r;i++) {
	    chkmax(ans,sum);
	    sum+=(a[i+m]>=x)-(a[i]>=x);
	}
	return ans;
    }

} T[M];

void modify(int l,int r,int k) {
    int L=bel[l],R=bel[r];
    if(L==R) T[L].modify(l,r);
    else {
	T[L].modify(l,T[L].br);
	for(int i=L+1;i<R;i++) T[i].modify();
	T[R].modify(T[R].bl,r);
    }
    pre[k]=T[L].max();
    suf[k]=T[R].max();
}

void save(int tim) {
    for(int i=1;i<=blo;i++) T[i].save(tim);
}

int query(int l,int r,int x) {
    int L=bel[l],R=bel[r];
    if(L==R) return T[L].query(l,r,x,s[L]);
    else {
	int ans=T[L].query(l,T[L].br,x,s[L]);
	for(int i=L+1;i<R;i++) chkmax(ans,w[i]+sum[i]);
	chkmax(ans,T[R].query(T[R].bl,r,x,s[R]));
	return ans;
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
#endif
    n=gi(),m=gi();
    for(int i=1;i<=n;i++) a[i]=gi(),id[i]=i;
    sort(id+1,id+n+1,[&](int x,int y) { return a[x]>a[y]; });
    sz=sqrt(n);
    for(int i=1;i<=n;i++) bel[i]=(i-1)/sz+1;
    blo=bel[n];
    for(int i=1;i<=blo;i++) T[i].bl=T[i-1].br+1,T[i].br=min(n,i*sz);
    for(int i=1;i<=n;i++) {
	int x=id[i];
	modify(max(1,x-m+1),x,i);
	if(bel[i]!=bel[i+1]) save(bel[i]);
    }
    int q=gi(),ans=0;
    while(q--) {
	int l=gi(),r=gi(),x=gi()^ans,t=1;
	while(t<blo&&a[id[T[t].br]]>=x) ++t;
	for(int i=1;i<=blo;i++) w[i]=T[i].f[t-1],s[i]=T[i].s[t-1],sum[i]=0;
	for(int i=T[t-1].br+1;i<=n&&a[id[i]]>=x;i++) {
	    int x=id[i],l=max(1,x-m+1),r=x,L=bel[l],R=bel[r];
	    if(L==R) w[L]=pre[i];
	    else {
		w[L]=pre[i];
		++sum[L+1],--sum[R];
		w[R]=suf[i];
	    }
	    if(l<=T[L].bl&&T[L].bl<=r) ++s[L];
	    if(L!=R&&l<=T[R].bl&&T[R].bl<=r) ++s[R];
	}
	for(int i=1;i<=blo;i++) sum[i]+=sum[i-1];
	ans=m-query(l,r,x);
	printf("%d\n",ans);
    }
    return 0;
}

#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
using ll = long long;
using db = long double;

const int N = 2e5 + 5, MOD = 1e9 + 7;
vector<int> go[N];
int n, m, q, a[N];
struct Node{
  Node * L, * R;
  int val, lzL, lzR;  ///let lz identify those that are under it
  node(){
    L = R = NULL;
    val = lzL = lzR = 0;
  }
} * trees[N];
Node * upd(Node * root, int s, int e, int l, int r){
//  cout << s << ' ' << e << '\n';
  ///add 1 to all elements between l and r
  ///this part is pretty trick
  ///if this is out of range forget it
  if(r<s||e<l)
    return root;
  if(l <= s && e <= r){
    Node * ret = new Node();
    ///remember the lz isn't for you it's for your children
    if(s != e){
      ret->lzL = root->lzL + 1;
      ret->lzR = root->lzR + 1;
    }
    ret->val = root->val + 1;
    ret->L = root->L, ret->R = root->R;
//    cout << "F " << ret->val << '\n';
    return ret;
  }
  int md = (s + e) >> 1;
  ///remember to push this one before doing anything
  Node * ret = new Node();
  ret->L = root->L, ret->R = root->R;
  if(l <= md){  /// i need the left child
    if(!root->L)root->L = new Node();
    root->L->val += root->lzL;
    root->L->lzL += root->lzL;
    root->L->lzR += root->lzL;
    root->lzL = 0;
    ret->L = upd(root->L,s,md,l,r);
  }
  if(r > md){ /// i need the right child
    if(!root->R)root->R = new Node();
    root->R->val += root->lzR;
    root->R->lzL += root->lzR;
    root->R->lzR += root->lzR;
    root->lzR = 0;
    ret->R = upd(root->R,md+1,e,l,r);
  }
  ret->lzL = root->lzL, ret->lzR = root->lzR;
  int x, y;
  if(ret->L)x = ret->L->val + ret->lzL;
  else x = ret->lzL;
  if(ret->R)y = ret->R->val + ret->lzR;
  else y = ret->lzR;
  ret->val = min(x,y);
  return ret;
}
int qry(Node * root, int s, int e, int l, int r){
//  cout << root->val << ' ' << s << ' ' << e << ' ' << l << ' ' << r << ' ' << root->lzL << ' ' << root->lzR << '\n';
  if(r < s || e < l)
    return INT_MAX;
  if(!root)return 0;
  if(l <= s && e <= r){
    ///fokk mn el lazy dh lel children ana 3yz just el value
    return root->val;
  }
  int md = (s + e) >> 1, ret = INT_MAX;
  if(l <= md){  /// i need the left child
    if(!root->L)root->L = new Node();
    root->L->val += root->lzL;
    root->L->lzL += root->lzL;
    root->L->lzR += root->lzL;
    root->lzL = 0;
    ret = min(ret, qry(root->L,s,md,l,r));
  }
  if(r > md){ /// i need the right child
    if(!root->R)root->R = new Node();
    root->R->val += root->lzR;
    root->R->lzL += root->lzR;
    root->R->lzR += root->lzR;
    root->lzR = 0;
    ret = min(ret, qry(root->R,md+1,e,l,r));
  }
  return ret;
}
int main(){
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(0);cin.tie(0);
#endif // ONLINE_JUDGE

  cin >> n >> m;
  vector<int> ord;
  for(int i = 0; i < n; i++)
    cin >> a[i], ord.pb(a[i]);
  ord.pb(-1);
  sort(ord.begin(), ord.end());
  ord.resize(unique(ord.begin(), ord.end()) - ord.begin());

  for(int i = 0; i < n; i++){
    a[i] = lower_bound(ord.begin(),ord.end(), a[i]) - ord.begin();
    go[a[i]].pb(i);
  }

  trees[0] = new Node();

  for(int i = 1; i < ord.size(); i++){
    trees[i] = trees[i-1];
    for(auto x : go[i]){
      ///check those from x-m+1 to x
      int l = max(0,x-m+1), r = min(x,n-m);
      trees[i] = upd(trees[i], 0, n - m, l, r);
//      cout << ord[i] << ' ' << l << ' ' << r << '\n';
    }
  }

  cin >> q;
  int ans = 0;
  while(q--){
    int l, r, v;
    cin >> l >> r >> v;
    v ^= ans;
    --l, --r, --v;
    ///find the biggest number less than or equal to v
    int idx = upper_bound(ord.begin(), ord.end(), v) - ord.begin(); --idx;
//    cout << "H" << ord[idx] << '\n';
    cout << (ans = qry(trees[idx], 0, n - m, l, r)) << '\n';
  }

  return 0;
}

//
//  main.cpp
//  Codeforces Round #302 (Div. 1) E
//
//  Created by Scholar on 6/10/15.
//  Copyright (c) 2015 Scholar. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

#define lson t[o].ls
#define rson t[o].rs
#define lmax (l==mid?int(lson):int(t[lson].mx))
#define rmax (mid+1==r?int(rson):int(t[rson].mx))

using namespace std;

const int Maxn=2e5+100,INF=0x3f3f3f3f;
int n,m,s,ql,qr,l,r,x,tot,root[Maxn];
struct treenode{
    int ls,rs,mx;
}t[20*Maxn];
pair<int,int>a[Maxn];

int update(int l,int r,int p){
    if(l==r)return p+1;
    int o=++tot;
    t[o]=t[p];
    if(ql<=l&&r<=qr){
        t[o].mx++;
        return o;
    }
    int mid=(l+r)>>1,tmp=t[o].mx-max(lmax,rmax);
    if(ql<=mid)lson=update(l,mid,t[p].ls);
    if(mid<qr)rson=update(mid+1,r,t[p].rs);
    t[o].mx=max(lmax,rmax);
    t[o].mx+=tmp;
    return o;
}

int query(int o,int l,int r){
    if(l==r)return o;
    if(ql<=l&&r<=qr)return t[o].mx;
    int mid=(l+r)>>1,tmx=0,tmp=t[o].mx-max(lmax,rmax);
    if(ql<=mid)tmx=max(tmx,query(lson,l,mid));
    if(mid<qr)tmx=max(tmx,query(rson,mid+1,r));
    return tmx+tmp;
}

int main(int argc, const char * argv[]) {
    ios::sync_with_stdio(0);
    int i;
    cin>>n>>m;
    for(i=1;i<=n;i++){
        cin>>a[i].first;
        a[i].first*=-1;
        a[i].second=i;
    }
    sort(a+1,a+n+1);
    for(i=1;i<=n;i++){
        ql=max(1,a[i].second-m+1);
        qr=min(n-m+1,a[i].second);
        root[i]=update(1,n-m+1,root[i-1]);
    }
    cin>>s;
    int tmp=0;
    while(s--){
        int q;
        cin>>ql>>qr>>q;
        q^=tmp;
        int id=upper_bound(a+1,a+n+1,make_pair(-q,INF))-a-1;
        printf("%d\n",tmp=m-query(root[id],1,n-m+1));
    }
    return 0;
}

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cassert>

using namespace std;

#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define foreach(e, x) for (__typeof(x.begin()) e = x.begin(); e != x.end(); ++e)
typedef long long LL;
typedef pair<int, int> PII;

struct Node {
    unsigned x, y, z;
    Node(unsigned val = 0, unsigned p = 0, unsigned le = 5555555, unsigned ri = 5555555) {
        x = val;
        x |= (p & 16383) << 18;
        y = (p >> 14);
        y |= le << 4;
        z = ri;
    }
};

inline void getNodeValues(const Node &node, int &val, int &p, int &le, int &ri) {
    val = node.x & ((1 << 18) - 1);
    p = node.x >> 18;
    p |= (node.y & 15) << 14;
    le = node.y >> 4;
    ri = node.z;
}

Node nodes[5000000];
int st[200001];
int nc = 0;

int stBuild(int L, int R) {
    if (L == R) {
        nodes[nc] = Node();
        return nc++;
    }
    int mid = (L + R) >> 1;
    int le = stBuild(L, mid);
    int ri = stBuild(mid + 1, R);
    nodes[nc] = Node(0, 0, le, ri);
    if (nc >= 5000000) { cout << -1 << endl; exit(0); } return nc++;
}

int puu(int v, int pu, bool eq) {
    int val, p, le, ri;
    getNodeValues(nodes[v], val, p, le, ri);
    val += pu;
    if (!eq) p += pu;
    nodes[nc] = Node(val, p, le, ri);
    if (nc >= 5000000) { cout << -1 << endl; exit(0); } return nc++;
}

int stInc(int v, int L, int R, int l, int r, int pu = 0) {
    int val, p, le, ri;
    getNodeValues(nodes[v], val, p, le, ri);
    val += pu;
    if (L != R) p += pu;
    if (l == L && r == R) {
        if (L != R) ++p;
        ++val;
        nodes[nc] = Node(val, p, le, ri);
        if (nc >= 5000000) { cout << -1 << endl; exit(0); } return nc++;
    }
    int mid = (L + R) >> 1;
    if (l <= mid) le = stInc(le, L, mid, l, min(r, mid), p);
    else if (p) le = puu(le, p, L == mid);
    if (r > mid) ri = stInc(ri, mid + 1, R, max(l, mid + 1), r, p);
    else if (p) ri = puu(ri, p, mid + 1 == R);
    nodes[nc] = Node(min(nodes[le].x & ((1 << 18) - 1), nodes[ri].x & ((1 << 18) - 1)), 0, le, ri);
    if (nc >= 5000000) { cout << -1 << endl; exit(0); } return nc++;
}

int stGet(int v, int L, int R, int l, int r, int pu = 0) {
    int val, p, le, ri;
    getNodeValues(nodes[v], val, p, le, ri);
    val += pu;
    if (L != R) p += pu;
    if (l == L && r == R) {
        return val;
    }
    int mid = (L + R) >> 1;
    int best = 5555555;
    if (l <= mid) best = stGet(le, L, mid, l, min(r, mid), p);
    if (r > mid) best = min(best, stGet(ri, mid + 1, R, max(l, mid + 1), r, p));
    return best;
}

int n, m;
int a[200000], ord[200000];

bool cmp(const int &lhs, const int &rhs) {
    return a[lhs] > a[rhs];
}

int main() {
    scanf("%d%d", &n, &m);
    st[n] = stBuild(0, n - m);
    REP(i, n) scanf("%d", a + i);
    REP(i, n) ord[i] = i;
    sort(ord, ord + n, cmp);
    for (int i = n - 1; i >= 0; --i) {
        int pos = ord[i];
        st[i] = stInc(st[i + 1], 0, n - m, max(0, pos - m + 1), min(n - m, pos));
    }
    int ans = 0, qcnt;
    scanf("%d", &qcnt);
    REP(query, qcnt) {
        int from, to, qual;
        scanf("%d%d%d", &from, &to, &qual), --from, --to, qual ^= ans;
        int lo = 0, hi = n, mid;
        while (lo < hi) {
            mid = (lo + hi) >> 1;
            if (a[ord[mid]] < qual)
                hi = mid;
            else
                lo = mid + 1;
        }
        ans = stGet(st[lo], 0, n - m, from, to);
        printf("%d\n", ans);
    }
    return 0;
}

#pragma GCC optimize ("O3")
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
using ll = long long;
using db = long double;

const int N = 2e5 + 5, MOD = 1e9 + 7;
vector<int> go[N];
int n, m, q, a[N];
struct Node{
  Node * L, * R;
  int val, lzL, lzR;  ///let lz identify those that are under it
  node(){
    L = R = NULL;
    val = lzL = lzR = 0;
  }
} * trees[N];
Node * upd(Node * root, int s, int e, int l, int r){
    assert(root);
  ///add 1 to all elements between l and r
  ///this part is pretty trick
  ///if this is out of range forget it
  if(l <= s && e <= r){
    Node * ret = new Node();
    ///remember the lz isn't for you it's for your children
    if(s != e){
      ret->lzL = root->lzL + 1;
      ret->lzR = root->lzR + 1;
    }
    ret->val = root->val + 1;
    ret->L = root->L, ret->R = root->R;
//    cout << "F " << ret->val << '\n';
    return ret;
  }
  int md = (s + e) >> 1;
  ///remember to push this one before doing anything
  Node * ret = new Node();
  ret->L = root->L, ret->R = root->R;
  if(l <= md){  /// i need the left child
    if(!root->L)root->L = new Node();
    root->L->val += root->lzL;
    root->L->lzL += root->lzL;
    root->L->lzR += root->lzL;
    root->lzL = 0;
    ret->L = upd(root->L,s,md,l,r);
  }
  if(r > md){ /// i need the right child
    if(!root->R)root->R = new Node();
    root->R->val += root->lzR;
    root->R->lzL += root->lzR;
    root->R->lzR += root->lzR;
    root->lzR = 0;
    ret->R = upd(root->R,md+1,e,l,r);
  }
  ret->lzL = root->lzL, ret->lzR = root->lzR;
  int x, y;
  if(ret->L)x = ret->L->val + ret->lzL;
  else x = ret->lzL;
  if(ret->R)y = ret->R->val + ret->lzR;
  else y = ret->lzR;
  ret->val = min(x,y);
  return ret;
}
int qry(Node * root, int s, int e, int l, int r){
  assert(root);
  if(l <= s && e <= r){
    ///fokk mn el lazy dh lel children ana 3yz just el value
    return root->val;
  }
  int md = (s + e) >> 1, ret = INT_MAX;
  if(l <= md){  /// i need the left child
    if(!root->L)root->L = new Node();
    root->L->val += root->lzL;
    root->L->lzL += root->lzL;
    root->L->lzR += root->lzL;
    root->lzL = 0;
    ret = min(ret, qry(root->L,s,md,l,r));
  }
  if(r > md){ /// i need the right child
    if(!root->R)root->R = new Node();
    root->R->val += root->lzR;
    root->R->lzL += root->lzR;
    root->R->lzR += root->lzR;
    root->lzR = 0;
    ret = min(ret, qry(root->R,md+1,e,l,r));
  }
  return ret;
}
int main(){
#ifdef ONLINE_JUDGE
  ios_base::sync_with_stdio(0);cin.tie(0);
#endif // ONLINE_JUDGE

  cin >> n >> m;
  vector<int> ord;
  for(int i = 0; i < n; i++)
    cin >> a[i], ord.pb(a[i]);
  ord.pb(-1);
  sort(ord.begin(), ord.end());
  ord.resize(unique(ord.begin(), ord.end()) - ord.begin());

  for(int i = 0; i < n; i++){
    a[i] = lower_bound(ord.begin(),ord.end(), a[i]) - ord.begin();
    go[a[i]].pb(i);
  }

  trees[0] = new Node();

  for(int i = 1; i < ord.size(); i++){
    trees[i] = trees[i-1];
    for(auto x : go[i]){
      ///check those from x-m+1 to x
      int l = max(0,x-m+1), r = min(x,n-m);
      trees[i] = upd(trees[i], 0, n - m, l, r);
//      cout << ord[i] << ' ' << l << ' ' << r << '\n';
    }
  }

  cin >> q;
  int ans = 0;
  while(q--){
    int l, r, v;
    cin >> l >> r >> v;
    v ^= ans;
    --l, --r, --v;
    ///find the biggest number less than or equal to v
    int idx = upper_bound(ord.begin(), ord.end(), v) - ord.begin(); --idx;
//    cout << "H" << ord[idx] << '\n';
    cout << (ans = qry(trees[idx], 0, n - m, l, r)) << '\n';
  }

  return 0;
}

#include<bits/stdc++.h>
#define maxn 200009
#define maxm 502
using namespace std;
struct data{
  int w,p;
}t[maxn];
struct Blo{
  int a[maxm],fr[maxm],tag[maxm];
}B[maxm];
int a[maxn],bl[maxn],L[maxn],R[maxn],w[maxn],fr[maxn],tag[maxn],zx[maxn],f[maxn][2],blo,ans=0,n,m,qes;
inline bool cmp(const data &A,const data &B){return A.w<B.w;}
inline int Find(int x){
  int l=1,r=n,p=0;
  while(l<=r){
    int mid=(l+r)>>1;
    if(t[mid].w<=x) l=mid+1,p=mid;
    else r=mid-1;
  }
  return p;
}
inline void work(int l,int r,int x){
  ans=1e9;int p=Find(x-1);
  if(!p){puts("0"),ans=0;return;}
  for(int i=1;i<=bl[n];i++) tag[i]=0,zx[i]=B[bl[p]-1].a[i],fr[i]=B[bl[p]-1].fr[i];
  for(int i=L[bl[p]];i<=p;i++){
    int hl=max(1,t[i].p+1-m),hr=t[i].p;
    if(bl[hl]!=bl[hr]) tag[bl[hl]+1]++,tag[bl[hr]]--;
    zx[bl[hl]]=f[i][0],zx[bl[hr]]=f[i][1];
    if(hl==L[bl[hl]]) ++fr[bl[hl]];
    if(bl[hl]!=bl[hr]) ++fr[bl[hr]];
  }
  for(int i=1;i<=bl[n];i++) tag[i]+=tag[i-1];
  for(int i=1;i<=bl[n];i++) tag[i]+=B[bl[p]-1].tag[i];
  int sum=fr[bl[l]]+tag[bl[l]];
  for(int i=L[bl[l]];i<=R[bl[l]];i++){
    if(i>=l && i<=r) ans=min(ans,sum);
    if(a[i]<x) --sum;if(a[i+m]<x) ++sum;
  }
  for(int i=bl[l]+1;i<bl[r];i++) ans=min(ans,tag[i]+zx[i]);
  sum=fr[bl[r]]+tag[bl[r]];
  for(int i=L[bl[r]];i<=R[bl[r]];i++){
    if(i>=l && i<=r) ans=min(ans,sum);
    if(a[i]<x) --sum;if(a[i+m]<x) ++sum;
  }
  printf("%d\n",ans);
}
inline void rebuild(int x){
  zx[x]=1e9,fr[x]=w[L[x]];
  for(int i=L[x];i<=R[x];i++)
    zx[x]=min(zx[x],w[i]);
}
int main(){
  scanf("%d%d",&n,&m);blo=sqrt(n);
  for(int i=1;i<=n;i++)
  scanf("%d",a+i),t[i].p=i,t[i].w=a[i];
  sort(t+1,t+n+1,cmp);
  for(int i=1;i<=n;i++) bl[i]=(i-1)/blo+1,L[i]=(i-1)*blo+1,R[i]=min(i*blo,n);
  for(int i=1;i<=bl[n];i++){
    for(int j=L[i];j<=R[i];j++){
      int p=t[j].p,l=max(1,p+1-m),r=p;
      for(int k=l;k<=R[bl[l]];k++) ++w[k];
      if(bl[l]!=bl[r])
	for(int k=L[bl[r]];k<=r;k++) ++w[k];
      for(int k=bl[l]+1;k<bl[r];k++) tag[k]++;
      rebuild(bl[l]),rebuild(bl[r]);
      f[j][0]=zx[bl[l]],f[j][1]=zx[bl[r]];
    }
    for(int j=1;j<=bl[n];j++)
      B[i].a[j]=zx[j],B[i].fr[j]=fr[j],B[i].tag[j]=tag[j];
  }
  scanf("%d",&qes);
  for(int i=1,l,r,x;i<=qes;i++)
    scanf("%d%d%d",&l,&r,&x),work(l,r,x^ans);
  return 0;
}

#include<stdio.h>
#include<algorithm>
#define mp make_pair
#define ft first
#define sd second
using namespace std;
typedef pair<int,int> pii;
typedef unsigned long long ULL;
const int maxn=200050,maxns=8000050;
int n,m,q,ans;
pii a[maxn];
int ts,root[maxn];
ULL tr[maxns];
bool cmp(const int &lhs,const int &rhs) { return a[lhs]<a[rhs]; }
inline void Get(bool lt,bool rt,ULL x,int &ls,int &rs,int &s,int &t)
{
	s=x%maxn,ls=x/maxn/maxns,rs=x/maxn%maxns;
	t=s-min(lt?ls:tr[ls]%maxn,rt?rs:tr[rs]%maxn);
}
inline ULL Chg(int ls,int rs,int s)
{
	return ((ULL)ls*maxns+rs)*maxn+s;
}
inline void add(int x0,int &x,int ll,int rr,int li,int ri,int k)
{
	if(ll==rr) { x=x0;return ; }
	tr[x=++ts]=tr[x0];
	if(li<=ll&&rr<=ri)
	{ tr[x]+=k;return ; }
	int mid=(ll+rr)>>1,ls0,rs0,s0,ls=0,rs=0,s,t;
	Get(ll==mid,rr==mid+1,tr[x0],ls0,rs0,s0,t);
	if(li<=mid)
		add(ls0,ls,ll,mid,li,ri,k);
	if(ri>mid)
		add(rs0,rs,mid+1,rr,li,ri,k);
	if(!ls) ls=ls0;
	if(!rs) rs=rs0;
	s=min(tr[ls]%maxn,tr[rs]%maxn)+t;
	tr[x]=Chg(ls,rs,s);
}
inline int query(int x,int ll,int rr,int li,int ri)
{
	if(!x) return 0;
	if(li<=ll&&rr<=ri) return tr[x]%maxn;
	int mid=(ll+rr)>>1,ls,rs,s,t;
	Get(ll==mid,rr==mid+1,tr[x],ls,rs,s,t);
	s=maxn;
	if(li<=mid)
		s=min(s,query(ls,ll,mid,li,ri));
	if(ri>mid)
		s=min(s,query(rs,mid+1,rr,li,ri));
	return s+t;
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("data.in","r",stdin);
	freopen("data.out","w",stdout);
#endif
	int i,j;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)
		scanf("%d",&a[i].ft),a[i].sd=i;
	sort(a+1,a+1+n);
	for(i=1;i<=n;i++)
		add(root[i-1],root[i],1,n-m+1,a[i].sd-m+1,a[i].sd,1);
	scanf("%d",&q);
	while(q--)
	{
		int li,ri,xi,pos;
		scanf("%d%d%d",&li,&ri,&xi);
		xi^=ans;
		pos=lower_bound(a+1,a+1+n,mp(xi,0))-a-1;
		ans=query(root[pos],1,n-m+1,li,ri);
		printf("%d\n",ans);
	}
	return 0;
}

# include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn(4e5 + 5);

int n, m, s, a[maxn], bl[maxn], br[maxn], b[maxn], o[maxn], len, id[maxn], blk;
int mn[505][505], st[505][505], tag[505], w[maxn], pmn[maxn], smn[maxn];
int curmn[505], curst[505], curtag[505];

inline void Modify(int l, int r, int id) {
	int i;
	pmn[id] = smn[id] = 1e9;
	if (b[l] == b[r]) for (i = l; i <= r; ++i) ++w[i];
	else {
		for (i = l; i <= br[l]; ++i) ++w[i];
		for (i = bl[r]; i <= r; ++i) ++w[i];
		for (i = b[l] + 1; i < b[r]; ++i) ++tag[i];
	}
	for (i = bl[l]; i <= br[l]; ++i) pmn[id] = min(pmn[id], w[i]);
	for (i = bl[r]; i <= br[r]; ++i) smn[id] = min(smn[id], w[i]);
}

inline int Binary(int v) {
	int l = 1, r = n, mid, ret = -1;
	while (l <= r) {
		mid = (l + r) >> 1;
		if (o[a[id[mid]]] <= v) ret = mid, l = mid + 1;
		else r = mid - 1;
	}
	return ret;
}

inline int Query(int l, int r, int x) {
	int val = curst[b[l]], i, ret = 1e9;
	for (i = bl[l]; i < l; ++i) val += (a[i + m] <= x) - (a[i] <= x);
	for (i = l; i <= r; ++i) ret = min(ret, val), val += (a[i + m] <= x) - (a[i] <= x);
	return ret;
}

int main() {
	int i, j, ans = 0, l, r, x, ql, qr;
	scanf("%d%d", &n, &m), len = n;
	for (i = 1; i <= n; ++i) scanf("%d", &a[i]), o[i] = a[i], id[i] = i;
	sort(o + 1, o + len + 1), len = unique(o + 1, o + len + 1) - o - 1;
	for (i = 1; i <= n; ++i) a[i] = lower_bound(o + 1, o + len + 1, a[i]) - o;
	sort(id + 1, id + n + 1, [](int x, int y) {  return a[x] < a[y];  });
	for (blk = sqrt(n), i = 1; i <= n; ++i) b[i] = (i - 1) / blk + 1, bl[i] = b[i] == b[i - 1] ? bl[i - 1] : i;
	for (i = n; i; --i) br[i] = b[i] == b[i + 1] ? br[i + 1] : i;
	/*for (i = 1; i <= n; ++i) cout << a[i] << " ";
	cout << endl;*/
	memset(mn, 127, sizeof(mn)), memset(mn[0], 0, sizeof(mn[0]));
	for (i = 1; i <= n; ++i) {
		Modify(max(1, id[i] - m + 1), id[i], i);
		if (b[i] != b[i + 1]) {
			for (j = 1; j <= n; ++j) w[j] += tag[b[j]];
			for (j = 1; j <= b[n]; ++j) tag[j] = 0;
			for (j = 1; j <= n; ++j) {
				mn[b[i]][b[j]] = min(mn[b[i]][b[j]], w[j]);
				if (b[j] != b[j - 1]) st[b[i]][b[j]] = w[j];
			}
		}
	}
	/*for (i = 1; i <= b[n]; ++i, cout << endl)
		for (j = 1; j <= b[n]; ++j) cout << mn[i][j] << " ";
	for (i = 1; i <= b[n]; ++i, cout << endl)
		for (j = 1; j <= b[n]; ++j) cout << st[i][j] << " ";*/
	scanf("%d", &s);
	while (s--) {
		scanf("%d%d%d", &l, &r, &x), x ^= ans, x = Binary(x - 1);
		if (x < 0) {  printf("%d\n", ans = 0); continue;  }
		for (i = 1; i <= b[n]; ++i) curmn[i] = mn[b[x] - 1][i], curst[i] = st[b[x] - 1][i], curtag[i] = 0;
		for (i = bl[x]; i <= x; ++i) {
			ql = max(1, id[i] - m + 1), qr = id[i];
			//cout << ql << " " << qr << " " << i << endl;
			curmn[b[ql]] = pmn[i], curmn[b[qr]] = smn[i];
			//cout << pmn[i] << " " << smn[i] << endl;
			if (b[ql] < b[qr]) ++curtag[b[ql] + 1], --curtag[b[qr]];
			if (bl[ql] >= ql && (b[ql] ^ b[qr])) ++curst[b[ql]];
			++curst[b[qr]];
		}
		/*cout << "!!" << x << endl;
		for (i = 1; i <= b[n]; ++i) cout << curst[i] << " ";
		cout << endl;
		for (i = 1; i <= b[n]; ++i) cout << curmn[i] << " ";
		cout << endl;*/
		for (i = 1; i <= b[n]; ++i) curtag[i] += curtag[i - 1], curmn[i] += curtag[i], curst[i] += curtag[i];
		/*for (i = 1; i <= b[n]; ++i) cout << curst[i] << " ";
		cout << endl;
		for (i = 1; i <= b[n]; ++i) cout << curmn[i] << " ";
		cout << endl;*/
		if (b[l] == b[r]) printf("%d\n", ans = Query(l, r, a[id[x]]));
		else {
			//cout << "??" << l << " " << r << endl;
			ans = min(Query(l, br[l], a[id[x]]), Query(bl[r], r, a[id[x]]));
			for (i = b[l] + 1; i < b[r]; ++i) ans = min(ans, curmn[i]);
			printf("%d\n", ans);
		}
		//if (l == 1 && r == 7) break;
	}
	return 0;
}

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <bitset>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define FI first
#define SE second
using namespace std;
typedef long long LL;
const int M = 500;
const int N = 200005;
pair <int, int> b[N];
int a[N], n, m;
int fi[M][M], mi[M][M], v[N], tv[N];
int vl[N], vr[N];
int tmi[M], tfi[M], lzmi[M], lzfi[M];

inline int start(int x) {
    return x * M;
}

inline int end(int x) {
    return min(n, (x + 1) * M) - 1;
}

inline int bkmin(int x) {
    return *min_element(tv + start(x), tv + end(x) + 1);
}

void update(int x) {
    int l = max(0, b[x].SE - m + 1);
    int r = b[x].SE;
    int L = l / M, R = r / M;
    ++lzfi[L + (l != start(L))];
    --lzfi[R + 1];
    ++L; --R;
    if(L > R) return;
    ++lzmi[L]; --lzmi[R + 1];
}

int brute(int l, int r, int q) {
    int val = tfi[l / M], ret = INF;
    for(int i = start(l / M); i <= r; ++i) {
        if(i >= l) ret = min(ret, val);
        if(a[i] < q) --val;
        if(a[i + m] < q) ++val;
    }
    return ret;
}

int main() {
    scanf("%d%d", &n, &m);
    for(int i = 0; i < n; ++i) {
        scanf("%d", a + i);
        b[i] = make_pair(a[i], i);
    }
    sort(b, b + n);
    int bkcnt = (n - 1) / M;
    for(int i = 0; i <= bkcnt; ++i) {
        for(int j = start(i); j <= end(i); ++j) {
            int l = max(0, b[j].SE - m + 1), r = b[j].SE;
            int L = l / M, R = r / M;
            if(L == R) {
                for(int k = l; k <= r; ++k) ++tv[k];
                vl[j] = vr[j] = bkmin(L);
                continue;
            }
            for(int k = l; k <= end(L); ++k) ++tv[k];
            vl[j] = bkmin(L);
            for(int k = start(R); k <= r; ++k) ++tv[k];
            vr[j] = bkmin(R);
        }
        for(int j = start(i); j <= end(i); ++j) {
            ++v[max(0, b[j].SE - m + 1)];
            --v[b[j].SE + 1];
        }
        for(int j = 0; j < n; ++j) tv[j] = v[j];
        for(int j = 1; j < n; ++j) tv[j] += tv[j - 1];
        for(int j = 0; j <= bkcnt; ++j) {
            mi[i][j] = bkmin(j);
            fi[i][j] = tv[start(j)];
        }
    }
    int q, ans = 0;
    scanf("%d", &q);
    while(q--) {
        int l, r, x;
        scanf("%d%d%d", &l, &r, &x);
        --l; --r; x ^= ans;
        int p = lower_bound(b, b + n, make_pair(x, -1)) - b - 1;
        if(p < 0) {
            printf("%d\n", ans = 0);
            continue;
        }
        int idx = p / M;
        memset(tmi, 0, sizeof(tmi));
        memset(tfi, 0, sizeof(tfi));
        memset(lzmi, 0, sizeof(lzmi));
        memset(lzfi, 0, sizeof(lzfi));
        if(idx) {
            for(int i = 0; i <= bkcnt; ++i) {
                tmi[i] = mi[idx - 1][i];
                tfi[i] = fi[idx - 1][i];
            }
        }
        for(int i = start(idx); i <= p; ++i) {
            int L = max(0, b[i].SE - m + 1) / M;
            int R = b[i].SE;
            tmi[L] = vl[i]; tmi[R] = vr[i];
            update(i);
        }
        for(int i = 1; i <= bkcnt; ++i) {
            lzfi[i] += lzfi[i - 1];
            lzmi[i] += lzmi[i - 1];
        }
        for(int i = 0; i <= bkcnt; ++i) {
            tmi[i] += lzmi[i];
            tfi[i] += lzfi[i];
        }
        ans = INF;
        int L = l / M, R = r / M;
        for(int i = L + 1; i < R; ++i) ans = min(ans, tmi[i]);
        ans = min(ans, brute(l, min(r, end(L)), x));
        if(L ^ R) ans = min(ans, brute(start(R), r, x));
        printf("%d\n", ans);
    }
    return 0;
}

#pragma comment(linker, "/STACK:100000000")
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <cstdlib>
#include <complex>
#include <sstream>
#include <cstring>
#include <fstream>
#include <vector>
#include <cstdio>
#include <string>
#include <bitset>
#include <queue>
#include <stack>
#include <ctime>
#include <cmath>
#include <map>
#include <set>

using namespace std;

typedef unsigned long long ull;
typedef complex < double > cd;
typedef long double ld;
typedef long long ll;

template < class T > void read(T &x) { char c, mi = 0; while(c = getchar(), c <= 32); if(c == '-') mi = 1, x = 0; else if(c < 48 || c > 57) return void(x = c); else x = c - 48; while(c = getchar(), c > 32) x = 10 * x + c - 48; if(mi == 1) x = -x; }
template < class T > void read(T &x, T &y) { read(x); read(y); }
template < class T > void read(T &x, T &y, T &z) { read(x, y); read(z); }
template < class T > void reada(T *a, int n) { for(int i = 0; i < n; ++i) read(a[i]); }
template < class T > void write(T x) { static char s[20]; char pt = 0, mi = (x < 0); if(mi == 1) x = -x; while(!pt || x > 0) { s[++pt] = (char)(x % 10 + '0'); x /= 10; } if(mi == 1) putchar('-'); while(pt > 0) putchar(s[pt--]); }
template < class T > void write(T x, T y) { write(x); putchar(' '); write(y); }
template < class T > void write(T x, T y, T z) { write(x, y); putchar(' '); write(z); }
template < class T > void writeln(T x) { write(x); puts(""); }
template < class T > void writea(T *a, int n) { for(int i = 0; i < n; ++i) { write(a[i]); putchar(i + 1 == n ? '\n' : ' '); } }
template < class T > T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template < class T > T lcm(T a, T b) { return a / gcd(a, b) * b; }
template < class T > T qpom(T a, T b, T mod = 1000000007) { T r = 1; while(b > 0) { if(b & 1) r = r * a % mod; a = a * a % mod; b /= 2; } return r; }
template < class T > T qpow(T a, T b) { T r = 1; while(b > 0) { if(b & 1) r *= a; a *= a; b /= 2; } return r; }
template < class T > T imin(T a, T b) { return a < b ? a : b; }
template < class T > T imax(T a, T b) { return a > b ? a : b; }
template < class T > inline void rmin(T &a, const T &b) { if(a > b) a = b; }
template < class T > inline void rmax(T &a, const T &b) { if(a < b) a = b; }
template < class T > T sqr(const T &a) { return a * a; }

#define debug(x) cout << #x << "=" << x
#define debuge(x, c) cout << #x << "=" << x << (c)
#define debugn(x) cout << #x << "=" << x << "\n"
#ifdef DEBUG
#define eprintf(...) fprintf(stderr, "%s -> ", string(to_string((long long)__LINE__)).c_str()), fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...)
#endif

#define ppb pop_back
#define pb push_back
#define mp make_pair
#define fs first
#define sd second

#define inf 1000000007
#define nmax 200010
#define mmax 300010
#define eps 1e-9

struct Node {
	unsigned long long mi : 18, l : 23, r : 23;
} t[8008888];

int findAdd(int ret, int xl, int xr) {
	return (int)t[ret].mi - (int)min(t[xl].mi, t[xr].mi);
}

void f(int ret, int xl, int xr, int add = 0) {
	t[ret].mi = add + min(t[xl].mi, t[xr].mi);
	t[ret].l = xl;
	t[ret].r = xr;
}

int pt = 0;
int roots[nmax];
int n, m, q;
int ans, l, r, x;
pair < int, int > p[nmax];

int copy(int v) {
	int ret = ++pt;
	if(pt == 8008888) {
		while(1);
	}
	t[ret].mi = t[v].mi;
	t[ret].l = t[v].l;
	t[ret].r = t[v].r;
	return ret;
}

int upd(int v, int L, int R, int l, int r, int add) {
	int ret = copy(v);
	if(L == l && r == R) {
		t[ret].mi += add;
		return ret;
	}
	int M = (L + R) / 2;
	if(r <= M) {
		t[ret].l = upd(t[v].l, L, M, l, r, add);
	} else if(l > M) {
		t[ret].r = upd(t[v].r, M + 1, R, l, r, add);
	} else {
		t[ret].l = upd(t[v].l, L, M, l, M, add);
		t[ret].r = upd(t[v].r, M + 1, R, M + 1, r, add);
	}
	f(ret, t[ret].l, t[ret].r, findAdd(v, t[v].l, t[v].r));
	return ret;
}

int get(int v, int L, int R, int l, int r) {
	if(L == l && r == R || v == 0) {
		return t[v].mi;
	}
	int M = (L + R) / 2;
	int z = findAdd(v, t[v].l, t[v].r);
	if(r <= M) {
		return z + get(t[v].l, L, M, l, r);
	}
	if(l > M) {
		return z + get(t[v].r, M + 1, R, l, r);
	}
	return z + min(get(t[v].l, L, M, l, M),
		get(t[v].r, M + 1, R, M + 1, r));
}

int main() {
//	freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
//	freopen("elevator.in", "r", stdin); freopen("elevator.out", "w", stdout);
	/*read(n, m);
	for(int i = 1; i <= n; ++i) {
		scanf("%d", &p[i].fs);
		p[i].sd = i;
	}
	roots[0] = ++pt;
	sort(p + 1, p + 1 + n);
	for(int i = 1; i <= n; ++i) {
		roots[i] = upd(roots[i - 1], 1, n, max(p[i].sd - m + 1, 1), p[i].sd, 1);
	}
	read(q);
	while(q --> 0) {
		read(l, r, x);
		x ^= ans;
		int id = upper_bound(p + 1, p + 1 + n, mp(x, -inf)) - (p + 1);
		ans = get(roots[id], 1, n, l, r);
		writeln(ans);
	}*/
	getchar(); getchar();
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

//INSERT ABOVE HERE
const int BS = 1<<9;
const int MAX = 1<<18;

int dat[MAX]={}; // state
int val[MAX/BS]={}; // max
int add[MAX/BS]={}; // add
int frt[MAX/BS]={}; // front

int psh[MAX/BS]={};

int bf[MAX/BS][MAX/BS]; // front
int bm[MAX/BS][MAX/BS]; // max

int cp[3][MAX]={}; // pos
int cv[3][MAX]={}; // value

signed main(){
  int n,m;
  cin>>n>>m;
  vector<int> as(n+n,0);
  for(int i=0;i<n;i++) cin>>as[i];

  using P = pair<int, int>;
  vector<P> vp;
  for(int i=0;i<n;i++) vp.emplace_back(-as[i],i);
  sort(vp.begin(),vp.end());

  auto eval=
    [&](int i){
      int l=(i/BS)*BS;
      if(add[l/BS]==0) return;
      for(int j=l;j<l+BS;j++) dat[j]+=add[j/BS];
      add[l/BS]=0;
    };

  auto process=
    [&](int lim)->void{
      memset(dat,0,sizeof(dat));
      memset(val,0,sizeof(val));
      memset(add,0,sizeof(add));
      memset(frt,0,sizeof(frt));

      memset(cp,-1,sizeof(cp));
      for(int i=0;i<lim;i++){
        if(i%BS==0){
          for(int j=0;j<MAX;j+=BS){
            val[j/BS]+=add[j/BS];
            eval(j);
            bf[i/BS][j/BS]=dat[j];
            bm[i/BS][j/BS]=val[j/BS];
          }
        }
        // [l, r)
        int r=vp[i].second+1;
        int l=max(0,r-m);

        int idx=1;
        for(int j=l;j<r;j++){
          if(j%BS==0 && j+BS<=r){
            add[j/BS]+=1;
            j+=BS-1;
          }else{
            dat[j]+=1;
            chmax(val[j/BS],dat[j]);

            if(cp[idx-1][i]!=j/BS){
              cp[idx][i]=j/BS;
              cv[idx][i]=val[j/BS];
              idx++;
            }
          }
        }

        while(idx<=2){
          cp[idx][i]=MAX/BS-1;
          cv[idx][i]=0;
          idx++;
        }
      }
    };

  auto calc=
    [&](int l,int r,int v)->int{
      int idx=upper_bound(vp.begin(),vp.end(),P(-v,n))-vp.begin();
      process(idx);

      int k=idx/BS*BS;
      for(int j=0;j<MAX;j+=BS)
        frt[j/BS]=bf[k/BS][j/BS];

      memset(psh,0,sizeof(psh));
      for(int i=k;i<idx;i++){
        int q=vp[i].second+1;
        int p=max(0,q-m);
        if(p%BS) p=(p/BS+1)*BS;
        if(p<=q){
          psh[p/BS]+=1;
          psh[q/BS]-=1;
        }
        //cout<<cp[1][i]<<":"<<val[cp[1][i]]<<" "<<cv[1][i]<<endl;;
        assert(val[cp[1][i]]>=cv[1][i]);
        assert(val[cp[2][i]]>=cv[2][i]);
        if(p<=q && q%BS) frt[q/BS]+=1;
      }

      for(int j=BS;j<MAX;j+=BS)
        psh[j/BS]+=psh[j/BS-1];

      for(int j=0;j<MAX;j+=BS)
        if(add[j/BS]!=psh[j/BS])
          cout<<j<<":"<<add[j/BS]<<" "<<psh[j/BS]<<endl;

      for(int j=0;j<MAX;j+=BS)
        assert(add[j/BS]==psh[j/BS]);

      int res=0,flg=0,tmp=0;
      for(int j=l;j<r;j++){
        if(j%BS==0 && j+BS<=r){
          chmax(res,val[j/BS]+add[j/BS]);
          j+=BS-1;
          flg=0;
        }else{
          if(flg==0){
            flg=1;
            int p=j/BS*BS;
            assert(dat[p]==frt[p/BS]);
            tmp=dat[p]+add[j/BS];
            while(p<j){
              tmp-=as[p]>=v;
              tmp+=as[p+m]>=v;
              p++;
              assert(dat[p]+add[p/BS]==tmp);
            }
          }
          assert(tmp==dat[j]+add[j/BS]);
          chmax(res,dat[j]+add[j/BS]);
          tmp-=as[j]>=v;
          tmp+=as[j+m]>=v;
        }
      }
      return m-res;
    };

  int q;
  cin>>q;
  int pre_ans=0;
  for(int i=0;i<q;i++){
    if(i>=10) break;
    int l,r,x;
    cin>>l>>r>>x;
    l--;
    int ans=calc(l,r,x^pre_ans);
    cout<<ans<<"\n";
    pre_ans=ans;
  }
  cout<<flush;
  return 0;
}