2025.07.11课堂总结
重点一
复习了前缀与前缀和有关的知识点。
定义:“前缀”指的是从数组的起始位置到当前元素的连续子序列。“前缀和”指的是从数组(或序列)的起始位置到当前元素的连续子序列的和。
作用:求区间和。
方法:准备一数组, 循环 次,第 次的值为 和 的和。
公式:
例题(P8218 【深进1.例1】求区间和):
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
const int N = 1e5 + 5;
int tmp, a[N], l, r, m, n;
int main(){
ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++){
cin >> tmp;
a[i] = a[i - 1] + tmp;
}
cin >> m;
for(int i = 1; i <= m; i++){
cin >> l >> r;
cout << a[r] - a[l - 1]<<endl;
}
return 0;
}重点二
讲了二分答案的概念,即将满足题目条件答案变成一,问区间有多少满足条件的。实际上就是统计区间内有多少个一。
前缀答案
1、将判断条件提前用0或者1表示,然后查询的时候,可以采用区间和判断是否出现过
2、前缀答案(不带修改)
所有的查询问题
1离线查询(前缀和)
只能查询,稍稍一点点修改,会导致时间复杂度猛然提升
2伪在线查询(差分)
修改多次,只查询1次
总分,语文+3-5-7+6
3在线查询(树状数组,线段树)
边修改,边查询(提高组)
例题(U427199书柜):
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
using namespace std;
int n, t;
int h_A, h_B;
string s;
const int N = 5e6 + 7;
int ans[N], pre[N];
void solve1(){
while(t--){
int l, r;
cin >> l >> r;
cout << 1 << endl;
}
return;
}
void solve2(){
ans[0] = -1;
for(int i = 1; i <= n; i++){
pre[i] = pre[i - 1] + (ans[i - 1] == 0 && ans[i] == 1);
}
while(t--){
int l, r;
cin >> l >> r;
if(pre[l] == pre[r]) cout << 1 << endl;
else cout << 2 << endl;
}
return ;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>t>>h_A>>h_B;
cin>>s;
if(h_A==h_B) solve1();
else{
int len=s.size();
for(int i=0;i<len;i++){
if(s[i]=='A') ans[i+1]=(h_A>h_B);
if(s[i]=='B') ans[i+1]=(h_B>h_A);
}
solve2();
}
return 0;
}重点三
介绍了如vjudge.net,codeforces.com等网站,介绍其功能并协助注册,(临时邮箱太垃圾了好吧)还说我们以后会经常用到的。