云智第五节学习记录

· · 个人记录

第五节-单调栈,单调队列,哈希表

A 题:P5788 【模板】单调栈

板题,切了。

清朝代码如下:

#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
const int maxn=INT_MAX;
const double INF=DBL_MAX;
inline int read() {
    int x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9') {
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9') {
        x=x*10+ch-48;
        ch=getchar();
    }
    return x*f;
}
inline void write(int x){
    if(x>9)write(x/10);
    putchar('0'+(x%10));
}
signed main() {
    int n;
    n=read();
    int *a=new int[n+1],*b=new int[n+1];
    stack<int>s;
    rep(i,1,n)a[i]=read();
    s.push(1);
    rep(i,2,n) {
        while(!s.empty()&&a[s.top()]<a[i])b[s.top()]=i,s.pop();
        s.push(i);
    }
    rep(i,1,n)write(b[i]),putchar(' ');
    return 0;
}

B 题:Imbalanced Array

题意转换:\sum_{i=1}^{n} \sum_{j=i}^{n} (\max_{i\le k\le j} a_k-\min_{i\le k\le j} a_k)
就是把所有连续子段的最大值减去所有连续子段的最小值,因此可以拆开来统计。对于每一个 a_i,可以计算出以 a_i 为最大值/最小值的最大区间,然后在最后统计时拿这些区间长度乘上 a_i 就是这些区间的最终结果,最大值加上,最小值减去。对于求最大值/最小值的最大区间,可以用一个单调栈维护,时间复杂度为 O(n)。

代码如下:

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 2e6+5;
const int inf = INT_MAX;
inline int read() {
    int x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9') {
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9') {
        x=x*10+ch-48;
        ch=getchar();
    }
    return x*f;
}
void fast() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
int sta1[N],sta2[N],top1,top2,l[N],r[N],ls[N],rs[N];

signed main() {
    //fast();
    int n;
    in(n);
    int *a=new int[n+1];
    rep(i,1,n)in(a[i]);
    int ans=0,sum;
    rep(i,1,n){
        while(top1&&a[sta1[top1]]<a[i])r[sta1[top1--]]=i-1;
        while(top2&&a[sta2[top2]]>a[i])rs[sta2[top2--]]=i-1;
        sta1[++top1]=sta2[++top2]=i;
    }                               
    while(top1)r[sta1[top1--]]=n;
    while(top2)rs[sta2[top2--]]=n;
    deap(i,n,1){
        while(top1&&a[sta1[top1]]<=a[i])l[sta1[top1--]]=i+1;
        while(top2&&a[sta2[top2]]>=a[i])ls[sta2[top2--]]=i+1;
        sta1[++top1]=sta2[++top2]=i;
    }
    while(top1)l[sta1[top1--]]=1;
    while(top2)ls[sta2[top2--]]=1;
    rep(i,1,n){
        ans=ans+a[i]*(i-l[i]+1)*(r[i]-i+1);
        ans=ans-a[i]*(i-ls[i]+1)*(rs[i]-i+1);
    }
    cout<<ans;
    delete[]a; 
    return 0;
}

C 题:P2422 良好的感觉

这不和 B 题一样?求出以第 i 为最小值的区间范围,然后直接让这个范围内的和于之相乘即可。单调栈与前缀和能够将复杂度优化至 O(n)级别。

代码如下:

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 2e6+5;
const int inf = INT_MAX;
inline int read() {
    int x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9') {
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9') {
        x=x*10+ch-48;
        ch=getchar();
    }
    return x*f;
}
void fast() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
int sta[N],top,l[N],r[N],c[N];

signed main() {
    //fast();
    int n;
    in(n);
    int *a=new int[n+1];
    rep(i,1,n)in(a[i]),c[i]=c[i-1]+a[i];
    int ans=0,sum;
    rep(i,1,n){
        while(top&&a[sta[top]]>a[i])r[sta[top--]]=i-1;
        sta[++top]=i;
    }                               
    while(top)r[sta[top--]]=n;
    deap(i,n,1){
        while(top&&a[sta[top]]>=a[i])l[sta[top--]]=i+1;
        sta[++top]=i;
    }
    while(top)l[sta[top--]]=1;
    rep(i,1,n){
     ans=max(ans,(c[r[i]]-c[l[i]-1])*a[i]);   
    }
    cout<<ans;
    delete[]a; 
    return 0;
}

D 题:P4147 玉蟾宫

