Advertisement
Guest User

Untitled

a guest
Apr 28th, 2019
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.39 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long ll;
  5. typedef unsigned long long ull;
  6. const int inf_int = 1e9 + 100;
  7. const ll inf_ll = 1e18;
  8. typedef pair<int, int> pii;
  9. typedef pair<ll, ll> pll;
  10. typedef long double dbl;
  11. #define pb push_back
  12. const double pi = 3.1415926535898;
  13. #define dout if(debug) cout
  14. #define fi first
  15. #define se second
  16. #define sp setprecision
  17. #define sz(a) (int(a.size()))
  18. #define all(a) a.begin(),a.end()
  19. bool debug = 0;
  20. const int MAXN = 1e5 + 100;
  21. const int LOG = 20;
  22. const int mod = 1e9 + 7;
  23. const int MX = 2e5 + 100;
  24. typedef long long li;
  25. const li MOD = 1000000000949747713ll;
  26.  
  27.  
  28. int n, k;
  29. int a[MAXN];
  30. int b[MAXN];
  31.  
  32. int a_mn[4 * MAXN];
  33. int a_mx[4 * MAXN];
  34. int a_push[4 * MAXN];
  35.  
  36. int b_mn[4 * MAXN];
  37. int b_mx[4 * MAXN];
  38. int b_push[4 * MAXN];
  39.  
  40. int push_cnt[4 * MAXN];
  41.  
  42. int sum[4 * MAXN];
  43.  
  44. int z = 0;
  45. #define left v<<1,tl,tm
  46. #define right v<<1|1,tm+1,tr
  47.  
  48. inline void upd(int v) {
  49.     a_mx[v] = max(a_mx[v << 1], a_mx[v << 1 | 1]);
  50.     b_mx[v] = max(b_mx[v << 1], b_mx[v << 1 | 1]);
  51.     a_mn[v] = min(a_mn[v << 1], a_mn[v << 1 | 1]);
  52.     b_mn[v] = min(b_mn[v << 1], b_mn[v << 1 | 1]);
  53.     sum[v] = sum[v << 1] + sum[v << 1 | 1];
  54. }
  55.  
  56. inline void push(int v) {
  57.     int v1 = v << 1;
  58.     int v2 = v << 1 | 1;
  59.     if (a_push[v] != -1) {
  60.         a_mn[v1] = a_mx[v1] = a_push[v1] = a_push[v];
  61.         a_mn[v2] = a_mx[v2] = a_push[v2] = a_push[v];
  62.         a_push[v] = -1;
  63.     }
  64.     if (b_push[v] != -1) {
  65.         b_mn[v1] = b_mx[v1] = b_push[v1] = b_push[v];
  66.         b_mn[v2] = b_mx[v2] = b_push[v2] = b_push[v];
  67.         b_push[v] = -1;
  68.     }
  69.     if(push_cnt[v]){
  70.         push_cnt[v<<1] = push_cnt[v<<1|1] = 1;
  71.         push_cnt[v] = 0;
  72.     }
  73. }
  74.  
  75. void build(int v, int tl, int tr) {
  76.     if (tl == tr) {
  77.         a_push[v] = -1;
  78.         b_push[v] = -1;
  79.         a_mn[v] = a_mx[v] = b_mn[v] = b_mx[v] = 0;
  80.         sum[v] = 0;
  81.         push_cnt[v] = 0;
  82.     } else {
  83.         int tm = (tl + tr) >> 1;
  84.         a_push[v] = -1;
  85.         b_push[v] = -1;
  86.         build(left);
  87.         build(right);
  88.         push_cnt[v] = 0;
  89.         upd(v);
  90.     }
  91. }
  92.  
  93. void process(int v, int tl, int tr) {
  94.     z++;
  95.     if (a_mx[v] + k < b_mn[v] || a_mn[v] - k > b_mx[v]) {
  96.         sum[v] = 0;
  97.         push_cnt[v] = 1;
  98.         return;
  99.     }
  100.     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)) {
  101.         sum[v] = tr - tl + 1;
  102.         dout << "HERE ! " << tl << " " << tr << " :: " << a_mn[v] << " " << a_mx[v] << " -- " << b_mn[v] << " "
  103.              << b_mx[v] << endl;
  104.         push_cnt[v] = 1;
  105.         return;
  106.     } else {
  107.         assert(tl != tr);
  108.         int tm = (tl + tr) >> 1;
  109.         push(v);
  110.         process(left);
  111.         process(right);
  112.         upd(v);
  113.     }
  114. }
  115.  
  116. void update_a(int v, int tl, int tr, int l, int r, int val) {
  117.     if (l > tr || r < tl) {
  118.         if(push_cnt[v])
  119.             process(v,tl,tr);
  120.         return;
  121.     }
  122.     if (l <= tl && tr <= r) {
  123.         a_push[v] = val;
  124.         a_mx[v] = a_mn[v] = val;
  125.         process(v, tl, tr);
  126.         return;
  127.     } else {
  128.         int tm = (tl + tr) >> 1;
  129.         push(v);
  130.         update_a(left, l, r, val);
  131.         update_a(right, l, r, val);
  132.         upd(v);
  133.     }
  134. }
  135.  
  136.  
  137. void update_b(int v, int tl, int tr, int l, int r, int val) {
  138.     if (l > tr || r < tl) {
  139.         if(push_cnt[v])
  140.             process(v,tl,tr);
  141.         return;
  142.     }
  143.     if (l <= tl && tr <= r) {
  144.         b_push[v] = val;
  145.         b_mx[v] = b_mn[v] = val;
  146.         process(v, tl, tr);
  147.         return;
  148.     } else {
  149.         int tm = (tl + tr) >> 1;
  150.         push(v);
  151.         update_b(left, l, r, val);
  152.         update_b(right, l, r, val);
  153.         upd(v);
  154.     }
  155. }
  156.  
  157.  
  158. void solve() {
  159.     z =  0;
  160.     cin >> n >> k;
  161.     for (int i = 1; i <= n; ++i) {
  162.         cin >> a[i];
  163.     }
  164.     for (int i = 1; i <= n; ++i) {
  165.         cin >> b[i];
  166.     }
  167.     build(1,1,n);
  168.  
  169.     stack<pii> st1;
  170.     st1.push({a[1], 1});
  171.     stack<pii> st2;
  172.     st2.push({b[1], 1});
  173.     ll ans = 0;
  174.  
  175.  
  176.     update_a(1,1,n, 1, 1, a[1]);
  177.     update_b(1,1,n, 1, 1, b[1]);
  178.  
  179.     ans = sum[1];
  180.     dout <<"ans : "<<ans<<endl;
  181.  
  182.     for (int i = 2; i <= n; ++i) {
  183.         int l = i, r = i;
  184.         while (!st1.empty() && st1.top().fi < a[i]) {
  185.             l = st1.top().se;
  186.             st1.pop();
  187.         }
  188.         st1.push({a[i], l});
  189.         update_a(1,1,n, l, r, a[i]);
  190.         dout << "A " << i << " : " << a[i] << " - " << l << " " << r << endl;
  191.  
  192.         l = i, r = i;
  193.         while (!st2.empty() && st2.top().fi < b[i]) {
  194.             l = st2.top().se;
  195.             st2.pop();
  196.         }
  197.         st2.push({b[i], l});
  198.         update_b(1,1,n, l, r, b[i]);
  199.         dout << "B " << i << " : " << b[i] << " - " << l << " " << r << endl;
  200.  
  201.         int cur = sum[1];
  202.         ans += cur;
  203.         dout << i << " : " << cur << endl;
  204.     }
  205.     cout << ans << "\n";
  206.  
  207. }
  208.  
  209. signed main() {
  210. #ifdef zxc
  211.     debug = 0;
  212.     freopen("../input.txt", "r", stdin);
  213.     //  freopen("../output1.txt", "w", stdout);
  214.  
  215. #else
  216.  
  217. #endif //zxc
  218.     ios_base::sync_with_stdio(0);
  219.     cin.tie(0);
  220.     cout.tie(0);
  221.     cout.setf(ios::fixed);
  222.     cout.precision(20);
  223.  
  224.     int t = 1;
  225.  
  226.     cin >> t;
  227.     for (int j = 1; j <= t; ++j) {
  228.         cout << "Case #" << j << ": ";
  229.         solve();
  230.     }
  231.  
  232.     //while (t--)
  233.     //    solve();
  234.     dout << endl << (1.0 * clock() / CLOCKS_PER_SEC) << endl;
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement