[POI2007]MEG-Megalopolis

傳送門:嘟嘟嘟

?

第一反應是樹鏈剖分,但是太長懶得寫,然后就想出了一個很不錯的做法。

想一下,如果我們改一條邊,那么影響的只有他的子樹,只要先搞一個dfs序,為什么搞出這個呢?因為有一個性質:一個節點的子樹在dfs序上是連續的,所以這道題就變成了一個單點查詢,區間修改的線段樹(樹狀數組)板子題。

?

然后我不到40分鐘寫完了,非說有的點TLE,debug了一個多點……現在想想,一般遇到這種情況,一定不是什么正經問題。然而當時的我并不這么想,以為線段樹太慢了,改成差分+樹狀數組,各種優化,雖然在luogu卡了過去,然而內網卻GG。

終于放棄,到網上找標程,發現寫的幾乎一模一樣,就一行行比對……然后發現我為了省事兒讀字母用了cin,按標稱改成了scanf……結果最慢的點直接從900多ms變成了200多ms……然后內網秒過了……老子在此立flag:我,mrclr,如果再用cin,我就女裝!

?

心路歷程講完了,發下代碼吧

先是線段樹的

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cmath>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<stack>
  7 #include<queue>
  8 #include<vector>
  9 #include<cstdlib>
 10 #include<cctype> 
 11 using namespace std;
 12 #define enter puts("")
 13 #define space putchar(' ')
 14 #define Mem(a) memset(a, 0, sizeof(a))
 15 typedef long long ll;
 16 typedef double db;
 17 const int INF = 0x3f3f3f3f;
 18 const db eps = 1e-8;
 19 const int maxn = 3e5 + 5;
 20 inline ll read()
 21 {
 22     ll ans = 0;
 23     char ch = getchar(), last = ' ';
 24     while(!isdigit(ch)) {last = ch; ch = getchar();}
 25     while(isdigit(ch))
 26     {
 27         ans = ans * 10 + ch - '0'; ch = getchar();
 28     }
 29     if(last == '-') ans = -ans;
 30     return ans;
 31 }
 32 inline void write(ll x)
 33 {
 34     if(x < 0) putchar('-'), x = -x;
 35     if(x >= 10) write(x / 10);
 36     putchar(x % 10 + '0');
 37 }
 38 
 39 int n;
 40 vector<int> v[maxn];
 41 
 42 int dis[maxn], dfsx[maxn], size[maxn], pos[maxn], cnt = 0;
 43 void dfs(const int& now)
 44 {
 45     dfsx[now] = ++cnt; pos[cnt] = now;
 46     size[now] = 1;
 47     for(int i = 0; i < (int)v[now].size(); ++i)
 48     {
 49         dis[v[now][i]] = dis[now] + 1;
 50         dfs(v[now][i]);
 51         size[now] += size[v[now][i]];
 52     }
 53     return;
 54 }
 55 
 56 int l[maxn << 2], r[maxn << 2], dis2[maxn << 2], lazy[maxn << 2];
 57 void build(const int& L, const int& R, const int& now)
 58 {
 59     l[now] = L; r[now] = R;
 60     if(L == R) {dis2[now] = dis[pos[L]]; return;}
 61     int mid = (L + R) >> 1;
 62     build(L, mid, now << 1);
 63     build(mid + 1, R, now << 1 | 1);
 64     dis2[now] = dis2[now << 1] + dis2[now << 1 | 1];
 65 }
 66 void pushdown(const int& now)
 67 {
 68     if(lazy[now])
 69     {
 70         lazy[now << 1] += lazy[now];
 71         lazy[now << 1 | 1] += lazy[now];
 72         dis2[now << 1] -= lazy[now];
 73         dis2[now << 1 | 1] -= lazy[now];
 74         lazy[now] = 0;
 75     }
 76 }
 77 void update(const int& L, const int& R, const int& now)
 78 {
 79     if(!dis2[now]) return;
 80     if(l[now] == L && r[now] == R)
 81     {
 82         dis2[now]--; lazy[now]++; 
 83         return;
 84     }
 85     pushdown(now);
 86     int mid = (l[now] + r[now]) >> 1;
 87     if(R <= mid) update(L, R, now << 1);
 88     else if(L > mid) update(L, R, now << 1 | 1);
 89     else {update(L, mid, now << 1); update(mid + 1, R, now << 1 | 1);}
 90 }
 91 int query(const int& id, const int& now)
 92 {
 93     if(!dis2[now]) return 0;
 94     if(l[now] == r[now]) return dis2[now];
 95     pushdown(now);
 96     int mid = (l[now] + r[now]) >> 1;
 97     if(id <= mid) return query(id, now << 1);
 98     else return query(id, now << 1 | 1);
 99 }
