码迷,mamicode.com
首页 > 其他好文 > 详细

[Luogu] 贪婪大陆

时间:2018-02-07 22:42:27      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:fine   uml   mes   bsp   log   class   str   main   show   

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;
}

 

[Luogu] 贪婪大陆

标签:fine   uml   mes   bsp   log   class   str   main   show   

原文地址:https://www.cnblogs.com/shandongs1/p/8428393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!