【题目分析】

问题放到了树上,直接链剖+线段树搞一搞。

调了300行+。

(还是码力不够)

【代码】

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <cstdlib>
  5.  
  6. #include <map>
  7. #include <set>
  8. #include <queue>
  9. #include <string>
  10. #include <iostream>
  11. #include <algorithm>
  12.  
  13. using namespace std;
  14.  
  15. #define maxn 1000005
  16. #define eps 1e-8
  17. #define db double
  18. #define ll long long
  19. #define inf 0x3f3f3f3f
  20. #define F(i,j,k) for (int i=j;i<=k;++i)
  21. #define D(i,j,k) for (int i=j;i>=k;--i)
  22.  
  23. void Finout()
  24. {
  25. #ifndef ONLINE_JUDGE
  26. freopen("in.txt","r",stdin);
  27. freopen("wa.txt","w",stdout);
  28. #endif
  29. }
  30.  
  31. int Getint()
  32. {
  33. int x=0,f=1; char ch=getchar();
  34. while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
  35. while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
  36. return x*f;
  37. }
  38.  
  39. struct Node{
  40. int lx,rx,mx,sum,lazy;
  41. Node operator + (Node x)
  42. {
  43. Node ret;
  44. ret.lx=max(lx,sum+x.lx);
  45. ret.rx=max(x.sum+rx,x.rx);
  46. ret.sum=sum+x.sum;
  47. ret.mx=max(max(mx,x.mx),max(rx+x.lx,max(ret.lx,ret.rx)));
  48. ret.lazy=-inf;
  49. return ret;
  50. }
  51. void print()
  52. {
  53. printf("lx %d rx %d sum %d mx %d lazy %d\n",lx,rx,sum,mx,lazy);
  54. }
  55. void init(){lx=rx=mx=sum=0;lazy=-inf;}
  56. }t[maxn];
  57.  
  58. int n,a[maxn],b[maxn],q,L,R,tot,x,y,c;
  59. int h[maxn],to[maxn],ne[maxn],en=0;
  60. int fa[maxn],top[maxn],dep[maxn],siz[maxn],pos[maxn],son[maxn];
  61.  
  62. void add(int a,int b)
  63. {
  64. to[en]=b; ne[en]=h[a]; h[a]=en++;
  65. to[en]=a; ne[en]=h[b]; h[b]=en++;
  66. }
  67.  
  68. void build(int o,int l,int r)
  69. {
  70. if (l==r)
  71. {
  72. t[o].lx=t[o].rx=t[o].mx=t[o].sum=a[l];
  73. t[o].lazy=-inf;
  74. return ;
  75. }
  76. int mid=l+r>>1;
  77. build(o<<1,l,mid);
  78. build(o<<1|1,mid+1,r);
  79. t[o]=t[o<<1]+t[o<<1|1];
  80. t[o].lazy=-inf;
  81. }
  82.  
  83. void dfs1(int o)
  84. {
  85. siz[o]=1;
  86. for (int i=h[o];i>=0;i=ne[i])
  87. {
  88. if (to[i]!=fa[o])
  89. {
  90. fa[to[i]]=o;
  91. dep[to[i]]=dep[o]+1;
  92. dfs1(to[i]);
  93. siz[o]+=siz[to[i]];
  94. if (siz[to[i]]>siz[son[o]]) son[o]=to[i];
  95. }
  96. }
  97. }
  98.  
  99. void dfs2(int o,int tp)
  100. {
  101. top[o]=tp;
  102. pos[o]=++tot;
  103. a[tot]=b[o];
  104. if (!son[o]) return ;
  105. dfs2(son[o],tp);
  106. for (int i=h[o];i>=0;i=ne[i])
  107. if ((to[i]!=fa[o])&&(to[i]!=son[o]))
  108. dfs2(to[i],to[i]);
  109. return ;
  110. }
  111.  
  112. Node q1[maxn],q2[maxn];
  113.  
  114. void cov(int o,int c,int l,int r)
  115. {
  116. // printf("Node %d %d %d %d\n",o,c,l,r);
  117. t[o].lx=c<0?c:(r-l+1)*c; //printf("%d %d\n",c,(l-r+1)*c);
  118. t[o].rx=c<0?c:(r-l+1)*c;
  119. t[o].sum=(r-l+1)*c;
  120. t[o].mx=c<0?c:(r-l+1)*c;
  121. // printf("lx:%d rx:%d sum:%d mx:%d\n",t[o].lx,t[o].rx,t[o].sum,t[o].mx);
  122. }
  123.  
  124. void pushdown(int o,int l,int r)
  125. {
  126. int mid=l+r>>1;
  127. if (t[o].lazy!=-inf)
  128. {
  129. // printf("pushdown %d\n",o);
  130. // printf("in %d\n",t[o].lazy);
  131. t[o<<1].lazy=t[o<<1|1].lazy=t[o].lazy;
  132. cov(o<<1,t[o].lazy,l,mid);
  133. cov(o<<1|1,t[o].lazy,mid+1,r);
  134. }
  135. t[o].lazy=-inf;
  136. }
  137.  
  138. Node Query(int o,int l,int r)
  139. {
  140. // printf("q down\n");
  141. pushdown(o,l,r);
  142. // printf("Query %d %d\n",l,r);
  143. if (L<=l&&r<=R) return t[o];
  144. int mid=l+r>>1;
  145. if (L>mid) return Query(o<<1|1,mid+1,r);
  146. else if (R<=mid) return Query(o<<1,l,mid);
  147. else return Query(o<<1,l,mid)+Query(o<<1|1,mid+1,r);
  148. }
  149.  
  150. void query()
  151. {
  152. x=Getint(); y=Getint();
  153. // printf("Query %d to %d\n",x,y);
  154. int p1=0,p2=0;
  155. while (top[x]!=top[y])
  156. {
  157. if (dep[top[x]]>dep[top[y]])
  158. {
  159. L=pos[top[x]]; R=pos[x];
  160. q1[++p1]=Query(1,1,n);
  161. // printf("Query %d %d\n",L,R);
  162. // printf("at q1\n"); q1[p1].print();
  163. // printf("x: %d to %d\n",x,fa[top[x]]);
  164. x=fa[top[x]];
  165. }
  166. else
  167. {
  168. L=pos[top[y]]; R=pos[y];
  169. // printf("%d %d\n",L,R);
  170. q2[++p2]=Query(1,1,n);
  171. // printf("Query %d %d\n",L,R);
  172. // printf("at q2\n"); q2[p2].print();
  173. // printf("y: %d to %d\n",y,fa[top[y]]);
  174. y=fa[top[y]];
  175. }
  176. }
  177. if (dep[x]>=dep[y])
  178. {
  179. L=pos[y]; R=pos[x];
  180. // printf("%d %d\n",L,R);
  181. q1[++p1]=Query(1,1,n);
  182. // printf("Query %d %d\n",L,R);
  183. // printf("at q1\n"); q1[p1].print();
  184. }
  185. else
  186. {
  187. L=pos[x]; R=pos[y];
  188. // printf("%d %d\n",L,R);
  189. q2[++p2]=Query(1,1,n);
  190. // printf("Query %d %d\n",L,R);
  191. // printf("at q2\n"); q2[p2].print();
  192. }
  193. /* Node ret1,ret2,ret; ret1.init();ret2.init();ret.init();
  194. printf("q1begin\n");
  195. D(i,p1,1)
  196. {
  197. ret1.print();
  198. ret1=ret1+q1[i];
  199. q1[i].print();
  200. ret1.print();
  201. }
  202. printf("q1 over\n");
  203. ret1.print();
  204. printf("q2 begin\n");
  205. D(i,p2,1)
  206. {
  207. ret2.print();
  208. ret2=ret2+q2[i];
  209. q2[i].print();
  210. ret2.print();
  211. }
  212. printf("q2 over\n");
  213. // ret1.init(); ret2.init();
  214. ret1.print(); ret2.print();
  215. swap(ret2.lx,ret2.rx);
  216. ret=ret1+ret2;
  217. */
  218. Node ret,ret1,ret2; ret.init(); ret1.init(); ret2.init();
  219. if (!p1)
  220. {
  221. // printf("only p2\n");
  222. ret=q2[1];
  223. F(i,2,p2) ret=q2[i]+ret;
  224. }
  225. else if (!p2)
  226. {
  227. // printf("only p1\n");
  228. ret=q1[1];
  229. F(i,2,p1) ret=q1[i]+ret;
  230. }
  231. else
  232. {
  233. // printf("both p1 and p2\n");
  234. ret1=q1[1];
  235. F(i,2,p1) ret1=q1[i]+ret1;
  236. ret2=q2[1];
  237. F(i,2,p2) ret2=q2[i]+ret2;
  238. swap(ret2.lx,ret2.rx);
  239. ret=ret2+ret1;
  240. }
  241. printf("%d\n",max(ret.mx,0));
  242. }
  243.  
  244. void mod(int o,int l,int r)
  245. {
  246. // printf("mod %d %d %d\n",o,l,r);
  247. pushdown(o,l,r);
  248. int mid=l+r>>1;
  249. if (L<=l&&r<=R)
  250. {
  251. // printf("tag on %d by %d %d to %d\n",o,c,l,r);
  252. t[o].lazy=c;
  253. cov(o,c,l,r);
  254. return ;
  255. }
  256. if (L<=mid) mod(o<<1,l,mid);
  257. if (R>mid) mod(o<<1|1,mid+1,r);
  258. t[o]=t[o<<1]+t[o<<1|1];
  259. }
  260.  
  261. void Modify()
  262. {
  263. x=Getint(); y=Getint(); c=Getint();
  264. while (top[x]!=top[y])
  265. {
  266. if (dep[top[x]]<dep[top[y]]) swap(x,y);
  267. L=pos[top[x]]; R=pos[x];
  268. // printf("modify %d %d %d\n",L,R,c);
  269. mod(1,1,n);
  270. x=fa[top[x]];
  271. }
  272. if (dep[x]<dep[y]) swap(x,y);
  273. L=pos[y]; R=pos[x];
  274. // printf("modify %d %d %d\n",L,R,c);
  275. mod(1,1,n);
  276. }
  277.  
  278. int main()
  279. {
  280. memset(h,-1,sizeof h);
  281. Finout(); n=Getint();
  282. F(i,1,n) b[i]=Getint();
  283. F(i,1,n-1) add(Getint(),Getint());
  284. dfs1(1); dep[0]=-1;
  285. dfs2(1,1);
  286. // F(i,1,n) printf("Node %d : fa %d pos %d dep %d\n",i,fa[i],pos[i],dep[i]);
  287. build(1,1,n);
  288. q=Getint();
  289. F(i,1,q)
  290. {
  291. int opt=Getint();
  292. switch(opt)
  293. {
  294. case 1: query(); break;
  295. case 2: Modify(); break;
  296. }
  297. }
  298. }

  