100 
101 int main()
102 {
103     n = read();
104     for(int i = 1; i < n; ++i)
105     {
106         int x = read(), y = read();
107         if(y < x) swap(x, y);
108         v[x].push_back(y);
109     }
110     dfs(1);
111     build(1, cnt, 1);
112     int m = read();
113     char ch[5];
114     for(int i = 1; i < n + m; ++i)
115     {
116         scanf("%s", ch);
117         if(ch[0] == 'W')
118         {
119             int x = read();
120             write(query(dfsx[x], 1)); enter;
121         }
122         else
123         {
124             int x = read(), y = read();
125             if(y > x) swap(x, y);
126             update(dfsx[x], dfsx[x] + size[x] - 1, 1);
127         }
128     }
129     return 0;
130 }
View Code

然后又寫了個差分+樹狀數組

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cmath>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<stack>
  7 #include<queue>
  8 #include<vector>
  9 #include<cstdlib>
 10 #include<cctype> 
 11 using namespace std;
 12 #define enter printf("\n")
 13 #define space putchar(' ')
 14 #define Mem(a) memset(a, 0, sizeof(a))
 15 typedef long long ll;
 16 typedef double db;
 17 const int INF = 0x3f3f3f3f;
 18 const db eps = 1e-8;
 19 const int maxn = 3e5 + 5;
 20 inline ll read()
 21 {
 22     ll ans = 0;
 23     char ch = getchar(), last = ' ';
 24     while(!isdigit(ch)) {last = ch; ch = getchar();}
 25     while(isdigit(ch))
 26     {
 27         ans = ans * 10 + ch - '0'; ch = getchar();
 28     }
 29     if(last == '-') ans = -ans;
 30     return ans;
 31 }
 32 inline void write(ll x)
 33 {
 34     if(x < 0) putchar('-'), x = -x;
 35     if(x >= 10) write(x / 10);
 36     putchar(x % 10 + '0');
 37 }
 38 
 39 int n;
 40 vector<int> v[maxn];
 41 
 42 int dis[maxn], dfsx[maxn], size[maxn], pos[maxn], cnt = 0;
 43 void dfs(const int& now)
 44 {
 45     dfsx[now] = ++cnt; pos[cnt] = now;
 46     size[now] = 1;
 47     if(!v[now].size()) return;
 48     for(int i = 0; i < (int)v[now].size(); ++i)
 49     {
 50         dis[v[now][i]] = dis[now] + 1;
 51         dfs(v[now][i]);
 52         size[now] += size[v[now][i]];
 53     }
 54 }
 55 
 56 int cf[maxn], c[maxn];
 57 inline int lowbit(int x)
 58 {
 59     return x & -x;
 60 }
 61 void add(int pos, int x)
 62 {
 63     while(pos <= cnt)
 64     {
 65         c[pos] += x;
 66         pos += lowbit(pos);
 67     }
 68 }
 69 int sum(int pos)
 70 {
 71     int ret = 0;
 72     while(pos)
 73     {
 74         ret += c[pos];
 75         pos -= lowbit(pos);
 76     }
 77     return ret;
 78 }
 79 
 80 
 81 int main()
 82 {
 83     n = read();
 84     for(int i = 1; i < n; ++i)
 85     {
 86         int x = read(), y = read();
 87         if(y < x) swap(x, y);
 88         v[x].push_back(y);
 89     }
 90     dfs(1);
 91     for(int i = 1; i <= cnt; ++i) cf[i] = dis[pos[i]] - dis[pos[i - 1]];
 92     for(int i = 1; i <= cnt; ++i) add(i, cf[i]);
 93     int m = read();
 94     char ch[5];
 95     for(int i = 1; i < n + m; ++i)
 96     {
 97         scanf("%s", ch);
 98         if(ch[0] == 'W')
 99         {
100             int x = read();
101             write(sum(dfsx[x])); enter;
102         }
103         else
104         {
105             int x = read(), y = read();
106             if(y > x) swap(x, y);
107             add(dfsx[x], -1); add(dfsx[x] + size[x], 1);
108         }
109     }
110     return 0;
111 }
View Code

