C++ 为什么我会得到C++;Google Kickstart C 2020第四轮问题中的运行时错误(Candies)

C++ 为什么我会得到C++;Google Kickstart C 2020第四轮问题中的运行时错误(Candies),c++,C++,这是我第一次尝试kickstart问题(第C轮2020问题4-Candies),尽管我尝试的解决方案在vscode上工作,并在kickstart平台上通过了示例案例,但它给了我测试集1的“运行时错误”。知道为什么会这样吗?我尝试过将N的值(最初是一个更大的值,它给了我TLE)和“int”改为“long-longs”,但似乎没有任何效果。我根据官方的分析写了我的尝试,所以我认为它会通过,尽管这是我第一次尝试使用分段树,所以可能我在实现中遗漏了一些明显的东西。任何帮助都将不胜感激 #include

这是我第一次尝试kickstart问题(第C轮2020问题4-Candies),尽管我尝试的解决方案在vscode上工作,并在kickstart平台上通过了示例案例,但它给了我测试集1的“运行时错误”。知道为什么会这样吗?我尝试过将N的值(最初是一个更大的值,它给了我TLE)和“int”改为“long-longs”,但似乎没有任何效果。我根据官方的分析写了我的尝试,所以我认为它会通过,尽管这是我第一次尝试使用分段树,所以可能我在实现中遗漏了一些明显的东西。任何帮助都将不胜感激

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 101; 
int n;
vector<ll> t(2*N);
vector<ll> t1(2*N);

void build() {
  for (int i = n - 1; i > 0; --i) t[i] = t[2*i] + t[2*i+1];
}
void build2() {
  for (int i = n - 1; i > 0; --i) t1[i] = t1[2*i] + t1[2*i+1];
}

int sum(int a, int b) {
    a += n; b += n;
    int s = 0;
    while (a <= b) {
        if (a%2 == 1) s += t[a++]; //array[i++] is equivalent to array[i] THEN ++i; or i++; N.B. array[++i]; would be equivalent to ++i; or i++ THEN array[i];
        if (b%2 == 0) s += t[b--];
        a /= 2; b /= 2;
    }
    return s;
}
int sum2(int a, int b) {
    a += n; b += n;
    int s = 0;
    while (a <= b) {
        if (a%2 == 1) s += t1[a++]; //array[i++] is equivalent to array[i] THEN ++i; or i++; N.B. array[++i]; would be equivalent to ++i; or i++ THEN array[i];
        if (b%2 == 0) s += t1[b--];
        a /= 2; b /= 2;
    }
    return s;
}

void replace(int k, int x) {
    k += n;
    t[k] = x;
    for (k /= 2; k >= 1; k /= 2) {
        t[k] = t[2*k]+t[2*k+1];
    }
}
void replace2(int k, int x) {
    k += n;
    t1[k] = x;
    for (k /= 2; k >= 1; k /= 2) {
        t1[k] = t1[2*k]+t1[2*k+1];
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    ll T, q;
    cin >> T;
    for(int i = 0; i < T; i++){
        cin >> n >> q;
        for(int i = 0; i<n; i++){
            int temp, temp2;
            cin >> temp;
            temp2 = temp;
            temp = pow((-1),(i))*temp;
            temp2 = pow((-1),(i))*temp2*(i+1);
            t[i+n] = temp;
            t1[i+n] = temp2;
        }
        build();
        build2();
        ll ans = 0;
        char c;
        ll l,r;
        for(int i = 0; i<q; i++){
            cin >> c >> l >> r;
            l--;
            r--;
            if(c=='Q'){
                if(l%2==1){
                    ans-=sum2(l,r)-((l)*sum(l,r));
                }
                else{
                    ans+=sum2(l,r)-((l)*sum(l,r));
                }
            }
            else{
                if(l%2==1){
                    replace(l,-(r+1));
                    replace2(l,-((r+1)*(l+1)));
                }
                else{
                    replace(l,(r+1));
                    replace2(l,((r+1)*(l+1)));
                }
            }
        }
        cout << "Case #" << i+1 << ": " << ans << endl;
    }
}
#包括
使用名称空间std;
typedef long-long-ll;
常数int N=101;
int n;
向量t(2*N);
向量t1(2*N);
void build(){
对于(inti=n-1;i>0;--i)t[i]=t[2*i]+t[2*i+1];
}
void build2(){
对于(inti=n-1;i>0;--i)t1[i]=t1[2*i]+t1[2*i+1];
}
整数和(整数a,整数b){
a+=n;b+=n;
int s=0;
而(a=1;k/=2){
t1[k]=t1[2*k]+t1[2*k+1];
}
}
int main(){
ios::将_与_stdio同步(0);
cin.tie(0);
llt,q;
cin>>T;
for(int i=0;i>n>>q;
对于(int i=0;i>temp;
temp2=temp;
温度=功率(-1),(i))*温度;
temp2=功率(-1),(i))*temp2*(i+1);
t[i+n]=温度;
t1[i+n]=temp2;
}
build();
build2();
ll-ans=0;
字符c;
ll l,r;
对于(inti=0;i>c>>l>>r;
l--;
r--;
如果(c=='Q'){
如果(l%2==1){
ans-=sum2(l,r)-(l)*和(l,r));
}
否则{
ans+=sum2(l,r)-(l)*和(l,r));
}
}
否则{
如果(l%2==1){
替换(l,-(r+1));
替换2(l,-((r+1)*(l+1));
}
否则{
替换(l,(r+1));
替换2(l,((r+1)*(l+1));
}
}
}

无法显示完整的错误消息。同时精简代码。同时使用有意义的变量名。@tadman据我所知,google kickstart平台上没有错误消息。它字面上只是说“RE”。如果我将鼠标悬停在上面,它会说“Runtime error”。采取更小的步骤。做一件事-测试,做下一件事,测试。听起来像是未定义的行为。bug不必表现一致。考虑到您如何在此处执行零边界检查,出现未定义的行为并不奇怪。有些
assert()
检查可能是正确的。使用调试器-或者如果您不习惯使用调试器-在代码中乱扔调试打印输出。我承认-这仍然是我的第一个“转到”调试解决方案。