记录编号 55356 评测结果 AAAAAAAAAAAAAA
题目名称 线段覆盖 最终得分 100
用户昵称 Gravatardigital-T 是否通过 通过
代码语言 C++ 运行时间 4.237 s
提交时间 2013-03-18 12:22:33 内存使用 17.01 MiB
显示代码纯文本
  1. #include<fstream>
  2. using namespace std;
  3. ifstream fi("xdfg.in");
  4. ofstream fo("xdfg.out");
  5. class sss
  6. {
  7. public:
  8. int left,right,Lchild,Rchild;//left,right为区间起点终点,Lchild,Rchild是左儿子右儿子
  9. bool coverleft,coverright;//左右区间是否被覆盖
  10. int len;//把以root为根的子树中所有的黑布条叠加起来,被染黑区间的总长度。
  11. int count;//被覆盖次数
  12. int interval;//黑色区间的个数
  13. int connect;//左右区间是否连接
  14. }tree[400000];
  15. int line,n,total;
  16. void maketree(int root,int l,int r)
  17. {
  18. tree[root].left=l;
  19. tree[root].right=r;
  20. tree[root].coverleft=0;
  21. tree[root].coverright=0;
  22. tree[root].count=0;
  23. tree[root].len=0;
  24. tree[root].interval=0;
  25. tree[root].connect=0;
  26. if(l<r-1)
  27. {
  28. int mid=(l+r)/2;
  29. total++;
  30. tree[root].Lchild=total;
  31. maketree(total,l,mid);
  32. total++;
  33. tree[root].Rchild=total;
  34. maketree(total,mid,r);
  35. }
  36. else
  37. {
  38. tree[root].Lchild=-1;
  39. tree[root].Rchild=-1;
  40. }
  41. }
  42. void dealwithit(int root)
  43. {
  44. if(tree[root].count>0)
  45. tree[root].len=tree[root].right-tree[root].left;
  46. else
  47. if(tree[root].Lchild!=-1)
  48. {
  49. tree[root].len=tree[tree[root].Lchild].len+tree[tree[root].Rchild].len;
  50. }
  51. else{tree[root].len=0;}
  52. if(tree[root].count>0)
  53. tree[root].coverleft=true,tree[root].coverright=true;
  54. else
  55. if(tree[root].Lchild!=-1)
  56. {
  57. tree[root].coverleft=tree[tree[root].Lchild].coverleft;
  58. tree[root].coverright=tree[tree[root].Rchild].coverright;
  59. }
  60. else
  61. {
  62. tree[root].coverleft=false;
  63. tree[root].coverright=false;
  64. }
  65. if(tree[root].Lchild!=-1)
  66. {
  67. if(tree[tree[root].Rchild].coverleft&&tree[tree[root].Lchild].coverright)
  68. tree[root].connect=1;
  69. else
  70. tree[root].connect=0;
  71. }
  72. else{tree[root].connect=0;}
  73. if(tree[root].count>0)
  74. tree[root].interval=1;
  75. else
  76. tree[root].interval=tree[tree[root].Lchild].interval+tree[tree[root].Rchild].interval-tree[root].connect;
  77. }
  78. void add(int root,int a,int b)
  79. {
  80. if(root==-1)return;
  81. if(tree[root].left>=a&&tree[root].right<=b)
  82. tree[root].count++;
  83. else
  84. {
  85. if(b<=tree[root].left||tree[root].right<=a)return;
  86. add(tree[root].Lchild,a,b);
  87. add(tree[root].Rchild,a,b);
  88. }
  89. dealwithit(root);
  90. }
  91. void del(int root,int a,int b)
  92. {
  93. if(root==-1)return;
  94. if(tree[root].left>=a&&tree[root].right<=b)tree[root].count--;
  95. else
  96. {
  97. if(b<=tree[root].left||tree[root].right<=a)return;
  98. del(tree[root].Lchild,a,b);
  99. del(tree[root].Rchild,a,b);
  100. }
  101. dealwithit(root);
  102. }
  103. int main()
  104. {
  105. fi>>line>>n;total=0;
  106. maketree(total,0,line);
  107. int style,x,y;
  108. for(int i=1;i<=n;i++)
  109. {
  110. fi>>style>>x>>y;
  111. if(style==1)add(0,x,x+y);
  112. if(style==2)del(0,x,x+y);
  113. fo<<tree[0].interval<<' '<<tree[0].len<<endl;
  114. /*for(int i=0;i<=total;i++)
  115. fo<<i<<' '<<tree[i].left<<' '<<tree[i].right<<' '<<tree[i].count<<' '<<tree[i].len<<endl;
  116. fo<<endl;*/
  117. }
  118. return 0;
  119. }