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
标算的做法,分治,复杂度100(nlogn+m)+mlogn
#include<set> #include<map> #include<ctime> #include<cmath> #include<stack> #include<queue> #include<bitset> #include<cstdio> #include<string> #include<cstring> #include<iostream> #include<algorithm> #include<functional> #define rep(i,j,k) for (int i = j; i <= k; i++) #define per(i,j,k) for (int i = j; i >= k; i--) using namespace std; typedef __int64 LL; const int low(int x) { return x&-x; } const double eps = 1e-8; const int INF = 0x7FFFFFFF; const int mod = 998244353; const int N = 2e4 + 10; const int M = 1e5 + 10; int T, n, m, x; int v[N], d[N], f[N][101]; int l[M], r[M], c[M], s[M], ans[M], a[M], b[M]; void solve(int L, int R, int ll, int rr) { if (ll > rr) return; if (L == R) { rep(i, ll, rr) ans[a[i]] = v[R] == s[a[i]] && d[R] <= c[a[i]]; } else { int mid = L + R >> 1, w1 = ll, w2 = rr, w3 = 0; rep(i, ll, rr) { if (r[a[i]] <= mid) a[w1++] = a[i]; else if (l[a[i]] > mid) b[w2--] = a[i]; else b[w3++] = a[i]; } rep(i, w2 + 1, rr) a[i] = b[i]; if (w3) { rep(i, 1, 100) f[mid][i] = f[mid + 1][i] = INF; f[mid][0] = f[mid + 1][0] = 0; f[mid][v[mid]] = d[mid]; f[mid + 1][v[mid + 1]] = d[mid + 1]; per(i, mid - 1, L) { per(j, 100, v[i]) f[i][j] = min(f[i + 1][j], max(f[i + 1][j - v[i]], d[i])); per(j, v[i] - 1, 0) f[i][j] = f[i + 1][j]; } rep(i, mid + 2, R) { rep(j, v[i], 100) f[i][j] = min(f[i - 1][j], max(f[i - 1][j - v[i]], d[i])); rep(j, 0, v[i] - 1) f[i][j] = f[i - 1][j]; } } rep(i, 0, w3 - 1) { rep(j, 0, s[b[i]]) ans[b[i]] |= max(f[l[b[i]]][j], f[r[b[i]]][s[b[i]] - j]) <= c[b[i]]; } solve(L, mid, ll, w1 - 1); solve(mid + 1, R, w2 + 1, rr); } } int main() { scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); rep(i, 1, n) scanf("%d", &v[i]); rep(i, 1, n) scanf("%d", &d[i]); rep(i, 1, m) a[i] = i, ans[i] = 0; rep(i, 1, m) scanf("%d%d%d%d", &l[i], &r[i], &c[i], &s[i]); solve(1, n, 1, m); rep(i, 1, m) printf("%d", ans[i] ^ 1); putchar(10); } return 0; }
树状数组+多重背包二进制优化,按道理来说复杂度要炸,然而实际上跑的比分治还快。#include<set> #include<map> #include<ctime> #include<cmath> #include<stack> #include<queue> #include<bitset> #include<cstdio> #include<string> #include<cstring> #include<iostream> #include<algorithm> #include<functional> #define rep(i,j,k) for (int i = j; i <= k; i++) #define per(i,j,k) for (int i = j; i >= k; i--) using namespace std; typedef __int64 LL; const int low(int x) { return x&-x; } const double eps = 1e-8; const int INF = 0x7FFFFFFF; const int mod = 998244353; const int N = 2e4 + 10; const int M = 1e5 + 10; int T, n, m, x, y, q, p; int v[N], d[N], f[101][N]; int l[M], r[M], c[M], s[M], ans[M]; int a[N], b[M], dp[101]; bool cmp1(const int &x, const int &y) { return d[x] < d[y]; } bool cmp2(const int &x, const int &y) { return c[x] < c[y]; } void insert(int x, int y) { if (x > p) return; for (int i = y; i <= q; i += low(i)) f[x][i]++; } int get(int x, int y) { if (x > p) return 0; int res = 0; for (int i = y; i; i -= low(i)) res += f[x][i]; return res; } int main() { scanf("%d", &T); while (T--) { scanf("%d%d", &n, &m); x = y = q = 0; rep(i, 1, n) scanf("%d", &v[i]), x = max(x, v[i]); rep(i, 1, n) scanf("%d", &d[i]), a[i] = i; rep(i, 1, m) { scanf("%d%d%d%d", &l[i], &r[i], &c[i], &s[i]); b[i] = i; y = max(y, s[i]); q = max(q, r[i]); } p = min(x, y); rep(i, 1, p) rep(j, 1, q) f[i][j] = 0; sort(a + 1, a + n + 1, cmp1); sort(b + 1, b + m + 1, cmp2); for (int i = 1, j = 1; i <= m; i++) { for (; j <= n&&d[a[j]] <= c[b[i]]; j++) insert(v[a[j]], a[j]); int S = s[b[i]], L = l[b[i]], R = r[b[i]]; if (ans[b[i]] = get(S, R) - get(S, L - 1)) continue; rep(k, 1, S) dp[k] = 0; dp[0] = 1; for (int k = 1; !dp[S] && k < S; k++) { x = min(S / k, get(k, R) - get(k, L - 1)); for (y = 1; !dp[S] && x; x -= min(x, y), y <<= 1) { int cost = min(x, y)*k; for (int w = S; !dp[S] && w >= cost; w--) dp[w] |= dp[w - cost]; } } ans[b[i]] = dp[S]; } rep(i, 1, m) printf("%d", ans[i] ? 0 : 1); putchar(10); } return 0; }