题目给出的矩阵内的若干个子矩阵,在满足同列的情况下,可以用单调栈维护和统计子矩阵最大面积,需要预处理每个数据的行左右边界,也就是子矩阵的宽,然后我们通过枚举每一列就可以得出答案。

代码如下:

#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define I_love_Foccarus return
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 1e3+5;
const int inf = INT_MAX;
inline int read() {
    int x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9') {
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9') {
        x=x*10+ch-48;
        ch=getchar();
    }
    return x*f;
}
void fast() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
int n,m,f[N][N],ans;
char a[N][N],b[N][N];
int s[N],h[N],top;
signed main() {
    fast();
    cin>>n>>m;
    rep(i,1,n)rep(j,1,m)cin>>a[i][j];
    rep(i,1,n)rep(j,1,m) if(a[i][j]=='F')f[i][j]=f[i][j-1]+1;
    rep(j,1,m) {
        top=1;
        s[top]=1;
        h[top]=f[1][j];
        int sum=0;
        rep(i,2,n) {
            int l=0;
            while(top&&h[top]>=f[i][j]) {
                l+=s[top];
                sum=max(sum,h[top]*l);
                top--;
            }
            s[++top]=l+1,h[top]=f[i][j];
        }
        int l=0;
        while(top) {
                l+=s[top];
                sum=max(sum,h[top]*l);
                top--;
        }
        ans=max(ans,sum);
    }
    cout<<ans*3;
    return 0;
}

E 题:P1578 奶牛浴场

虽然此题和 D 题很像,但不允许 O(WL) 的暴力复杂度,需要从障碍点入手,因为障碍点的数量满足 O(n^2) 的时间复杂度。
思路很简单,枚举每个障碍点作为矩阵的一点,维护其长宽的正确性,满足矩阵内不存在障碍点,对每个矩阵取 \max 就行了。对数据排好序,把原矩阵的四个顶点带上,然后直接爆力扫一遍就可以了。

代码如下:

#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define I_love_Foccarus return
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
#define fi first
#define se second
const int N = 3e4+5;
const int inf = INT_MAX;
inline int read() {
    int x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9') {
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9') {
        x=x*10+ch-48;
        ch=getchar();
    }
    return x*f;
}
void fast() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
int n,m,k,b[N],tot,ans;
pair<int,int> a[N];
int minx[N],maxx[N];
signed main() {
    in(n),in(m),in(k);
    rep(i,1,k) {
        int x,y;
        in(x),in(y);
        a[i]=make_pair(x,y);b[++tot]=y;
    }
    a[++k]=make_pair(0,m),a[++k]=make_pair(n,0),a[++k]=make_pair(0,0),a[++k]=make_pair(n,m);
    b[++tot]=0,b[++tot]=m;
    sort(a+1,a+k+1);
    sort(b+1,b+tot+1);
    tot=unique(b+1,b+tot+1)-b-1;
    rep(i,2,tot)ans=max(ans,n*(b[i]-b[i-1]));
    rep(i,1,k){
        int  maxx=0,minx=m,y;   
        rep(j,i+1,k){
            y=a[j].fi-a[i].fi;
            ans=max(ans,(minx-maxx)*y);
            if(a[j].se>a[i].se)minx=min(minx,a[j].se);
            else maxx=max(maxx,a[j].se);
        }
    }
    cout<<ans;
    return 0;
}

F 题:P3400 仓鼠窝

思路:通过单调栈求出每行每列的左右边界,维护高度的单调性,在最后统计左右边界与高度的积的和即可。
为啥单调栈求左右边界的题目这么多啊?

代码如下:

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 3e3+5;
const int inf = INT_MAX;
inline int read() {
    int x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9') {
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9') {
        x=x*10+ch-48;
        ch=getchar();
    }
    return x*f;
}
void fast() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
int a[N][N],h[N],top,sta[N],l[N],r[N];
signed main() {
    //fast();
    int n,m;
    in(n),in(m);
    rep(i,1,n)rep(j,1,m)in(a[i][j]);
    int ans=0,sum;
    rep(i,1,n) {
        rep(j,1,m)h[j]=a[i][j]==0?0:h[j]+1;
        rep(j,1,m) {
            while(top&&h[sta[top]]>h[j])r[sta[top--]]=j-1;
            sta[++top]=j;
        }
        while(top)r[sta[top--]]=m;
        deap(j,m,1) {
            while(top&&h[sta[top]]>=h[j])l[sta[top--]]=j+1;
            sta[++top]=j;
        }
        while(top)l[sta[top--]]=1;
        rep(j,1,m) {
            ans+=(j-l[j]+1)*(r[j]-j+1)*h[j];
        }
    }
    cout<<ans;
    return 0;
}

