其他分享
首页 > 其他分享> > 总结-二分

总结-二分

作者:互联网

CDQ(时间二分)

BZOJ2683简单题

拆询问,二分过程中归并解决偏序

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++a)
#define nR(a,b,c) for(register int a = (b); a >= (c); --a)
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) ((a) ^= (b) ^= (a) ^= (b))

#define ON_DEBUGG

#ifdef ON_DEBUGG

#define D_e_Line printf("\n-----------\n")
#define D_e(x) std::cout << (#x) << " : " <<x << "\n"
#define FileOpen() freopen("in.txt", "r", stdin)
#define FileSave() freopen("out.txt", "w", stdout)
#define Pause() system("pause")
#include <ctime>
#define TIME() fprintf(stderr, "\nTIME : %.3lfms\n", clock() * 1000.0 / CLOCKS_PER_SEC)

#else

#define D_e_Line ;
#define D_e(x) ;
#define FileOpen() ;
#define FilSave ;
#define Pause() ;
#define TIME() ;

#endif

struct ios {
    template<typename ATP> ios& operator >> (ATP &x) {
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x *= f;
        return *this;
    }
}io;

using namespace std;

template<typename ATP> inline ATP Min(ATP a, ATP b) {
    return a < b ? a : b;
}
template<typename ATP> inline ATP Max(ATP a, ATP b) {
    return a > b ? a : b;
}

const int N = 1e6 + 7;

int n, tim, qid;

struct Ques {
    int opt, x, y, val, id, qid;
    bool operator < (const Ques &com) const {
        if(x != com.x) return x < com.x;
        if(opt != com.opt) return opt < com.opt;
        return y < com.y;
    }
} q[N], tmp[N];

long long t[N];
inline void Updata(int x, int w) {
    for(; x <= n; x += x & -x) t[x] += w;
}

inline long long Query(int x) {
    long long sum = 0;
    for(; x; x -= x & -x) sum += t[x];
    return sum;
}

long long ans[N];
inline void CDQ(int l, int r) {
    if(l == r) return;
    int mid = (l + r) >> 1;
    CDQ(l, mid), CDQ(mid + 1, r);
    int i = l, j = mid + 1, k = l;
    while(i <= mid || j <= r){
        if(j > r || (i <= mid && q[i] < q[j])){
            if(q[i].opt == 1) Updata(q[i].y, q[i].val);
            tmp[k++] = q[i++];
        }
        else{
            if(q[j].opt == 2) ans[q[j].qid] += 1ll * q[j].val * Query(q[j].y);
            tmp[k++] = q[j++];
        }
    }
    R(i,l,r) if(q[i].id <= mid && q[i].opt == 1) Updata(q[i].y, -q[i].val);
    R(i,l,r) q[i] = tmp[i];
} 

int main() {
//FileOpen();
//FileSave();
    io >> n;
    
    while(1){
        int opt;
        io >> opt;
        if(opt == 1){
            int x, y, val;
            io >> x >> y >> val;
            q[++tim] = (Ques){ opt, x, y, val, tim};
        }
        else if(opt == 2){
            int X1, X2, Y1, Y2;
            io >> X1 >> Y1 >> X2 >> Y2;
            ++qid;
            q[++tim] = (Ques){ opt, X1 - 1, Y1 - 1, 1, tim, qid};
            q[++tim] = (Ques){ opt, X1 - 1, Y2, -1, tim, qid};
            q[++tim] = (Ques){ opt, X2, Y1 - 1, -1, tim, qid};
            q[++tim] = (Ques){ opt, X2, Y2, 1, tim, qid};
        }
        else{
            break;
        }
    }
    
    CDQ(1, tim);
    
    R(i,1,qid){
        printf("%lld\n", ans[i]);
    }
    
    return 0;
} 

标签:二分,总结,int,qid,tim,Ques,opt,define
来源: https://www.cnblogs.com/bingoyes/p/11681721.html