有
n
个物品,键值为为
n≤2∗104,m≤105,1≤sumi,vi≤100,di,ci≤109
上古神犇Claris的题。
好像为了防止拆掉背包不是分块(CF 500F New Year Shopping)就是分治(本题)。
原来dp状态里只存能否达到(bitset?),然后按
di
从小到大更新背包。
价值
val
最多更新(r-l+1)个背包
100/val
次,乍一看还是很和谐的==>TLE。
正解dp状态里存的是,达成这个价值的最大键值最小是多少。
分治+背包dp
#include <set>
#include <ctime>
#include <queue>
#include <cstdio>
#include <bitset>
#include <cctype>
#include <bitset>
#include <cstdlib>
#include <cassert>
#include <cstring>
#include <iostream>
#include <algorithm>
#define inf (1<<30)
#define INF (1ll<<62)
#define fi first
#define se second
#define rep(x,s,t) for(register int x=s,t_=t;x<t_;++x)
#define per(x,s,t) for(register int x=t-1,s_=s;x>=s_;--x)
#define prt(x) cout<<#x<<":"<<x<<" "
#define prtn(x) cout<<#x<<":"<<x<<endl
#define pb(x) push_back(x)
#define hash asfmaljkg
#define rank asfjhgskjf
#define y1 asggnja
#define y2 slfvm
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
template<class T>void sc(T &x){
int f=1;char c;x=0;
while(c=getchar(),c<48)if(c=='-')f=-1;
do x=x*10+(c^48);
while(c=getchar(),c>47);
x*=f;
}
template<class T>void nt(T x){
if(!x)return;
nt(x/10);
putchar(x%10+'0');
}
template<class T>void pt(T x){
if(x<0)putchar('-'),x=-x;
if(!x)putchar('0');
else nt(x);
}
template<class T>void ptn(T x){
pt(x);putchar('\n');
}
template<class T>void pts(T x){
pt(x);putchar(' ');
}
template<class T>inline void Max(T &x,T y){if(x<y)x=y;}
template<class T>inline void Min(T &x,T y){if(x>y)x=y;}
const int maxn=120006;
const int maxs=106;
int n,m;
struct node{
int id,l,r,c,s;
bool operator<(const node&a)const{
if(c^a.c)return c<a.c;
return id==-1;
}
}d[maxn];
int dp[maxn][maxs];
int ans[maxn];
int a[maxn],b[maxn];
void pack(int *bag,int v,int c){
per(i,0,101-v)
if(bag[i]^inf)
Min(bag[i+v],max(c,bag[i]));
}
void divide(int l,int r,int tot){
if(!tot)return;
if(l==r){
rep(i,0,tot)ans[d[i].id]=(d[i].c<b[l]||(d[i].s^a[l]));
return;
}
int mid=l+r>>1,nn;
nn=0;
rep(i,0,tot)if(d[i].r<=mid)swap(d[nn],d[i]),nn++;
divide(l,mid,nn);
nn=0;
rep(i,0,tot)if(d[i].l>mid)swap(d[nn],d[i]),nn++;
divide(mid+1,r,nn);
nn=0;
rep(i,0,tot)if(d[i].id==-1||(d[i].l<=mid&&d[i].r>mid))swap(d[nn],d[i]),nn++;
rep(j,1,101)dp[mid][j]=dp[mid+1][j]=inf;
dp[mid][a[mid]]=b[mid];
dp[mid+1][a[mid+1]]=b[mid+1];
per(i,l,mid){
rep(j,1,101)dp[i][j]=dp[i+1][j];
pack(dp[i],a[i],b[i]);
}
rep(i,mid+2,r+1){
rep(j,1,101)dp[i][j]=dp[i-1][j];
pack(dp[i],a[i],b[i]);
}
rep(i,0,nn){
int id=d[i].id,lx=d[i].l,rx=d[i].r,s=d[i].s,c=d[i].c;
ans[id]=1;
rep(j,0,s+1){
if(dp[lx][j]<=c&&dp[rx][s-j]<=c){
ans[id]=0;
break;
}
}
}
}
void solve(){
sc(n);sc(m);
rep(i,1,n+1)sc(a[i]);
rep(i,1,n+1)sc(b[i]);
rep(i,0,m){
d[i].id=i;
sc(d[i].l);
sc(d[i].r);
sc(d[i].c);
sc(d[i].s);
}
divide(1,n,m);
rep(i,0,m)pt(ans[i]);
putchar('\n');
}
int main(){
int cas;sc(cas);
while(cas--)solve();
return 0;
}