清明节入门模拟赛题解

· · 题解

T1

解法1(30分):

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,q;
int a[10005];
template<typename T>
inline void read(T &x) {
    x = 0;
    register char c = getchar();
    register short f = 1;
    while (c < '0' || c > '9') {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    x *= f;
}
template <typename T, typename... Args>
inline void read(T &x, Args &...temps)
{
    read(x), read(temps...);
}
signed main(){
//  freopen("test10.in","r",stdin);
//  freopen("test10.out","w",stdout);
    cin>>n>>q;
    for(int i=1;i<=n;i++){
        read(a[i]);
    }
    int l,r,k,p;
    while(q--){
        read(p);
        if(p==1){
            read(l,r,k);
            for(int i=l;i<=r;i++){
                a[i]+=k;
            }
        }
        else{
            read(l,r);
            int maxn=0;
            int ans=0;
            for(int x=l;x<=r;x++){
                for(int y=x;y<=r;y++){
                    int now=0;
                    for(int i=x;i<=y;i++){
                        now+=a[i];
                    }
                    ans=max(ans,now);
                }
            }
            printf("%d\n",ans);
        }
    //  cout<<q<<endl;
    }
    return 0;
}

解法2(30分):

解法2(100分):

标程

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,q;
int a[10005];
template<typename T>
inline void read(T &x) {
    x = 0;
    register char c = getchar();
    register short f = 1;
    while (c < '0' || c > '9') {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    x *= f;
}
template <typename T, typename... Args>
inline void read(T &x, Args &...temps)
{
    read(x), read(temps...);
}
signed main(){
//  freopen("test10.in","r",stdin);
//  freopen("test10.out","w",stdout);
    cin>>n>>q;
    for(int i=1;i<=n;i++){
        read(a[i]);
    }
    int l,r,k,p;
    while(q--){
        read(p);
        if(p==1){
            read(l,r,k);
            for(int i=l;i<=r;i++){
                a[i]+=k;
            }
        }
        else{
            read(l,r);
            int maxn=0;
            int ans=0;
            for(int i=l;i<=r;i++){
                maxn+=a[i];
                if(maxn<0) maxn=0;
                ans=max(ans,maxn);
            }   
            ans=max(ans,maxn);
            printf("%d\n",ans);
        }
    }
    return 0;
}

T2

解法1(30分):

12 7 16 6

*/


### 解法2(30分):
* 对于操作1,我们暴力给每个$a_i+k$。$O(n)$
* 对于操作2,我们暴力枚举左右端点,再利用前缀和$O(1)$取区间和。遇到长度大于$m$时不取。$O(n^2)$
* 时间复杂度$O(n^2q)$。
* 感觉其实没什么思想难度,就只给$30$分了。

### 解法3(100分)
* 对于操作1,我们暴力给每个$a_i+k$。$O(n)$
* 对于操作2的序列,先进行前缀和预处理,再枚举右端点$i$,左端点从1开始。
* 比较一下左端点可能在的两个位置$j,k$,若$k<j<i$并且$sum[j] \leq sum[k]$,则$k$永远不会被取。因此我们使用单调队列维护。
```c++
/**
 *
 *    ┏┓   ┏┓
 *   ┏┛┻━━━┛┻┓
 *   ┃       ┃
 *   ┃   ━   ┃
 *   ┃ ┳┛ ┗┳ ┃
 *   ┃       ┃
 *   ┃   ┻   ┃
 *   ┃       ┃
 *   ┗━┓   ┏━┛Code is far away from bug with the animal protecting
 *     ┃   ┃    神兽保佑,代码无bug
 *     ┃   ┃
 *     ┃   ┗━━━┓
 *     ┃      ┣┓
 *     ┃     ┏┛
 *     ┗┓┓┏━┳┓┏┛
 *      ┃┫┫ ┃┫┫
 *      ┗┻┛ ┗┻┛
 *
 */
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,q;
template<typename T>
inline void read(T &x) {
    x = 0;
    register char c = getchar();
    register short f = 1;
    while (c < '0' || c > '9') {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    x *= f;
}
template <typename T, typename... Args>
inline void read(T &x, Args &...temps)
{
    read(x), read(temps...);
}
int sum[10005];
int b[10005];
int qq[10005];
int a[10005];
signed main(){
//  freopen("test10.in","r",stdin);
    //freopen("test10.out","w",stdout);
    cin>>n>>q;
    for(int i=1;i<=n;i++){
        read(a[i]);
    }
    int ls,rs,k,p;
    while(q--){
        read(p);
        if(p==1){
            read(ls,rs,k);
            for(int i=ls;i<=rs;i++){
                a[i]+=k;
            }
        }
        else{
            read(ls,rs,k);
            int ans=-1e9;
            int l=1,r=1;
            for(int i=ls;i<=rs;i++){
                b[i-ls+1]=a[i];
            }
            int len=rs-ls+1;            
            for(int i=1;i<=len;i++){
                sum[i]=sum[i-1]+b[i];
            }
            memset(qq,0,sizeof qq);
            qq[1]=0;
            for(int i=1;i<=len;i++){
                while(l<=r&&qq[l]<i-k) l++;
                ans=max(ans,sum[i]-sum[qq[l]]);
                while(l<=r&&sum[qq[r]]>=sum[i]){
                    r--;
                }
                qq[++r]=i;
            }
            printf("%lld\n",ans);
        }
    }
    return 0;
}
/*
5 5
2 3 5 6 1
1 2 3 1
2 2 4 2
2 4 5 2
2 1 5 3
2 3 3 2

12
7
16
6

*/

T3

1、要求O(logn)找到序列中对应的值并修改。

2、要求维护一个值出现的时间刻。

3、要求找到已知值的接近值。

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,q;
template<typename T>
inline void read(T &x) {
    x = 0;
    register char c = getchar();
    register short f = 1;
    while (c < '0' || c > '9') {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    x *= f;
}
template <typename T, typename... Args>
inline void read(T &x, Args &...temps)
{
    read(x), read(temps...);
}
int a[100005];
multiset<int>g;
map<int,int >mp;
signed main(){
//  freopen("test10.in","r",stdin);
//  freopen("test10.out","w",stdout);
    cin>>n>>q;
    for(int i=1;i<=n;i++){
        read(a[i]);
        g.insert(a[i]);
        mp[a[i]]=-1;
    }
    for(int i=1;i<=q;i++){
        int p,l,r,k,w,c,d;
        read(p);
        if(p==1){
            read(k,w);
            auto pos=g.find(k);
            if(pos!=g.end()) g.erase(pos);
            g.insert(k*w+w);
            mp[k*w+w]=i;
        }
        else if(p==2){
            read(c);
            if(mp[c]==0) printf("%d\n",-1);
            else if(mp[c]==-1) printf("%d\n",0);
            else printf("%lld\n",mp[c]);
        }
        else{
            read(d);
            auto it=g.upper_bound(d);
            int res;
            if(it==g.end()){
                res=*(--it);
            }
            else if(it==g.begin()){
                res=*it;
            }
            else{
                int next = *it;
                int prev = *(--it);
                if (d - prev <= next - d) { 
                    res = prev;
                } else {
                    res = next;
                }
            }
            printf("%lld\n",res);
        }
    }
    return 0;
}
/*
5 3
3 2 1 4 5
1 2 1
1 4 2
2 4
*/