H 题:P1886 滑动窗口 /【模板】单调队列

板题,切了。

清朝代码如下:

#include<bits/stdc++.h>
using namespace std;
int n,k;
deque<int>q;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>k;
    int *a=new int[n+1];
    for(int i=1; i<=n; i++)cin>>a[i];
    for(int i=1; i<=n; i++) {
        while(!q.empty()&&a[q.back()]>a[i])q.pop_back();
        q.push_back(i);
        while(!q.empty()&&q.front()<=i-k)q.pop_front();
        if(i>=k)cout<<a[q.front()]<<' ';
    }
    while(!q.empty())q.pop_front();
    cout<<'\n';
    for(int i=1; i<=n; i++) {
        while(!q.empty()&&a[q.back()]<a[i])q.pop_back();
        q.push_back(i);
        while(!q.empty()&&q.front()<=i-k)q.pop_front();
        if(i>=k)cout<<a[q.front()]<<' ';
    }
}

I 题:P3088 [USACO13NOV] Crowded Cows S

单调队列维护区间最大值,从左往右枚举 i,如果 [x_i-D,x_i] 中的最大值是 h_i 的两倍,打一个 vis 标记。然后再从右往左枚举 i,如果 [x_i,x_i+D] 中的最大值是 h_i 的两倍,打再一个 vis 标记。然后进行统计,答案为打了两个标记的总数。

代码如下:

#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define I_love_Foccarus return
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 5e4+5;
const int inf = INT_MAX;
inline int read() {
    int x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9') {
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9') {
        x=x*10+ch-48;
        ch=getchar();
    }
    return x*f;
}
void fast() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
struct node {
    int x , h;
} a[N];
int n , d , ans , vis[N];
bool cmp(node x,node y) {
    return x.x < y.x;
}
deque<int>q;
signed main() {
    //fast();
    in(n),in(d);
    for(int i = 1 ; i <= n ; i++)in(a[i].x),in(a[i].h);
    sort(a + 1 , a + n + 1 , cmp);
    for(int i = 1 ; i <= n ; i++) {
        while(!q.empty() && a[q.front()].x + d < a[i].x)q.pop_front();
        while(!q.empty() && a[q.back()].h < a[i].h)q.pop_back();
        if(!q.empty() && a[q.front()].h >= a[i].h * 2) vis[i]++;
        q.push_back(i);
    }
    while(!q.empty())q.pop_back();
    for(int i = n ; i > 0 ; i--) {
        while(!q.empty() && a[q.front()].x - d > a[i].x)q.pop_front();
        while(!q.empty() && a[q.back()].h < a[i].h)q.pop_back();
        if(!q.empty() && a[q.front()].h >= a[i].h * 2) vis[i]++;
        q.push_back(i);
    }
    for(int i = 1 ; i <= n ; i++) ans += vis[i] == 2;
    cout<<ans;
    return 0;
}

J 题:P2629 好消息,坏消息

这题?不线段树吗?
维护一个长度为 2n 的区间前缀和,用两个指针 l,r 模拟一个长度为 n 的“滑动窗口”,然后强行模拟转换过程,区间右移时相当于删除原区间左边界的数 a_l,由于我们维护的是前缀和,所以需要区间修改,直接对整个区间 [l,r] 减去 a_l 即可,然后要拓展右边界,就是用 [l+1,r] 的和加上 a_{r-n+1},直接对 [r+1,r+1] 单点修改即可,然后直接求区间 [l,r] 的区间最小值,如果其大于 0,更新答案。

