• [Educational Codeforces Round 56 (Rated for Div. 2)] —G. Multidimensional Queries(二进制状压,线段树)


    [Educational Codeforces Round 56 (Rated for Div. 2)] —G. Multidimensional Queries(二进制状压,线段树)

    G. Multidimensional Queries

    time limit per test

    6 seconds

    memory limit per test

    512 megabytes

    input

    standard input

    output

    standard output

    You are given an array aa of nn points in kk-dimensional space. Let the distance between two points axax and ayay be ∑i=1k|ax,i−ay,i|∑i=1k|ax,i−ay,i| (it is also known as Manhattan distance).

    You have to process qq queries of the following two types:

    • 11 ii b1b1 b2b2 ... bkbk — set ii-th element of aa to the point (b1,b2,…,bk)(b1,b2,…,bk);
    • 22 ll rr — find the maximum distance between two points aiai and ajaj, where l≤i,j≤rl≤i,j≤r.

    题意:

    在一个(mathit k)维度坐标系中,点(a_x,a_y)的距离为(sum limits_{i = 1}^{k} |a_{x, i} - a_{y, i}|)

    现在给定(mathit n)个点,和(mathit q)个询问,每一个询问有( ext 2)种:

    • $1 i b_1 b_2 ... b_k (代表将第)mathit i(个点的坐标改为)b_1 b_2 ... b_k$
    • (2 l r),代表让你找出两个距离最大的节点(a_i,a_j,l le i, j le r),输出他们的距离即可。

    思路:

    (a_x,a_y)的距离为(sum limits_{i = 1}^{k} |a_{x, i} - a_{y, i}|=max(sum_{i=1}^{k}Q_i*a_i+sum_{i=1}^{k}(!Q_i)*b_i,Q_iin{{-1,1}}))

    仔细思考上面等式,右边表达式中会有很多无效的状态,但是所有状态中的最大值一定是等于左侧表达式的。

    然后我们可以建立(2^k)个线段树,第(mathit i)颗数维护的是((i)_2)中第(mathit j)位为( ext 1)(a_{i,j})取正好,否则取符号的总和最大值。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #include <iomanip>
    #include <sstream>
    #include <bitset>
    #include <unordered_map>
    // #include <bits/stdc++.h>
    #define ALL(x) (x).begin(), (x).end()
    #define sz(a) int(a.size())
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
    #define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
    #define du2(a,b) scanf("%d %d",&(a),&(b))
    #define du1(a) scanf("%d",&(a));
    using namespace std;
    typedef long long ll;
    ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
    ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
    ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
    ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
    void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
    ");}}}
    void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("
    ");}}}
    inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
    inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;}
    void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '
    ' : ' ');}}
    void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '
    ' : ' ');}}
    const int maxn = 200010;
    const int inf = 0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    #define DEBUG_Switch 0
    ll segment_tree[maxn << 2][35];
    int n, k;
    int a[maxn][6];
    void pushup(int rt)
    {
        repd(j, 0, (1<<k)- 1)
        {
            segment_tree[rt][j] = max(segment_tree[rt << 1][j], segment_tree[rt << 1 | 1][j]);
        }
    }
    void show(int rt)
    {
        repd(j, 0, k - 1)
        printf("%d %d %lld
    ",rt,j,segment_tree[rt][j]);
    }
    void build(int rt, int l, int r)
    {
        if (l == r)
        {
            for (int i = 0; i < (1 << k); ++i)
            {
                ll sum = 0ll;
                repd(j, 0, k - 1)
                {
                    if (i & (1 << j))
                    {
                        sum += a[l][j + 1];
                    } else
                    {
                        sum -= a[l][j + 1];
                    }
                }
    //            printf("%d %d %lld
    ",l,i,sum);
                segment_tree[rt][i] = sum;
            }
        } else
        {
            int mid = (l + r) >> 1;
            build(rt << 1, l, mid);
            build(rt << 1 | 1, mid + 1, r);
            pushup(rt);
        }
    //    show(rt);
    }
    void update(int rt, int l, int r, int id)
    {
        if (l == r && l == id)
        {
            for (int i = 0; i < (1 << k); ++i)
            {
                ll sum = 0ll;
                repd(j, 0, k - 1)
                {
                    if (i & (1 << j))
                    {
                        sum += a[id][j + 1];
                    } else
                    {
                        sum -= a[id][j + 1];
                    }
                }
                segment_tree[rt][i] = sum;
            }
            return ;
        } else
        {
            int mid = (l + r) >> 1;
            if (id > mid)
                update(rt << 1 | 1, mid + 1, r, id);
            else
                update(rt << 1, l, mid, id);
            pushup(rt);
        }
    }
    ll f[35];
    void query(int rt, int l, int r, int ql, int qr)
    {
        if (l >= ql && qr >= r)
        {
            for (int i = 0; i < (1 << k); ++i)
            {
                f[i] = max(f[i], segment_tree[rt][i]);
            }
        } else
        {
            int mid = (l + r) >> 1;
            if (ql <= mid)
            {
                query(rt << 1, l, mid, ql, qr);
            }
            if (qr > mid)
            {
                query(rt << 1 | 1, mid + 1, r, ql, qr);
            }
        }
    }
    
    int main()
    {
    #if DEBUG_Switch
        freopen("C:\code\input.txt", "r", stdin);
    #endif
        //freopen("C:\code\output.txt","r",stdin);
        n = readint();
        k = readint();
        repd(i, 1, n)
        {
            repd(j, 1, k)
            {
                a[i][j] = readint();
            }
        }
        build(1, 1, n);
        int q = readint();
        while (q--)
        {
            int op = readint();
            if (op == 1)
            {
                int id = readint();
                repd(j, 1, k)
                {
                    a[id][j] = readint();
                }
                update(1, 1, n, id);
            } else
            {
                int l = readint();
                int r = readint();
                for (int i = 0; i < (1 << k); ++i)
                {
                    f[i] = -1e18;
                }
                query(1, 1, n, l, r);
                ll ans = -1e18;
                for (int i = 0; i < (1 << k); ++i)
                {
                    int opponent =((1 << k)-1)^i;
                    ans = max(ans, f[i] + f[opponent]);
                }
                printf("%lld
    ", ans );
            }
        }
        return 0;
    }
    
     
    
    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    Ananagrams(反片语)【map的应用】P114
    并查集初步——
    预处理器,编译器,汇编器,连接器
    https://pta.patest.cn/pta/test/15/exam/3/question/724
    stl入门--reverse函数
    无向图的深度遍历
    01背包
    动态规划——数字三角形(递归or递推or记忆化搜索)
    八皇后问题
    幂集的计算
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/13192021.html
Copyright © 2020-2023  润新知