// only core code constint N = 2e5 + 7; int T, n, ans[N]; vector<pair<int, int> > a;
signedmain(){ kin >> T; while (T--) { kin >> n; a.clear(); for (int i = 1; i <= n; ++i) a.push_back(make_pair(kin.get<int>(), i)); sort(a.begin(), a.end()); int64 sum = 0; int cur = 0; for (int i = 0; i < n; ++i) { while ((cur < n && a[cur].first <= sum) || i >= cur) { sum += a[cur++].first; } ans[a[i].second] = cur - 1; } for (int i = 1; i <= n; ++i) kout << ans[i] << ' '; kout << '\n'; } return0; }
namespace STmax { int st[N][20], lg[N]; voidinit(int n, int *a){ for (int i = 1; i <= n; ++i) st[i][0] = a[i]; for (int step = 1; (1 << step) <= n; ++step) { for (int i = 1; i + (1 << step) - 1 <= n; ++i) { st[i][step] = max(st[i][step - 1], st[i + (1 << (step - 1))][step - 1]); } } lg[1] = 0; for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1; } intquery(int l, int r){ int k = lg[r - l + 1]; returnmax(st[l][k], st[r - (1 << k) + 1][k]); } }
namespace STmin { int st[N][20], lg[N]; voidinit(int n, int *a){ for (int i = 1; i <= n; ++i) st[i][0] = a[i]; for (int step = 1; (1 << step) <= n; ++step) { for (int i = 1; i + (1 << step) - 1 <= n; ++i) { st[i][step] = min(st[i][step - 1], st[i + (1 << (step - 1))][step - 1]); } } lg[1] = 0; for (int i = 2; i <= n; ++i) lg[i] = lg[i >> 1] + 1; } intquery(int l, int r){ int k = lg[r - l + 1]; returnmin(st[l][k], st[r - (1 << k) + 1][k]); } }
int T, n; int a[N], b[N]; bool archievable[N]; int closet[N];
signedmain(){ kin >> T; while (T--) { kin >> n; for (int i = 1; i <= n; ++i) kin >> a[i]; for (int i = 1; i <= n; ++i) kin >> b[i]; STmax::init(n, a); STmin::init(n, b); for (int i = 1; i <= n; ++i) { archievable[i] = false; closet[i] = 0; } for (int i = 1; i <= n; ++i) { closet[a[i]] = i; if (closet[b[i]]) { if (STmax::query(closet[b[i]], i) <= b[i] && STmin::query(closet[b[i]], i) >= b[i]) { archievable[i] = true; } } } for (int i = 1; i <= n; ++i) { closet[i] = 0; } for (int i = n; i >= 1; --i) { closet[a[i]] = i; if (closet[b[i]]) { if (STmax::query(i, closet[b[i]]) <= b[i] && STmin::query(i, closet[b[i]]) >= b[i]) { archievable[i] = true; } } } bool flag = true; for (int i = 1; i <= n; ++i) { if (!archievable[i]) { flag = false; break; } } if (flag) kout << "YES\n"; else kout << "NO\n"; } return0; }