hdu 4747 mex 線段樹+思維

http://acm.hdu.edu.cn/showproblem.php?pid=4747

題意:

我們定義mex(l,r)表示一個序列a[l]....a[r]中沒有出現過得最小的非負整數, 然后我們給出一個長度為n的序列,求他所有的連續的子序列的mex(l,r)的和。

思路:

首先因為n的最大值就是2*10^5 所有我們字需要考慮200000之內的數就好了,然后O(2*n)可以求出(1,1),(1,2), (1,3),(1,4) ... (1,n)來 mex是不減的。

然后我們考慮將第一個數拿走我們就能夠得到(2,2),(2,3) ......(2,n) , 如何求他們?下邊給出圖解:

下邊是粘貼別人的,感覺有個例子很好理解。

例: ? ? ? ??? 1, 6,0,2,3,1,4,3

初始mex 0,?0,2,3,4,4,5,5 ? ? ???mex[1,r]

刪除1后 ? 0, ?0,1,1,1,4,5,5 ? ? ??? mex[2,r]

……

當刪除第一個1后,紅色的mex不變!,紫色的mex值變為1,橙色的mex值不變,刪除點的mex置0

因此,用線段樹維護一個單調不遞增隊列,每次求和。查找位置時用二分。線段樹延時標記即可。

?

ps:我這里二筆了一下,題意一下大家,lazy一定要出事化為-1,因為這里面有置0的操作。我因此wa好多次。。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>#define CL(arr, val)    memset(arr, val, sizeof(arr))#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0)
#define ll __int64
#define L(x)    (x) << 1
#define R(x)    (x) << 1 | 1
#define MID(l, r)   (l + r) >> 1
#define Min(x, y)   (x) < (y) ? (x) : (y)
#define Max(x, y)   (x) < (y) ? (y) : (x)
#define E(x)        (1 << (x))
#define iabs(x)     (x) < 0 ? -(x) : (x)
#define OUT(x)  printf("%I64d\n", x)
#define keyTree (chd[chd[root][1]][0])
#define Read()  freopen("din.txt", "r", stdin)
#define Write() freopen("dout.txt", "w", stdout);#define M 100007
#define N 200017using namespace std;int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};const int inf = 0x7f7f7f7f;
const int mod = 1000000007;const double eps = 1e-8;int mex[N],a[N],next[N];
int vis[N];
ll val[4*N], lz[4*N];
int n;void pushup(int rt)
{val[rt] = val[rt<<1] + val[rt<<1|1];
}
void pushdown(int rt,int m)
{if (lz[rt] != -1){lz[rt<<1] = lz[rt<<1|1] = lz[rt];val[rt<<1] = lz[rt] * (m - (m>>1));val[rt<<1|1] = lz[rt] * (m>>1);lz[rt] = -1;}
}
void build(int l, int r, int rt)
{lz[rt] = -1;val[rt] = 0;if (l == r){val[rt] = mex[l];return ;}int m = (l + r) >> 1;build(lc);  build(rc);pushup(rt);
}
void update(int L, int R, ll sc, int l, int r, int rt)
{if (l >= L && r <= R){lz[rt] = sc;val[rt] = sc*(r - l + 1);return ;}pushdown(rt,r - l + 1);int m = (l + r) >> 1;if (L <= m) update(L,R,sc,lc);if (R > m) update(L,R,sc,rc);pushup(rt);
}
ll query(int pos, int l,int r, int rt)
{if (l == r) return val[rt];int m = (l + r) >> 1;pushdown(rt, r - l + 1);if (pos <= m) return query(pos,lc);else return query(pos,rc);
}
int BSR(int l, int r, int v)
{int ans = -1;while (l <= r){int mid = (l + r) >> 1;if (query(mid,1,n,1) > v){ans = mid;r = mid - 1;} else l = mid + 1;}return ans;
}
int main()
{while (~scanf("%d",&n)){if (!n) break;CL(vis,0); CL(next,0);for (int i = 1; i <= n; ++i){scanf("%d",&a[i]);a[i] = min(a[i],200001);if (vis[a[i]]) next[vis[a[i]]] = i;vis[a[i]] = i;}for (int i = 1; i <= n; ++i) if (!next[i]) next[i] = n + 1;CL(vis,0);  int last = 0;for (int i = 1; i <= n; ++i){vis[a[i]] = 1;while (true){if (!vis[last]){mex[i] = last;break;}last++;}}build(1,n,1); ll ans = 0;for (int i = 1; i <= n; ++i){ans += val[1];if (i == n) continue;int l = i + 1;int r = next[i] - 1;int pos = BSR(l,r,a[i]);if (pos != -1) update(pos, r, a[i], 1, n, 1);update(i, i, 0, 1, n, 1);}printf("%I64d\n",ans);}return 0;
}

  