代码如下(TLE):

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 2e6+5;
const int inf = INT_MAX;
inline long long read() {
    long long x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9') {
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9') {
        x=x*10+ch-48;
        ch=getchar();
    }
    return x*f;
}
void fast() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
int c[N];
struct linetree{
    int cl,cr,ans,lazy;
};
vector<linetree>t;
int build(int l,int r){
    linetree tmp;
    if(l==r){
        tmp.cl=tmp.cr=-1;
        tmp.ans=c[l];
        tmp.lazy=0;
        t.push_back(tmp);
        return t.size()-1;
    }
    int mid=(l+r)>>1;
    tmp.cl=build(l,mid);
    tmp.cr=build(mid+1,r);
    tmp.ans=min(t[tmp.cl].ans,t[tmp.cr].ans);
    tmp.lazy=0;
    t.push_back(tmp);
    return t.size()-1;
}
void push_down(int idx){
    t[t[idx].cl].ans+=t[idx].lazy;
    t[t[idx].cr].ans+=t[idx].lazy;
    t[t[idx].cl].lazy+=t[idx].lazy;
    t[t[idx].cr].lazy+=t[idx].lazy;
    t[idx].lazy=0;
}
void update(int l,int r,int ql,int qr,int idx,int val){
    if(qr<l||r<ql)return;
    if(ql<=l&&r<=qr){
        t[idx].ans+=val;
        t[idx].lazy+=val;
        return;
    }
    int mid=(l+r)>>1;
    push_down(idx);
    update(l,mid,ql,qr,t[idx].cl,val);
    update(mid+1,r,ql,qr,t[idx].cr,val);
    t[idx].ans=min(t[t[idx].cl].ans,t[t[idx].cr].ans);
}
int query(int l,int r,int ql,int qr,int idx){
    if(qr<l||r<ql)return inf;
    if(ql<=l&&r<=qr){
        return t[idx].ans;
    }
    int mid=(l+r)>>1;
    push_down(idx);
    return min(query(l,mid,ql,qr,t[idx].cl),query(mid+1,r,ql,qr,t[idx].cr));
    t[idx].ans=min(t[t[idx].cl].ans,t[t[idx].cr].ans);
}
int a[N];
signed main() {
    //fast();
    int n,p=1,ans=0,sum;
    in(n);
    rep(i,1,n)in(a[i]);
    rep(i,1,n){
    c[i]=c[i-1]+a[i];
    }
    sum=c[n];
    int root=build(1,2*n);
    int l=1,r=n;
    while(l<=n){
    if(query(1,2*n,l,r,root)>=0)ans++;
    update(1,2*n,l,r,root,-a[l]);
    sum=sum-a[l]+a[r-n+1];
    l++,r++;    
    update(1,2*n,r,r,root,sum);
    }
    cout<<ans;
    return 0;
}

TLE?线段树被卡常了,因此需要优化。其实直接可以优化标记下溢的常数,如果不存在标记,则无需下溢,这样就可以通过了。

代码如下:

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 2e6+5;
const int inf = INT_MAX;
inline long long read() {
    long long x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9') {
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9') {
        x=x*10+ch-48;
        ch=getchar();
    }
    return x*f;
}
void fast() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
int c[N];
struct linetree{
    int cl,cr,ans,lazy;
};
vector<linetree>t;
int build(int l,int r){
    linetree tmp;
    if(l==r){
        tmp.cl=tmp.cr=-1;
        tmp.ans=c[l];
        tmp.lazy=0;
        t.push_back(tmp);
        return t.size()-1;
    }
    int mid=(l+r)>>1;
    tmp.cl=build(l,mid);
    tmp.cr=build(mid+1,r);
    tmp.ans=min(t[tmp.cl].ans,t[tmp.cr].ans);
    tmp.lazy=0;
    t.push_back(tmp);
    return t.size()-1;
}
void push_down(int idx){
    if(!t[idx].lazy)return;
    t[t[idx].cl].ans+=t[idx].lazy;
    t[t[idx].cr].ans+=t[idx].lazy;
    t[t[idx].cl].lazy+=t[idx].lazy;
    t[t[idx].cr].lazy+=t[idx].lazy;
    t[idx].lazy=0;
}
void update(int l,int r,int ql,int qr,int idx,int val){
    if(qr<l||r<ql)return;
    if(ql<=l&&r<=qr){
        t[idx].ans+=val;
        t[idx].lazy+=val;
        return;
    }
    int mid=(l+r)>>1;
    push_down(idx);
    update(l,mid,ql,qr,t[idx].cl,val);
    update(mid+1,r,ql,qr,t[idx].cr,val);
    t[idx].ans=min(t[t[idx].cl].ans,t[t[idx].cr].ans);
}
int query(int l,int r,int ql,int qr,int idx){
    if(qr<l||r<ql)return inf;
    if(ql<=l&&r<=qr){
        return t[idx].ans;
    }
    int mid=(l+r)>>1;
    push_down(idx);
    return min(query(l,mid,ql,qr,t[idx].cl),query(mid+1,r,ql,qr,t[idx].cr));
    t[idx].ans=min(t[t[idx].cl].ans,t[t[idx].cr].ans);
}
int a[N];
signed main() {
    //fast();
    int n,p=1,ans=0,sum;
    in(n);
    rep(i,1,n)in(a[i]);
    rep(i,1,n){
    c[i]=c[i-1]+a[i];
    }
    sum=c[n];
    int root=build(1,2*n);
    int l=1,r=n;
    while(l<=n){
    if(query(1,2*n,l,r,root)>=0)ans++;
    update(1,2*n,l,r,root,-a[l]);
    sum=sum-a[l]+a[r-n+1];
    l++,r++;    
    update(1,2*n,r,r,root,sum);
    }
    cout<<ans;
    return 0;
}

