Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: main.cpp
- * Author: Hrayr [HarHro94]
- * Problem:
- * IDE: Visual C++ 2012
- */
- #pragma comment(linker, "/STACK:66777216")
- #define _CRT_SECURE_NO_WARNINGS
- #include <functional>
- #include <algorithm>
- #include <iostream>
- #include <sstream>
- #include <fstream>
- #include <cassert>
- #include <iomanip>
- #include <cstring>
- #include <cstdio>
- #include <string>
- #include <vector>
- #include <ctime>
- #include <queue>
- #include <stack>
- #include <cmath>
- #include <set>
- #include <map>
- using namespace std;
- typedef long long LL;
- typedef long double LD;
- #define pb push_back
- #define mp make_pair
- #define all(v) (v).begin(), (v).end()
- #define sz(v) (int)(v).size()
- const int N = 100007;
- LL sx[N], sy[N], rx[N], ry[N], sumx[N], sumy[N];
- int n, q, x[N], y[N];
- struct tree
- {
- struct node
- {
- LL *sorted, *sum;
- } node[8 * N];
- void build(int v, int tl, int tr, LL x[])
- {
- node[v].sorted = new LL [tr - tl + 1];
- node[v].sum = new LL[tr - tl + 1];
- if (tl == tr)
- {
- node[v].sorted[0] = x[tl];
- node[v].sum[0] = x[tl];
- return;
- }
- int tm = (tl + tr) >> 1;
- build((v << 1), tl, tm, x);
- build((v << 1) + 1, tm + 1, tr, x);
- LL *a = node[(v << 1)].sorted;
- LL *b = node[(v << 1) + 1].sorted;
- LL *c = node[v].sorted;
- int i = 0;
- int j = 0;
- int k = 0;
- int sza = tm - tl + 1;
- int szb = tr - tm;
- while (i != sza || j != szb)
- {
- if (i == sza)
- {
- c[k++] = b[j++];
- }
- else if (j == szb)
- {
- c[k++] = a[i++];
- }
- else if (a[i] < b[j])
- {
- c[k++] = a[i++];
- }
- else
- {
- c[k++] = b[j++];
- }
- }
- LL *s = node[v].sum;
- s[0] = c[0];
- for (int i = 1; i < tr - tl + 1; ++i)
- {
- s[i] = c[i] + s[i - 1];
- }
- }
- int query(int v, int tl, int tr, int l, int r, LL val)
- {
- if (tl == l && tr == r)
- {
- LL *a = node[v].sorted;
- int lo = 0;
- int hi = tr - tl;
- while (lo < hi)
- {
- int mi = (lo + hi + 1) >> 1;
- if (a[mi] <= val)
- {
- lo = mi;
- }
- else
- {
- hi = mi - 1;
- }
- }
- if (a[lo] <= val)
- {
- return lo + 1;
- }
- return 0;
- }
- int tm = (tl + tr) >> 1;
- if (r <= tm)
- {
- return query((v << 1), tl, tm, l, r, val);
- }
- if (l > tm)
- {
- return query((v << 1) + 1, tm + 1, tr, l, r, val);
- }
- return query((v << 1), tl, tm, l, tm, val) + query((v << 1) + 1, tm + 1, tr, tm + 1, r, val);
- }
- LL rsq(int v, int tl, int tr, int l, int r, LL val)
- {
- if (tl == l && tr == r)
- {
- LL *a = node[v].sorted;
- int lo = 0;
- int hi = tr - tl;
- while (lo < hi)
- {
- int mi = (lo + hi + 1) >> 1;
- if (a[mi] <= val)
- {
- lo = mi;
- }
- else
- {
- hi = mi - 1;
- }
- }
- if (a[lo] <= val)
- {
- return node[v].sum[lo];
- }
- return 0;
- }
- int tm = (tl + tr) >> 1;
- if (r <= tm)
- {
- return rsq((v << 1), tl, tm, l, r, val);
- }
- if (l > tm)
- {
- return rsq((v << 1) + 1, tm + 1, tr, l, r, val);
- }
- return rsq((v << 1), tl, tm, l, tm, val) + rsq((v << 1) + 1, tm + 1, tr, tm + 1, r, val);
- }
- } X, Y;
- LL myabs(LL x)
- {
- return x < 0LL ? - x : x;
- }
- int main()
- {
- #ifdef harhro94
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- scanf("%d%d", &n, &q);
- for (int i = 0; i < n; ++i)
- {
- scanf("%d", x + i);
- }
- for (int i = 0; i < n; ++i)
- {
- scanf("%d", y + i);
- }
- for (int i = 0; i < n; ++i)
- {
- rx[i] = x[i] + y[i];
- ry[i] = x[i] - y[i];
- sx[i] = rx[i];
- sy[i] = ry[i];
- }
- sort(sx, sx + n);
- sort(sy, sy + n);
- for (int i = 1; i <= n; ++i)
- {
- sumx[i] = sumx[i - 1] + rx[i - 1];
- sumy[i] = sumy[i - 1] + ry[i - 1];
- }
- /*
- for (int i = 0; i < n; ++i)
- {
- cerr << rx[i] << " ";
- }
- cerr << endl;
- for (int i = 0; i < n; ++i)
- {
- cerr << ry[i] << " ";
- }
- cerr << endl;
- */
- X.build(1, 0, n - 1, rx);
- Y.build(1, 0, n - 1, ry);
- cerr << fixed << setprecision(3) << "\nExecution time = " << clock() / 1000.0 << "s\n";
- while (q--)
- {
- int l, r;
- scanf("%d%d", &l, &r);
- --l, --r;
- int cnt = r - l + 1;
- if (cnt < 50)
- {
- vector<int> xx(rx + l, rx + r + 1), yy(ry + l, ry + r + 1);
- sort(all(xx));
- sort(all(yy));
- cnt = r - l + 1;
- LD rcx = xx[cnt >> 1];
- LD rcy = yy[cnt >> 1];
- //cerr << rcx << endl << rcy << endl;
- LD ans = 0.0;
- for (int i = l; i <= r; ++i)
- {
- ans += fabs(rx[i] - rcx) + fabs(ry[i] - rcy);
- }
- printf("%.7lf\n", (double)ans / 2.0);
- }
- else
- {
- int need = cnt / 2 + 1;
- LL ans = 0LL;
- int lo = 0;
- int hi = n - 1;
- while (lo < hi)
- {
- int mi = (lo + hi) >> 1;
- if (X.query(1, 0, n - 1, l, r, sx[mi]) >= need)
- {
- hi = mi;
- }
- else
- {
- lo = mi + 1;
- }
- }
- LL med = sx[lo];
- LL all = sumx[r + 1] - sumx[l];
- LL less = X.rsq(1, 0, n - 1, l, r, med);
- LL greater = all - less;
- int lessCnt = X.query(1, 0, n - 1, l, r, med);
- ans += greater - less + lessCnt * med - (cnt - lessCnt) * med;
- lo = 0;
- hi = n - 1;
- while (lo < hi)
- {
- int mi = (lo + hi) >> 1;
- if (Y.query(1, 0, n - 1, l, r, sy[mi]) >= need)
- {
- hi = mi;
- }
- else
- {
- lo = mi + 1;
- }
- }
- med = sy[lo];
- all = sumy[r + 1] - sumy[l];
- less = Y.rsq(1, 0, n - 1, l, r, med);
- greater = all - less;
- lessCnt = Y.query(1, 0, n - 1, l, r, med);
- ans += greater - less + lessCnt * med - (cnt - lessCnt) * med;
- printf("%.7lf\n", (double)ans / 2.0);
- }
- }
- #ifdef harhro94
- cerr << fixed << setprecision(3) << "\nExecution time = " << clock() / 1000.0 << "s\n";
- #endif
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement