Advertisement
Guest User

Untitled

a guest
Aug 14th, 2013
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.14 KB | None | 0 0
  1. /*
  2.  * File:      main.cpp
  3.  * Author:    Hrayr [HarHro94]
  4.  * Problem:
  5.  * IDE:       Visual C++ 2012
  6.  */
  7. #pragma comment(linker, "/STACK:66777216")
  8. #define _CRT_SECURE_NO_WARNINGS
  9. #include <functional>
  10. #include <algorithm>
  11. #include <iostream>
  12. #include <sstream>
  13. #include <fstream>
  14. #include <cassert>
  15. #include <iomanip>
  16. #include <cstring>
  17. #include <cstdio>
  18. #include <string>
  19. #include <vector>
  20. #include <ctime>
  21. #include <queue>
  22. #include <stack>
  23. #include <cmath>
  24. #include <set>
  25. #include <map>
  26. using namespace std;
  27.  
  28. typedef long long LL;
  29. typedef long double LD;
  30. #define pb push_back
  31. #define mp make_pair
  32. #define all(v) (v).begin(), (v).end()
  33. #define sz(v) (int)(v).size()
  34.  
  35. const int N = 100007;
  36. const int V = 40 * N;
  37. struct persistentSegmentTree
  38. {
  39.     LL sum[V];
  40.     int ptr, cnt[V], left[V], right[V];
  41.     int rootCnt, roots[N];
  42.     int size;
  43.     LL *xx;
  44.  
  45.     inline int newLeaf(int c, LL s)
  46.     {
  47.         ++ptr;
  48.         sum[ptr] = s;
  49.         cnt[ptr] = c;
  50.         left[ptr] = 0;
  51.         right[ptr] = 0;
  52.         return ptr;
  53.     }
  54.  
  55.     inline int newNode(int l, int r)
  56.     {
  57.         ++ptr;
  58.         left[ptr] = l;
  59.         right[ptr] = r;
  60.         sum[ptr] = 0LL;
  61.         cnt[ptr] = 0;
  62.         if (l)
  63.         {
  64.             cnt[ptr] += cnt[l];
  65.             sum[ptr] += sum[l];
  66.         }
  67.         if (r)
  68.         {
  69.             cnt[ptr] += cnt[r];
  70.             sum[ptr] += sum[r];
  71.         }
  72.         return ptr;
  73.     }
  74.  
  75.     void start(int n, LL x[], int szx)
  76.     {
  77.         xx = x;
  78.         size = szx;
  79.         roots[rootCnt++] = build(0, szx - 1);
  80.     }
  81.  
  82.     inline int countSmaller(int id, int pos)
  83.     {
  84.         return cntQuery(roots[pos + 1], 0, size - 1, 0, id);
  85.     }
  86.  
  87.     inline LL sumSmaller(int id, int pos)
  88.     {
  89.         return sumQuery(roots[pos + 1], 0, size - 1, 0, id);
  90.     }
  91.  
  92.     int build(int tl, int tr)
  93.     {
  94.         if (tl == tr)
  95.         {
  96.             return newLeaf(0, 0LL);
  97.         }
  98.         int tm = (tl + tr) / 2;
  99.         return newNode(build(tl, tm), build(tm + 1, tr));
  100.     }
  101.  
  102.     LL sumQuery(int T, int tl, int tr, int l, int r)
  103.     {
  104.         if (tl == l && tr == r)
  105.         {
  106.             return sum[T];
  107.         }
  108.         int tm = (tl + tr) >> 1;
  109.         if (r <= tm)
  110.         {
  111.             return sumQuery(left[T], tl, tm, l, r);
  112.         }
  113.         if (l > tm)
  114.         {
  115.             return sumQuery(right[T], tm + 1, tr, l, r);
  116.         }
  117.         return sumQuery(left[T], tl, tm, l, tm) + sumQuery(right[T], tm + 1, tr, tm + 1, r);
  118.     }
  119.  
  120.     int cntQuery(int T, int tl, int tr, int l, int r)
  121.     {
  122.         if (tl == l && tr == r)
  123.         {
  124.             return cnt[T];
  125.         }
  126.         int tm = (tl + tr) >> 1;
  127.         if (r <= tm)
  128.         {
  129.             return cntQuery(left[T], tl, tm, l, r);
  130.         }
  131.         if (l > tm)
  132.         {
  133.             return cntQuery(right[T], tm + 1, tr, l, r);
  134.         }
  135.         return cntQuery(left[T], tl, tm, l, tm) + cntQuery(right[T], tm + 1, tr, tm + 1, r);
  136.     }
  137.  
  138.     int update(int T, int tl, int tr, int pos, int delta)
  139.     {
  140.         if (tl == tr)
  141.         {
  142.             return newLeaf(cnt[T] + delta, sum[T] + delta * xx[pos]);
  143.         }
  144.         int tm = (tl + tr) / 2;
  145.         if (pos <= tm)
  146.         {
  147.             return newNode(update(left[T], tl, tm, pos, delta), right[T]);
  148.         }
  149.         return newNode(left[T], update(right[T], tm + 1, tr, pos, delta));
  150.     }
  151. } X, Y;
  152.  
  153. LL sx[N], sy[N], rx[N], ry[N], sumx[N], sumy[N];
  154. int n, q, sxsize, sysize, x[N], y[N];
  155.  
  156. LL myabs(LL x)
  157. {
  158.     return x < 0LL ? - x : x;
  159. }
  160.  
  161. int main()
  162. {
  163. #ifdef harhro94
  164.     freopen("input.txt", "r", stdin);
  165.     freopen("output.txt", "w", stdout);
  166. #endif
  167.  
  168.     scanf("%d%d", &n, &q);
  169.     for (int i = 0; i < n; ++i)
  170.     {
  171.         scanf("%d", x + i);
  172.     }
  173.     for (int i = 0; i < n; ++i)
  174.     {
  175.         scanf("%d", y + i);
  176.     }
  177.     for (int i = 0; i < n; ++i)
  178.     {
  179.         rx[i] = x[i] + y[i];
  180.         ry[i] = x[i] - y[i];
  181.         sx[i] = rx[i];
  182.         sy[i] = ry[i];
  183.     }
  184.     sort(sx, sx + n);
  185.     sort(sy, sy + n);
  186.     sxsize = unique(sx, sx + n) - sx;
  187.     sysize = unique(sy, sy + n) - sy;
  188.     for (int i = 1; i <= n; ++i)
  189.     {
  190.         sumx[i] = sumx[i - 1] + rx[i - 1];
  191.         sumy[i] = sumy[i - 1] + ry[i - 1];
  192.     }
  193.     X.start(n, sx, sxsize);
  194.     Y.start(n, sy, sysize);
  195.     cerr << fixed << setprecision(3) << "\nExecution time = " << clock() / 1000.0 << "s\n";
  196.     for (int i = 0; i < n; ++i)
  197.     {
  198.         int lo = 0;
  199.         int hi = sxsize - 1;
  200.         while (lo < hi)
  201.         {
  202.             int mi = (lo + hi + 1) >> 1;
  203.             if (sx[mi] > rx[i])
  204.             {
  205.                 hi = mi - 1;
  206.             }
  207.             else
  208.             {
  209.                 lo = mi;
  210.             }
  211.         }
  212.         X.roots[X.rootCnt++] = X.update(X.roots[X.rootCnt - 1], 0, X.size - 1, lo, 1);
  213.     }
  214.     for (int i = 0; i < n; ++i)
  215.     {
  216.         int lo = 0;
  217.         int hi = sysize - 1;
  218.         while (lo < hi)
  219.         {
  220.             int mi = (lo + hi + 1) >> 1;
  221.             if (sy[mi] > ry[i])
  222.             {
  223.                 hi = mi - 1;
  224.             }
  225.             else
  226.             {
  227.                 lo = mi;
  228.             }
  229.         }
  230.         Y.roots[Y.rootCnt++] = Y.update(Y.roots[Y.rootCnt - 1], 0, Y.size - 1, lo, 1);
  231.     }
  232.     cerr << fixed << setprecision(3) << "\nExecution time = " << clock() / 1000.0 << "s\n";
  233.     while (q--)
  234.     {
  235.         int l, r;
  236.         scanf("%d%d", &l, &r);
  237.         --l, --r;
  238.         int cnt = r - l + 1;
  239.         int need = cnt / 2 + 1;
  240.         LL ans = 0LL;
  241.         int lo = 0;
  242.         int hi = sxsize - 1;
  243.         while (lo < hi)
  244.         {
  245.             int mi = (lo + hi) >> 1;
  246.             int c1 = X.countSmaller(mi, r);
  247.             int c2 = X.countSmaller(mi, l - 1);
  248.             if (c1 < need || (c1 - c2) < need)
  249.             {
  250.                 lo = mi + 1;
  251.             }
  252.             else
  253.             {
  254.                 hi = mi;
  255.             }
  256.         }
  257.         LL med = sx[lo];
  258.         LL all = sumx[r + 1] - sumx[l];
  259.         LL less = X.sumSmaller(lo, r) - X.sumSmaller(lo, l - 1);
  260.         int lessCnt = X.countSmaller(lo, r) - X.countSmaller(lo, l - 1);
  261.         ans += all - 2 * less + lessCnt * med - (cnt - lessCnt) * med;
  262.  
  263.         lo = 0;
  264.         hi = sysize - 1;
  265.         while (lo < hi)
  266.         {
  267.             int mi = (lo + hi) >> 1;
  268.             int c1 = Y.countSmaller(mi, r);
  269.             int c2 = Y.countSmaller(mi, l - 1);
  270.             if (c1 < need || (c1 - c2) < need)
  271.             {
  272.                 lo = mi + 1;
  273.             }
  274.             else
  275.             {
  276.                 hi = mi;
  277.             }
  278.         }
  279.         med = sy[lo];
  280.         all = sumy[r + 1] - sumy[l];
  281.         less = Y.sumSmaller(lo, r) - Y.sumSmaller(lo, l - 1);
  282.         lessCnt = Y.countSmaller(lo, r) - Y.countSmaller(lo, l - 1);
  283.         ans += all - 2 * less + lessCnt * med - (cnt - lessCnt) * med;
  284.         if (ans & 1LL)
  285.         {
  286.             printf("%lld.5\n", ans / 2);
  287.         }
  288.         else
  289.         {
  290.             printf("%lld\n", ans / 2);
  291.         }
  292.     }
  293.  
  294. #ifdef harhro94
  295.     cerr << fixed << setprecision(3) << "\nExecution time = " << clock() / 1000.0 << "s\n";
  296. #endif
  297.     return 0;
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement