总结
tiko_vency · · 个人记录
下一次我再也不背代码,而是要把代码弄懂。
ST表我是几乎是弄懂了,但是树状数组我是纯靠背,其实没有记住。
接下来说题。
第一题(P3865)是ST表模板,我也做对了,无需多讲。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+10;
int n,m,a[N],lg[N],dp[N][20],l,r;
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m;
lg[0]=-1;
for(int i=1;i<=n;i++){
cin>>a[i];
dp[i][0]=a[i];
lg[i]=lg[i/2]+1;
}
for(int j=1;(1<<j)<=n;j++)
for(int i=1;i+(1<<j)-1<=n;i++)
dp[i][j]=max(dp[i][j-1],dp[i+(1<<j-1)][j-1]);
while(m--){
cin>>l>>r;
int j=lg[r-l+1];
cout<<max(dp[l][j],dp[r-(1<<j)+1][j])<<"\n";
}
return 0;
}
第二题(P3374)是树状数组的模板,但是我是靠背的,没有真正理解,三个函数我都会写,就是主函数部分有点搞不清。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+10;
int n,m,k,c[N],op,x,y;
int lowbit(int x){
return x&-x;
}
int get_sum(int x){
int res=0;
while(x){
res+=c[x];
x-=lowbit(x);
}
return res;
}
void modify(int x,int val){
while(x<=n){
c[x]+=val;
x+=lowbit(x);
}
return;
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>k;
modify(i,k);//算前缀和
}
while(m--){
cin>>op>>x>>y;
if(op==1)
modify(x,y);//将第x个数字加上y
else
cout<<get_sum(y)-get_sum(x-1)<<"\n";//输出x~y的和
}
return 0;
}
第三题(P2412)其实和第一题差不多,只是要给max特判,不用区分大小写。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+10;
int n,m,lg[N],l,r;
string a[N],dp[N][20];
string MAX(string a,string b){
string c=a,d=b;
for(int i=0;i<a.size();i++)
if(a[i]<'a')
a[i]+=32;
for(int i=0;i<b.size();i++)
if(b[i]<'a')
b[i]+=32;
if(a>b)
return c;
else
return d;
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m;
lg[0]=-1;
for(int i=1;i<=n;i++){
cin>>a[i];
dp[i][0]=a[i];
lg[i]=lg[i/2]+1;
}
for(int j=1;(1<<j)<=n;j++)
for(int i=1;i+(1<<j)-1<=n;i++)
dp[i][j]=MAX(dp[i][j-1],dp[i+(1<<j-1)][j-1]);
while(m--){
cin>>l>>r;
int j=lg[r-l+1];
cout<<MAX(dp[l][j],dp[r-(1<<j)+1][j])<<"\n";
}
return 0;
}
第四题(P5149)也是树状数组,难度要高一些。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+10;
int n,c[N],ans,idx,x;
string a[N];
map<string,int>ma;
int lowbit(int x){
return x&-x;
}
int get_sum(int x){
int res=0;
while(x){
res+=c[x];
x-=lowbit(x);
}
return res;
}
void modify(int x,int val){
while(x<=n){
c[x]+=val;
x+=lowbit(x);
}
return;
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];//输入原来位置
ma[a[i]]=++x;
}
for(int i=1;i<=n;i++)
cin>>a[i];//输入现在位置
for(int i=n;i>=1;i--){
int y=ma[a[i]];
ans+=get_sum(ma[a[i]]-1);//统计不满值
modify(ma[a[i]],1);
}
cout<<ans;
return 0;
}
第五题(P1714)是ST表,统计最大幸运值。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=5e5+10;
int n,m,a[N],dp[N][20],lg[N],s[N],minn,maxx;
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m;
lg[0]=-1;
for(int i=1;i<=n;i++){
cin>>a[i];
s[i]=s[i-1]+a[i],dp[i][0]=s[i],lg[i]=lg[i/2]+1;
}
for(int j=1;(1<<j)<=n;j++)
for(int i=1;i+(1<<j)-1<=n;i++)
dp[i][j]=min(dp[i][j-1],dp[i+(1<<j-1)][j-1]);
for(int r=1;r<=n;r++){
int l=max(0ll,r-m),j=lg[r-l+1];
minn=min(dp[l][j],dp[r-(1<<j)+1][j]);//算最小值
maxx=max(maxx,s[r]-minn);//求最大幸运值
}
if(maxx==0)
cout<<-1;
else
cout<<maxx;
return 0;
}
第六题(P4378)用树状数组做。
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+10;
int n,a[N],b[N],c[N],idx,x,maxx;
map<int,int>mp;
int lowbit(int x){
return x&-x;
}
int get_sum(int x){
int res=0;
while(x){
res+=c[x];
x-=lowbit(x);
}
return res;
}
void modify(int x,int val){
while(x<=n){
c[x]+=val;
x+=lowbit(x);
}
return;
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
b[i]=a[i];
}
sort(b+1,b+n+1);
idx=unique(b+1,b+n+1)-b-1;
for(int i=1;i<=n;i++){
x=lower_bound(b+1,b+idx+1,a[i])-b;
maxx=max(maxx,get_sum(idx)-get_sum(x));//求“moo”的最大值
modify(x,1);
}
cout<<maxx+1;
return 0;
}
好吧,我还有继续努力。