当前位置: 首页 > news >正文

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;
}
/*

*/


相关文章:

  • 2014 I/O归来:Google连接一切
  • kvm虚拟化技术中虚拟机vcpu与物理cpu绑定
  • YMP开发框架快速上手(一)
  • Apache CXF 框架结构和基本原理
  • PostgreSQL 9.5 使用 import foreign schema 语法一键创建外部表
  • 应用如何在后台执行
  • c语言随机数
  • 混合使用Azure LB和ILB访问相同web服务(3)
  • git服务器修改ssh端口后配置方法
  • POJ 1005 I Think I Need a Houseboat
  • compileDebugJavaWithJavac
  • OSI七层与TCP/IP四层模型
  • 3-9-模拟银行排队过程-栈和队列-第3章-《数据结构》课本源码-严蔚敏吴伟民版...
  • 《从零开始学Swift》学习笔记(Day 24)——枚举(Day 24)——枚举
  • 日志管理
  • (ckeditor+ckfinder用法)Jquery,js获取ckeditor值
  • 【MySQL经典案例分析】 Waiting for table metadata lock
  • ➹使用webpack配置多页面应用(MPA)
  • 0基础学习移动端适配
  • android 一些 utils
  • angular2开源库收集
  • create-react-app做的留言板
  • CSS相对定位
  • Docker下部署自己的LNMP工作环境
  • JavaScript设计模式之工厂模式
  • Python_网络编程
  • SSH 免密登录
  • vue中实现单选
  • 给新手的新浪微博 SDK 集成教程【一】
  • 面试题:给你个id,去拿到name,多叉树遍历
  • 适配mpvue平台的的微信小程序日历组件mpvue-calendar
  • 云大使推广中的常见热门问题
  • media数据库操作,可以进行增删改查,实现回收站,隐私照片功能 SharedPreferences存储地址:
  • JavaScript 新语法详解:Class 的私有属性与私有方法 ...
  • 机器人开始自主学习,是人类福祉,还是定时炸弹? ...
  • ​queue --- 一个同步的队列类​
  • ​Z时代时尚SUV新宠:起亚赛图斯值不值得年轻人买?
  • #100天计划# 2013年9月29日
  • #ifdef 的技巧用法
  • (Bean工厂的后处理器入门)学习Spring的第七天
  • (C#)Windows Shell 外壳编程系列9 - QueryInfo 扩展提示
  • (笔试题)分解质因式
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (附源码)计算机毕业设计ssm电影分享网站
  • (论文阅读40-45)图像描述1
  • (篇九)MySQL常用内置函数
  • (转)LINQ之路
  • (轉貼) 資訊相關科系畢業的學生,未來會是什麼樣子?(Misc)
  • .360、.halo勒索病毒的最新威胁:如何恢复您的数据?
  • .NET / MSBuild 扩展编译时什么时候用 BeforeTargets / AfterTargets 什么时候用 DependsOnTargets?
  • .NET CORE Aws S3 使用
  • .net framework4与其client profile版本的区别
  • .Net IE10 _doPostBack 未定义
  • .net mvc 获取url中controller和action
  • .NET 中选择合适的文件打开模式(CreateNew, Create, Open, OpenOrCreate, Truncate, Append)