SPOJ QTREE4 lct
这个题已经处于花式tle了,改版后的spoj更慢了。。
tle的话就多交几把。。。
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <time.h>
- #include <vector>
- #include <map>
- #include <queue>
- #include <algorithm>
- #include <stack>
- #include <cstring>
- #include <cmath>
- #include <set>
- #include <vector>
- using namespace std;
- template <class T>
- inline bool rd(T &ret) {
- char c; int sgn;
- if (c = getchar(), c == EOF) return 0;
- while (c != '-' && (c<'0' || c>'9')) c = getchar();
- sgn = (c == '-') ? -1 : 1;
- ret = (c == '-') ? 0 : (c - '0');
- while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
- ret *= sgn;
- return 1;
- }
- template <class T>
- inline void pt(T x) {
- if (x < 0) {
- putchar('-');
- x = -x;
- }
- if (x > 9) pt(x / 10);
- putchar(x % 10 + '0');
- }
- typedef long long ll;
- typedef pair<int, int> pii;
- const int N = 200000 + 10;
- const int inf = 1e9;
- struct Node *null;
- inline int First(multiset<int>&x) {//返回x中最大的数
- return *x.rbegin();
- }
- inline int Second(multiset<int>&x) {//返回x中次大的数
- multiset<int>::reverse_iterator it = x.rbegin();
- it++; return *it;
- }
- struct Node {
- Node *fa, *ch[2];
- int size;
- multiset<int>path, chain;
- int ls, rs, ms;
- int col, len, id;
- bool rev;
- inline void put() {
- 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);
- // cout << "path: ";for (auto i : path)cout << i << " ";puts("");
- // cout << "chain:";for (auto i : chain)cout << i << " ";puts("");
- }
- inline void clear(int _col, int _id) {
- fa = ch[0] = ch[1] = null;
- rev = 0;
- id = _id;
- col = _col;
- size = len = 0;
- ls = rs = ms = -inf;
- path.clear(); chain.clear();
- chain.insert(-inf); chain.insert(-inf); path.insert(-inf);
- }
- inline void push_up() {
- if (this == null)return;
- size = len + ch[0]->size + ch[1]->size;
- int _chain = max(col, First(chain));
- int L = max(_chain, ch[0]->rs + len);//从(虚边 or 左子树)的白点到this的最远距离
- int R = max(_chain, ch[1]->ls);//从(虚边 or 右子树)的白点到this的最远距离
- ls = max(ch[0]->ls, ch[0]->size + len + R);
- rs = max(ch[1]->rs, ch[1]->size + L);
- ms = max(ch[0]->rs + len + R, L + ch[1]->ls);
- ms = max(ms, max(ch[0]->ms, ch[1]->ms));
- ms = max(ms, First(path));
- ms = max(ms, First(chain) + Second(chain));
- if (col == 0)
- ms = max(max(ms, First(chain)), 0);
- }
- inline void push_down() {
- if (rev) {
- ch[0]->flip();
- ch[1]->flip();
- rev = 0;
- }
- }
- inline void setc(Node *p, int d) {
- ch[d] = p;
- p->fa = this;
- }
- inline bool d() {
- return fa->ch[1] == this;
- }
- inline bool isroot() {
- return fa == null || fa->ch[0] != this && fa->ch[1] != this;
- }
- inline void flip() {
- if (this == null)return;
- swap(ch[0], ch[1]);
- rev ^= 1;
- }
- inline void go() {//从链头開始更新到this
- if (!isroot())fa->go();
- push_down();
- }
- inline void rot() {
- Node *f = fa, *ff = fa->fa;
- int c = d(), cc = fa->d();
- f->setc(ch[!c], c);
- this->setc(f, !c);
- if (ff->ch[cc] == f)ff->setc(this, cc);
- else this->fa = ff;
- f->push_up();
- }
- inline Node*splay() {
- // go();
- while (!isroot()) {
- if (!fa->isroot())
- d() == fa->d() ? fa->rot() : rot();
- rot();
- }
- push_up();
- return this;
- }
- inline Node* access() {//access后this就是到根的一条splay,而且this已经是这个splay的根了
- for (Node *p = this, *q = null; p != null; q = p, p = p->fa) {
- p->splay();
- if (p->ch[1] != null) {
- p->chain.insert(p->ch[1]->ls);
- p->path.insert(p->ch[1]->ms);
- }
- if (q != null) {
- p->chain.erase(p->chain.find(q->ls));
- p->path.erase(p->path.find(q->ms));
- }
- p->setc(q, 1);
- p->push_up();
- }
- return splay();
- }
- inline Node* find_root() {
- Node *x;
- for (x = access(); x->push_down(), x->ch[0] != null; x = x->ch[0]);
- return x;
- }
- void make_root() {
- access()->flip();
- }
- void cut() {//把这个点的子树脱离出去
- access();
- ch[0]->fa = null;
- ch[0] = null;
- push_up();
- }
- void cut(Node *x) {
- if (this == x || find_root() != x->find_root())return;
- else {
- x->make_root();
- cut();
- }
- }
- void link(Node *x) {
- if (find_root() == x->find_root())return;
- else {
- make_root(); fa = x;
- }
- }
- };
- void debug(Node *x) {
- if (x == null)return;
- x->put();
- debug(x->ch[0]);
- debug(x->ch[1]);
- }
- Node pool[N], *tail;
- Node *node[N];
- int n, q;
- struct Edge {
- int to, next, dis;
- }edge[N<<1];
- int head[N], edgenum;
- inline void add(int u, int v, int dis) {
- Edge E = { v, head[u], dis };
- edge[edgenum] = E;
- head[u] = edgenum++;
- }
- void dfs(int u, int fa) {
- for (int i = head[u]; ~i; i = edge[i].next){
- int v = edge[i].to; if (v == fa)continue;
- node[v]->fa = node[u];
- node[v]->len = edge[i].dis;
- dfs(v, u);
- node[u]->path.insert(node[v]->ms);
- node[u]->chain.insert(node[v]->ls);
- }
- node[u]->push_up();
- }
- int main() {
- while (cin >> n) {
- tail = pool;
- null = tail++;
- null->clear(-inf, 0);
- edgenum = 0;
- for (int i = 1; i <= n; i++) {
- head[i] = -1;
- node[i] = tail++;
- node[i]->clear(0, i);
- }
- for (int i = 1, u, v, d; i < n; i++) {
- rd(u); rd(v); rd(d);
- add(u, v, d); add(v, u, d);
- }
- dfs(1, 1);
- rd(q); char str[5]; int u;
- int ans = node[1]->ms;
- while (q--) {
- scanf("%s", str);
- if (str[0] == 'C') {
- rd(u);
- node[u]->access();
- if (node[u]->col == 0)node[u]->col = -inf;
- else node[u]->col = 0;
- node[u]->push_up();
- ans = node[u]->ms;
- }
- else {
- if (ans < 0)puts("They have disappeared.");
- else pt(ans), puts("");
- }
- // for (int i = 1; i <= n; i++)debug(node[i]), puts("");
- }
- }
- return 0;
- }
- /*
- */
SPOJ QTREE4 lct的更多相关文章
- SPOJ - OTOCI LCT
OTOCI Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/viewProblem. ...
- SPOJ QTREE3 lct
题目链接 题意: 给定n个点 q个询问 以下n-1行给出树边,点有黑或白色.初始化为白色 以下q行: 询问有2种: 1. 0 x 把x点黑变白,白变黑 2.1 x 询问Path(1,x)路径上第一个黑 ...
- 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. ...
- SPOJ QTREE2 lct
题目链接 题意: 给一棵树.有边权 1.询问路径的边权和 2.询问沿着路径的第k个点标. 思路:lct裸题. #include <iostream> #include <fstrea ...
- SPOJ QTREE5 lct
题目链接 对于每一个节点,记录这个节点所在链的信息: ls:(链的上端点)距离链内部近期的白点距离 rs:(链的下端点)距离链内部近期的白点距离 注意以上都是实边 虚边的信息用一个set维护. set ...
- SPOJ QTREE4 - Query on a tree IV 树分治
题意: 给出一棵边带权的树,初始树上所有节点都是白色. 有两种操作: C x,改变节点x的颜色,即白变黑,黑变白 A,询问树中最远的两个白色节点的距离,这两个白色节点可以重合(此时距离为0). 分析: ...
- 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. ...
- SPOJ QTREE4 Query on a tree IV ——动态点分治
[题目分析] 同bzoj1095 然后WA掉了. 发现有负权边,只好把rmq的方式改掉. 然后T了. 需要进行底(ka)层(chang)优(shu)化. 然后还是T 下午又交就A了. [代码] #in ...
- Query on a tree IV SPOJ - QTREE4
https://vjudge.net/problem/SPOJ-QTREE4 点分就没有一道不卡常的? 卡常记录: 1.把multiset换成手写的带删除堆(套用pq)(作用很大) 2.把带删除堆里面 ...
随机推荐
- J2SE知识点摘记(四)
1. 抽象类(abstract) 抽象类和抽象方法都必须用abstract关键字来修饰. 抽象类不能被直接实例化,也就是不能直接用new关键字去产生对象. 抽象方法只需声明,而不需实现. ...
- 汽车总线obd模拟器,obd仿真器,ecu模拟器,obd开发测试工具,可以模拟ecu中的obd协议 MRD-5050
汽车总线OBD模拟器MRD-5050型号是在车辆越来越趋于网络化的趋势下研发的,是汽车产品开发.调试.生产必备的工具,能为为开发人员节省大量的时间.当前车辆上的总线设备越来越多,有的高端车上甚至多到有 ...
- Linux中service命令和/etc/init.d/的关系
Linux中service命令和/etc/init.d/的关系 service xxx启动 /etc/init.d/ 目录下的xxx脚本 如一个脚本名为 mysvc保存在/etc/init.d/下 ...
- SQLServer,仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表xx中的标识列指定显式值
情景: 如果此表的主键或者其中有一个列使用了 IDENTITY(1,1) 自增长时,但又想手动为此列指定值时,当用如下解决方案: set identity_insert 表名 ON 使用此命令把表的 ...
- mongodb use db show dbs
mongodb 常用命令: 在dbs间切换用 use xxxdb 之后再操作就是只针对 xxxdb了: show dbs显示全部数据库 show collections 显示全部集合 mongodb数 ...
- Jquery弹窗插件Lhgdialog的用法
Lhgdialog的用法 大家都知道用js可以实现,但是在使用js实现的弹窗时得考虑很东西:浏览器的兼容.页面的交互等等问题. 在这里简单介绍一下lhgdialog的用法. 参数有: Title:弹窗 ...
- Dreamweaver PHP代码护眼配色方案
结果展示 [1]主菜单选择编辑->首选项.在分类中选择"字体".设置代码视图的字体为Courier New [2]在分类中选择 "代码颜色",点击 &qu ...
- KVC在定义Model类中的妙用
@我们应用程序使用MVC架构的话,对于处理数据类,我们会单独的定义Model类,在里面为要展示的属性进行初始化赋值,一般採用的方法是通过定义相应的属性,挨个赋值.如今我要介绍的就是通过KVC,key- ...
- Web API Login with Cookie
public HttpWebResponse InitiliazeWebRequest() { string responseData =string.Empty;//string url = Get ...
- php什么是变量的数据类型
什么是变量的数据类型 在变量中,由于变量占用的空间单元不一样(占的地盘大小不一样),也分成几种数据类型,就像超市商品的包装袋,有几种不同类型,不同的商品使用不同的包装袋.我们可以通过使用“memory ...