SPOJ GSS7 Can you answer these queries VII ——树链剖分 线段树的更多相关文章

  1. GSS7 spoj 6779. Can you answer these queries VII 树链剖分+线段树

    GSS7Can you answer these queries VII 给出一棵树,树的节点有权值,有两种操作: 1.询问节点x,y的路径上最大子段和,可以为空 2.把节点x,y的路径上所有节点的权 ...

  2. SPOJ QTREE Query on a tree 树链剖分+线段树

    题目链接:http://www.spoj.com/problems/QTREE/en/ QTREE - Query on a tree #tree You are given a tree (an a ...

  3. SPOJ QTREE Query on a tree ——树链剖分 线段树

    [题目分析] 垃圾vjudge又挂了. 树链剖分裸题. 垃圾spoj,交了好几次,基本没改动却过了. [代码](自带常数,是别人的2倍左右) #include <cstdio> #incl ...

  4. SPOJ GSS7 - Can you answer these queries VII

    板的不能再板,链剖+线段树或者是LCT随便维护. 感觉唯一要注意的是跳链的时候要对$x$向上跳和$y$向上跳的情况分开讨论,而不能直接$swap$,因为只有两段接触的端点才能相互合并,而且每一次向上跳 ...

  5. QTREE3 spoj 2798. Query on a tree again! 树链剖分+线段树

    Query on a tree again! 给出一棵树,树节点的颜色初始时为白色,有两种操作: 0.把节点x的颜色置反(黑变白,白变黑). 1.询问节点1到节点x的路径上第一个黑色节点的编号. 分析 ...

  6. SPOJ 375 (树链剖分+线段树)

    题意:一棵包含N 个结点的树,每条边都有一个权值,要求模拟两种操作:(1)改变某条边的权值,(2)询问U,V 之间的路径中权值最大的边. 思路:最近比赛总是看到有树链剖分的题目,就看了论文,做了这题, ...

  7. spoj QTREE - Query on a tree(树链剖分+线段树单点更新,区间查询)

    传送门:Problem QTREE https://www.cnblogs.com/violet-acmer/p/9711441.html 题解: 树链剖分的模板题,看代码比看文字解析理解来的快~~~ ...

  8. Spoj Query on a tree SPOJ - QTREE(树链剖分+线段树)

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

  9. SP6779 GSS7 - Can you answer these queries VII

    纯数据结构题,没有思维难度.直接用线段树求最大子段和的方法完成树上路径的合并.注意链上合并顺序要符合序列的前后顺序. #include <cstdio> #include <cstr ...

