數據結構小結+模板

\[OI中的數據結構\]

\[By\;TYQ\]

線性結構

大部略

單調棧

棧 , 但是push的時候要彈出所有比他小/大的(多用于優化Dp)

單調隊列

隊列 , 同單調棧

樹狀結構

樹狀數組

核心:lowbit(x) = (x) & (-x)

...其實lowbit(x) = 2^x的最低非0位

PION8012初賽中考了...但只涉及正數...

  • 為什么lowbit(x) = (x) & (-x)

考慮x>0時-x等于多少:-x在二進制中的意義為x所有位取反后+1 , 那么他的第一個非0位以前的都是0 , &后結果為0 , 在第一個非0位時-x發生進位使哪位為1 , 那么這位為1 , 再以后呢?為0 , 所以&也為0

  • 樹狀數組每個節點的值

\(C_{i} = \sum_{j=i}^{j!=0 , j=lowbit(j)} C_{j}\)

  • 樹狀數組怎么求和 :

我們為什么要設計前面的\(C_{i}\)為這個奇怪的數?

答案揭曉!為了查詢!當查詢時我們只需要遍歷查詢數的每個lowbit , 將值加上\(C_{x}\)

查詢時間復雜度為\(O(logN)\)

那么這為什么對呢?因為:

.

.

.

畫出這顆樹發現的確是對的QAQ其實是我不會證

關于代碼:

void modify(int x , int y)/*modify a[x] to a[x]+y*/{for(int i=x;i<=n;C[i]+=y,i+=(i)&(-i)) ; }
int query(int x)/*query a[1]+...+a[x]*/{int ret = 0 ;for(int i=x;i;i-=(i)&(-i))ret+=C[i] ; return ret ;}

線段樹

其實一開始你覺得比較難 , 但其實很基礎

線段樹每次將區間分成兩個小區間 , 到底層遞歸 ;

用指針寫偽代碼就是

Query()int sum = 0 ;if(!this->ls) sum+=this->ls.Query()if(!this->rs)sum+=this->rs.Query()return sum ;//什么?你問我為什么要寫指針?代碼短!

但是你代碼肯定不能這么寫...線段樹維護的不只和 , 需要zici區間查詢 :

關于區間查詢

Query(i,j,ni,nj,p)if(i<=ni && j<=nj) return Value[p] ;int m = (s + t) >> 2, sum = 0;if (l <= m) sum += Query(l, r, s, m, p * 2);if (r > m) sum += Query(l, r, m + 1, t, p * 2 + 1);return sum;

pushdown

pushdown(p,m,s,t)d[p * 2] += b[p] * (m - s + 1), d[p * 2 + 1] += b[p] * (t - m),b[p * 2] += b[p], b[p * 2 + 1] += b[p];

區間修改:

每次執行單點修改并給節點打上LazyTag , 需要時下放

板子

#include<iostream>
#include<cstdio>
#define ll      long long
#define MAXN 100000
int p , arr[MAXN+5];
using namespace std ;
struct node{ll v,mul,add;
}tr[4*MAXN+5];
void build(int root,int l,int r){tr[root].mul=1,tr[root].add=0;if(l==r) tr[root].v=arr[l];else {int m=(l+r)/2; build(root*2,l,m) ; build(root*2+1,m+1,r) ;tr[root].v=tr[root*2].v+tr[root*2+1].v ;}tr[root].v%=p ;
}
void pushdown(int root,int l,int r){int mid=(l+r)/2 ;tr[root*2].v=(tr[root*2].v*tr[root].mul+tr[root].add*(mid-l+1))%p,tr[root*2+1].v=(tr[root*2+1].v*tr[root].mul+tr[root].add*(r-mid))%p,tr[root*2].mul=(tr[root*2].mul*tr[root].mul)%p,tr[root*2+1].mul=(tr[root*2+1].mul*tr[root].mul)%p,tr[root*2].add=(tr[root*2].add*tr[root].mul+tr[root].add)%p,tr[root*2+1].add=(tr[root*2+1].add*tr[root].mul+tr[root].add)%p,tr[root].mul=1,tr[root].add=0;
}
void U_M(int root,int il,int ir,int l,int r,ll k){if(r<il||ir<l) return ;if(l<=il&&ir<=r) {tr[root].v=(tr[root].v*k)%p,tr[root].mul=(tr[root].mul*k)%p,tr[root].add=(tr[root].add*k)%p; return ;}pushdown(root,il,ir) ;int mid=(il+ir)/2 ;U_M(root*2,il,mid,l,r,k) ;U_M(root*2+1,mid+1,ir,l,r,k) ;tr[root].v=(tr[root*2].v+tr[root*2+1].v)%p ;return ;
}
void U_A(int root,int il,int ir,int l,int r,ll k){if(r<il||ir<l) return ;if(l<=il&&ir<=r) {tr[root].add=(tr[root].add+k)%p,tr[root].v=(tr[root].v+k*(ir-il+1))%p; return ;}pushdown(root,il,ir) ;int mid=(il+ir)/2 ;U_A(root*2,il,mid,l,r,k) ;U_A(root*2+1,mid+1,ir,l,r,k) ;tr[root].v=(tr[root*2].v+tr[root*2+1].v)%p ;return ;
}
ll query(int root,int il,int ir,int l,int r){if(r<il||ir<l) return 0;if(l<=il&&ir<=r) {return tr[root].v;}pushdown(root,il,ir) ;int mid=(il+ir)/2 ;return (query(root*2,il,mid,l,r)+query(root*2+1,mid+1,ir,l,r))%p;
}
int main(){int n,m ;scanf("%d%d%d",&n,&m,&p); for(int i=1;i<=n;++i) scanf("%d",&arr[i]) ; build(1,1,n) ;//for(int i=1;i<=n;++i) cout<<tr[i].v<<" " ; cout<<endl ;while(m--){int x,y,z; ll k;scanf("%d",&x) ;if(x==1)scanf("%d%d%lld",&y,&z,&k) , U_M(1,1,n,y,z,k) ;else if(x==2)scanf("%d%d%lld",&y,&z,&k) , U_A(1,1,n,y,z,k) ;else scanf("%d%d",&y,&z),printf("%lld\n",query(1,1,n,y,z)) ;//for(int i=1;i<=n;++i) cout<<tr[i].v<<" " ; cout<<endl ;}return 0 ;
}

