题目链接

这个题已经处于花式tle了,改版后的spoj更慢了。。

tle的话就多交几把。。。

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <time.h>
  5. #include <vector>
  6. #include <map>
  7. #include <queue>
  8. #include <algorithm>
  9. #include <stack>
  10. #include <cstring>
  11. #include <cmath>
  12. #include <set>
  13. #include <vector>
  14. using namespace std;
  15. template <class T>
  16. inline bool rd(T &ret) {
  17. char c; int sgn;
  18. if (c = getchar(), c == EOF) return 0;
  19. while (c != '-' && (c<'0' || c>'9')) c = getchar();
  20. sgn = (c == '-') ? -1 : 1;
  21. ret = (c == '-') ? 0 : (c - '0');
  22. while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
  23. ret *= sgn;
  24. return 1;
  25. }
  26. template <class T>
  27. inline void pt(T x) {
  28. if (x < 0) {
  29. putchar('-');
  30. x = -x;
  31. }
  32. if (x > 9) pt(x / 10);
  33. putchar(x % 10 + '0');
  34. }
  35. typedef long long ll;
  36. typedef pair<int, int> pii;
  37. const int N = 200000 + 10;
  38. const int inf = 1e9;
  39. struct Node *null;
  40. inline int First(multiset<int>&x) {//返回x中最大的数
  41. return *x.rbegin();
  42. }
  43. inline int Second(multiset<int>&x) {//返回x中次大的数
  44. multiset<int>::reverse_iterator it = x.rbegin();
  45. it++; return *it;
  46. }
  47. struct Node {
  48. Node *fa, *ch[2];
  49. int size;
  50. multiset<int>path, chain;
  51. int ls, rs, ms;
  52. int col, len, id;
  53. bool rev;
  54. inline void put() {
  55. printf("%d siz:%d len:%d (%d,%d,%d) son{%d,%d} fa:%d col:%d \n", id, size, len, ls, rs, ms, ch[0]->id, ch[1]->id, fa->id, col);
  56. // cout << "path: ";for (auto i : path)cout << i << " ";puts("");
  57. // cout << "chain:";for (auto i : chain)cout << i << " ";puts("");
  58. }
  59. inline void clear(int _col, int _id) {
  60. fa = ch[0] = ch[1] = null;
  61. rev = 0;
  62. id = _id;
  63. col = _col;
  64. size = len = 0;
  65. ls = rs = ms = -inf;
  66. path.clear(); chain.clear();
  67. chain.insert(-inf); chain.insert(-inf); path.insert(-inf);
  68. }
  69. inline void push_up() {
  70. if (this == null)return;
  71. size = len + ch[0]->size + ch[1]->size;
  72. int _chain = max(col, First(chain));
  73. int L = max(_chain, ch[0]->rs + len);//从(虚边 or 左子树)的白点到this的最远距离
  74. int R = max(_chain, ch[1]->ls);//从(虚边 or 右子树)的白点到this的最远距离
  75. ls = max(ch[0]->ls, ch[0]->size + len + R);
  76. rs = max(ch[1]->rs, ch[1]->size + L);
  77.  
  78. ms = max(ch[0]->rs + len + R, L + ch[1]->ls);
  79. ms = max(ms, max(ch[0]->ms, ch[1]->ms));
  80. ms = max(ms, First(path));
  81. ms = max(ms, First(chain) + Second(chain));
  82. if (col == 0)
  83. ms = max(max(ms, First(chain)), 0);
  84. }
  85. inline void push_down() {
  86. if (rev) {
  87. ch[0]->flip();
  88. ch[1]->flip();
  89. rev = 0;
  90. }
  91. }
  92. inline void setc(Node *p, int d) {
  93. ch[d] = p;
  94. p->fa = this;
  95. }
  96. inline bool d() {
  97. return fa->ch[1] == this;
  98. }
  99. inline bool isroot() {
  100. return fa == null || fa->ch[0] != this && fa->ch[1] != this;
  101. }
  102. inline void flip() {
  103. if (this == null)return;
  104. swap(ch[0], ch[1]);
  105. rev ^= 1;
  106. }
  107. inline void go() {//从链头開始更新到this
  108. if (!isroot())fa->go();
  109. push_down();
  110. }
  111. inline void rot() {
  112. Node *f = fa, *ff = fa->fa;
  113. int c = d(), cc = fa->d();
  114. f->setc(ch[!c], c);
  115. this->setc(f, !c);
  116. if (ff->ch[cc] == f)ff->setc(this, cc);
  117. else this->fa = ff;
  118. f->push_up();
  119. }
  120. inline Node*splay() {
  121. // go();
  122. while (!isroot()) {
  123. if (!fa->isroot())
  124. d() == fa->d() ? fa->rot() : rot();
  125. rot();
  126. }
  127. push_up();
  128. return this;
  129. }
  130. inline Node* access() {//access后this就是到根的一条splay,而且this已经是这个splay的根了
  131. for (Node *p = this, *q = null; p != null; q = p, p = p->fa) {
  132. p->splay();
  133. if (p->ch[1] != null) {
  134. p->chain.insert(p->ch[1]->ls);
  135. p->path.insert(p->ch[1]->ms);
  136. }
  137. if (q != null) {
  138. p->chain.erase(p->chain.find(q->ls));
  139. p->path.erase(p->path.find(q->ms));
  140. }
  141. p->setc(q, 1);
  142. p->push_up();
  143. }
  144. return splay();
  145. }
  146. inline Node* find_root() {
  147. Node *x;
  148. for (x = access(); x->push_down(), x->ch[0] != null; x = x->ch[0]);
  149. return x;
  150. }
  151. void make_root() {
  152. access()->flip();
  153. }
  154. void cut() {//把这个点的子树脱离出去
  155. access();
  156. ch[0]->fa = null;
  157. ch[0] = null;
  158. push_up();
  159. }
  160. void cut(Node *x) {
  161. if (this == x || find_root() != x->find_root())return;
  162. else {
  163. x->make_root();
  164. cut();
  165. }
  166. }
  167. void link(Node *x) {
  168. if (find_root() == x->find_root())return;
  169. else {
  170. make_root(); fa = x;
  171. }
  172. }
  173. };
  174. void debug(Node *x) {
  175. if (x == null)return;
  176. x->put();
  177. debug(x->ch[0]);
  178. debug(x->ch[1]);
  179. }
  180. Node pool[N], *tail;
  181. Node *node[N];
  182. int n, q;
  183. struct Edge {
  184. int to, next, dis;
  185. }edge[N<<1];
  186. int head[N], edgenum;
  187. inline void add(int u, int v, int dis) {
  188. Edge E = { v, head[u], dis };
  189. edge[edgenum] = E;
  190. head[u] = edgenum++;
  191. }
  192. void dfs(int u, int fa) {
  193. for (int i = head[u]; ~i; i = edge[i].next){
  194. int v = edge[i].to; if (v == fa)continue;
  195. node[v]->fa = node[u];
  196. node[v]->len = edge[i].dis;
  197. dfs(v, u);
  198. node[u]->path.insert(node[v]->ms);
  199. node[u]->chain.insert(node[v]->ls);
  200. }
  201. node[u]->push_up();
  202. }
  203. int main() {
  204. while (cin >> n) {
  205. tail = pool;
  206. null = tail++;
  207. null->clear(-inf, 0);
  208. edgenum = 0;
  209. for (int i = 1; i <= n; i++) {
  210. head[i] = -1;
  211. node[i] = tail++;
  212. node[i]->clear(0, i);
  213. }
  214. for (int i = 1, u, v, d; i < n; i++) {
  215. rd(u); rd(v); rd(d);
  216. add(u, v, d); add(v, u, d);
  217. }
  218. dfs(1, 1);
  219. rd(q); char str[5]; int u;
  220. int ans = node[1]->ms;
  221. while (q--) {
  222. scanf("%s", str);
  223. if (str[0] == 'C') {
  224. rd(u);
  225. node[u]->access();
  226. if (node[u]->col == 0)node[u]->col = -inf;
  227. else node[u]->col = 0;
  228. node[u]->push_up();
  229. ans = node[u]->ms;
  230. }
  231. else {
  232. if (ans < 0)puts("They have disappeared.");
  233. else pt(ans), puts("");
  234. }
  235. // for (int i = 1; i <= n; i++)debug(node[i]), puts("");
  236. }
  237. }
  238. return 0;
  239. }
  240. /*
  241.  
  242. */

