hdu1556 分块水题

1 篇文章 0 订阅

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1556


中文题面就不说意思了,很明显用树状数组或者线段树都可以解决,现在使用分块做


判断一下a和b是不是在同一个块里就行了


#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <cstdio>
#include <time.h>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <math.h>
//#include <bits/stdc++.h>
#define ll long long
#define int64 long long
#define mem(x,y) memset(x,y,sizeof(x))
#define ull unsigned long long
#define pb push_back
#define INF 0x3f3f3f3f
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
#define pi 3.141592653589793238462
#define stdio std::ios::sync_with_stdio(false)

using namespace std;

inline bool nextDouble(double &num) {
    char in;
    double Dec = 0.1;
    bool IsN = false, IsD = false;
    in = getchar();
    if (in == EOF) return false;
    while (in != '-' && in != '.' && (in < '0' || in > '9'))
        in = getchar();
    if (in == '-') {
        IsN = true;
        num = 0;
    } else if (in == '.') {
        IsD = true;
        num = 0;
    } else num = in - '0';
    if (!IsD) {
        while (in = getchar(), in >= '0' && in <= '9') {
            num *= 10;
            num += in - '0';
        }
    }
    if (in != '.') {
        if (IsN) num = -num;
        return true;
    } else {
        while (in = getchar(), in >= '0' && in <= '9') {
            num += Dec * (in - '0');
            Dec *= 0.1;
        }
    }
    if (IsN) num = -num;
    return true;
}
inline bool nextInt(int &num) {
    char in;
    bool IsN = false;
    in = getchar();
    if (in == EOF) return false;
    while (in != '-' && (in < '0' || in > '9')) in = getchar();
    if (in == '-') {
        IsN = true;
        num = 0;
    } else num = in - '0';
    while (in = getchar(), in >= '0' && in <= '9') {
        num *= 10, num += in - '0';
    }
    if (IsN) num = -num;
    return true;
}
inline bool nextLong(ll &num) {
    char in;
    bool IsN = false;
    in = getchar();
    if (in == EOF) return false;
    while (in != '-' && (in < '0' || in > '9')) in = getchar();
    if (in == '-') {
        IsN = true;
        num = 0;
    } else num = in - '0';
    while (in = getchar(), in >= '0' && in <= '9') {
        num *= 10, num += in - '0';
    }
    if (IsN) num = -num;
    return true;
}
const int maxn = 1e5 + 5;
const int maxm = 1e3 + 5;
const int mod = 1e9 + 7;
const int seed = 10007;
const double eps = 1e-6;
int n, m;
int a[maxn], lazy[maxm];

int main() {
    stdio;
    while (cin >> n && n != 0) {
        int x, y;
        int block = static_cast<int>(sqrt(n) + eps);
        for (int i = 0; i < n; i++) a[i] = 0;
        for (int i = 0; i < n / block + 1; i++) lazy[i] = 0;
        for (int i = 0; i < n; i++) {
            cin >> x >> y;
            x--;
            y--;
            int xb = x / block;
            int yb = y / block;
            if (xb == yb) {
                for (int i = x; i <= y; i++) {
                    a[i]++;
                }
            } else {
                for (int i = x; i < (x / block + 1)*block; i++) {
                    a[i]++;
                }
                for (int i = x / block + 1; i < y / block; i++) {
                    lazy[i]++;
                }
                for (int i = y / block * block; i <= y; i++) {
                    a[i]++;
                }
            }
        }
        for (int i = 0; i < n; i++) {
            cout << a[i] + lazy[i / block];
            if (i == n - 1) cout << endl;
            else cout << " ";
        }
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值