C++ 输出调试的一些技巧
主要利用了宏和stderr...
#define enable_debug
#ifdef enable_debug
FILL some macros/functions here
#else
/// set some debug functions to NULL
#endif
1. 输出到stderr
#define debug(...) { \
fprintf(stderr,__VA_ARGS__); \
fflush(stderr); \
} \
2. 若debug则插入一段代码
#define ProgDBG(...) { __VA_ARGS__; }
example.
#include "cstdio"
#include "vector"
#include "cstring"
#include "cstdlib"
#include "algorithm"
#define pb push_back
#define frp(x,y,z) freopen(#x"."#z,#y,std##z);
using namespace std;
const int
maxn = 50010,
maxm = 100010;
struct node;
struct ed{
node*f,*t;
ed*r;
} AlEd[maxn<<2],*p=AlEd;
struct node{
vector<ed*> To,Eto;
int id,nid,color;
int dist1,dist2;
} nd [maxn];
inline void ade(int f,int t){
*p=(ed){nd+f,nd+t,p+1};
nd[f].Eto.pb(p);
nd[f].To.pb(p++);
*p=(ed){nd+t,nd+f,p-1};
nd[t].Eto.pb(p);
nd[t].To.pb(p++);
}
struct qry{ int f,t,id; } qrs[maxm];
int nowColor,ans[maxm];
//#define idbg
#ifdef idbg
#define debug(...) {fprintf(stderr,__VA_ARGS__);fflush(stderr);}
// #define assert(...)
inline void tensenx(int&p,int b){
if(b<p) p=b;
}
#define xassert(x,...) if(!(x)){ \
debug(__VA_ARGS__);exit(3); \
}
#define tensen(a,...) { \
int t=__VA_ARGS__; \
xassert(t >= 0,"Tensened with: %d, code:\n" #__VA_ARGS__,t); \
tensenx(a,t); \
}
#define ProgDBG(...) { __VA_ARGS__; }
#else
#define debug(...)
inline void tensen(int&p,int b){
if(b<p) p=b;
}
#define ProgDBG(...)
#endif
typedef vector<node*> vn;
typedef vector<qry*> vq;
/// generate bfs functions
#define bfsx(x) inline void bfs##x(node*from){ \
ProgDBG( debug("BFS"#x"\n"); ) \
static node*queue[maxn]; \
int ql=0,qr=1,col=from->color; \
queue[0]=from; \
from->dist##x=0; \
while(ql-qr){ \
node*v=queue[ql++]; \
ProgDBG( \
debug("Vis %d\n",v->id); \
) \
for(int i=0,_=v->Eto.size();i<_;++i){ ed*p=v->Eto[i]; \
if(p->t->color==col && !(~p->t->dist##x)){ \
p->t->dist##x=v->dist##x+1; \
queue[qr++]=p->t; \
}else{ \
ProgDBG( \
if(p->t->color!=col){ \
debug("%d color not match\n",p->t->id); \
}else{ \
debug("%d dist"#x"(%d) mismatch\n",p->t->id,p->t->dist##x); \
} \
) \
} \
} \
} \
}
/// bfs1 & bfs2
bfsx(1);
bfsx(2);
void div(vn nodeList,vq qryList){
ProgDBG(
debug("Div Nodes: ");
for(int i=0,_=nodeList.size();i<_;++i){
debug("%d ",nodeList[i]->id);
}
debug("\n");
);
if(nodeList.size()<=3){
for(int i=0,_=qryList.size();i<_;++i){
tensen(ans[qryList[i]->id],qryList[i]->f!=qryList[i]->t);
}
}else{
int color=nowColor++;
for(int i=0,_=nodeList.size();i<_;++i){
nodeList[i]->color=color;
nodeList[i]->nid =i;
nodeList[i]->dist1=-1;
nodeList[i]->dist2=-1;
}
/// Find edge to cut.
int len=nodeList.size();
int maxlen=len;
ed*cur=NULL;
for(int i=0,_=nodeList.size();i<_;++i){
for(int j=0,_=nodeList[i]->Eto.size();j<_;++j){ ed*p=nodeList[i]->Eto[j];
if(p->t->color==color){
ProgDBG(
debug("Has Edge: %d -> %d\n",p->f->id,p->t->id);
)
int fx=p->f->nid,
tx=p->t->nid;
if(fx>tx) std::swap(fx,tx);
int na=fx+len-tx+1;
int nb=tx-fx+1;
na=na>nb?na:nb;
if(na<maxlen) maxlen=na, cur=p;
}
}
}
/// Calculate answer through this edge and divide the queries
bfs1(cur->f);
bfs2(cur->t);
int ef=cur->f->nid;
int et=cur->t->nid;
ProgDBG(
debug("Cut edge: (NID) %d %d\n",ef,et);
debug(" (ID) %d %d\n",cur->f->id,cur->t->id);
)
if(ef>et) std::swap(ef,et);
vq qryA,qryB;
for(int i=0,_=qryList.size();i<_;++i){
qry*p=qryList[i];
tensen(ans[p->id],nd[p->f].dist1+nd[p->t].dist1);
tensen(ans[p->id],nd[p->f].dist1+nd[p->t].dist2+1);
tensen(ans[p->id],nd[p->f].dist2+nd[p->t].dist1+1);
debug("%d %d %d %d\n",p->f,nd[p->f].dist2,p->t,nd[p->t].dist2);
tensen(ans[p->id],nd[p->f].dist2+nd[p->t].dist2);
int qf=nd[p->f].nid;
int qt=nd[p->t].nid;
if(qf>qt) std::swap(qf,qt);
if(qf==ef && qt==et) continue;
if(qf<=ef){
if(qt>=et || qt<=ef) qryA.pb(p);
}else
if(qf>=et) qryA.pb(p);
if(qf>=ef && qf<=et){
if(qt>=ef && qt<=et) qryB.pb(p);
}
}
/// divide nodes
vn ndA,ndB;
for(int i=0,_=nodeList.size();i<_;++i){
if(i<=ef) ndA.pb(nodeList[i]);
if(i>=et) ndA.pb(nodeList[i]);
if(i>=ef && i<=et) ndB.pb(nodeList[i]);
}
vector<ed*> pat=nodeList[ef]->Eto;
vector<ed*> pbt=nodeList[et]->Eto;
vector<ed*> qat, qbt, xat, xbt;
for(int i=0,_=pat.size();i<_;++i){
if(pat[i]->t->nid<=ef) qat.pb(pat[i]);
if(pat[i]->t->nid>=et) qat.pb(pat[i]);
if(pat[i]->t->nid>=ef && pat[i]->t->nid<=et) qbt.pb(pat[i]);
}
for(int i=0,_=pbt.size();i<_;++i){
if(pbt[i]->t->nid<=ef) xat.pb(pbt[i]);
if(pbt[i]->t->nid>=et) xat.pb(pbt[i]);
if(pbt[i]->t->nid>=ef && pbt[i]->t->nid<=et) xbt.pb(pbt[i]);
}
nodeList[ef]->Eto=qat; nodeList[et]->Eto=xat;
div(ndA,qryA);
nodeList[ef]->Eto=qbt; nodeList[et]->Eto=xbt;
div(ndB,qryB);
}
}
int main(){
frp(B,r,in);
frp(B,w,out);
int n; scanf("%d",&n);
for(int i=1;i<=n;++i) ade(i,i%n+1),nd[i].id=i;
for(int i=3;i< n;++i){
int a,b; scanf("%d%d",&a,&b); ade(a,b);
}
int q; scanf("%d",&q);
for(int i=1;i<=q;++i){ scanf("%d%d",&qrs[i].f,&qrs[i].t); qrs[i].id=i; }
vn A; vq B;
for(int i=1;i<=n;++i) A.pb(nd+i);
for(int i=1;i<=q;++i) B.pb(qrs+i);
memset(ans,0x3f,sizeof(ans));
div(A,B);
for(int i=1;i<=q;++i) printf("%d\n",ans[i]);
return 0;
}
C++ 输出调试的一些技巧的更多相关文章
- Java常用的输出调试技巧
--------siwuxie095 Eclipse 开发中常用的输出调试技巧: 先在左侧的 Package Explorer,右键->New->J ...
- VS调试的简单技巧
学习之路三十二:VS调试的简单技巧 这段时间园子里讲了一些关于VS的快捷键以及一些配置技巧,挺好的,大家一起学习,一起进步. 这段时间重点看了一下关于VS调试技巧方面的书,在此记录一下学习的内容吧 ...
- C++ MFC控制台输出调试信息
1.#include <conio.h> 2.在需要开启控制台窗口的地方调用 AllocConsole();//注意检查返回值 3.在需要输出调试的时候调用_cprintf等函数 如_cp ...
- js操作dom---创建一个域来输出调试信息
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 在MFC中,使用控制台Console输出调试信息
1.在MFC的应用类的InitInstance()函数里添加: AllocConsole(); 有时候需要调整这行代码在InitInstance函数的位置 2.继续添加以下代码 freopen(&qu ...
- 使用OutputDebugString输出调试信息
在编写控制台程序的时候我们经常会使用printf输出调试信息,使我们了解程序的状态,方便调试,但是当编写非控制台程序的时候这种方法就行不通了,那我们应该怎么办?上网查了一些方法,大致就如下几种 使用L ...
- shell输出调试信息
[shell输出调试信息] 1.使用trap命令 trap命令用于捕获指定的信号并执行预定义的命令. 其基本的语法是: trap 'command' signal 其中signal是要捕获的信号,co ...
- Delphi中使用Dos窗口输出调试信息
在项目文件 *.DPR (Project->View Source) 里加上{$APPTYPE CONSOLE} 然后,在需要输出处加上 Writeln(‘your debug messa ...
- 【OT1.0 + TP3.2】开启trace调试、输出调试信息、开启自定义菜单
1.开启trace调试 A- 后台系统设置 show-page-trace = 1 B-config.php文件.配置 show-page-trace = true 2.输出调试信息 很奇怪,OT竟然 ...
随机推荐
- ecshop 配送方式支持"货到付款"
ecshop配送方式,支持货到付款方法. 打开表 ecs_shipping,找到字段support_cod,修改1则支持货到付款 扩展 问题描述:已经修改了ecs_shipping表里的support ...
- flask笔记---url、变量规则
1.路由: route() 装饰器用于把一个函数绑定到一个 URL,可以动态变化 URL 的某些部分,还可以为一个函数指定多个规则,从而方便用户访问与记忆. 例子: @app.route('/') # ...
- 使用Lucene开发自己的搜索引擎
1.下载Lucene开发包,请到:http://lucene.apache.org/ 2.在myeclipse环境部署该开发包: 3.代码编写: package Lucene; import java ...
- [C#]集合已修改;可能无法执行枚举操作
摘要 我相信很多人对这个再熟悉不过了.对已经修改的集合进行操作就会出现这个错. 解决办法 比如有下面的一段代码,我们创建一个集合,并向集合中添加10个数,然后,我们循环再将这些数移除了. static ...
- 使程序在Linux下后台运行
一.为什么要使程序在后台执行 我们计算的程序都是周期很长的,通常要几个小时甚至一个星期.我们用的环境是用putty远程连接到日本Linux服务器.所以使程序在后台跑有以下三个好处: 1:我们这边是否关 ...
- jquery 获取和设置 checkbox radio 和 select option的值?
============== 获取和设置 checkbox radio 和 select的值? === val()函数, 其名字就表达了 它的意思: 他就是= value 的简写! val就是valu ...
- Hadoop伪分布式搭建(一)
下面内容主要说明在Windows虚拟机上面,怎么搭建一个Hadoop伪分布式,并如何运行wordcount程序和网页查看HDFS文件系统. 1 相关软件下载和安装 APACH官网提供hadoop版本 ...
- Java字节流:ByteArrayInputStream ByteArrayOutputStream
----------------------------------------------------------------------------------- ByteArrayInputSt ...
- [译]AngularJS中几种Providers(Factory, Service, Provider)的区别
原文: http://blog.xebia.com/2013/09/01/differences-between-providers-in-angularjs/ 什么是Provider? Angula ...
- WCF-复合类型使用;传输图片
一:WCF服务端 IService1.cs中: public interface IService1 { [OperationContract] [WebInvoke(Method = "P ...