• T2:中间值(median)———2019.10.15


     

     代码:

     1 #include <bits/stdc++.h>
     2 
     3 int ri() {
     4     char c = getchar(); int x = 0, f = 1; for(;c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
     5     for(;c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) - '0' + c; return x * f;
     6 }
     7 const int N = 5e5+50;
     8 
     9 int a[N], b[N];
    10 
    11 int kth(int ta[], int sa, int tb[], int sb, int k) {
    12     if (sa > sb) return kth(tb, sb, ta, sa, k);
    13     if (sa == 0) return tb[k];
    14     if (k == 1) return std::min(ta[1], tb[1]);
    15     int ka = std::min(sa, k/2), kb = k - ka;
    16     if (ta[ka] < tb[kb]) return kth(ta+ka, sa-ka, tb, sb, k-ka);
    17     return kth(ta, sa, tb+kb, sb-kb, k-kb);
    18 }
    19 
    20 int query(int la, int ra, int lb, int rb) {
    21     int sa = ra-la+1, sb = rb-lb+1, siz = sa + sb;
    22     return kth(a+la-1, sa, b+lb-1, sb, siz/2+1);
    23 }
    24 
    25 int main() {
    26     freopen("median.in", "r", stdin);
    27     freopen("median.out", "w", stdout);
    28     int n, m;
    29     n = ri(); m = ri();
    30     for (int i = 1; i <= n; i++) a[i] = ri();
    31     for (int i = 1; i <= n; i++) b[i] = ri();
    32     for (int opt; m--; ) {
    33         opt = ri();
    34         if (opt == 2) {
    35             int la = ri(), ra = ri(), lb = ri(), rb = ri();
    36             printf("%d
    ", query(la, ra, lb, rb));
    37         } else {
    38             int p = ri(), pos = ri(), val = ri();
    39             if (p == 0) a[pos] = val;
    40             else b[pos] = val;
    41         }
    42     }
    43     return 0;
    44 }
    45 /*
    46 5 5
    47 12 41 46 68 69
    48 35 61 82 84 96
    49 2 1 4 3 5
    50 1 0 5 75
    51 2 2 4 3 4
    52 2 3 4 1 5
    53 2 1 4 2 4
    54 */
  • 相关阅读:
    CSS3 @font-face
    CSS3 Media Queries
    简单弹出层示例
    mysql查询排名
    ajax 简单学习
    js ajax 传送xml dom对象到服务器
    雨燕权限管理后端技术总结
    日期date出参入参和timestamp转化
    雨燕权限管理前端技术总结
    jwt再度理解
  • 原文地址:https://www.cnblogs.com/ydclyq/p/11677778.html
Copyright © 2020-2023  润新知