I am solving a problem named Knight Tournament. My approach is based upon segment trees.
I think my update function and overall code works in logarithmic time
However i’m getting time limit exceeded.
Can someone point out where i’m going wrong in terms of complexity
(because i think i’m doing updates in O(log(n)) ) )
Before i show my code, i would like to add that, I’m already using Fast IO, and no it doesn’t work. I’ve tried #pragma optimizations too… and that too didn’t help much
Here is my code, help is appreciated
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=100005;
int tree[4*N+1];
int num[N];
int numtotree[N];
void update(int idx,int s,int e,int l,int r,int val){
if(r<s||l>e)return;
if(s==e){
if(tree[idx]==0)
tree[idx]=val;
numtotree[s]=idx;
return;
}
int m=(s+e)/2;
update(2*idx,s,m,l,r,val);
update(2*idx+1,m+1,e,l,r,val);
tree[idx]=val;
return;
}
void display(int n){
for(int i = 0; i < n ; i ++){
cout << tree[numtotree[i]] << ' ';
}
cout<<endl;
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(0);
int n , m ;
cin >> n >> m;
while(m--){
int l , r , x;
cin >> l >> r >> x;
update(1,0,n-1,l-1,r-1,x);
tree[numtotree[x-1]]=0;
}
display(n);
}