日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU - 6183 Color it(动态开点线段树/树状数组套动态开点线段树)

發(fā)布時間:2024/4/11 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU - 6183 Color it(动态开点线段树/树状数组套动态开点线段树) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目鏈接:點(diǎn)擊查看

題目大意:給出一個二維平面坐標(biāo)系,需要完成四種操作:

  • 0:刪除所有點(diǎn)
  • 1 xycx\ y\ cx?y?c:在點(diǎn) (x,y)(x,y)(x,y) 處添加顏色 ccc
  • 2 xy1y2x\ y1\ y2x?y1?y2:查詢左下角為 (1,y1)(1,y1)(1,y1) 右上角為 (x,y2)(x,y2)(x,y2) 矩陣內(nèi)有多少種不同的顏色
  • 題目分析:點(diǎn)數(shù)和詢問都是 1e51e51e5 級別的,從矩陣的左側(cè)只能等于 111 入手,將問題轉(zhuǎn)換為求 xxx 的前綴和 yyy 的區(qū)間

    那么自然而然想到開 505050 個樹狀數(shù)組套線段樹單獨(dú)維護(hù)每個顏色,詢問時單獨(dú)查詢即可,但這樣的空間復(fù)雜度有點(diǎn)炸

    考慮優(yōu)化,因?yàn)轭伾炼嘤?505050 種,所以將顏色狀壓,將線段樹求 “頂點(diǎn)個數(shù)” 的操作轉(zhuǎn)換為求 “按位或” 的操作,這樣只需要開一個樹狀數(shù)組套線段樹即可,時間復(fù)雜度是 O(nlog2n)O(nlog^2n)O(nlog2n)

    上面是最簡單的思路,但是實(shí)現(xiàn)起來有一定的困難,所以考慮另一種思路

    還是將每種顏色獨(dú)立,每次查詢我們只需要確定,是否存在著一個點(diǎn) (xx,yy)(xx,yy)(xx,yy),滿足 xx>=1&&xx<=x&&yy>=y1&&yy<=y2xx>=1\ \&\&\ xx<=x\ \&\&\ yy>=y1\ \&\&\ yy<=y2xx>=1?&&?xx<=x?&&?yy>=y1?&&?yy<=y2 即可,不難發(fā)現(xiàn)對于 xxx 坐標(biāo)而言,有一個條件永遠(yuǎn)都滿足的,所以我們不妨找出區(qū)間 [y1,y2][y1,y2][y1,y2]內(nèi)的點(diǎn),xxx 軸的最小值,判斷其是否小于等于閾值即可,于是將題目轉(zhuǎn)換為了區(qū)間求最小值,單點(diǎn)更新的題目了

    然后就是 yyy 軸的坐標(biāo)比較大,可以選擇離散化或動態(tài)開點(diǎn),時間復(fù)雜度 O(nlogn)O(nlogn)O(nlogn)

    代碼:
    動態(tài)開點(diǎn):

    // #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=1000000; const int N=1e6+100; struct Node {int l,r,mmin; }tree[N]; int cnt,root[51]; bool flag; int newnode() {cnt++;tree[cnt].l=tree[cnt].r=0;tree[cnt].mmin=inf;return cnt; } void update(int &k,int l,int r,int pos,int val) {if(!k) {k=newnode();}tree[k].mmin=min(tree[k].mmin,val);if(l==r) {return;}int mid=(l+r)>>1;if(pos<=mid) {update(tree[k].l,l,mid,pos,val);} else {update(tree[k].r,mid+1,r,pos,val);} } void query(int k,int l,int r,int ql,int qr,int val) {if(!k||l>qr||r<ql||flag) {return;}if(l>=ql&&r<=qr) {if(tree[k].mmin<=val) {flag=true;}return;}int mid=(l+r)>>1;query(tree[k].l,l,mid,ql,qr,val);query(tree[k].r,mid+1,r,ql,qr,val);} int cal(int x,int y1,int y2) {int ans=0;for(int i=0;i<=50;i++) {flag=0;query(root[i],1,inf,y1,y2,x);ans+=flag;}return ans; } void init() {cnt=-1;newnode();for(int i=0;i<=50;i++) {root[i]=0;} } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int op;while(scanf("%d",&op)!=EOF&&op!=3) {if(op==0) {init();} else if(op==1) {int x,y,c;read(x),read(y),read(c);update(root[c],1,inf,y,x);} else if(op==2) {int x,y1,y2;read(x),read(y1),read(y2);printf("%d\n",cal(x,y1,y2));}}return 0; }

    樹狀數(shù)組套線段樹(動態(tài)開點(diǎn))

    // #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<unordered_map> #define lowbit(x) x&-x using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; int cnt; struct Seg {struct Node {int l,r;LL sum;}tree[150010*20*20];int newnode() {cnt++;tree[cnt].l=tree[cnt].r=0;tree[cnt].sum=0;return cnt;}void update(int &k,int l,int r,int pos,LL val) {if(!k) {k=newnode();}tree[k].sum|=val;if(l==r) {return;}int mid=(l+r)>>1;if(pos<=mid) {update(tree[k].l,l,mid,pos,val);} else {update(tree[k].r,mid+1,r,pos,val);}}LL query(int k,int l,int r,int ql,int qr) {if(!k||l>qr||r<ql) {return 0;}if(l>=ql&&r<=qr) {return tree[k].sum;}int mid=(l+r)>>1;return query(tree[k].l,l,mid,ql,qr)|query(tree[k].r,mid+1,r,ql,qr);} }SEG; struct Bit {int root[N];void add(int x,int y,LL val) {for(int i=x;i<N;i+=lowbit(i)) {SEG.update(root[i],1,1e6,y,val);}}LL ask(int x,int y1,int y2) {LL ans=0;for(int i=x;i>0;i-=lowbit(i)) {ans|=SEG.query(root[i],1,1e6,y1,y2);}return ans;} }BIT; int cal(LL x) {int ans=0;while(x) {ans+=x&1;x>>=1;}return ans; } void init() {cnt=-1;SEG.newnode();memset(BIT.root,0,sizeof(BIT.root)); } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int op;while(scanf("%d",&op)!=EOF&&op!=3) {if(op==0) {init();} else if(op==1) {int x,y,c;read(x),read(y),read(c);BIT.add(x,y,1LL<<c);} else if(op==2) {int x,y1,y2;read(x),read(y1),read(y2);printf("%d\n",cal(BIT.ask(x,y1,y2)));}}return 0; }

    總結(jié)

    以上是生活随笔為你收集整理的HDU - 6183 Color it(动态开点线段树/树状数组套动态开点线段树)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。