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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

洛谷 - P2163 [SHOI2007]园丁的烦恼(不带修二维数点-树状数组/主席树)

發布時間:2024/4/11 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 洛谷 - P2163 [SHOI2007]园丁的烦恼(不带修二维数点-树状数组/主席树) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:二維平面坐標系中給出 nnn 個坐標點,然后是 mmm 次詢問,每次詢問需要回答一個閉合矩陣中有多少個點

題目分析:想掛樹套樹來著,但是復雜度有點大。本題不帶修且可以離線,考慮將一個詢問拆成四個二維前綴和的表示形式,然后就可以排個序直接用樹狀數組統計啦

有個小細節就是當 xxx 坐標相同時,需要先加點然后再詢問。注意樹狀數組下標不能從 000 開始,所以需要坐標統一偏移 111 個單位

2021.8.26 UPDATE:

聽camp的時候意識到主席樹也能二維數點,對 xxx 軸可持久化,對 yyy 軸建立權值線段樹就可以啦,帶修的話可以掛帶修主席樹

本題內存給的不是很多,如果是桶排 vector 存點的話會 MLE,map 離散化的話會 TLE,毒瘤題當然要用毒瘤的 unordered_map 去卡

代碼:
樹狀數組

// Problem: P2163 [SHOI2007]園丁的煩惱 // Contest: Luogu // URL: https://www.luogu.com.cn/problem/P2163 // Memory Limit: 125 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org)// #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<list> #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=1e7+100; struct Node {int x,y,type,op,id;bool operator<(const Node& t)const {if(x!=t.x) {return x<t.x; } else {return type<t.type;}} }; vector<Node>node; int ans[N],c[N]; void add(int x) {for(int i=x;i<N;i+=lowbit(i)) c[i]++; } int ask(int x) {int ans=0;for(int i=x;i>0;i-=lowbit(i)) ans+=c[i];return ans; } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int n,m;read(n),read(m);for(int i=1,x,y;i<=n;i++) {read(x),read(y);x++,y++;node.push_back({x,y,0,-1,-1});}for(int i=1,x1,x2,y1,y2;i<=m;i++) {read(x1),read(y1),read(x2),read(y2);x1++,x2++,y1++,y2++;node.push_back({x2,y2,1,1,i});node.push_back({x1-1,y1-1,1,1,i});node.push_back({x1-1,y2,1,-1,i});node.push_back({x2,y1-1,1,-1,i});}sort(node.begin(),node.end());for(auto it:node) {if(it.type==1) {//askans[it.id]+=it.op*ask(it.y);} else {//addadd(it.y);}}for(int i=1;i<=m;i++) {printf("%d\n",ans[i]);}return 0; }

主席樹:

// Problem: P2163 [SHOI2007]園丁的煩惱 // Contest: Luogu // URL: https://www.luogu.com.cn/problem/P2163 // Memory Limit: 125 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org)// #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<list> #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=1e7+100; struct Node {int l,r,sum; }tree[500005*30]; int root[N],cnt; unordered_map<int,vector<int>>node; void update(int &k,int pos,int l,int r) {tree[++cnt]=tree[k];k=cnt;tree[k].sum++;if(l==r) return;int mid=(l+r)>>1;if(pos<=mid) update(tree[k].l,pos,l,mid);else update(tree[k].r,pos,mid+1,r); } int query(int i,int j,int l,int r,int L,int R) {if(L>r||R<l) return 0;if(L>=l&&R<=r) return tree[j].sum-tree[i].sum;int mid=(L+R)>>1;return query(tree[i].l,tree[j].l,l,r,L,mid)+query(tree[i].r,tree[j].r,l,r,mid+1,R); } void init() {cnt=0;root[0]=0;tree[0].l=tree[0].r=tree[0].sum=0; } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);init();int n,m;read(n),read(m);for(int i=1;i<=n;i++) {int x,y;read(x),read(y);x++,y++;node[x].push_back(y);}for(int x=1;x<N;x++) {root[x]=root[x-1];if(!node.count(x)) {continue;}for(auto y:node[x]) {update(root[x],y,1,N);}}while(m--) {int x1,y1,x2,y2;read(x1),read(y1),read(x2),read(y2);x1++,x2++,y1++,y2++;printf("%d\n",query(root[x1-1],root[x2],y1,y2,1,N));}return 0; }

總結

以上是生活随笔為你收集整理的洛谷 - P2163 [SHOI2007]园丁的烦恼(不带修二维数点-树状数组/主席树)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。