雖然都能過,不過樹狀數組比線段樹快了一倍

轉載于:https://www.cnblogs.com/mrclr/p/9485885.html

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

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

相關文章

memcache在ThinkPHP中的使用1---PHP下安裝memcache

1.什么是Memcached緩存 Memcached是一套小巧、高效且成熟的內存數據庫。與普通的數據庫不同&#xff0c;Memcached存儲的數據只能是簡單的鍵值對&#xff0c;在查詢時需要根據存放的key獲取數據。 Memcached最大的特點是數據存放于內存&#xff0c;性能會比傳統文件系統高出…

【集合工具類:Collections】

集合工具類&#xff1a;Collections(1) 是針對集合進行操作的工具類(2) 面試題&#xff1a;Collection 和 Collections 的區別A:Collection 是單列集合的頂層接口&#xff0c;有兩個子接口 List 和 SetB:Collections 是針對集合進行操作的工具類&#xff0c;可以對集合進行排序…

骨骼收集器01背包

來源hdu2602 問題描述 許多年前&#xff0c;在泰迪的家鄉&#xff0c;有一個人被稱為“骨頭收藏家”。這個男人喜歡收集各種各樣的骨頭&#xff0c;比如狗狗&#xff0c;牛&#xff0c;還有他去了墳墓...... 骨頭收藏家有一個大容量的V袋&#xff0c;沿著他的收集之旅有很多骨頭…

ZooKeeper的安裝與部署

本文講述如何安裝和部署ZooKeeper。 一、系統要求 ZooKeeper可以運行在多種系統平臺上面&#xff0c;表1展示了zk支持的系統平臺&#xff0c;以及在該平臺上是否支持開發環境或者生產環境。 表1&#xff1a;ZooKeeper支持的運行平臺 | 系統 | 開發環境 | 生產環境| | Linux…

java基礎1之java語言基礎1

一、常量的概述和使用 A:什么是常量 * 在程序執行的過程中其值不可以發生改變 B:Java中常量的分類 * 字面值常量 * 自定義常量(面向對象部分講) C:字面值常量的分類 * 字符串常量 用雙引號括起來的內容 * 整數常量 所有整數 * 小數常量 所有小數 * 字符常量 …

w3c 跨域請求規范

w3c 官方跨域請求規范地址&#xff1a;https://www.w3.org/TR/cors/ 前段時間開發的一個api在IE8/9瀏覽器不能正常響應跨域請求&#xff0c;以下是解決問題中對跨域請求新的認識 1.復雜跨域 post請求在發送請求之前&#xff0c;會先發送options 請求&#xff0c;有的服務器拒絕…

python爬取豆瓣前25個影片內容的正則表達式練習

通過python正則表達式獲取豆瓣top250的第一頁的25個影片排名,影片名字,影片連接,導演,主演,上映日期,國家,劇情,評分,評價人數的內容 網頁html內容: 1 <ol class"grid_view">2 <li>3 <div class"item">4 …

JavaScript 面向對象的程序設計1

一、理解對象 1.創建一個對象&#xff0c;然后給這個對象新建屬性和方法。 ①常見的創建方式 var person new Object(); //創建一個Object 對象person.name XIE; //創建一個name 屬性并賦值person.age 20; //創建一個age 屬性并賦值person.sayName function () { //創建…

Zookeeper 使用

安裝和配置詳解 本文介紹的 Zookeeper 是以 3.2.2 這個穩定版本為基礎&#xff0c;最新的版本可以通過官網 http://hadoop.apache.org/zookeeper/來獲取&#xff0c;Zookeeper 的安裝非常簡單&#xff0c;下面將從單機模式和集群模式兩個方面介紹 Zookeeper 的安裝和配置。 單…