随机推荐

  1. (转)RAM、ROM、SRAM、DRAM、SSRAM、SDRAM、FLASH、EEPROM的区别

    RAM(Random Access Memory) 随机存储器.存储单元的内容可按需随意取出或存入,且存取的速度与存储单元的位置无关的存储器.这种存储器在断电时将丢失其存储内容,故主要用于存储短时间使 ...

  2. Azure 项目构建 - 用 Azure 认知服务在微信公众号上搭建智能会务系统

    通过完整流程详细介绍了如何在Azure平台上快速搭建基于微信公众号的智慧云会务管理系统. 此系列的全部课程 https://school.azure.cn/curriculums/11 立即访问htt ...

  3. ASP.NET MVC执行流程图

  4. java中的堆与栈

    Java 中的堆和栈 Java把内存划分成两种:一种是栈内存,一种是堆内存. 在函数中定义的一些基本类型的变量和对象的引用变量都在函数的栈内存中分配 . 当在一段代码块定义一个变量时,Java就在栈中 ...

  5. UVA 1347 Tour 双调TSP

    TSP是NP难,但是把问题简化,到最右点之前的巡游路线只能严格向右,到最右边的点以后,返回的时候严格向左,这个问题就可以在多项式时间内求出来了. 定义状态d[i][j]表示一个人在i号点,令一个人在j ...

  6. 国家气象局提供的天气预报接口(完整Json接口)

    国家气象局提供的天气预报接口主要有三个,分别是:http://www.weather.com.cn/data/sk/101010100.htmlhttp://www.weather.com.cn/da ...

  7. STL:string类中size()与length()的区别

    结论是:两者没有任何区别 解释: C++Reference中对于两者的解释: 两者的具体解释都一模一样: 理解: length是因为C语言的习惯而保留下来的,string类最初只有length,引进S ...

  8. 基于KMeans的指数择时策略

    [导语]:聚类分析是指将物理或者抽象对象的结合分组为由类似对象组成的多个类的分析过程.简单来讲,聚类就是通过一些特征去自动识别一个大群体中的多个子群体,这些子群体中的对象彼此之间相似度高,而子群体之间 ...

  9. Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml]

    这是因为我把 [/WEB-INF/dispatcher-servlet.xml]的位置换成了[config/springmvc/dispatcher-servlet.xml] 因此idea在原来的位置 ...

  10. flume启动报错

    执行flume-ng agent -c conf -f conf/load_balancer_server.conf -n a1 -Dflume.root.logger=DEBUG,console , ...