Jack_1218 @ 2024-08-31 23:55:59
#include<bits/stdc++.h>
using namespace std;
int N,M;
int A[1005],B[1005];
int T[2005],cnt=0;
map<int,int> MAP;
int sum[32005];
bool tag[32005];
void pushup(int rt){
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void pushdown(int rt,int ls,int rs){
if(tag[rt]==false) return;
sum[rt<<1]=ls;
sum[rt<<1|1]=rs;
tag[rt<<1]=true;
tag[rt<<1|1]=true;
tag[rt]=false;
}
void upd(int rt,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr){
sum[rt]=r-l+1;
tag[rt]=true;
return;
}
int mid=l+r>>1;
pushdown(rt,mid-l+1,r-mid);
if(ql<=mid) upd(rt<<1,l,mid,ql,qr);
if(qr>mid) upd(rt<<1|1,mid+1,r,ql,qr);
pushup(rt);
}
int query(int rt,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr){
return sum[rt];
}
int mid=l+r>>1,ans=0;
pushdown(rt,mid-l+1,r-mid);
if(ql<=mid) ans+=query(rt<<1,l,mid,ql,qr);
if(qr>mid) ans+=query(rt<<1|1,mid+1,r,ql,qr);
return ans;
}
int main(){
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
cin>>N>>M;
for(int i=1;i<=M;i++){
cin>>A[i]>>B[i];
T[++cnt] = A[i];
T[++cnt] = B[i];
}
sort(T+1, T+cnt+1);
int len = unique(T+1, T+cnt+1) - (T+1);
//cout<<len<<endl;
MAP[T[1]] = 1;
for(int i=2;i<=len;i++){
if(T[i] - T[i-1] == 1) MAP[T[i]] = MAP[T[i-1]] + 1;
else MAP[T[i]] = MAP[T[i-1]] + 2;
}
for(int i=1;i<=M;i++){
A[i] = MAP[A[i]];
B[i] = MAP[B[i]];
}
int ans=0;
for(int i=M;i>=1;i--){
if(query(1,1,32000,A[i],B[i]) != B[i] - A[i] + 1){
ans++;
upd(1,1,32000,A[i],B[i]);
}
}
cout<<ans;
return 0;
}
这份代码WA了最后一个点 #12
但仅仅将T数组的空间调节至2785
如下:
#include<bits/stdc++.h>
using namespace std;
int N,M;
int A[1005],B[1005];
int T[2785],cnt=0;
map<int,int> MAP;
int sum[32005];
bool tag[32005];
void pushup(int rt){
sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void pushdown(int rt,int ls,int rs){
if(tag[rt]==false) return;
sum[rt<<1]=ls;
sum[rt<<1|1]=rs;
tag[rt<<1]=true;
tag[rt<<1|1]=true;
tag[rt]=false;
}
void upd(int rt,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr){
sum[rt]=r-l+1;
tag[rt]=true;
return;
}
int mid=l+r>>1;
pushdown(rt,mid-l+1,r-mid);
if(ql<=mid) upd(rt<<1,l,mid,ql,qr);
if(qr>mid) upd(rt<<1|1,mid+1,r,ql,qr);
pushup(rt);
}
int query(int rt,int l,int r,int ql,int qr){
if(ql<=l&&r<=qr){
return sum[rt];
}
int mid=l+r>>1,ans=0;
pushdown(rt,mid-l+1,r-mid);
if(ql<=mid) ans+=query(rt<<1,l,mid,ql,qr);
if(qr>mid) ans+=query(rt<<1|1,mid+1,r,ql,qr);
return ans;
}
int main(){
//freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
cin>>N>>M;
for(int i=1;i<=M;i++){
cin>>A[i]>>B[i];
T[++cnt] = A[i];
T[++cnt] = B[i];
}
sort(T+1, T+cnt+1);
int len = unique(T+1, T+cnt+1) - (T+1);
//cout<<len<<endl;
MAP[T[1]] = 1;
for(int i=2;i<=len;i++){
if(T[i] - T[i-1] == 1) MAP[T[i]] = MAP[T[i-1]] + 1;
else MAP[T[i]] = MAP[T[i-1]] + 2;
}
for(int i=1;i<=M;i++){
A[i] = MAP[A[i]];
B[i] = MAP[B[i]];
}
int ans=0;
for(int i=M;i>=1;i--){
if(query(1,1,32000,A[i],B[i]) != B[i] - A[i] + 1){
ans++;
upd(1,1,32000,A[i],B[i]);
}
}
cout<<ans;
return 0;
}
就可以AC
其中T数组是我开的一个用于离散化的临时数组,我认为只需开两倍M的空间就够了呀......
有没有大佬来解释一下,谢谢!
by N_z_ @ 2024-09-01 08:02:13
拿个 sanitize 跑一下就知道了
未命名2.cpp:16:14: runtime error: index 32774 out of bounds for type 'int [32005]'
未命名2.cpp:16:15: runtime error: store to address 0x55a3be78d4d8 with insufficient space for an object of type 'int'
0x55a3be78d4d8: note: pointer points here
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
^
未命名2.cpp:17:16: runtime error: index 32775 out of bounds for type 'int [32005]'
未命名2.cpp:17:17: runtime error: store to address 0x55a3be78d4dc with insufficient space for an object of type 'int'
0x55a3be78d4dc: note: pointer points here
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
^
未命名2.cpp:18:14: runtime error: index 32774 out of bounds for type 'bool [32005]'
未命名2.cpp:18:15: runtime error: store to address 0x55a3be76d786 with insufficient space for an object of type 'bool'
0x55a3be76d786: note: pointer points here
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
^
未命名2.cpp:19:16: runtime error: index 32775 out of bounds for type 'bool [32005]'
未命名2.cpp:19:17: runtime error: store to address 0x55a3be76d787 with insufficient space for an object of type 'bool'
0x55a3be76d787: note: pointer points here
00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
^
未命名2.cpp:38:22: runtime error: index 32775 out of bounds for type 'int [32005]'
未命名2.cpp:38:22: runtime error: load of address 0x55a3be78d4dc with insufficient space for an object of type 'int'
0x55a3be78d4dc: note: pointer points here
01 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
^
未命名2.cpp:25:14: runtime error: index 32769 out of bounds for type 'int [32005]'
未命名2.cpp:25:15: runtime error: store to address 0x55a3be78d4c4 with insufficient space for an object of type 'int'
0x55a3be78d4c4: note: pointer points here
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 01 00 00 00
^
未命名2.cpp:26:14: runtime error: index 32769 out of bounds for type 'bool [32005]'
未命名2.cpp:26:15: runtime error: store to address 0x55a3be76d781 with insufficient space for an object of type 'bool'
0x55a3be76d781: note: pointer points here
00 00 00 00 00 00 00 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
^
未命名2.cpp:11:24: runtime error: index 32768 out of bounds for type 'int [32005]'
未命名2.cpp:11:24: runtime error: load of address 0x55a3be78d4c0 with insufficient space for an object of type 'int'
0x55a3be78d4c0: note: pointer points here
00 00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00
^
未命名2.cpp:11:39: runtime error: index 32769 out of bounds for type 'int [32005]'
未命名2.cpp:11:39: runtime error: load of address 0x55a3be78d4c4 with insufficient space for an object of type 'int'
0x55a3be78d4c4: note: pointer points here
00 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 01 00 00 00
32005 的数组越界了。
by N_z_ @ 2024-09-01 08:02:25
@Jack_1218
by Jack_1218 @ 2024-09-01 21:05:54
@Nz 哦对忘记了线段树四倍空间了,谢谢啦!已关注!