• 洛谷P4169 天使玩偶 (算竞进阶习题)


    CDQ分治

    分成四个方向讨论最小值,把所有坐标全部离线处理。

    把左边按x轴排序,保证x的顺序,然后树状数组维护每个方向需要的最值。。

    然后CDQ分治。。必须手动撤销树状数组的修改,保证分治的时间复杂度。

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    #define full(a, b) memset(a, b, sizeof a)
    #define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
    using namespace std;
    typedef long long ll;
    inline int lowbit(int x){ return x & (-x); }
    inline int read(){
        int X = 0, w = 0; char ch = 0;
        while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
        while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
        return w ? -X : X;
    }
    inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
    inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
    template<typename T>
    inline T max(T x, T y, T z){ return max(max(x, y), z); }
    template<typename T>
    inline T min(T x, T y, T z){ return min(min(x, y), z); }
    template<typename A, typename B, typename C>
    inline A fpow(A x, B p, C lyd){
        A ans = 1;
        for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
        return ans;
    }
    const int N = 1000005;
    int n, m, tot, t[N], k, ans[N];
    struct rec {
        int x, y, z;
        bool operator < (const rec &rhs) const {
            return x < rhs.x;
        }
    }a[N], b[N];
    
    inline void add(int index, int val){
        for(; index < tot; index += lowbit(index))
            t[index] = max(t[index], val);
    }
    
    inline int query(int index){
        int ret = -(1 << 30);
        for(; index; index -= lowbit(index))
            ret = max(ret, t[index]);
        return ret;
    }
    
    inline void calc1(){
        for(int i = 1; i <= k; i ++){
            int y = b[i].y;
            int temp = b[i].x + b[i].y;
            if(a[b[i].z].z == 1) add(y, temp);
            else ans[b[i].z] = min(ans[b[i].z], abs(temp - query(y)));
        }
        for(int i = 1; i <= k; i ++){
            int y = b[i].y;
            if(a[b[i].z].z == 1){
                for(int j = y; j < tot; j += lowbit(j)) t[j] = -(1 << 30);
            }
        }
    }
    
    inline void calc2(){
        for(int i = 1; i <= k; i ++){
            int y = b[i].y;
            int temp = b[i].x - b[i].y;
            if(a[b[i].z].z == 1) add(tot - y, temp);
            else ans[b[i].z] = min(ans[b[i].z], abs(temp - query(tot - y)));
        }
        for(int i = 1; i <= k; i ++){
            int y = b[i].y;
            if(a[b[i].z].z == 1){
                for(int j = tot - y; j < tot; j += lowbit(j)) t[j] = -(1 << 30);
            }
        }
    }
    
    inline void calc3(){
        for(int i = k; i >= 1; i --){
            int y = b[i].y;
            int temp = -b[i].x - b[i].y;
            if(a[b[i].z].z == 1) add(tot - y, temp);
            else ans[b[i].z] = min(ans[b[i].z], abs(temp - query(tot - y)));
        }
        for(int i = k; i >= 1; i --){
            int y = b[i].y;
            if(a[b[i].z].z == 1){
                for(int j = tot - y; j < tot; j += lowbit(j)) t[j] = -(1 << 30);
            }
        }
    }
    
    inline void calc4(){
        for(int i = k; i >= 1; i --){
            int y = b[i].y;
            int temp = -b[i].x + b[i].y;
            if(a[b[i].z].z == 1) add(y, temp);
            else ans[b[i].z] = min(ans[b[i].z], abs(temp - query(y)));
        }
        for(int i = k; i >= 1; i --){
            int y = b[i].y;
            if(a[b[i].z].z == 1){
                for(int j = y; j < tot; j += lowbit(j)) t[j] = -(1 << 30);
            }
        }
    }
    
    void cdqDiv(int l, int r){
        if(l == r) return;
        int mid = (l + r) >> 1;
        cdqDiv(l, mid), cdqDiv(mid + 1, r);
        k = 0;
        for(int i = l; i <= r; i ++){
            if((i <= mid && a[i].z == 1) || (i > mid && a[i].z == 2))
                b[++k] = a[i], b[k].z = i;
        }
        sort(b + 1, b + k + 1);
        calc1(), calc2();
        calc3(), calc4();
    }
    
    int main(){
    
        full(t, 0xcf), full(ans, INF);
        n = read(), m = read();
        m += n;
        for(int i = 1; i <= n; i ++){
            a[i].x = read(), a[i].y = read() + 1, a[i].z = 1;
        }
        for(int i = n + 1; i <= m; i ++){
            a[i].z = read(), a[i].x = read(), a[i].y = read() + 1;
        }
        for(int i = 1; i <= m; i ++) tot = max(tot, a[i].y);
        tot ++;
        cdqDiv(1, m);
        for(int i = 1; i <= m; i ++){
            if(a[i].z == 2) printf("%d
    ", ans[i]);
        }
        return 0;
    }
    
  • 相关阅读:
    求一个数的阶乘在 m 进制下末尾 0 的个数
    区间dp
    最长公共子序列变形
    学习stm32专区
    C/C++中static关键字详解
    ASP.NET调用Office Com组件权限设置
    TreeView控件
    SQL笔记(1)索引/触发器
    NPOI 1.2.5 教程
    SQL Povit
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/10942599.html
Copyright © 2020-2023  润新知