AtCoder Beginner Contest 278 - D
D-All Assign Point Add
题目链接:https://atcoder.jp/contests/abc278/tasks/abc278_d
刚开始的思路是进行操作1的时候,将整个数组都赋成x,但是这样的操作会导致用时过长。
在他人的帮助下,学到了覆盖这一用法。利用cnt来标记每个数是否在新出现的1之后有修改的操作。
下见代码
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<vector>
#define ll long long
#define ull unsigned long long
#define mem(x,y) memset(x,y,sizeof(x))
#define int long long
inline ll read()
{
ll 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;
}
using namespace std;
const int maxm=2e5+5,inf=0x3f3f3f3f;
int n,q,lazy=0;
vector<int> a,b;
void solve(){
cin>>n;
int c,y,x,cnt=0;
a.assign(n+5,0);
b.assign(n+5,0);
for(int i=1;i<=n;++i){
cin>>a[i];
}
cin>>q;
while(q--){
cin>>c;
if(c==1){
cin>>lazy;
++cnt;
}else if(c==2){
cin>>y>>x;
if(b[y]<cnt){
b[y]=cnt;
a[y]=lazy+x;
}
else a[y]+=x;
}else{
cin>>y;
if(b[y]<cnt){
cout<<lazy<<endl;
}
else cout<<a[y]<<endl;
}
}
return ;
}
signed main(){
// ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
int _=1;
// cin>>_;
while(_--){
solve();
}
return 0;
}
本文来自博客园,作者:Qiansui,转载请注明原文链接:https://www.cnblogs.com/Qiansui/p/16907442.html

浙公网安备 33010602011771号