zoukankan      html  css  js  c++  java
  • 离散化

    离散化:离散化就是把大范围的数,映射到一个小范围,即把无限空间中的有限个体,映射到有限空间中去,以此来提高算法的时空效率。

    例题:求区间和

    假定有一个无限长的数轴,数轴上每个坐标上的数都是0。

    现在,我们首先进行 n 次操作,每次操作将某一位置x上的数加c。

    接下来,进行 m 次询问,每个询问包含两个整数l和r,你需要求出在区间[l, r]之间的所有数的和。

    输入格式
    第一行包含两个整数n和m。

    接下来 n 行,每行包含两个整数x和c。

    再接下里 m 行,每行包含两个整数l和r。

    输出格式
    共m行,每行输出一个询问中所求的区间内数字和。

    数据范围
    −10^9 ≤ x ≤ 10^9,
    1 ≤ n,m ≤ 10^5,
    −10^9 ≤ l ≤ r ≤ 10^9,
    −10000 ≤ c ≤ 10000
    输入样例:
    3 3
    1 2
    3 6
    7 5
    1 3
    4 6
    7 8
    输出样例:
    8
    0
    5

    思路:看到这道题想到的肯定是利用前缀和进行计算,但是如果直接使用前缀和的话我们需要开一个数量级为1e9的数组,这显然不太可能,而发现n与m最多就访问1e5次,所以我们用的空间最多数量级也就1e5,所以 n m x 一共用到的空间是3e5就足够了,因此我们就把用到的数都通过离散化映射到小范围中去,再使用前缀和。

    那么离散化的方法:首先把所有要离散化的数据都 push_back()all 数组中去,然后先排序,再去重,再利用二分来离散化。

    排序:

    sort(alls.begin(), alls.end());
    

    去重:运用erase,unique来去重(经典方法)

    alls.erase(unique(alls.begin(), alls.end()), alls.end());
    

    unique的去重原理是将所用不重复的元素放到前边,将重复的元素放到数组后边,最后将迭代器指向最后不重复下标并返回,所以这个操作完美去重。

    离散化:即运用二分,也可直接用 low_bound ,把数插入到a数组中去。

    二分写法:因为是用到前缀和,所以我们的下标从1开始,返回的是r+1

    int find(int x)
    {
        int l = 0, r = alls.size()-1;
        while(l < r)
        {
            int mid = l+r >> 1; 
            if(alls[mid] >= x) r = mid;  //依据的是原先数字在alls数组中的相对位置
            else l = mid+1;
        }
    
        return r+1;
    }
    

    low_bound() 写法:

    int find(int x) //lower_bound二分写法
    {
        return lower_bound(alls.begin(), alls.end(), x) - alls.begin() + 1;
    }
    

    总代码

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    using namespace std;
    
    typedef pair<int, int> PII;
    
    const int N = 300010;
    int n, m;
    int a[N], s[N];  //a存放离散化后的序列
    
    vector<int> alls;  //要散化的所有数据
    vector<PII> add, query;  //add是存x+c的  query用来存区间两边
    
    int find(int x)
    {
        int l = 0, r = alls.size()-1;
        while(l < r)
        {
            int mid = l+r >> 1; 
            if(alls[mid] >= x) r = mid;  //依据的是原先数字在alls数组中的相对位置
            else l = mid+1;
        }
    
        return r+1;
    }
    
    int main()
    {
        cin >> n >> m;
        for(int i = 0; i < n; i++)
        {
            int x, c;
            cin >> x >> c;
            add.push_back({x, c});  //在某个数上加c
    
            alls.push_back(x);
        }
    
        for(int i = 0; i < m; i++)
        {
            int l, r;
            cin >> l >> r;
            query.push_back({l, r});
    
            alls.push_back(l);
            alls.push_back(r);
        }
    
        //去重
        sort(alls.begin(), alls.end());
        alls.erase(unique(alls.begin(), alls.end()), alls.end());
    
        //处理插入
        for(auto item : add)
        {
            int x = find(item.first);
            a[x] += item.second;
        }
    
        //预处理前缀和
        for(int i = 1; i <= alls.size(); i++) s[i] = s[i-1] + a[i];
    
        //处理询问
        for(auto item : query)
        {
            int l = find(item.first), r = find(item.second);
            cout << s[r] - s[l-1] << endl;
        }
        system("pause");
        return 0;
    }
    
  • 相关阅读:
    [POJ3635]Full Tank? 题解
    洪水题解
    [HNOI2009]最小圈 题解
    Grazing on the Run 题解
    [BZOJ4237]稻草人 题解
    [POJ3783]Balls 题解
    [POI2005]Bank notes 题解
    字符串题解
    pyinstaller利用spec文件打包的使用模板
    Pycharm2020 永久激活
  • 原文地址:https://www.cnblogs.com/ZhengLijie/p/13406242.html
Copyright © 2011-2022 走看看