[Luogu] 贪婪大陆

https://www.luogu.org/problemnew/show/P2184

区间修改时只需修改区间端点的numl或numr值
区间查询x-y只需用1-y的numr - 1-(x - 1)的numl值
线段树 单点修改 + 区间查询

 

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;
const int N = 1e5 + 10;

#define yxy getchar()
#define lson jd << 1
#define rson jd << 1 | 1

#define RR freopen("gg.in", "r", stdin)

int n, Ty, Ans, Ans1, Ans2;
int Numl[N << 2], Numr[N << 2];

inline int read() {
    int x = 0; char c = yxy;
    while(c < '0' || c > '9') c = yxy;
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = yxy;
    return x;
} 

void Poi_G(int l, int r, int jd, int x, int how) {
    if(l == r) {
        if(!how) Numl[jd] ++;
        else Numr[jd] ++;
        return ; 
    }
    int mid = (l + r) >> 1;
    if(x <= mid) Poi_G(l, mid, lson, x, how);
    else Poi_G(mid + 1, r, rson, x, how);
    Numl[jd] = Numl[lson] + Numl[rson];
    Numr[jd] = Numr[lson] + Numr[rson];
}

void Sec_A(int l, int r, int jd, int x, int y, int how) {
    if(x <= l && r <= y) {
        if(!how) Ans += Numl[jd];
        else Ans += Numr[jd];
        return ;
    }
    int mid = (l + r) >> 1;
    if(x <= mid) Sec_A(l, mid, lson, x, y, how);
    if(y > mid)  Sec_A(mid + 1, r, rson, x, y, how); 
} 

int main() {
    n = read();
    Ty = read();
    while(Ty --) {
        int opt = read(), x = read(), y = read();
        if(opt == 1) {
            Poi_G(1, n, 1, x, 0);
            Poi_G(1, n, 1, y, 1);
        } else {
            Ans = 0;
            Sec_A(1, n, 1, 1, y, 0); Ans1 = Ans; Ans = 0;
            Sec_A(1, n, 1, 1, x - 1, 1); Ans2 = Ans;
            cout << Ans1 - Ans2 << "\n"; 
        } 
    }
    return 0;
}

 

posted @ 2018-02-07 20:47  xayata  阅读(245)  评论(0编辑  收藏  举报