Problem Description
There are
n
shops numbered with successive integers from
1
to
n
in Byteland. Every shop sells only one kind of goods, and the price of the
i
-th shop's goods is
vi
. The distance between the
i
-th shop and Byteasar's home is
di
.
Every day, Byteasar will purchase some goods. On the i -th day, he will choose an interval [li,ri] and an upper limit ci . Then, he will visit each shop with distance at most ci away from home in [li,ri] , buy at most one piece of goods from each shop and go back home. Of course, he can also choose to buy nothing. Back home, Byteasar will calculate the total amount of money he has costed that day and write it down on his account book, denoted as sumi .
However, due to Byteasar's poor math, he may calculate it wrong.
Please write a program to help Byteasar judge whether each number is sure to be calculated wrong.
Every day, Byteasar will purchase some goods. On the i -th day, he will choose an interval [li,ri] and an upper limit ci . Then, he will visit each shop with distance at most ci away from home in [li,ri] , buy at most one piece of goods from each shop and go back home. Of course, he can also choose to buy nothing. Back home, Byteasar will calculate the total amount of money he has costed that day and write it down on his account book, denoted as sumi .
However, due to Byteasar's poor math, he may calculate it wrong.
Please write a program to help Byteasar judge whether each number is sure to be calculated wrong.
Input
The first line of the input contains an integer
T
(1≤T≤10)
, denoting the number of test cases.
In each test case, the first line of the input contains two integers n,m (1≤n≤20000,1≤m≤100000) , denoting the number of shops and the number of records on Byteasar's account book.
The second line of the input contains n integers v1,v2,...,vn (1≤vi≤100) , denoting the price of the i -th shop's goods.
The third line of the input contains n integers d1,d2,...,dn (1≤di≤109) , denoting the distance between the i -th shop and Byteasar's home.
Each of the next m lines contains four integers li,ri,ci,sumi (1≤li≤ri≤n,1≤ci≤109,1≤sumi≤100) , denoting a record on Byteasar's account book.
In each test case, the first line of the input contains two integers n,m (1≤n≤20000,1≤m≤100000) , denoting the number of shops and the number of records on Byteasar's account book.
The second line of the input contains n integers v1,v2,...,vn (1≤vi≤100) , denoting the price of the i -th shop's goods.
The third line of the input contains n integers d1,d2,...,dn (1≤di≤109) , denoting the distance between the i -th shop and Byteasar's home.
Each of the next m lines contains four integers li,ri,ci,sumi (1≤li≤ri≤n,1≤ci≤109,1≤sumi≤100) , denoting a record on Byteasar's account book.
Output
For each test case, print a line with
m
characters. If the
i
-th number is sure to be calculated wrong, then the
i
-th character should be '1'. Otherwise, it should be '0'.
Sample Input
2 3 3 3 3 3 2 4 3 3 3 5 3 3 3 3 1 2 3 1 3 5 4 5 1 2 4 2 1 8 9 2 1 1 5 1 3 4 4 1 5 1 5 3 5 1 3 5 1
Sample Output
0111101
#include <set> #include <map> #include <stack> #include <queue> #include <deque> #include <cmath> #include <vector> #include <string> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define L(i) i<<1 #define R(i) i<<1|1 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-9 #define maxn 10010 #define MOD 1000000007 const int mod = 998244353; struct Node { int l,r,c,sum,id; } que[120000]; int ans[120000]; int f[22000][120]; int g[22000][120]; int v[22000]; int d[22000]; void cdq(int l,int r,int x,int y) { int mid = (l+r)/2; vector<Node> R,L,M; //R.clear(),L.clear(),M.clear(); for(int i=x; i<=y; i++) { if(que[i].l>mid) R.push_back(que[i]); else if(que[i].r<mid) L.push_back(que[i]); else M.push_back(que[i]); } if(M.size()>0) { for(int i=0;i<=100;i++) f[mid][i] = g[mid][i]=INF; f[mid][v[mid]]=d[mid]; for(int i=mid-1;i>=l;--i) { for(int j=0;j<=100;++j) { if(j>=v[i]) { if(j-v[i]==0) f[i][j] = min(f[i+1][j],d[i]); else f[i][j] = min(f[i+1][j],max(f[i+1][j-v[i]],d[i])); } else f[i][j] = f[i+1][j]; } } for(int i=mid+1;i<=r;++i) { for(int j=0;j<=100;j++) { if(j>=v[i]) { if(j-v[i]==0) g[i][j] = min(g[i-1][j],d[i]); else g[i][j] =min(g[i-1][j],max(g[i-1][j-v[i]],d[i])); }else g[i][j] = g[i-1][j]; } } for(int i=0;i<M.size();i++) { if(M[i].r==mid) { if(f[M[i].l][M[i].sum] > M[i].c) ans[M[i].id]=1; else ans[M[i].id]=0; continue; } bool flag=false; for(int j=0;j<=M[i].sum;j++) { int dis; if(j==0) dis = g[M[i].r][M[i].sum]; else if(j==M[i].sum) dis = f[M[i].l][M[i].sum]; else dis = max(f[M[i].l][j],g[M[i].r][M[i].sum-j]); if(dis<=M[i].c) { flag=true; break; } } if(flag) ans[M[i].id]=0; else ans[M[i].id]=1; } } int cnt=x; if(L.size()>0) { for(int i=0; i<L.size(); i++) { que[cnt++] = L[i]; } cdq(l,mid-1,x,cnt-1); } if(R.size()>0) { cnt++; x=cnt; for(int i=0;i<R.size();i++) { que[cnt++] = R[i]; } cdq(mid+1,r,x,cnt-1); } } int n,m; int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(int i=1; i<=n; i++) scanf("%d",&v[i]); for(int i=1; i<=n; i++) scanf("%d",&d[i]); for(int i=1; i<=m; i++) { scanf("%d%d%d%d",&que[i].l,&que[i].r,&que[i].c,&que[i].sum); que[i].id=i; } cdq(1,n,1,m); for(int i=1; i<=m; i++) printf("%d",ans[i]); printf("\n"); } return 0; }