ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

总结-二分

2019-10-15 23:01:31  阅读:246  来源: 互联网

标签:二分 总结 int qid tim Ques opt define


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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有