Correct code does not works on CF but works on vscode (usign cph)

Revision en1, by pande22pic0103, 2025-05-08 11:15:55

I learnt segment trees recently and began solving its questions. I was solving below problem:

https://codeforces.me/contest/339/problem/D

which is Xenia and Bit Operations. The code I wrote works well and good locally on vscode (i use cph extension) but when I paste the same code on Codeforces, it gives me WA every time on some test cases. But when I see the results and copy the test case on which my code doesn't work on CF and test that testcase locally, I got correct answer as Jury's answer. Below is my code in C++. Please tell me that whether the issue is in my code or it's the compiler.

include <bits/stdc++.h>

include <ctype.h>

using namespace std;

define int long long int

define faster ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)

class Tree { public: vector seg; int lvl; string type; Tree(int n, int x) { seg.resize(4*n+1); if(x%2) type="or"; else type="xor"; } void build(int ind, int lo, int hi, int arr[]) { if(lo==hi) { seg[ind] = arr[lo]; return; } int mid=(lo+hi)>>1; lvl++; build(2*ind+1, lo, mid, arr); build(2*ind+2, mid+1, hi, arr); lvl--; if(type=="or"){ if(lvl%2) seg[ind]=seg[2*ind+1]^seg[2*ind+2]; else seg[ind]=seg[2*ind+1]|seg[2*ind+2]; } else{ if(lvl%2==0) seg[ind]=seg[2*ind+1]^seg[2*ind+2]; else seg[ind]=seg[2*ind+1]|seg[2*ind+2]; } }

int query(int ind, int lo, int hi, int l , int r) {
    // no overlap
    if(r<lo or l>hi) return INT_MAX;

    // complete overlap
    if(lo>=l and hi<=r) return seg[ind];

    // partial overlap
    int mid = lo+hi>>1;
    int left=query(2*ind+1, lo, mid, l, r);
    int right=query(2*ind+2, mid+1, hi, l, r);
    return min(left, right);
}

void update(int ind, int lo, int hi, int i, int val) {
    if(lo == hi){
        seg[ind] = val;
        return; 
    }
    int mid = (lo+hi)>>1;
    lvl++;
    if(i<=mid) update(2*ind+1, lo, mid, i, val);
    else update(2*ind+2, mid+1, hi, i, val);
    lvl--;
    if(type=="or"){
        if(lvl%2) seg[ind]=seg[2*ind+1]^seg[2*ind+2];
        else seg[ind]=seg[2*ind+1]|seg[2*ind+2];
    }
    else{
        if(lvl%2==0) seg[ind]=seg[2*ind+1]^seg[2*ind+2];
        else seg[ind]=seg[2*ind+1]|seg[2*ind+2];
    }
}

};

void solve(){ int n, q; cin>>n>>q; int x=n; n=1<<n; int arr[n]; for(int i=0;i<n;i++) cin>>arr[i]; Tree tree(n, x); tree.build(0, 0, n-1, arr);

//for(int i=0;i<tree.seg.size();i++) cout<<tree.seg[i]<<" "; cout<<endl;

while(q--){
    int i, val;
    cin>>i>>val;
    i--;
    tree.lvl=0;
    tree.update(0, 0, n-1, i, val);
    arr[i]=val;
    cout<<tree.seg[0]<<endl;
}

}

int32_t main(){ solve(); return 0; }

Community, please help to resolve the issue.

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English pande22pic0103 2025-05-08 11:19:18 1945
en1 English pande22pic0103 2025-05-08 11:15:55 3214 Initial revision (published)