SPOJ QTREE4 lct的更多相关文章

  1. SPOJ - OTOCI LCT

    OTOCI Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/viewProblem. ...

  2. SPOJ QTREE3 lct

    题目链接 题意: 给定n个点 q个询问 以下n-1行给出树边,点有黑或白色.初始化为白色 以下q行: 询问有2种: 1. 0 x 把x点黑变白,白变黑 2.1 x 询问Path(1,x)路径上第一个黑 ...

  3. SPOJ QTREE4 SPOJ Query on a tree IV

    You are given a tree (an acyclic undirected connected graph) with N nodes, and nodes numbered 1,2,3. ...

  4. SPOJ QTREE2 lct

    题目链接 题意: 给一棵树.有边权 1.询问路径的边权和 2.询问沿着路径的第k个点标. 思路:lct裸题. #include <iostream> #include <fstrea ...

  5. SPOJ QTREE5 lct

    题目链接 对于每一个节点,记录这个节点所在链的信息: ls:(链的上端点)距离链内部近期的白点距离 rs:(链的下端点)距离链内部近期的白点距离 注意以上都是实边 虚边的信息用一个set维护. set ...

  6. SPOJ QTREE4 - Query on a tree IV 树分治

    题意: 给出一棵边带权的树,初始树上所有节点都是白色. 有两种操作: C x,改变节点x的颜色,即白变黑,黑变白 A,询问树中最远的两个白色节点的距离,这两个白色节点可以重合(此时距离为0). 分析: ...

  7. SPOJ QTREE4 - Query on a tree IV

    You are given a tree (an acyclic undirected connected graph) with N nodes, and nodes numbered 1,2,3. ...

  8. SPOJ QTREE4 Query on a tree IV ——动态点分治

    [题目分析] 同bzoj1095 然后WA掉了. 发现有负权边,只好把rmq的方式改掉. 然后T了. 需要进行底(ka)层(chang)优(shu)化. 然后还是T 下午又交就A了. [代码] #in ...

  9. Query on a tree IV SPOJ - QTREE4

    https://vjudge.net/problem/SPOJ-QTREE4 点分就没有一道不卡常的? 卡常记录: 1.把multiset换成手写的带删除堆(套用pq)(作用很大) 2.把带删除堆里面 ...

