Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef unsigned long long ull;
- const int inf_int = 1e9 + 100;
- const ll inf_ll = 1e18;
- typedef pair<int, int> pii;
- typedef pair<ll, ll> pll;
- typedef long double dbl;
- #define pb push_back
- const double pi = 3.1415926535898;
- #define dout if(debug) cout
- #define fi first
- #define se second
- #define sp setprecision
- #define sz(a) (int(a.size()))
- #define all(a) a.begin(),a.end()
- bool debug = 0;
- const int MAXN = 1e5 + 100;
- const int LOG = 20;
- const int mod = 1e9 + 7;
- const int MX = 2e5 + 100;
- typedef long long li;
- const li MOD = 1000000000949747713ll;
- int n, k;
- int a[MAXN];
- int b[MAXN];
- int a_mn[4 * MAXN];
- int a_mx[4 * MAXN];
- int a_push[4 * MAXN];
- int b_mn[4 * MAXN];
- int b_mx[4 * MAXN];
- int b_push[4 * MAXN];
- int push_cnt[4 * MAXN];
- int sum[4 * MAXN];
- int z = 0;
- #define left v<<1,tl,tm
- #define right v<<1|1,tm+1,tr
- inline void upd(int v) {
- a_mx[v] = max(a_mx[v << 1], a_mx[v << 1 | 1]);
- b_mx[v] = max(b_mx[v << 1], b_mx[v << 1 | 1]);
- a_mn[v] = min(a_mn[v << 1], a_mn[v << 1 | 1]);
- b_mn[v] = min(b_mn[v << 1], b_mn[v << 1 | 1]);
- sum[v] = sum[v << 1] + sum[v << 1 | 1];
- }
- inline void push(int v) {
- int v1 = v << 1;
- int v2 = v << 1 | 1;
- if (a_push[v] != -1) {
- a_mn[v1] = a_mx[v1] = a_push[v1] = a_push[v];
- a_mn[v2] = a_mx[v2] = a_push[v2] = a_push[v];
- a_push[v] = -1;
- }
- if (b_push[v] != -1) {
- b_mn[v1] = b_mx[v1] = b_push[v1] = b_push[v];
- b_mn[v2] = b_mx[v2] = b_push[v2] = b_push[v];
- b_push[v] = -1;
- }
- if(push_cnt[v]){
- push_cnt[v<<1] = push_cnt[v<<1|1] = 1;
- push_cnt[v] = 0;
- }
- }
- void build(int v, int tl, int tr) {
- if (tl == tr) {
- a_push[v] = -1;
- b_push[v] = -1;
- a_mn[v] = a_mx[v] = b_mn[v] = b_mx[v] = 0;
- sum[v] = 0;
- push_cnt[v] = 0;
- } else {
- int tm = (tl + tr) >> 1;
- a_push[v] = -1;
- b_push[v] = -1;
- build(left);
- build(right);
- push_cnt[v] = 0;
- upd(v);
- }
- }
- void process(int v, int tl, int tr) {
- z++;
- if (a_mx[v] + k < b_mn[v] || a_mn[v] - k > b_mx[v]) {
- sum[v] = 0;
- push_cnt[v] = 1;
- return;
- }
- if ((a_mn[v] - k <= b_mn[v] && b_mx[v] <= a_mn[v] + k) && (a_mx[v] - k <= b_mn[v] && b_mx[v] <= a_mx[v] + k)) {
- sum[v] = tr - tl + 1;
- dout << "HERE ! " << tl << " " << tr << " :: " << a_mn[v] << " " << a_mx[v] << " -- " << b_mn[v] << " "
- << b_mx[v] << endl;
- push_cnt[v] = 1;
- return;
- } else {
- assert(tl != tr);
- int tm = (tl + tr) >> 1;
- push(v);
- process(left);
- process(right);
- upd(v);
- }
- }
- void update_a(int v, int tl, int tr, int l, int r, int val) {
- if (l > tr || r < tl) {
- if(push_cnt[v])
- process(v,tl,tr);
- return;
- }
- if (l <= tl && tr <= r) {
- a_push[v] = val;
- a_mx[v] = a_mn[v] = val;
- process(v, tl, tr);
- return;
- } else {
- int tm = (tl + tr) >> 1;
- push(v);
- update_a(left, l, r, val);
- update_a(right, l, r, val);
- upd(v);
- }
- }
- void update_b(int v, int tl, int tr, int l, int r, int val) {
- if (l > tr || r < tl) {
- if(push_cnt[v])
- process(v,tl,tr);
- return;
- }
- if (l <= tl && tr <= r) {
- b_push[v] = val;
- b_mx[v] = b_mn[v] = val;
- process(v, tl, tr);
- return;
- } else {
- int tm = (tl + tr) >> 1;
- push(v);
- update_b(left, l, r, val);
- update_b(right, l, r, val);
- upd(v);
- }
- }
- void solve() {
- z = 0;
- cin >> n >> k;
- for (int i = 1; i <= n; ++i) {
- cin >> a[i];
- }
- for (int i = 1; i <= n; ++i) {
- cin >> b[i];
- }
- build(1,1,n);
- stack<pii> st1;
- st1.push({a[1], 1});
- stack<pii> st2;
- st2.push({b[1], 1});
- ll ans = 0;
- update_a(1,1,n, 1, 1, a[1]);
- update_b(1,1,n, 1, 1, b[1]);
- ans = sum[1];
- dout <<"ans : "<<ans<<endl;
- for (int i = 2; i <= n; ++i) {
- int l = i, r = i;
- while (!st1.empty() && st1.top().fi < a[i]) {
- l = st1.top().se;
- st1.pop();
- }
- st1.push({a[i], l});
- update_a(1,1,n, l, r, a[i]);
- dout << "A " << i << " : " << a[i] << " - " << l << " " << r << endl;
- l = i, r = i;
- while (!st2.empty() && st2.top().fi < b[i]) {
- l = st2.top().se;
- st2.pop();
- }
- st2.push({b[i], l});
- update_b(1,1,n, l, r, b[i]);
- dout << "B " << i << " : " << b[i] << " - " << l << " " << r << endl;
- int cur = sum[1];
- ans += cur;
- dout << i << " : " << cur << endl;
- }
- cout << ans << "\n";
- }
- signed main() {
- #ifdef zxc
- debug = 0;
- freopen("../input.txt", "r", stdin);
- // freopen("../output1.txt", "w", stdout);
- #else
- #endif //zxc
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cout.setf(ios::fixed);
- cout.precision(20);
- int t = 1;
- cin >> t;
- for (int j = 1; j <= t; ++j) {
- cout << "Case #" << j << ": ";
- solve();
- }
- //while (t--)
- // solve();
- dout << endl << (1.0 * clock() / CLOCKS_PER_SEC) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement