首先将(x, y)变换为((x + y) / 2, (x - y) / 2)

然后变成查询区间中位数的问题,把x, y分开做

离散化完直接用主席树维护区间和

 

  1 /**************************************************************
  2     Problem: 2735
  3     User: rausen
  4     Language: C++
  5     Result: Accepted
  6     Time:5960 ms
  7     Memory:81280 kb
  8 ****************************************************************/
  9  
 10 #include <cstdio>
 11 #include <algorithm>
 12  
 13 using namespace std;
 14 typedef long long ll;
 15 typedef double lf;
 16 const int N = 1e5 + 5;
 17 const int M = 4e6 + 5;
 18  
 19 int n, Q;
 20 int x[N], y[N], tx[N], ty[N];
 21  
 22 struct chair_node {
 23     chair_node *ls, *rs;
 24     int cnt;
 25     ll sum;
 26 } *chair_root_x[N], *chair_root_y[N], mempool[M], *cnt_chair = mempool, *null;
 27  
 28 inline int read() {
 29     int x = 0, sgn = 1;
 30     char ch = getchar();
 31     while (ch < '0' || '9' < ch) {
 32         if (ch == '-') sgn = -1;
 33         ch = getchar();
 34     }
 35     while ('0' <= ch && ch <= '9') {
 36         x = x * 10 + ch - '0';
 37         ch = getchar();
 38     }
 39     return sgn * x;
 40 }
 41  
 42 #define Cnt p -> cnt
 43 #define Sum p -> sum
 44 #define Ls p -> ls
 45 #define Rs p -> rs
 46 #define mid (l + r >> 1)
 47 void chair_modify(chair_node *&p, int l, int r, int pos, int d) {
 48     *(++cnt_chair) = *p, p = cnt_chair, ++Cnt, Sum += d;
 49     if (l == r) return;
 50     if (pos <= mid) chair_modify(Ls, l, mid, pos, d);
 51     else chair_modify(Rs, mid + 1, r, pos, d);
 52 }
 53  
 54 ll chair_query(chair_node *p, chair_node *q, int rank) {
 55     int l = 1, r = n, cnt = 0, t;
 56     ll res = 0;
 57     while (l < r) {
 58         t = p -> ls -> cnt - q -> ls -> cnt;
 59         if (rank <= t) {
 60             cnt += p -> rs -> cnt - q -> rs -> cnt, res += p -> rs -> sum - q -> rs -> sum;
 61             p = p -> ls, q = q -> ls, r = mid;
 62         } else {
 63             cnt -= t, res -= p -> ls -> sum - q -> ls -> sum;
 64             rank -= t, p = p -> rs, q = q -> rs, l = mid + 1;
 65         }
 66     }
 67     return res - Sum / Cnt * cnt;
 68 }
 69 #undef mid
 70 #undef Ls
 71 #undef Rs
 72 #undef Sum
 73 #undef Cnt
 74  
 75 int main() {
 76     int i, j, x1, y1, tmp;
 77     n = read(), Q = read();
 78     for (i = 1; i <= n; ++i) x[i] = read();
 79     for (i = 1; i <= n; ++i) {
 80         tmp = read();
 81         y[i] = x[i] - tmp, x[i] += tmp;
 82     }
 83     for (i = 1; i <= n; ++i) tx[i] = x[i];
 84     sort(tx + 1, tx + n + 1);
 85     for (i = 1; i <= n; ++i) x[i] = lower_bound(tx + 1, tx + n + 1, x[i]) - tx;
 86     for (i = 1; i <= n; ++i) ty[i] = y[i];
 87     sort(ty + 1, ty + n + 1);
 88     for (i = 1; i <= n; ++i) y[i] = lower_bound(ty + 1, ty + n + 1, y[i]) - ty;
 89      
 90     null = cnt_chair;
 91     null -> ls = null -> rs = null, null -> cnt = null -> sum = 0;
 92     chair_root_x[0] = chair_root_y[0] = null;
 93     for (i = 1; i <= n; ++i) {
 94         chair_modify(chair_root_x[i] = chair_root_x[i - 1], 1, n, x[i], tx[x[i]]);
 95         chair_modify(chair_root_y[i] = chair_root_y[i - 1], 1, n, y[i], ty[y[i]]);
 96     }
 97     while (Q--) {
 98         x1 = read(), y1 = read(), tmp = (y1 - x1 + 2) / 2;
 99         printf("%.2lf\n", (lf) (chair_query(chair_root_x[y1], chair_root_x[x1 - 1], tmp) +  chair_query(chair_root_y[y1], chair_root_y[x1 - 1], tmp)) / 2.0);
100     }
101     return 0;
102 }
View Code

 

posted on 2015-03-10 22:46  Xs酱~  阅读(230)  评论(0编辑  收藏  举报