K 题:P1725 琪露诺

考虑动态规划,可得转换方程:dp_i=\max_{i-R\le j\le i-L}dp_j+A_i。哦,单点修改,区间极值,线段树!
好了,这题写完了。

代码如下:

#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define I_love_Foccarus return
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
#define fi first
#define se second
const int N = 3e5 + 5;
const int inf = INT_MAX;
inline int read() {
    int x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9') {
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9') {
        x=x*10+ch-48;
        ch=getchar();
    }
    I_love_Foccarus x*f;
}
void fast() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
int a[N],dp[N];
struct linetree{
    int cl,cr,ans,lazy;
};
vector<linetree>t;
int build(int l,int r){
    linetree tmp;
    if(l==r){
        tmp.cl=tmp.cr=-1;
        tmp.ans=0;
        tmp.lazy=0;
        t.push_back(tmp);
        return t.size()-1;
    }
    int mid=(l+r)>>1;
    tmp.cl=build(l,mid);
    tmp.cr=build(mid+1,r);
    tmp.ans=max(t[tmp.cl].ans,t[tmp.cr].ans);
    tmp.lazy=0;
    t.push_back(tmp);
    return t.size()-1;
}
void push_down(int idx){
    if(!t[idx].lazy)return;
    t[t[idx].cl].ans+=t[idx].lazy;
    t[t[idx].cr].ans+=t[idx].lazy;
    t[t[idx].cl].lazy+=t[idx].lazy;
    t[t[idx].cr].lazy+=t[idx].lazy;
    t[idx].lazy=0;
}
void update(int l,int r,int ql,int qr,int idx,int val){
    if(qr<l||r<ql)return;
    if(ql<=l&&r<=qr){
        t[idx].ans=val;
        t[idx].lazy=val;
        return;
    }
    int mid=(l+r)>>1;
    push_down(idx);
    update(l,mid,ql,qr,t[idx].cl,val);
    update(mid+1,r,ql,qr,t[idx].cr,val);
    t[idx].ans=max(t[t[idx].cl].ans,t[t[idx].cr].ans);
}
int query(int l,int r,int ql,int qr,int idx){
    if(qr<l||r<ql)return INT_MIN;
    if(ql<=l&&r<=qr){
        return t[idx].ans;
    }
    int mid=(l+r)>>1;
    push_down(idx);
    return max(query(l,mid,ql,qr,t[idx].cl),query(mid+1,r,ql,qr,t[idx].cr));
    t[idx].ans=max(t[t[idx].cl].ans,t[t[idx].cr].ans);
}
signed main() {
    //fast();
   int n,l,r;
   in(n),in(l),in(r);
   rep(i,0,n)in(a[i]);
   n+=l;
   int root=build(0,n);
   rep(i,l,n){
   int sum=query(0,n,max(i-r,0),i-l,root);
   update(0,n,i,i,root,sum+a[i]);
   }

   cout<<query(0,n,n,n,root);

    I_love_Foccarus 0;
}

L 题:P3522 [POI2011] TEM-Temperature

切绿题好爽哈。
思路:用单调队列进行维护,队首维护正确性,队尾维护单调性,正确性需要满足队首表示的那天的最低温度需要满足小于当前枚举的最高温度,也就是维护题目要求的段内可能温度不降。单调性就是维护单日最低温的不上升序列,这样就可以确定温度的左边界,每次枚举的答案就是最后一个不满足正确性的单调最大左边界与当前 i 的差。然后对每次枚举的答案取 max 就行了。

代码如下:

#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
const int N = 1e6+5;
const int inf = INT_MAX;
inline int read() {
    int x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9') {
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9') {
        x=x*10+ch-48;
        ch=getchar();
    }
    return x*f;
}
void fast() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
int l[N],r[N],n,ans,nl;
deque<int>q;
signed main() {
    //fast();
    in(n);
    rep(i,1,n)in(l[i]),in(r[i]);
    rep(i,1,n){
        while(!q.empty()&&l[q.front()]>r[i])nl=q.front(),q.pop_front();
        if(!q.empty())
        ans=max(ans,i-nl);
        while(!q.empty()&&l[q.back()]<l[i])q.pop_back();
        q.push_back(i);
    }
    cout<<ans;
    return 0;
}        