Asp.Net Core 工作單元 UnitOfWork UOW

Asp.Net Core 工作單元示例 來自 ABP UOW 去除所有無用特性 代碼下載 &#xff1a; 去除所有無用特性版本&#xff0c;原生AspNetCore實現 差不多 2278 行代碼&#xff1a; 鏈接&#xff1a;https://pan.baidu.com/s/1NoEIDSAPNr46xNHYEx9KCA 提取碼&#xff1a;570i 包含C…

網站性能優化--CRP

網站性能優化–CRP 為了把HTML、CSS和JavaScript轉化成活靈活現、絢麗多彩的網頁&#xff0c;瀏覽器需要處理一系列的中間過程&#xff0c;優化性能其實就是了解這個過程中發生了什么-即CRP(Critical Rendering Path&#xff0c;關鍵渲染路徑)。首先&#xff0c;我們從頭開始快…

Dubbo+zookeeper基礎講解

一、dubbo是什么&#xff1f; 1&#xff09;本質&#xff1a;一個Jar包,一個分布式框架,&#xff0c;一個遠程服務調用的分布式框架。 既然是新手教學&#xff0c;肯定很多同學不明白什么是分布式和遠程服務調用&#xff0c;為什么要分布式&#xff0c;為什么要遠程調用。我簡…

What Are You Talking About HDU1075

一開始我也想用map 但是處理不好其他字符。。 看了題解 多多學習&#xff01; 很巧妙 就是粗暴的一個字符一個字符的來 分為小寫字母和非小寫字母兩個部分 一但單詞結束的時候就開始判斷。 #include<bits/stdc.h> using namespace std;int main() {string a,b;map&l…

開通博客第一天

今天是開通博客第一天&#xff0c; 第一次寫博客&#xff0c;也不知道寫什么&#xff0c; 以后寫點技術文&#xff0c;把我的經驗分享給大家&#xff0c; 不對的地方請大家指正&#xff0c;一起進步。我要把我每遇到的難題以及學到的知識和技術為大家踩坑&#xff0c; 做研究。…

學習File API用于前端讀取文件

1. File API簡介 File API對于某些專門的網站的不可或缺的。現在常用它實現對文件的預覽等功能。 File API規定怎么從硬盤上提取文件&#xff0c;直接交給在網頁中運行中的Javascript代碼。然后代碼可以打開文件探究數據&#xff0c;無論是本地文件還是其他文件。注意&#x…

kafka筆記1

Kafka是一款基于發布和訂閱的消息系統。一般被稱為分布式提交日志或分布式流平臺。 Kafka系統是按照一定的順序持久化保存的&#xff0c;可以按需讀取。 Kafka的數據單元被稱為消息。類似于數據庫中表的一行記錄&#xff0c;消息由字節組成&#xff0c;所以沒有特別的格式和含義…

Dubbo入門教程

服務端&#xff08;dubbo-server&#xff09; 1. pom.xml <?xml version"1.0" encoding"UTF-8"?> <project xmlns"http://maven.apache.org/POM/4.0.0"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaL…

NSAssert和NSParameterAssert

2016.05.05 18:34* 字數 861 閱讀 5127評論 0喜歡 17https://www.jianshu.com/p/3072e174554fNSAssert和NSParameterAssert在開發環境中經常被使用&#xff0c;調試和驗證代碼參數的完整性&#xff0c;斷言為真&#xff0c;則表明程序運行正常&#xff0c;而斷言為假&#xff0…

【PAT】B1070 結繩(25 分)

此題太給其他25分的題丟人了&#xff0c;只值15分 注意要求最終結果最長&#xff0c;而且向下取整 #include<stdio.h> #include<algorithm> using namespace std; float arr[10005]; int main(){int N;scanf("%d",&N);for(int i0;i<N;i)//輸入數據…

Java代碼實現負載均衡五種算法

前言&#xff1a; 負載均衡是為了解決并發情況下&#xff0c;多個請求訪問&#xff0c;把請求通過提前約定好的規則轉發給各個server。其中有好幾個種經典的算法。在用java代碼編寫這幾種算法之前&#xff0c;先來了解一下負載均衡這個概念。 1.概念 負載&#xff0c;從字面…