可持久化

見可持久化數據結構

轉載于:https://www.cnblogs.com/tyqtyq/p/9906386.html

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

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

相關文章

視頻翻錄_將DVD解密并復制到硬盤驅動器而無需翻錄

視頻翻錄Have you ever wanted to make backup copies of your DVDs but didn’t want to mess with confusing DVD ripping software? Today, we’ll look at drop dead simple method to decrypt DVDs on the fly with DVD43 so you can easily copy them to your hard dri…

詳解面向對象、構造函數、原型與原型鏈

詳解面向對象、構造函數、原型與原型鏈 為了幫助大家能夠更加直觀的學習和了解面向對象&#xff0c;我會用盡量簡單易懂的描述來展示面向對象的相關知識。并且也準備了一些實用的例子幫助大家更加快速的掌握面向對象的真諦。 jQuery的面向對象實現封裝拖拽簡易版運動框架封裝這…

如何將Wii遙控器用作陀螺儀鼠標

If you have a spare Nintendo Wii remote with the Motion Plus add-on, you can use it to control your Windows PC from across the room. Here’s how to get it working in a couple of easy steps. 如果您有帶Motion Plus附加組件的備用Nintendo Wii遙控器&#xff0c;則…

68.iOS設備尺寸及型號代碼(iPhoneXR/XS)

所有設備型號官網地址&#xff1a; https://www.theiphonewiki.com/wiki/Models iPhone: 機型像素比例像素密度屏幕尺寸機型代碼發布日期iPhone 2g4803203:2163ppi3.5iPhone1,12008.01iPhone 3g4803203:2163ppi3.5iPhone1,22008.06iPhone 3gs4803203:2163ppi3.5iPhone2,12009.0…

php 自帶 web server 如何重寫 rewrite .htaccess

為什么80%的碼農都做不了架構師&#xff1f;>>> <?php // filename: route.php if (preg_match(/\.(?:png|jpg|jpeg|gif|css|js)$/, $_SERVER["REQUEST_URI"])) {return false; } else {include __DIR__ . /index.php; } 更好的寫法&#xff1a; &l…

oracle11g導入錯誤,oracle 11g導入到10g引起的錯誤

環境介紹老環境新環境操作系統&#xff1a;redhat5.8 64位redhat6.4 64位數據庫版本&#xff1a;oracle 10.2.0.4 64位oracle 11.2.0.4 64位背景&#xff1a;之前有一套老的數據庫rac是基于oracle10g搭建&#xff0c;跑了幾年了。現在前端應用程序準備升級&#xff0c;考慮到前…

sci-hub谷歌插件_Google Home Hub具有隱藏屏幕設置菜單

sci-hub谷歌插件You can adjust the brightness or set an alarm on your Google Home Hub with a voice command. But if you’re trying to be quiet or there’s a lot of background noise, you can also do these things using a hidden Screen Settings menu. 您可以使用…