?

?

轉載于:https://www.cnblogs.com/E-star/p/3348552.html

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

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

相關文章

十、評估指標

我看過很多課程&#xff0c;不過內容都大差不差&#xff0c;也可以參考這篇模型評估方法 一、K折交叉驗證 一般情況&#xff0c;我們得到一份數據集&#xff0c;會分為兩類&#xff0c;一類是trainset訓練集&#xff0c;另一類十testset測試集。通俗一點也就是訓練集相當于平…

leetcode 47. 全排列 II 思考分析

題目 給定一個可包含重復數字的序列 nums &#xff0c;按任意順序 返回所有不重復的全排列。 思考分析以及代碼 這一題和前面的做過的兩個題目有所關聯&#xff1a; leetcode 46. 全排列 思考分析 再加上leetcode 491. 遞增子序列 思考分析類似的去重操作。 先畫出解空間樹…

python添加數組元素_在Python中向數組添加元素

python添加數組元素An array can be declared by using "array" module in Python. 可以通過在Python中使用“數組”模塊來聲明數組 。 Syntax to import "array" module: 導入“數組”模塊的語法&#xff1a; import array as array_alias_nameHere, im…

hdu 4472 Count(遞推即dp)

題目鏈接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid4472 代碼&#xff1a; #include <cstdio> #include <cstring> #include <iostream> #include <cmath> #include <algorithm> #include <queue> #include <vector> …

如何在Java中同步ArrayList?

同步ArrayList (Synchronizing ArrayList) In java, there are two ways to synchronize ArrayList, 在Java中&#xff0c;有兩種同步ArrayList的方法&#xff0c; With the help of synchronizedList() method 借助syncedList()方法 With the help of CopyOnWriteArrayList&l…

十一、決策樹和隨機森林

這門課和另一門課內容都差不多&#xff0c;可以參考七、決策樹算法和集成算法該篇博文。 一、決策樹相關概念 邏輯回歸本質 邏輯回歸&#xff1a;線性有監督分類模型。常用求解二分類問題&#xff0c;要么是A類別要么是B類別&#xff0c;一般會以0.5作為劃分閾值&#xff0c…

【C++grammar】繼承與構造

目錄1.繼承1、Inheritance (繼承)2、避免一個類被繼承&#xff08; C11 &#xff09;3、繼承實例4、完整代碼5、繼承的優缺點是什么?2.繼承中的構造函數1、 派生類繼承的成員2、調用基類構造函數3.繼承中的默認構造函數1、基類的無參構造函數2、由編譯器自動生成的基類構造函數…

C語言預處理

所謂預處理是指在進行編譯的第一遍掃描(詞法掃描和語法分析)之前所作的工作。預處理是&#xff23;語言的一個重要功能&#xff0c; 它由預處理程序負責完成。當對一個源文件進行編譯時&#xff0c; 系統將自動引用預處理程序對源程序中的預處理部分作處理&#xff0c; 處理完畢…

(轉)將cocos2dx項目從VS移植到Eclipse

本文轉自:http://www.cnblogs.com/Z-XML/p/3349518.html 引言&#xff1a;我們使用cocos2d-x引擎制作了一款飛行射擊游戲&#xff0c;其中創新性地融入了手勢識別功能。但是我們在移植過程中遇到了很多的問題&#xff0c;同時也發現網上的資料少而不全。所以在項目行將結束的時…

十二、聚類算法——相似度測量

