#include #include #include #include #include #include #include #include #include #include #include #include #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define REP(I, N) for (int I = 0; I < (N); ++I) #define REPP(I, A, B) for (int I = (A); I < (B); ++I) #define RI(X) scanf("%d", &(X)) #define RII(X, Y) scanf("%d%d", &(X), &(Y)) #define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z)) #define DRI(X) int (X); scanf("%d", &X) #define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y) #define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z) #define RS(X) scanf("%s", (X)) #define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0) #define MP make_pair #define PB push_back #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define F first #define S second typedef long long LL; using namespace std; const int SIZE = 3e4+10; const int MM = 100; vectorg[SIZE*2]; int a[SIZE]; int dp[SIZE],cnt[SIZE]; int an[SIZE]; int x[SIZE*2],y[SIZE*2]; bool used[SIZE]; int BIT[SIZE]; void ins(int x){ for(;x >e[SIZE*2]; int main(){ int n; while(RI(n)==1){ MS0(used); MS0(an); REP(i,SIZE)g[i].clear(),e[i].clear(); DRI(K); REPP(i,1,n+1){ DRI(x); a[i]=x; g[x].PB(i); } DRI(m); m*=2; REP(i,m){ RII(x[i],y[i]); e[x[i]-1].PB(MP(i^1,-1)); e[y[i]].PB(MP(i^1,1)); } for(int even=2;even=SIZE||even>=SIZE||odd<0)continue; if(SZ(g[even])>MM){ MS0(dp); MS0(cnt); REP(i,SZ(g[even])){ dp[g[even][i]]++; } REP(i,SZ(g[odd])){ used[g[odd][i]]=1; cnt[g[odd][i]]++; } for(int i=1;i<=n;i++)dp[i]+=dp[i-1]; for(int i=1;i<=n;i++)cnt[i]+=cnt[i-1]; for(int i=0;i>1]+=(cnt[y[i]]-cnt[x[i]-1])*(dp[y[i^1]]-dp[x[i^1]-1]); } } MS0(BIT); for(int id=1;id<=n;id++){ if(!used[id]&&a[id]%2==1){ int odd=a[id]; int even=K-odd; if(even0){ REP(i,SZ(g[even]))ins(g[even][i]); } } REP(i,SZ(e[id])){ int qid=e[id][i].F; int mul=e[id][i].S; an[qid>>1]+=qq(y[qid])*mul; an[qid>>1]-=qq(x[qid]-1)*mul; } } m>>=1; REP(i,m)printf("%d\n",an[i]); } return 0; }