CodeForces - 641ELittle Artem and Time Machine——map+樹狀數組

【題目描述】
CodeForces - 641ELittle Artem and Time Machine
【題目分析】
題目的意思大概是有三種操作
1.在時間t加入一個數字x
2.在時間t刪除一個數字x
3.詢問在時間t集合里面x的個數

雖然題目描述很簡單,但是t和x的范圍都是109,我一開始想到的是主席樹,因為之前做過一道類似的題目HDU - 4348To the moon——主席樹+區間修改,然后我就開始興沖沖的開始離散化,然后正準備寫主席樹的維護的時候才發現因為這個時間是跳躍的,所以我們很不容易用到之前的數據,也不容易修改,所以卡住了。
在網上找其他大佬的博客發現用map以后就不用進行離散化了,而且代碼量非常少,仔細一看發現他用的是樹狀數組,大概的思想就是對于每一個x都用一個樹狀數組維護時間區間,每次修改都在時間軸上進行修改,每次查詢也只是針對這個數字在時間軸上進行查詢。我覺得對每一個數字用一個線段樹進行維護應該也可以做,就是得把每個數字的修改的時間進行離散化,然后再分別建樹可能復雜度會有點高,但應該也是可做的吧,反正我是懶得那樣做了,這個map+樹狀數組真的好爽啊,什么離散化什么的完全不需要啊。
【AC代碼】

#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;const int MAXN=100005;
const int INF=1e9+5;
map<int,int> mp[MAXN];
map<int,int> vis;
int cnt=0;int lowbit(int t)
{return t&(-t);
}void update(int pos,int t,int val)
{while(t<INF){mp[pos][t]+=val;t+=lowbit(t);}
}int sum(int pos,int t)
{int ans=0;while(t){ans+=mp[pos][t];t-=lowbit(t);}return ans;
}int main()
{int n,cmd,t,x;while(~scanf("%d",&n)){cnt=0;vis.clear();while(n--){scanf("%d%d%d",&cmd,&t,&x);if(cmd==1){if(!vis[x]){vis[x]=++cnt; mp[cnt].clear();}update(vis[x],t,1);}else if(cmd==2){update(vis[x],t,-1);}else if(cmd==3){printf("%d\n",sum(vis[x],t));}}}return 0;
}

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

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

相關文章

I/O多路轉接之poll 函數

http://blog.csdn.net/li_ning_/article/details/52167224 poll 一、poll()函數&#xff1a; 這個函數是某些Unix系統提供的用于執行與select()函數同等功能的函數&#xff0c;自認為poll和select大同小異&#xff0c;下面是這個函數的聲明&#xff1a; [cpp] view plaincopy …

鏈表相關筆試面試題

1.判斷兩個鏈表是否相交 兩個鏈表是否相交可分為以下幾種情況 ????&#xff08;1&#xff09;兩個鏈表都不帶環&#xff0c;此時兩個鏈表所對應的最后一個節點是相等的 ????&#xff08;2&#xff09;兩個鏈表一個帶環&#xff0c;一個不帶環&#xff0c;兩個鏈表一定…

Linux經典問題—五哲學家就餐問題

http://m.blog.csdn.net/aspenstars/article/details/70149038 一、問題介紹 由Dijkstra提出并解決的哲學家進餐問題(The Dinning Philosophers Problem)是典型的同步問題。該問題是描述有五個哲學家共用一張圓桌&#xff0c;分別坐在周圍的五張椅子上&#xff0c;在圓桌上有五…

修改之前的myshell使之支持輸入輸出重定向

1.open函數 ????這個函數是打開一個文件&#xff08;文件名叫pathname),以 flag 權限打開&#xff0c;flag 包括了以下幾種 O_RDONLY&#xff08;只讀&#xff09;, O_WRONLY&#xff08;只寫&#xff09;, O_RDWR&#xff08;讀寫&#xff09;&#xff0c;當文件打開成…

HDU - 6621 K-th Closest Distance——主席樹+二分

【題目描述】 HDU - 6621 K-th Closest Distance 【題目分析】 因為看到第kkk大的要求&#xff0c;剛開始的時候一直都在想怎么運用第kkk大來解決問題&#xff0c;但是后來看其他人的博客才發現并不需要用第k大&#xff0c;但是主席樹維護權值線段樹還是需要的&#xff0c;這…

鏈表相關的算法題大匯總 — 數據結構之鏈表奇思妙想