兩套學習資料都類似&#xff0c;可參考聚類算法實戰 一、聚類 聚類&#xff1a;物以類聚&#xff0c;人以群分&#xff0c;是無監督學習中的一種。 沒有y&#xff0c;只有x&#xff0c;把不同的x根據相似度自動的聚成好多堆兒 本質上&#xff0c;N個樣本&#xff0c;映射到K個…

操作系統磁盤調度_磁盤調度| 操作系統

操作系統磁盤調度磁盤調度 (Disk Scheduling) One of the major duties of the operating is that, to use the hardware orderly and accurately. For disk drives, it has a duty of having a fast access time and disk bandwidth. Generally, bandwidth is the total numbe…

leetcode 344. 反轉字符串 541. 反轉字符串 II 雙指針解

目錄leetcode 344.反轉字符串1、題目2、思考leetcode 541. 反轉字符串 II1、題目2、思考leetcode 344.反轉字符串 1、題目 2、思考 典型的雙指針解法&#xff1a; 一個從前往后&#xff0c;一個從后往前&#xff0c;指針對應的交換即可。 class Solution { public:void reve…

關于銀聯在線支付和短彩信接口的開發——總結

9月份開始做用二維碼做閉環的一個在線訂購景區門票的項目&#xff0c;其中這樣做是很好的&#xff0c;用二維碼連接了線上與線下的交易和兌券。銀聯在線支付接口&#xff08;asp.net cs&#xff09;做的很好&#xff0c;方便調用開發。就是處理回值的時候得找個更好的方法才能顯…

十三、聚類算法

六、聚類算法實戰 一、聚類 聚類是一種無監督的機器學習任務&#xff0c;可以自動將數據劃分為類cluster&#xff0c;因此聚類分組不需要提前被告知所劃分的組應該是什么樣子的。因為我們甚至可能都不知道我們在尋找什么&#xff0c;所以聚類是用于知識發現而不是預測。 聚類…

pl/sql中的賦值運算符_如何在SQL中使用AND / OR運算符?

pl/sql中的賦值運算符Basically, AND / OR operator is used to retrieving the record from the database. If we give more than one conditions by using AND Operator, then it retrieves the data from the database when both the conditions are true. And if we use OR…

【C++grammar】名字隱藏與重定義

目錄1、繼承中的名字隱藏1.基類同名函數被隱藏的現象描述2.問題理解3.避免現象2、重定義1.現象描述2.重定義與重載的區別3.能否使用 using 將基類成員引入到派生類定義中1、繼承中的名字隱藏 1.基類同名函數被隱藏的現象描述 在學習變量作用域的時候知道&#xff0c;全局變量…

javascript 核心概念(1)-數據類型

語法 &#xff08;1&#xff09;到現在為止&#xff0c;大多數瀏覽器也還是支持到ECMAScript 第三版的標準。 核心概念就是一個語言的基本工作原理&#xff0c;涉及語法&#xff0c;操作符&#xff0c;數據類型。 &#xff08;2&#xff09;javascript的一切--變量&#xff0c;…

注解的力量 -----Spring 2.5 JPA hibernate 使用方法的點滴整理(五):使用@Component 來簡化bean的配置...

雖然我們可以通過 Autowired 在 Bean 類中使用自動注入功能&#xff0c;但是 Bean 還是在 applicatonContext.xml 文件中通過 <bean> 進行定義 —— 在前面的例子中&#xff0c;我們還是在配置文件中定義 Bean&#xff0c;通過 Autowired為 Bean 的成員變量、方法形參或構…

c語言條件語句示例_PHP中的條件語句和示例

c語言條件語句示例PHP條件語句 (PHP Conditional Statements) While coding, you may get to a point where your results can only be gotten when a condition is valid. We make use of conditional statements. Conditional statements are statements that can only be ex…

十四、聚類實戰——圖片壓縮

對同一像素點值的像素點歸為一類&#xff0c;通過平均值進行取代&#xff0c;從而將圖像進行壓縮并且保證圖像盡可能不失真&#xff0c;關鍵信息仍保留。 from PIL import Image import numpy as np from sklearn.cluster import KMeans import matplotlib import matplotlib.…