求查UB(?)

P2801 教主的魔法

201810tflsdjl @ 2022-07-27 17:50:45

rt,在线IDE上开O2和不开O2结果不一样,测试数据是第一个数据,这里给出一部分:

10 3
719 583 321 556 741 430 50 28 326 804
A 1 10 321
M 1 10 101
A 1 10 337

代码如下:

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
using std::lower_bound, std::sort;

typedef long long lol;
const int Maxn = 1e6+5, Maxnsq = 1e3+5;
lol h[Maxn], copy[Maxn], tag[Maxnsq];
int n, q, tot, start[Maxn], end[Maxn], block;
int query(int, int, lol);
void add(int, int, lol), update(int);

int main(){
//  freopen("data.in", "r", stdin);

    scanf("%d %d", &n, &q);
    block = sqrt(n); tot = n/block;
    for(int i=0; i<tot; i++){
        start[i] = i*block;
        end[i] = (i+1)*block;
        for(int j=start[i]; j<end[i]; j++){
            scanf("%d", &h[j]);
        }
    }
    start[tot] = tot*block; end[tot] = n;
    for(int i=tot*block; i<n; i++){
        scanf("%d", &h[i]);
    }
    for(int i=0; i<n; i++) copy[i] = h[i];
    for(int i=0; i<tot; i++){
        sort(copy+start[i], copy+end[i]);
    }
    int l, r, w, c;
    char ty;
    getchar();
    while(q--){
        scanf("%c", &ty);
        if(ty == 'M'){
            scanf("%d %d %lld\n", &l, &r, &w);
            add(l-1, r-1, w);
        }
        else{
            scanf("%d %d %lld\n", &l, &r, &c);
            printf("%d\n", query(l-1, r-1, c));
        }
    }

    return 0;
}

void add(int l, int r, lol w){
    int lb = l/block, rb = r/block;
    for(int i=l; i==l||i%block!=0; i++){
        h[i] += w;
    }
    update(lb);
    if(lb!=rb){
        for(int i=r; i==r||i%block!=block-1; i--){
            h[i] += w;
        }
    }
    update(rb);
    for(int i=lb+1; i<rb; i++){
        tag[i] += w;
    }
}

int query(int l, int r, lol c){
    int lb = l/block, rb = r/block, ans = 0;
    lol ct;
    for(int i=l; i==l||i%block!=0; i++){
        if(h[i]+tag[lb] >= c) ans++;
    }
    if(lb!=rb){
        for(int i=r; i==r||i%block!=block-1; i--){
            if(h[i]+tag[rb] >= c) ans++;
        }
    }
    for(int i=lb+1; i<rb; i++){
        ct = c-tag[i];
        ans += copy+(i+1)*block-lower_bound(copy+i*block, copy+(i+1)*block, ct);
    }
    return ans;
}

void update(int id){
    for(int i=start[id]; i<end[id]; i++){
        copy[i] = h[i]+tag[id];
        h[i] += tag[id];
    }
    tag[id] = 0;
    sort(copy+start[id], copy+end[id]);
}

by 201810tflsdjl @ 2022-07-27 17:51:23

本地和开O2之后都是正确的


by Carnival @ 2022-07-27 18:08:31

输入的时候 %d%lld 混用了,不知道是不是这个问题?


by Carnival @ 2022-07-27 18:09:13

@Gamemode 就是有几个 long long 输入用了 %d


by 201810tflsdjl @ 2022-07-27 18:13:48

@Gamemode 确实是个问题,但是改了之后还是如此


by 201810tflsdjl @ 2022-07-27 18:21:47

@Gamemode 好像解决了,是34行把w和c定义成int了,输入是long long,不过又开始RE了,用什么编译器都不行,而且在线IDE正常


by Carnival @ 2022-07-27 18:22:39

@201810tflsdjl 还有几个 int 也用 %lld 输入了,剩下的我就不知道了


by 201810tflsdjl @ 2022-07-27 18:27:18

@Gamemode 感谢!


|