北大2777Count Color提交时出现Runtime Error不知道哪里出错了,请高手指点

xuyapeng1991 2011-10-08 07:53:55
#include<iostream>
using namespace std;
int flag[35];
struct Seg_tree
{
int left,right;
int color;
int num;
int calmid()
{
return (left+right)/2;
}
};
Seg_tree tree[200005];
void build(int left,int right,int idx)
{
tree[idx].left=left;
tree[idx].right=right;
tree[idx].color=1;
tree[idx].num=1;
if(left==right)
return ;
int mid=tree[idx].calmid();
build(left,mid,idx*2);
build(mid+1,right,idx*2+1);
}
void update(int left,int right,int color,int idx)
{
if(tree[idx].left==left&&tree[idx].right==right)
{
tree[idx].color=color;
return ;
}
if(tree[idx].color>=1)
{
tree[idx*2].color=tree[idx].color;
tree[idx*2+1].color=tree[idx].color;
tree[idx].color=-1;
}
int mid=tree[idx].calmid();
if(right<=mid)
update(left,right,color,idx*2);
else if(left>mid)
update(left,right,color,idx*2+1);
else
{
update(left,mid,color,idx*2);
update(mid+1,right,color,idx*2+1);
}
}
void search(int left,int right,int idx)
{
if(tree[idx].color>=1)
{
flag[tree[idx].color]++;
return ;
}
int mid=tree[idx].calmid();
if(right<=mid)
search(left,right,idx*2);
else if(left>mid)
search(left,right,idx*2+1);
else
{
search(left,mid,idx*2);
search(mid+1,right,idx*2+1);
}
}

int main()
{
int M,N,Q;
while(cin>>M>>N>>Q)
{
char ch;
int a,b,c,i;
build(1,M,1);
for(i=0;i<Q;++i)
{
cin>>ch;
if(ch=='C')
{
cin>>a>>b>>c;
update(a,b,c,1);
}
else
{
memset(flag,0,sizeof(flag));
cin>>a>>b;
search(a,b,1);
int ans=0;
for(int j=1;j<=30;++j)
if(flag[j]>0)
ans++;
cout<<ans<<endl;
}
}
}
return 0;
}
...全文
93 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuyapeng1991 2011-11-15
  • 打赏
  • 举报
回复
多谢大家对我的帮助。。
logiciel 2011-10-09
  • 打赏
  • 举报
回复
Seg_tree tree[200005];
不够大,所以出现Runtime Error.试改为以下:
Seg_tree tree[1000000];
mskmc_mc 2011-10-08
  • 打赏
  • 举报
回复
太长,没看完,看到一个

void build(int left,int right,int idx)
{
tree[idx].left=left;
tree[idx].right=right;
tree[idx].color=1;
tree[idx].num=1;
if(left==right)
return ;
jessierzlz 2011-10-08
  • 打赏
  • 举报
回复
//表示本程序在vs2010上可以运行
#include<iostream>
#include<cstdlib>
using namespace std;
int flag[35];
struct Seg_tree
{
int left,right;
int color;
int num;
int calmid()
{
return (left+right)/2;
}
};
Seg_tree tree[200005];
void build(int left,int right,int idx)
{
tree[idx].left=left;
tree[idx].right=right;
tree[idx].color=1;
tree[idx].num=1;
if(left==right)
return ;
int mid=tree[idx].calmid();
build(left,mid,idx*2);
build(mid+1,right,idx*2+1);
}
void update(int left,int right,int color,int idx)
{
if(tree[idx].left==left&&tree[idx].right==right)
{
tree[idx].color=color;
return ;
}
if(tree[idx].color>=1)
{
tree[idx*2].color=tree[idx].color;
tree[idx*2+1].color=tree[idx].color;
tree[idx].color=-1;
}
int mid=tree[idx].calmid();
if(right<=mid)
update(left,right,color,idx*2);
else if(left>mid)
update(left,right,color,idx*2+1);
else
{
update(left,mid,color,idx*2);
update(mid+1,right,color,idx*2+1);
}
}
void search(int left,int right,int idx)
{
if(tree[idx].color>=1)
{
flag[tree[idx].color]++;
return ;
}
int mid=tree[idx].calmid();
if(right<=mid)
search(left,right,idx*2);
else if(left>mid)
search(left,right,idx*2+1);
else
{
search(left,mid,idx*2);
search(mid+1,right,idx*2+1);
}
}

int main()
{
int M,N,Q;
while(cin>>M>>N>>Q)
{
char ch;
int a,b,c,i;
build(1,M,1);
for(i=0;i<Q;++i)
{
cin>>ch;
if(ch=='C')
{
cin>>a>>b>>c;
update(a,b,c,1);
}
else
{
memset(flag,0,sizeof(flag));
cin>>a>>b;
search(a,b,1);
int ans=0;
for(int j=1;j<=30;++j)
if(flag[j]>0)
ans++;
cout<<ans<<endl;
}
}
}
system("pause");
return 0;
}
aizibion 2011-10-08
  • 打赏
  • 举报
回复
内存越界,递归导致堆栈溢出,楼主自己找吧。。。
matrixcl 2011-10-08
  • 打赏
  • 举报
回复
题目都懒得贴吗?

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