主要利用了宏和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++ 输出调试的一些技巧的更多相关文章

  1. Java常用的输出调试技巧

    --------siwuxie095                 Eclipse 开发中常用的输出调试技巧:     先在左侧的 Package Explorer,右键->New->J ...

  2. VS调试的简单技巧

    学习之路三十二:VS调试的简单技巧   这段时间园子里讲了一些关于VS的快捷键以及一些配置技巧,挺好的,大家一起学习,一起进步. 这段时间重点看了一下关于VS调试技巧方面的书,在此记录一下学习的内容吧 ...

  3. C++ MFC控制台输出调试信息

    1.#include <conio.h> 2.在需要开启控制台窗口的地方调用 AllocConsole();//注意检查返回值 3.在需要输出调试的时候调用_cprintf等函数 如_cp ...

  4. js操作dom---创建一个域来输出调试信息

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  5. 在MFC中,使用控制台Console输出调试信息

    1.在MFC的应用类的InitInstance()函数里添加: AllocConsole(); 有时候需要调整这行代码在InitInstance函数的位置 2.继续添加以下代码 freopen(&qu ...

  6. 使用OutputDebugString输出调试信息

    在编写控制台程序的时候我们经常会使用printf输出调试信息,使我们了解程序的状态,方便调试,但是当编写非控制台程序的时候这种方法就行不通了,那我们应该怎么办?上网查了一些方法,大致就如下几种 使用L ...

  7. shell输出调试信息

    [shell输出调试信息] 1.使用trap命令 trap命令用于捕获指定的信号并执行预定义的命令. 其基本的语法是: trap 'command' signal 其中signal是要捕获的信号,co ...

  8. Delphi中使用Dos窗口输出调试信息

    在项目文件 *.DPR (Project->View Source)  里加上{$APPTYPE   CONSOLE} 然后,在需要输出处加上 Writeln(‘your debug messa ...

  9. 【OT1.0 + TP3.2】开启trace调试、输出调试信息、开启自定义菜单

    1.开启trace调试 A- 后台系统设置 show-page-trace = 1 B-config.php文件.配置 show-page-trace = true 2.输出调试信息 很奇怪,OT竟然 ...

随机推荐

  1. $.ajax()引发的对Deferred的总结 (转)

    传统的ajax写法: $.ajax({ url:"1.json", type:"get", success:function(data){}, error:fu ...

  2. yourphp添加KindEditor编辑器

    <tr> <td align="right">故障描述</td> <script charset="utf-8" sr ...

  3. setTimeout不断重复执行

    setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. setTimeout(code,millisec) code  必需.要调用的函数后要执行的 JavaScript 代码串. ...

  4. DESCryptoServiceProvider

    public static byte[] DESEncrypt(byte[] data, byte[] sKey) { return DESEncrypt(data, sKey, sKey); } / ...

  5. thinkphp 3.2.3 连接sql server 2014 WAMPSERVER环境包

    安装 sqlsrv 扩展 首先  sql server 2014 安装没啥说的 链接信息自己设置 php 版本 :5.5.12 sqlsrv 驱动  微软提供了 3.0 和3.1 版本  3.0 对应 ...

  6. mysql 索引及其原理

    mysql 索引 KEY与INDEX的区别: KEY is something on the logical level, describes your table and database desi ...

  7. Yii2创建多界面主题(Theme)

    Yii2界面主题上的设计总体上和Yii1.x保持一致,区别在于两个地方: 1. 由于Yii2引入了独立的视图(View)类,因此界面主题(Theme)也交由视图来管理: 2. 视图文件和Web资源在目 ...

  8. NancyFx中使用自带的IOC容器

    /// <summary> /// Cors扩展 /// </summary> public static class IPipelinesExtensions { /// & ...

  9. 【帖子】怎么彻底删除kafka的topic,然后重建?

    怎么彻底删除kafka的topic,然后重建? 网上都说用kafka-run-class.shkafka.admin.DeleteTopicCommand 命令删除topic,但是并没有成功,用kaf ...

  10. php正则表达式匹配用户名规则:由字母开头的6-16位字母和数字组成的字符串

    $pattern = '/^[a-zA-Z]{1}[a-zA-Z0-9]{5,15}$/i';