概念
離散化,把無限空間中有限的個體映射到有限的空間中去,以此提高算法的時空效率。
通俗的說,離散化是在不改變數據相對大小的條件下,對數據進行相應的縮小。
步驟:
1.將所有需要離散化的數據(如下面例子中的下標)放到一個容器中(以下使用vector,當然可以用數組代替);
2.排序,去重(可以手寫,也可以用STL的algorithm庫中的unique函數);
3.要查詢原數據在容器中的位置只需在容器中二分查找第一個大於等於該數據的數的位置即可。
舉例
模板題鏈接:區間和
代碼如下:
#include <iostream> #include <algorithm> #include <vector> using namespace std; const int N = 100010; vector<int> alls; int n, m; int x[N], c[N], l[N], r[N]; int a[N * 3], s[N * 3]; // 找到x在alls中的下標 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; else l = mid + 1; } return r + 1; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; i ++ ) { scanf("%d%d", &x[i], &c[i]); alls.push_back(x[i]); } for (int i = 0; i < m; i ++ ) { scanf("%d%d", &l[i], &r[i]); alls.push_back(l[i]), alls.push_back(r[i]); } // 排序 + 判重 sort(alls.begin(), alls.end()); alls.erase(unique(alls.begin(), alls.end()), alls.end()); // 插入操作 for (int i = 0; i < n; i ++ ) a[find(x[i])] += c[i]; // 預處理前綴和 for (int i = 1; i <= alls.size(); i ++ ) s[i] = s[i - 1] + a[i]; // 查詢操作 for (int i = 0; i < m; i ++ ) printf("%d\n", s[find(r[i])] - s[find(l[i]) - 1]); return 0; }
推薦習題:趕牛入圈(POJ3179)