随机推荐

  1. J2SE知识点摘记(四)

    1.        抽象类(abstract) 抽象类和抽象方法都必须用abstract关键字来修饰. 抽象类不能被直接实例化,也就是不能直接用new关键字去产生对象. 抽象方法只需声明,而不需实现. ...

  2. 汽车总线obd模拟器,obd仿真器,ecu模拟器,obd开发测试工具,可以模拟ecu中的obd协议 MRD-5050

    汽车总线OBD模拟器MRD-5050型号是在车辆越来越趋于网络化的趋势下研发的,是汽车产品开发.调试.生产必备的工具,能为为开发人员节省大量的时间.当前车辆上的总线设备越来越多,有的高端车上甚至多到有 ...

  3. Linux中service命令和/etc/init.d/的关系

    Linux中service命令和/etc/init.d/的关系   service xxx启动 /etc/init.d/ 目录下的xxx脚本 如一个脚本名为 mysvc保存在/etc/init.d/下 ...

  4. SQLServer,仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表xx中的标识列指定显式值

    情景: 如果此表的主键或者其中有一个列使用了  IDENTITY(1,1) 自增长时,但又想手动为此列指定值时,当用如下解决方案: set identity_insert 表名 ON 使用此命令把表的 ...

  5. mongodb use db show dbs

    mongodb 常用命令: 在dbs间切换用 use xxxdb 之后再操作就是只针对 xxxdb了: show dbs显示全部数据库 show collections 显示全部集合 mongodb数 ...

  6. Jquery弹窗插件Lhgdialog的用法

    Lhgdialog的用法 大家都知道用js可以实现,但是在使用js实现的弹窗时得考虑很东西:浏览器的兼容.页面的交互等等问题. 在这里简单介绍一下lhgdialog的用法. 参数有: Title:弹窗 ...

  7. Dreamweaver PHP代码护眼配色方案

    结果展示 [1]主菜单选择编辑->首选项.在分类中选择"字体".设置代码视图的字体为Courier New [2]在分类中选择 "代码颜色",点击 &qu ...

  8. KVC在定义Model类中的妙用

    @我们应用程序使用MVC架构的话,对于处理数据类,我们会单独的定义Model类,在里面为要展示的属性进行初始化赋值,一般採用的方法是通过定义相应的属性,挨个赋值.如今我要介绍的就是通过KVC,key- ...

  9. Web API Login with Cookie

    public HttpWebResponse InitiliazeWebRequest() { string responseData =string.Empty;//string url = Get ...

  10. php什么是变量的数据类型

    什么是变量的数据类型 在变量中,由于变量占用的空间单元不一样(占的地盘大小不一样),也分成几种数据类型,就像超市商品的包装袋,有几种不同类型,不同的商品使用不同的包装袋.我们可以通过使用“memory ...