二叉樹的前序、中序、后序遍歷與創建

#include <iostream> #include <string> #include <stack> using namespace std; struct node; typedef node *pNode; struct node { char data; pNode left, right; }; string line; string::iterator it; // 前序擴展序列建立二叉樹 void plan…

flex-2

1、 2、 justify&#xff1a;整理版面 3、 4、歸納 justify-content&#xff1a;flex-start&#xff08;默認&#xff09;、center、flex-end 下面還會提到剩下的兩種項目在主軸上對齊方式&#xff1a; space-between:兩端對齊&#xff08;項目間距離相等&#xff09; space-ar…

TextInput

TextInput /** TextInput 是一個允許用戶在應用中通過鍵盤輸入文本的基本組件* 本組件的屬性提供了多種特性的配置,如自動完成,自動大小寫,占位文字,鍵盤類型等* 常用:* placeholder 占位符* value 輸入框的值* password 是否密文輸入* editable 是否可編輯* retureKeyType …

如何使用oracle查詢,oracle 表查詢

Oracle 的 oracle 表查詢通過scott用戶下的表來演示如何使用select語句&#xff0c;接下來對emp、dept、salgrade表結構進行解說。emp 雇員表字段名稱 數據類型 是否為空 備注-------- ----------- -------- --------EMPNO NUMBER(4) 員工編…

火狐標簽在中間_在Firefox中保留未使用的標簽

火狐標簽在中間If you have a lot of content heavy webpages open in Firefox, it soon adds up on memory usage. The BarTab extension puts unused tabs on hold and keeps them unloaded until you are ready to access them. 如果您在Firefox中打開了大量內容繁重的網頁&…

[CQOI2012]模擬工廠 題解(搜索+貪心)

[CQOI2012]模擬工廠 題解(搜索貪心) 標簽&#xff1a;題解 閱讀體驗&#xff1a;https://zybuluo.com/Junlier/note/1327574 鏈接題目地址&#xff1a;洛谷P3161 BZOJ P2667 這個題練一練綜合思想還是不錯的。。。&#xff08;然而蒟蒻不會啊&#xff09; 做法 肯定是在能完成某…

上傳文件的input問題以及FormData特性

1.input中除了type"file"還要加上name"file"&#xff0c;否則$_FILES為空&#xff0c;input的name值就是為了區分每一個input的 2.var formdata new FormData($("#form")[0]); 或者var formdata new FormData(document.getElementById("f…

iphone手機備忘錄遷移_如何在iPhone和iPad上使用語音備忘錄

iphone手機備忘錄遷移Whether you’re recording a voice message as a reminder of that million dollar idea or catching a snippet of a new song you know you’ll forget, the iPhone and iPad’s Voice Memos app is the perfect tool. 無論您是錄制語音消息來提醒這一百…

php 執行文件tar打包,利用tar for windows對大量文件進行快速打包

近期將某些網站換服務器&#xff0c;由于網站數量巨大&#xff0c;加上附件和靜態頁&#xff0c;文件數量異常多&#xff0c;考慮先打包然后直接傳過去。起初嘗試用winrar打包&#xff0c;但是發現即使選擇”僅儲存”速度仍然慢到無法接受&#xff0c;后來想到了tar&#xff0c…

DDD~領域事件中使用分布式事務

對于一個聚合來說&#xff0c;它可能會被附加很多事件&#xff0c;這里我們叫它領域事務&#xff0c;因為一個聚會我們可以把它理解成一個領域&#xff0c;一個業務。對于領域事件不清楚的同學可以看看我的這篇文章《DDD~領域事件與事件總線》&#xff0c;里面有詳細的說明&…

如何在PowerPoint中制作打字機或命令行動畫

Adding quirky animations to your Microsoft PowerPoint presentation gives your slideshow a little extra life. Not only will adding a typewriter or command line animation entertain your audience, but it will also keep them focused on the text. 在您的Microsof…

oracle中spool卸數,數據卸載--spool的使用

&#xfeff;&#xfeff;引言在項目中&#xff0c;我們經常會遇到數據的卸載、裝載需求。卸載就是需要將數據從數據庫中導入到文本文件中的需求&#xff0c;這樣的方法有很多&#xff0c;比較常用的就是spool命令。裝載就是需要將數據從文本文件中導入到數據庫中。方法也有很多…

Objective-C中的@property

1.property是什么 Property是聲明屬性的語法&#xff0c;它可以快速方便的為實例變量創建存取器&#xff0c;并允許我們通過點語法使用存取器。 存取器&#xff08;accessor&#xff09;&#xff1a;指用于獲取和設置實例變量的方法。用于獲取實例變量值的存取器是getter&#…