我创建了以下代码:
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
long long int a;
long long int b;
long long int h;
long long int ind;
};
struct node_seg{
long long int a;
long long int b;
long long int h;
long long int ind;
long long int val;
};
bool bcomp(struct node l, struct node r){
if(l.b!=r.b)
return l.b>r.b;
else
return l.a>r.a;
}
bool acomp(struct node_seg l, struct node_seg r){
return l.a>r.a;
}
void build_seg_tree(long long int l,
long long int r,
long long int node,
long long int tree[],
node_seg arr[]) {
if(l==r) {
tree[node]=arr[l].val;
return;
}
long long int mid=(l+r)/2;
build_seg_tree(l,mid,2*node+1,tree,arr);
build_seg_tree(mid+1,r,2*node+2,tree,arr);
tree[node]=max(tree[2*node+1],tree[2*node+2]);
}
void update(long long int l,
long long int r,
long long int node,
long long int tree[],
long long int c_i,
long long int v) {
if(l==r) {
tree[node]=v;
return;
}
long long int mid=(l+r)/2;
if(l<=c_i && c_i<=mid)
update(l,mid,2*node+1,tree,c_i,v);
else if(mid+1<=c_i && c_i<=r)
update(mid+1,r,2*node+2,tree,c_i,v);
tree[node]=max(tree[2*node+1],tree[2*node+2]);
}
long long int query(long long int t_l,
long long int t_r,
long long int node,
long long int q_l,
long long int q_r,
long long int tree[]) {
if(t_r<q_l || q_r<t_l) {
return 0;
} else if(q_l<=t_l && t_r<=q_r) {
return tree[node];
} else{
long long int mid=(t_l+t_r)/2;
return max(query(t_l,mid,2*node+1,q_l,q_r,tree),
query(mid+1,t_r,2*node+2,q_l,q_r,tree));
}
}
long long int bin_search(long long int val,
long long int l,
long long int r,
node_seg arr[]) {
if(l==r) {
return l;
}
long long int mid=l+(r-l)/2;
if(arr[mid].a<val) {
return bin_search(val,l,mid,arr);
} else {
return bin_search(val,mid+1,r,arr);
}
}
long long int find(long long int val,
struct node_seg arr[],
long long int n,
long long int my_seg_tree[]) {
long long int l=bin_search(val,0,n-1,arr);
long long int r=n-1;
//if(val==641)
// cout<<"l is "<<l<<endl;
long long int res=query(0,n-1,0,l,r,my_seg_tree);
//cout<<"and result is "<<res<<endl;
return res;
}
int main() {
long long int n,i,temp,u,v,w,res;
cin>>n;
struct node arr[n];
struct node arr_sort_b[n];
struct node_seg arr_sort_a[n];
for(i=0; i<n; i++) {
cin>>u>>v>>w;
struct node nd;
nd.a=u;
nd.b=v;
nd.h=w;
nd.ind=i;
arr[i]=nd;
}
for(i=0; i<n; i++) {
arr_sort_b[i]=arr[i];
struct node_seg ns;
ns.a=arr[i].a;
ns.b=arr[i].b;
ns.h=arr[i].h;
ns.ind=arr[i].ind;
ns.val=0;
arr_sort_a[i]=ns;
}
sort(arr_sort_b,arr_sort_b+n,bcomp);
sort(arr_sort_a,arr_sort_a+n,acomp);
for(i=0; i<n; i++) {
cout<<arr_sort_b[i].a<<" "<<arr_sort_b[i].b<<" "<<arr_sort_b[i].h;
cout<<endl;
}
cout<<endl<<endl;
for(i=0; i<n; i++) {
cout<<arr_sort_a[i].a<<" "<<arr_sort_a[i].b<<" "<<arr_sort_a[i].h;
cout<<endl;
}
int mapping2[n];
for(i=0; i<n; i++) {
temp=arr_sort_a[i].ind;
mapping2[temp]=i;
}
for(i=0; i<n; i++) {
cout<<mapping2[i]<<" ";
}
cout<<endl;
long long int my_seg_tree[2*n];
build_seg_tree(0,n-1,0,my_seg_tree,arr_sort_a);
long long int ans[n];
for(i=0;i<n;i++) {
if(i==0) {
ans[i]=arr_sort_b[i].h;
temp=mapping2[arr_sort_b[i].ind];
arr_sort_a[temp].val=ans[i];
update(0,n-1,0,my_seg_tree,temp,ans[i]);
res=ans[i];
} else {
ans[i]=arr_sort_b[i].h+find(arr_sort_b[i].b,arr_sort_a,n,my_seg_tree);
if(ans[i]>res)
res=ans[i];
//cout<<"temp is "<<temp<<" "<<arr_sort_b[i].ind<<endl;
cout<<"index is "<<arr_sort_b[i].ind<<endl;
for(int j=0; j<n; j++) {
cout<<mapping2[j]<<" ";
}
cout<<endl;
temp=mapping2[arr_sort_b[i].ind];
arr_sort_a[temp].val=ans[i];
update(0,n-1,0,my_seg_tree,temp,ans[i]);
}
cout<<ans[i]<<endl;
}
cout<<res;
return 0;
}
这里我有一个数组mapping2
。我用它来映射索引。
我没有在任何地方改变它的值,但是当我在最后一个循环中访问它的值时,它们正在改变。
我无法理解为什么会这样。 我正在使用的输入是:
10
312 838 197
713 996 320
427 767 520
490 800 465
575 681 995
781 843 908
545 615 400
36 667 129
85 641 27
396 539 350
我得到的输出是:
Runtime error time: 0 memory: 15248 signal:11
713 996 320
781 843 908
312 838 197
490 800 465
427 767 520
575 681 995
36 667 129
85 641 27
545 615 400
396 539 350
781 843 908
713 996 320
575 681 995
545 615 400
490 800 465
427 767 520
396 539 350
312 838 197
85 641 27
36 667 129
7 1 5 4 2 0 3 9 8 6
320
index is 5
7 1 5 4 2 0 0 0 0 0
1228
index is 0
7 1 5 4 2 0 0 0 0 0
1425
index is 3
7 1 5 4 2 0 0 0 0 0
1890
index is 2
7 1 5 4 2 0 0 0 0 0
2410
index is 4
7 1 5 4 2 0 2410 0 0 0
3405
index is 7
7 1 5 4 2 0 2410 0 0 0
3534
index is 8
7 1 5 4 2 0 2410 0 0 0
3432
index is 6
7 1 5 4 2 0 2410 0 0 0
最初mapping2
值为:
7 1 5 4 2 0 3 9 8 6
但是当循环迭代时,这些值会自动改变。但是为了可靠的映射,它们在每次迭代时应该是相同的。