SPOJ QTREE6 lct

題目鏈接

島娘出的題。還是比較easy的

#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 = 100005; const int inf = 10000000; struct Node *null; struct Node { Node *fa, *ch[2]; int id; int s[2];//s[0] this虛邊所連的全部子樹的連續白色點數總和(不是鏈) int col; int ls[2], rs[2], siz; //ls[0]是對于這條鏈 最上端向下的連續白色點數 int Ls[2], Rs[2]; //Ls[0]是對于這棵子樹 與最上端點相連的連續白色點數 bool rev; inline void clear(int _col, int _id) { fa = ch[0] = ch[1] = null; siz = 1; rev = 0; id = _id; col = _col; for (int i = 0; i < 2; i++) { ls[i] = rs[i] = s[i] = 0; } } inline void push_up() { if (this == null)return; siz = ch[0]->siz + ch[1]->siz + 1; for (int i = 0; i < 2; i++) { ls[i] = ch[0]->ls[i], rs[i] = ch[1]->rs[i]; Ls[i] = ch[0]->Ls[i], Rs[i] = ch[1]->Rs[i]; if (ch[0]->ls[i] == ch[0]->siz && i == col) { ls[i] = ch[0]->siz + 1 + ch[1]->ls[i]; Ls[i]++; Ls[i] += s[i]; Ls[i] += ch[1]->Ls[i]; } if (ch[1]->rs[i] == ch[1]->siz && i == col) { rs[i] = ch[1]->siz + 1 + ch[0]->rs[i]; Rs[i]++; Rs[i] += s[i]; Rs[i] += ch[0]->Rs[i]; } } } 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) for (int i = 0;i < 2;i++) p->s[i] += p->ch[1]->Ls[i]; if (q != null) for (int i = 0; i < 2; i++) p->s[i] -= q->Ls[i]; 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; } } }; Node pool[N], *tail; Node *node[N]; int n, q; struct Edge { int to, nex; }edge[N << 1]; int head[N], edgenum; void add(int u, int v) { Edge E = { v, head[u] }; edge[edgenum] = E; head[u] = edgenum++; } void dfs(int u, int fa) { for (int i = head[u]; ~i; i = edge[i].nex) { int v = edge[i].to; if (v == fa)continue; node[v]->fa = node[u]; dfs(v, u); for (int j = 0; j < 2; j++) node[u]->s[j] += node[v]->Ls[j]; } node[u]->push_up(); } int main() { while (cin >> n) { memset(head, -1, sizeof head); edgenum = 0; for (int i = 1, u, v; i < n; i++) { rd(u); rd(v); add(u, v);add(v, u); } tail = pool; null = tail++; null->clear(-1, 0); null->siz = 0; for (int i = 1; i <= n; i++) { node[i] = tail++; node[i]->clear(1, i); } dfs(1, 1); rd(q); int u, v; while (q--) { rd(u); rd(v); if (!u) { node[v]->access(); pt(max(node[v]->Rs[0], node[v]->Rs[1])); puts(""); } else { node[v]->access(); node[v]->col ^= 1; node[v]->push_up(); } } } return 0; }



本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/395697.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395697.shtml
英文地址,請注明出處:http://en.pswp.cn/news/395697.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

使用charles 抓取手機上的操作

Charles上的設置要截取iPhone上的網絡請求&#xff0c;我們首先需要將Charles的代理功能打開。在Charles的菜單欄上選擇“Proxy”->“Proxy Settings”&#xff0c;填入代理端口8888&#xff0c;并且勾上”Enable transparent HTTP proxying” 就完成了在Charles上的設置。如…

FreeCodeCamp納什維爾聚會的回顧

by Seth Alexander塞斯亞歷山大(Seth Alexander) FreeCodeCamp納什維爾聚會的回顧 (A Recap from the freeCodeCamp Nashville Meetup) At a recent freeCodeCamp meetup, a small group of campers got together to solve some coding challenges and we talk shop.在最近的f…

php查詢車位系統代碼,php車輛違章查詢數據示例

方便有車一族隨時了解自己是否有過交通違章&#xff0c;避免因遺忘或逾期處理違章罰單而造成的不必要損失。本代碼示例是基于聚合數據全國車輛違章查詢API的調用&#xff0c;有需要的可以往下看。使用前你需要&#xff1a;一、引入封裝好的請求類class.juhe.wz.phpheader(Conte…

[HNOI2011]XOR和路徑

嘟嘟嘟 一看到異或&#xff0c;就想到按位處理&#xff0e; 當處理到第\(i\)位的時候&#xff0c;\(f[u]\)表示節點\(u\)到\(n\)的路徑&#xff0c;這一位為\(1\)的期望&#xff0c;那么為\(0\)就是\(1 - f[u]\)&#xff0c;于是有\[f[u] \frac{1}{d[u]} (\sum _ {v \in V, w …

PHP 文件加密Zend Guard Loader 學習和使用(如何安裝ioncube擴展對PHP代碼加密)

一、大體流程圖 二、PHP 項目文件加密 下表列出了Zend產品中的PHP版本及其內部API版本和Zend產品版本。 如何加密請往后看 三、如何使用 第一步&#xff1a;確認當前環境 Amai Phalcon 前&#xff0c;請確認您具備以下兩個條件&#xff0c;如果您的環境不滿足此條件&#xff0c…

前向聲明

前向聲明的定義&#xff1a;有些時候我們可以聲明一些類但是并不去定義它&#xff0c;當然這個類的作用也很有限了。 如&#xff1a;class A; 聲明一個foo類&#xff0c;這個聲明&#xff0c;有時候也叫做前向聲明(forward declaration)&#xff0c;在聲明完這個foo類之后&…