M 题:P2698 [USACO12MAR] Flowerpot S

这题很明显可以二分答案,以区间长度为二分对象。那么判定的函数怎么写呢?可以模拟一个滑动窗口,通过单调队列实现,但是我不会,所以我用 ST 表 A 掉的这道题。
用 ST 表预处理好后,直接在值域范围内扫描,通过 ST 实现最大/最小值的查询,然后相减判定答案是否成立。
由于数据过水,本该 MTE 的代码通过了本题,但我懒得改了。

代码如下:

#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define I_love_Foccarus return
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define deap(i,a,b) for(int i=a;i>=b;i--)
#define in(a) a=read()
#define fi first
#define se second
const int N = 5e5 + 5;
const int inf = INT_MAX;
inline int read() {
    int x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9') {
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9') {
        x=x*10+ch-48;
        ch=getchar();
    }
    I_love_Foccarus x*f;
}
void fast() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
int n,d;
pair<int,int>a[N];
int st[N],dp[N][20][2],m;
signed main() {
    //fast();
    in(n),in(d);
    rep(i,1,N)dp[i][0][1]=inf;
    rep(i,1,n) {
        int x,y;
        in(x),in(y);
        a[i]=make_pair(x,y);
        dp[x][0][0]=max(dp[x][0][0],y);
        dp[x][0][1]=min(dp[x][0][1],y); 
        m=max(m,x);
    }
    st[1]=0;
    rep(i,2,m) st[i]=st[i>>1]+1;
    rep(j,1,st[m])
    rep(i,1,m-(1<<j)+1) {
        dp[i][j][0]=max(dp[i][j-1][0],dp[i+(1<<(j-1))][j-1][0]);
        dp[i][j][1]=min(dp[i][j-1][1],dp[i+(1<<(j-1))][j-1][1]);
    }
    int ls=1,rs=m,ans=-1;
    while(ls<=rs) {
        int mid=(ls+rs)>>1,p=0;
        rep(i,1,m-mid) {
            int l=i,r=i+mid;
            int maxx=max(dp[l][st[r-l+1]][0],dp[r-(1<<st[r-l+1])+1][st[r-l+1]][0]);
            int minx=min(dp[l][st[r-l+1]][1],dp[r-(1<<st[r-l+1])+1][st[r-l+1]][1]); 
            if(maxx-minx>=d) {
                p=1;
                break;
            }
        }
        if(p)ans=mid,rs=mid-1;
        else ls=mid+1;
    }
    cout<<ans;
    I_love_Foccarus 0;
}

N 题:P4824 [USACO15FEB] Censoring S

对于某个想参考我代码的大哥哥:

我的思路并不怎么明确,就是在线 KMP 进行字符串匹配,用一个栈来维护,建立该栈的 nex 数组,然后在匹配过程中删除超出的字符,最终得出我们想要的答案(伦敦大雾)。虽然可以直接上 AC 自动机,或许这样我就可以讲得更明白些,但我太懒,没有这么做。
这是代码,自行脑补。

代码如下:

#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define I_love_Foccarus return
#define in(a) a=read()
const int N = 1e7 + 5;
const int inf = INT_MAX;
inline int read() {
    int x=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9') {
        if (ch=='-') f=-1;
        ch=getchar();
    }
    while (ch>='0'&&ch<='9') {
        x=x*10+ch-48;
        ch=getchar();
    }
    I_love_Foccarus x*f;
}
void fast() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}
int cnt;
char s[N],t[N<<1];
char stk[N<<1];
int nex[N<<1];
signed main() {
    fast();
    cin>>t>>s;
    int m=strlen(t),n=strlen(s);
    for(int i=0;i<m;i++) s[i+n]=t[i];
    nex[0]=0,stk[0]=s[0];
    for(int i=1;i<n+m;i++){
        stk[++cnt]=s[i];
        int j=nex[cnt-1];
        while(j>0&&stk[cnt]!=stk[j]) j=nex[j-1];
        if(stk[cnt]==stk[j])j++;
        nex[cnt]=j;
        if(nex[cnt]==n){
            int k=n;
            while(k--) cnt--;
        }
    }
    stk[cnt+1]=0;
    cout<<(stk+n);
    I_love_Foccarus 0;
}