7.23 完成2题
| 题目 | 独立/看题解 | 是否理解 |
|---|---|---|
| P1886滑动窗口 | 看题解 | 完全理解 |
| P2580 于是他错误的点名开始了 | 独立 | 完全理解 |
源码
P1886滑动窗口
#include<bits/stdc++.h>
using namespace std;
struct po{
int num,val;
};
deque<po> big;
deque<po> small;
int s[2][1000005];
int main(){
int n,k,cnt=0;
cin>>n>>k;
int x;
po rr;
for(int i=1;i<=n;++i){
cin>>x;
rr.num=i;
rr.val=x;
while(!big.empty()&&x>=big.back().val)
big.pop_back();
while(!small.empty()&&x<=small.back().val)
small.pop_back();
big.push_back(rr);
small.push_back(rr);
while(i-k>=big.front().num)
big.pop_front();
while(i-k>=small.front().num)
small.pop_front();
if(i>=k)
{
s[0][cnt]=big.front().val;
s[1][cnt]=small.front().val;
cnt++;
}
}
for(int i=0;i<cnt;i++){
cout<<s[1][i]<<' ';
}
cout<<endl;
for(int i=0;i<cnt;i++){
cout<<s[0][i]<<' ';
}
cout<<endl;
return 0;
}P2580 于是他错误的点名开始了
#include<bits/stdc++.h>
using namespace std;
int n,m;
string tname[10005];
int tnamen[10005];
string x;
int find(string x){
int start=0,endd=n;
while(endd-start>1){
int mid=(start+endd)/2;
if(tname[mid]>x){
endd=mid;
}
else if(tname[mid]<x){
start=mid;
}
else{
return mid;
}
}
if(tname[start]==x){
return start;
}
return -1;
}
int main(){
cin>>n;
for(int i=0;i<n;++i){
cin>>tname[i];
}
sort(tname,tname+n);
cin>>m;
for(int i=0;i<m;++i){
cin>>x;
int isfind=find(x);
if(isfind==-1){
cout<<"WRONG"<<endl;
}
else if(tnamen[isfind]>0){
cout<<"REPEAT"<<endl;
}
else{
cout<<"OK"<<endl;
tnamen[isfind]++;
}
}
return 0;
}7.24 完成2题
| 题目 | 独立/看题解 | 是否理解 |
|---|---|---|
| P3372 线段树 | 独立 | 完全理解 |
| P2273 线段树进阶 | 部分看题解 | 完全理解 |
源码
P3372 线段树
#include<iostream>
using namespace std;
const int maxn=100005;
int n,m;
int a[maxn];
struct treel{
int l,r;
long long num,add=0;
};
treel t[maxn*4+10];
void build(int l,int r,int x){
t[x].l=l;t[x].r=r;
if(r==l){
t[x].num=a[l];
return ;
}
else{
int mid=(l+r)/2;
build(l,mid,x*2);
build(mid+1,r,x*2+1);
t[x].num=t[x*2].num+t[x*2+1].num;
}
}
void addpress(int x){
if(t[x].add!=0){
t[x*2].num+=t[x].add*(t[x*2].r-t[x*2].l+1);
t[x*2+1].num+=t[x].add*(t[x*2+1].r-t[x*2+1].l+1);
t[x*2].add+=t[x].add;
t[x*2+1].add+=t[x].add;
t[x].add=0;
}
}
void add(int q,int l,int r,int x){
if(t[x].l>=l && t[x].r<=r){
t[x].num+=(long long)q*(t[x].r-t[x].l+1);
t[x].add+=q;
return ;
}
addpress(x);
if(t[x*2].r>=l){
add(q,l,r,x*2);
}
if(t[x*2+1].l<=r){
add(q,l,r,x*2+1);
}
t[x].num=t[x*2].num+t[x*2+1].num;
}
long long check(int l,int r,int x){
if(l<=t[x].l && t[x].r<=r){
return t[x].num;
}
addpress(x);
long long xs=0;
if(t[x*2].r>=l){
xs+=check(l,r,x*2);
}
if(t[x*2+1].l<=r){
xs+=check(l,r,x*2+1);
}
return xs;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
int k,l,r,q;
build(1,n,1);
for(int i=0;i<m;i++){
cin>>k>>l>>r;
if(k==1){
cin>>q;
add(q,l,r,1);
}
else if(k==2){
cout<<check(l,r,1)<<endl;
}
}
return 0;
}P2273 线段树进阶
#include<iostream>
using namespace std;
const int maxn=100005;
int n,m,p;
int a[maxn];
struct treel{
int l,r;
long long num,add=0,time=1;
};
treel t[maxn*4+10];
void build(int l,int r,int x){
t[x].l=l;t[x].r=r;
if(r==l){
t[x].num=a[l];
return ;
}
else{
int mid=(l+r)/2;
build(l,mid,x*2);
build(mid+1,r,x*2+1);
t[x].num=t[x*2].num+t[x*2+1].num;
}
}
void addpress(int x){
//if(t[x].add!=0){
t[x*2].num=(t[x*2].num*t[x].time+t[x].add*(t[x*2].r-t[x*2].l+1))%p;
t[x*2+1].num=(t[x*2+1].num*t[x].time+t[x].add*(t[x*2+1].r-t[x*2+1].l+1))%p;
t[x*2].add=(t[x*2].add*t[x].time+t[x].add)%p;
t[x*2+1].add=(t[x*2+1].add*t[x].time+t[x].add)%p;
t[x*2+1].time=(t[x*2+1].time*t[x].time)%p;
t[x*2].time=(t[x*2].time*t[x].time)%p;
t[x].add=0;
t[x].time=1;
//}
}
void add(int q,int l,int r,int x){
if(t[x].l>=l && t[x].r<=r){
t[x].num=(t[x].num+(long long)q*(t[x].r-t[x].l+1))%p;
t[x].add=(t[x].add+q)%p;
return ;
}
addpress(x);
if(t[x*2].r>=l){
add(q,l,r,x*2);
}
if(t[x*2+1].l<=r){
add(q,l,r,x*2+1);
}
t[x].num=(t[x*2].num+t[x*2+1].num)%p;
}
void time(int q,int l,int r,int x){
if(t[x].l>=l && t[x].r<=r){
t[x].num=(t[x].num*q)%p;
t[x].time=(t[x].time*q)%p;
t[x].add=(t[x].add*q)%p;
return ;
}
addpress(x);
if(t[x*2].r>=l){
time(q,l,r,x*2);
}
if(t[x*2+1].l<=r){
time(q,l,r,x*2+1);
}
t[x].num=(t[x*2].num+t[x*2+1].num)%p;
}
long long check(int l,int r,int x){
if(l<=t[x].l && t[x].r<=r){
return t[x].num;
}
addpress(x);
long long xs=0;
if(t[x*2].r>=l){
xs=(check(l,r,x*2)+xs)%p;
}
if(t[x*2+1].l<=r){
xs=(xs+check(l,r,x*2+1))%p;
}
return xs;
}
int main(){
cin>>n>>m>>p;
for(int i=1;i<=n;i++){
cin>>a[i];
}
int k,l,r,q;
build(1,n,1);
for(int i=0;i<m;i++){
cin>>k>>l>>r;
if(k==1){
cin>>q;
time(q,l,r,1);
}
else if(k==2){
cin>>q;
add(q,l,r,1);
}
else{
cout<<check(l,r,1)<<endl;
}
}
return 0;
}7.25完成2题
| 题目 | 独立/看题解 | 是否理解 |
|---|---|---|
| P3374 树状数组1 | 独立 | 完全理解 |
| P3368 【模板】树状数组 2 | 独立 | 完全理解 |
注:虽然题目为树状数组,但是用的是线段树解。
源代码:
树状数组1
#include<iostream>
using namespace std;
const int maxn=1000005;
int n,m;
int a[maxn];
struct treel{
int l,r;
long long num,add=0;
};
treel t[maxn*4+10];
void build(int l,int r,int x){
t[x].l=l;t[x].r=r;
if(r==l){
t[x].num=a[l];
return ;
}
else{
int mid=(l+r)/2;
build(l,mid,x*2);
build(mid+1,r,x*2+1);
t[x].num=t[x*2].num+t[x*2+1].num;
}
}
void addpress(int x){
if(t[x].add!=0){
t[x*2].num+=t[x].add*(t[x*2].r-t[x*2].l+1);
t[x*2+1].num+=t[x].add*(t[x*2+1].r-t[x*2+1].l+1);
t[x*2].add+=t[x].add;
t[x*2+1].add+=t[x].add;
t[x].add=0;
}
}
void add(int q,int l,int r,int x){
if(t[x].l>=l && t[x].r<=r){
t[x].num+=(long long)q*(t[x].r-t[x].l+1);
t[x].add+=q;
return ;
}
addpress(x);
if(t[x*2].r>=l){
add(q,l,r,x*2);
}
if(t[x*2+1].l<=r){
add(q,l,r,x*2+1);
}
t[x].num=t[x*2].num+t[x*2+1].num;
}
long long check(int l,int r,int x){
if(l<=t[x].l && t[x].r<=r){
return t[x].num;
}
addpress(x);
long long xs=0;
if(t[x*2].r>=l){
xs+=check(l,r,x*2);
}
if(t[x*2+1].l<=r){
xs+=check(l,r,x*2+1);
}
return xs;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
int k,l,r,q;
build(1,n,1);
for(int i=0;i<m;i++){
cin>>k;
if(k==1){
cin>>l>>q;
add(q,l,l,1);
}
else if(k==2){
cin>>l>>r;
cout<<check(l,r,1)<<endl;
}
}
return 0;
}树状数组2
#include<iostream>
using namespace std;
const int maxn=1000005;
int n,m;
int a[maxn];
struct treel{
int l,r;
long long num,add=0;
};
treel t[maxn*4+10];
void build(int l,int r,int x){
t[x].l=l;t[x].r=r;
if(r==l){
t[x].num=a[l];
return ;
}
else{
int mid=(l+r)/2;
build(l,mid,x*2);
build(mid+1,r,x*2+1);
t[x].num=t[x*2].num+t[x*2+1].num;
}
}
void addpress(int x){
if(t[x].add!=0){
t[x*2].num+=t[x].add*(t[x*2].r-t[x*2].l+1);
t[x*2+1].num+=t[x].add*(t[x*2+1].r-t[x*2+1].l+1);
t[x*2].add+=t[x].add;
t[x*2+1].add+=t[x].add;
t[x].add=0;
}
}
void add(int q,int l,int r,int x){
if(t[x].l>=l && t[x].r<=r){
t[x].num+=(long long)q*(t[x].r-t[x].l+1);
t[x].add+=q;
return ;
}
addpress(x);
if(t[x*2].r>=l){
add(q,l,r,x*2);
}
if(t[x*2+1].l<=r){
add(q,l,r,x*2+1);
}
t[x].num=t[x*2].num+t[x*2+1].num;
}
long long check(int l,int r,int x){
if(l<=t[x].l && t[x].r<=r){
return t[x].num;
}
addpress(x);
long long xs=0;
if(t[x*2].r>=l){
xs+=check(l,r,x*2);
}
if(t[x*2+1].l<=r){
xs+=check(l,r,x*2+1);
}
return xs;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
int k,l,r,q;
build(1,n,1);
for(int i=0;i<m;i++){
cin>>k;
if(k==1){
cin>>l>>r>>q;
add(q,l,r,1);
}
else if(k==2){
cin>>l;
cout<<check(l,l,1)<<endl;
}
}
return 0;
}7.26完成2题
源代码:
小B的询问
#include<bits/stdc++.h>
using namespace std;
const int inf=50005;
struct side{
int l,r;
int i;
};
bool bi(side a,side b){
if(a.l==b.l){
return a.r<b.r;
}
return a.l<b.l;
}
int n,m,k;
int a[inf];
side sid[inf];
int b[inf];
long long c=0,ans[inf];
inline void add(int x){
c+=2*b[x]+1;
//b[x]+1对于平方和c的影响(小学数学)
b[x]++;
}
inline void del(int x){
c-=2*b[x]-1;//同上
b[x]--;
}
int main(){
cin>>n>>m>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=m;i++){
cin>>sid[i].l>>sid[i].r;
sid[i].i=i;
}
sort(sid+1,sid+1+m,bi);
int lstl=1,lstr=0;
int nowl,nowr;
for(int i=1;i<=m;++i){
nowl=sid[i].l;
nowr=sid[i].r;
//cout<<nowl<<' '<<nowr<<endl;
while(lstl<nowl){
del(a[lstl]);lstl++;
}
while(lstr<nowr){
lstr++;add(a[lstr]);
}
while(lstr>nowr){
del(a[lstr]);lstr--;
}
ans[sid[i].i]=c;
}
for(int i=1;i<=m;i++){
cout<<ans[i]<<endl;
}
return 0;
}ST表
#include<bits/stdc++.h>
using namespace std;
int n,m;
int main(){
cin>>n>>m;
int f[n+2][20];
for(int i=1;i<=n;i++){
cin>>f[i][0];
}
for(int j=1;(1<<j)<=n;j++){
for(int i=1;i<=n-(1<<j)+1;i++){
f[i][j]=max(f[i][j-1],f[i+(1<<j-1)][j-1]);
}
}
int l,r,s;
for(int i=0;i<m;i++){
scanf("%d %d",&l,&r);
s=log2(r-l+1);
printf("%d\n",max(f[l][s],f[r-(1<<s)+1][s]));
}
return 0;
}