php尋找文本,PHP文本數據庫的搜索方法_php

//php文本數據庫的搜索方法searchstr("/".preg_quote($searchstr)."/");//$searchstr是查找的關鍵字$recordsfile($file);//獲取所有的記錄數http://www.gaodaima.com/45906.htmlPHP文本數據庫的搜索方法_php//$file是查找的數據文件$search_reocrdspreg_g…

react vs 2017_我在React Europe 2017上學到了什么

react vs 2017by Nicolas Cuillery由Nicolas Cuillery 我在React Europe 2017上學到了什么 (What I learned at React Europe 2017) Few days ago, the 3rd edition of the biggest React conference in Europe took place in Paris. No heatwave or transportation strike th…

rem 之js代碼獲取font-size值(適合移動手機端)

這兩天學的是自適應&#xff0c;代碼有點亂。而且這幾天忙著寫實習報告&#xff0c;也沒有時間去整理。 但是&#xff0c;這下面代碼吧&#xff0c;是可以獲取html的font-size值的&#xff0c;然后用來設置相對單位rem的從而達到自適應效果的&#xff1b;看到紅色的width了吧&a…

關于C#中委托的一點理解

C#中委托是一種類型。可以這么籠統的理解&#xff1a;int型變量代表一個整型&#xff0c;而委托類型的變量代表一個方法的地址&#xff08;將方法名稱傳入constructor并實例化該委托變量&#xff09;。 --By Brisk Yu 1 為何要使用委托 我覺得網上關于什么現實生活的舉例并不好…

阿里的事前驗尸_(不太完全)100天的代碼-驗尸

阿里的事前驗尸by JS由JS (不太完全)100天的代碼-驗尸 ((Not quite) 100 Days of Code — A Postmortem) At the end of last year, I wrote about my experience coding and making daily commits to GitHub for 30 consecutive days. I also pledged to keep the streak goi…

php超市管理系統論文,超市管理系統的設計與實現

當今社會為信息社會&#xff0c;世界已經進入在計算機信息管理領域中激烈競爭的時代。對于一般的商戶而言&#xff0c;雜亂無章地陳放著的商品無疑會耗費他們大量的時間去對其整理并一一分類。他們需要更加便捷的手段去管理他們的商品以節約他們的時間成本以及人工成本。并且就…

只能輸入正整數 以及常用的正則表達式

<input typetext idSYS_PAGE_JumpPage nameSYS_PAGE_JumpPage size3 maxlength5 οnkeyupthis.valuethis.value.replace(/[^1-9]/D*$/,"") οndragenter"return false" οnpaste"return !clipboardData.getData(text).match(//D/)"" sty…

jq 自動滑動輪換(向后插入小塊)

// JavaScript Documentvar Marquee { arrIdObj : {/*marqueebox : {distance:-95,//移動距離delay:3000,//延遲時間speed:1000//移動時間},minCount:2 */}, //創建對象 startMarquee:function(){ //給參數賦值 if(this.arrIdObj ! null && typeof this.arrIdObj &qu…

bzoj 2178 圓的面積并 —— 辛普森積分

題目&#xff1a;https://www.lydsy.com/JudgeOnline/problem.php?id2178 先看到這篇博客&#xff1a;https://www.cnblogs.com/heisenberg-/p/6740654.html 好像本應算弓形面積、三角形面積之類的&#xff0c;但不會...于是用辛普森積分硬做... 參考了這篇博客&#xff1a;ht…

php獲取訪問者ip地址匯總,php獲取訪問者IP地址匯總_PHP

//方法1&#xff1a;$ip $_SERVER["REMOTE_ADDR"];echo $ip;//方法2&#xff1a;代碼如下:$user_IP ($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];$user_IP ($user_IP) ? $user_IP : $…

Charles抓包工具的使用

2019獨角獸企業重金招聘Python工程師標準>>> 感謝唐巧分享的文章&#xff0c;受益匪淺 文章目錄 1. 目錄及更新說明2. Charles 限時優惠3. 簡介4. 安裝 Charles5. 將 Charles 設置成系統代理6. Charles 主界面介紹7. 過濾網絡請求8. 截取 iPhone 上的網絡封包 8.1. …

python每秒20個請求_使用Python每秒百萬個請求

python每秒20個請求by Pawe? Piotr Przeradowski通過Pawe?Piotr Przeradowski 使用Python每秒百萬個請求 (A million requests per second with Python) Is it possible to hit a million requests per second with Python? Probably not until recently.使用Python每秒可以…

iOS開發——處理1000張圖片的內存優化

一、項目需求 在實際項目中&#xff0c;用戶在上傳圖片時&#xff0c;有時會一次性上傳大量的圖片。在上傳圖片前&#xff0c;我們要進行一系列操作&#xff0c;比如&#xff1a;旋轉圖片為正確方向&#xff0c;壓縮圖片等&#xff0c;這些操作需要將圖片加載到內存中&#xff…

jquery ui php,php – 打開帶有動態內容的jQuery UI對話框

我有一個關于jQuery UI對話框的問題,并顯示數據庫中的動態內容.所以我得到了一個web應用程序,我還需要創建一個管理模塊來管理所有用戶和其他信息.我創建了一個頁面,顯示列表中的所有用戶,在每一行中我也創建了一個編輯按鈕.我想這樣做,當你按下用戶的編輯按鈕時,會打開一個對話…