P4970 全村最好的嘤嘤刀 题解

· · 题解

此题难度:

读题难度:黑

代码难度:红

综合难度:绿

题意:

给你一个数列 a,你要对它做以下3种操作:

  1. 给你x,w,将a[x]修改为w - a[x],并给x这个位置打上标记。

  2. 给你一个区间x,y,如果这个区间有被标记的位置,ans += 这些位置上面的数的总和,并把标记去掉,输出所有的数,数变为0。如果这个区间没有被标记的数,ans += 这个区间最大的数,输出这个数,并把这个数改为0。

  3. 给你一个区间x,y,给你一个数w,给这个区间所有的数加上w。

对于最终的ans,依照题意进行对应的输出。

做法:

暴力模拟即可。

注意,这道题不可以开long long,WA 80的请注意这一点。

代码:

/*
  Author: EnderDeer
  Online Judge: Luogu
*/

#include<bits/stdc++.h>

#define mem(x) memset(x,0,sizeof(x))

using namespace std;

int read(){
   int s = 0,w = 1;
   char ch = getchar();
   while(ch < '0' || ch > '9'){if(ch == '-')w = -1;ch = getchar();}
   while(ch >= '0' && ch <= '9')s = s * 10 + ch - '0',ch = getchar();
   return s * w;
}

int n,m;
int a[1000010];
bool bk[1000010];
int ans;

signed main(){
    cin>>n>>m;
    for(int i = 1;i <= n;i ++)a[i] = read();
    while(m --){
        int op,x,y,w;
        op = read();
        if(op == 1){
            x = read(),w = read();
            a[x] = w - a[x];
            bk[x] = true;
        }
        if(op == 2){
            x = read(),y = read();
            bool flag = false;
            for(int i = x;i <= y;i ++){
                if(bk[i]){
                    bk[i] = false;
                    ans += a[i];
                    printf("%d\n",a[i]);
                    a[i] = 0;
                    flag = true;
                }
            }
            if(flag)continue;
            int s = 0;
            int num;
            for(int i = x;i <= y;i ++){
                if(a[i] >= s){
                    s = a[i];
                    num = i;
                }
            }
            ans += s;
            printf("%d\n",a[num]);
            a[num] = 0;
        }
        if(op == 3){
            x = read(),y = read(),w = read();
            for(int i = x;i <= y;i ++)a[i] += w;
        }
    }
    if(ans < 10000)puts("QAQ");
    else if(ans < 10000000)puts("Sakura");
    else puts("ice");
    return 0;
}