# 前缀

#### 输出格式

#include <stdio.h>
const int N = 1e5 + 1;
int a[N];
int main() {
int n, m, l, r;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
int t;
scanf("%d", &t);
a[i] = a[i - 1] + t;
}
for (int i = 0; i < m; i++) {
scanf("%d%d", &l, &r);
printf("%d\n", a[r] - a[l - 1]);
}
}


#### 输出格式

#include <iostream>
using namespace std;
const int N = 1111;
int A[N][N], B[N][N];
int main() {
int n, m, q;
cin >> n >> m >> q;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &A[i][j]);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
B[i][j] = B[i - 1][j] + B[i][j - 1] - B[i - 1][j - 1] + A[i][j];
}
}
for (int i = 0; i < q; i++) {
int x1, x2, y1, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
printf("%d\n", B[x2][y2] - B[x1 - 1][y2] - B[x2][y1 - 1] + B[x1 - 1][y1 - 1]);
}
}


# 差分

#### 输出格式

#include <stdio.h>
int a[100001],b[100001];
void cf(int l, int r, int c) {
b[l] += c;
b[r + 1] -= c;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) cf(i, i, a[i]);
while (m--) {
int l, r, c;
scanf("%d%d%d", &l, &r, &c);
cf(l, r, c);
}
//对b[i]进行累加就能得到变化后的序列
for (int i = 1; i <= n; i++) b[i] += b[i - 1];
for (int i = 1; i <= n; i++) printf("%d ",b[i]);
}



#### 输出格式

b(x1,y1) +=c ; 对应图1 ,让整个a数组中蓝色矩形面积的元素都加上了c。 b(x1,y2+1)-=c ; 对应图2 ,让整个a数组中绿色矩形面积的元素再减去c，使其内元素不发生改变。
b(x2+1,y1)- =c ; 对应图3 ,让整个a数组中紫色矩形面积的元素再减去c，使其内元素不发生改变。 b(x2+1,y2+1)+=c; 对应图4,让整个a数组中红色矩形面积的元素再加上c，红色内的相当于被减了两次，再加上一次c，才能使其恢复。

#include <stdio.h>
const int N = 1111;
int a[N][N], b[N][N];
void cf(int x1, int y1, int x2, int y2 ,int c) {
b[x1][y1] += c;
b[x1][y2 + 1] -= c;
b[x2 + 1][y1] -= c;
b[x2 + 1][y2 + 1] += c;
}
int main() {
int n, m,q;
scanf("%d%d%d", &n, &m, &q);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
scanf("%d", &a[i][j]);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cf(i, j, i, j, a[i][j]);
}
}
while (q--) {
int x1, y1, x2, y2, c;
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &c);
cf(x1, y1, x2, y2, c);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
b[i][j] += b[i][j - 1] + b[i - 1][j] - b[i - 1][j - 1];
}
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++) printf("%d ", b[i][j]);
puts("");
}

}


# 离散化

#### 输出格式

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int N = 3e5 + 10;
int a[N], s[N];
int n, m;
typedef pair<int, int >PII;
vector<int>alls;//存的是序列的值，序列值离散化映射后对应的数是其值在数组a的下标
//二分查找,查找大于等于当前数的最小值映射对应的数
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() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
int x, c;
cin >> x >> c;
alls.push_back(x);
}
for (int i = 0; i < m; i++) {
int l, r;
cin >> l >> r;
alls.push_back(l);
alls.push_back(r);
query.push_back({ l,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, r;
l = find(item.first), r = find(item.second);
printf("%d\n", s[r] - s[l - 1]);
}
}


# 区间合并

#### 输出格式

• 贪心
• 具体就是按着起点排序
• 如果前一个终点< 下一个起点。那么单独成为一个区间。更新 起点
• 如果前一个终点>= 下一个起点. max(end1,end2)。更新终点
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

typedef pair<int, int>PII;

void merge(vector<PII>& segs) {//使用引用可以将原数组传入函数，不用复制一份，节省时间。
vector<PII>res;
sort(segs.begin(), segs.end());
int st = -2e9, ed = -2e9;
for (auto seg : segs) {
if (ed < seg.first) {
if (st != -2e9) res.push_back({ st,ed });
st = seg.first, ed = seg.second;
}
else ed = max(ed, seg.second);
}
if (st != -2e9) res.push_back({ st,ed });//没有if(st != -2e9)的判断，输入数据为空区间时，会把(-2e9, -2e9) 插入res,上面的-2e9原理一样，防止插入的是空区间
segs = res;
}
int main() {
vector<PII>segs;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int l, r;
cin >> l >> r;
segs.push_back({ l,r });
}
merge(segs);
printf("%d", segs.size());
}

posted @ 2022-01-27 16:37  计科废物1  阅读(47)  评论(0编辑  收藏  举报
@media only screen and (max-width: 767px){ #sidebar_search_box input[type=text]{width:calc(100% - 24px)} } L2Dwidget.init({ "model": { jsonPath: "https://unpkg.com/live2d-widget-model-hijiki/assets/hijiki.model.json", "scale": 1 }, "display": { "position": "left", "width": 100, "height": 200, "hOffset": 70, "vOffset": 0 }, "mobile": { "show": true, "scale": 0.5 }, "react": { "opacityDefault": 0.7, "opacityOnHover": 0.2 } }); window.onload = function(){ \$("#live2dcanvas").attr("style","position: fixed; opacity: 0.7; left: 70px; bottom: 0px; z-index: 1; pointer-events: none;") }