http://blog.csdn.net/lanxuezaipiao/article/details/22100021基本函數&#xff08;具體代碼實現見后面&#xff09; 1&#xff0c;構造節點 //定義節點類型 struct Node { int value; Node*next; }; 2&#xff0c;分配節點 //之所以要分配節點原因是需要在分配函數中…

CodeForces - 372CWatching Fireworks is Fun+DP+單調隊列優化

【題目描述】 CodeForces - 372CWatching Fireworks is Fun 題目的大概意思就是在一個編號為1…n的街道上現在按照時間順序放煙花&#xff0c;每個煙花獲得的幸福感為b?abs(a?x)b-abs(a-x)b?abs(a?x)&#xff0c;x為觀看煙花的位置&#xff0c;為了提升我們的幸福感&#x…

雙向鏈表的基本操作

1.雙向鏈表的數據結構 typedef char DLinkType;typedef struct DLinkNode { DLinkType data; struct DLinkNode* next; struct DLinkNode* prev; }DLinkNode; 雙向帶頭結點的鏈表有三個成員&#xff0c; 一個是數據&#xff0c; 一個是指針 next 指向當前結點的下一個結點&…

匿名管道

1.進程通信的目的 (1) 數據傳輸: 一個進程需要將它的數據傳輸給另一個進程 ????(2) 資源共享: 多個進程之間共享同樣的資源 ????(3) 通知事件: 一個進程需要向另一個或一組進程發送消息, 通知它們發生了什么事情 2.管道 管道是一種進程之間通信的一種方式, 我們把從…

Currency Exchange——最短路Bellman-Ford算法

【題目描述】 Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the sam…

C++實現String類

http://blog.csdn.net/randyjiawenjie/article/details/6709539 C實現String類&#xff0c;還沒有完成&#xff0c;待繼續。 有以下注意的點&#xff1a; &#xff08;1&#xff09;賦值操作符返回的是一個MyString&&#xff0c;而重載的返回的是一個MyString。其中的原因…

POJ 3370 Halloween treats——鴿巢原理+思維

【題目描述】 POJ 3370 Halloween treats Description Every year there is the same problem at Halloween: Each neighbour is only willing to give a certain total number of sweets on that day, no matter how many children call on him, so it may happen that a chi…

將信號量代碼生成靜態庫以及動態庫

1.信號量相關代碼生成靜態庫 2.信號量相關代碼生成動態庫

Wormholes——Bellman-Ford判斷負環

【題目描述】 While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of…

C++11 標準新特性:Defaulted 和 Deleted 函數

https://www.ibm.com/developerworks/cn/aix/library/1212_lufang_c11new/index.html Defaulted 函數 背景問題 C 的類有四類特殊成員函數&#xff0c;它們分別是&#xff1a;默認構造函數、析構函數、拷貝構造函數以及拷貝賦值運算符。這些類的特殊成員函數負責創建、初始化、…

順序表實現棧相關操作

1.棧的相關概念 棧是一種特殊的線性表, 其中只允許在固定的一端進行插入和刪除元素.進行數據插入和刪除的一端叫做棧頂, 另一端成為棧底. 不含任何元素的棧稱為空棧, 棧又稱為先進先出的線性表. 2. 順序棧的結構 3. 順序棧的具體操作 (1). 數據結構 typedef char SeqStackTyp…

MPI Maelstrom——Dijkstra

【題目描述】 BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKee’s research advisor, Jack Swigert, has asked her to bench…

雙向帶環帶頭結點的鏈表實現棧

1. 數據結構 利用帶頭結點帶環的結點實現棧的相關操作.因此, 每一個結點包括了一個前驅, 一個后繼, 還有一個數據成員 typedef char DLinkStackType;typedef struct DLinkStack {DLinkStackType data;struct DLinkStack* next;struct DLinkStack* prev; }DLinkStack;2. 初始化…

Cow Contest——Floyed+連通性判斷

【題目描述】 N (1 ≤ N ≤ 100) cows, conveniently numbered 1…N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors. The contest …

C++11 標準新特性:委派構造函數

https://www.ibm.com/developerworks/cn/rational/1508_chenjing_c11/index.html陳 晶2015 年 8 月 11 日發布WeiboGoogle用電子郵件發送本頁面 1本文首先介紹了在委派構造函數提出之前類成員構造所面臨的問題&#xff0c;再結合實例介紹了委派構造函數的用法&#xff0c;并說明…