洛谷 - P2163 [SHOI2007]园丁的烦恼(不带修二维数点-树状数组/主席树)
生活随笔
收集整理的這篇文章主要介紹了
洛谷 - 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 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]园丁的烦恼(不带修二维数点-树状数组/主席树)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Gym - 101173H Hangar
- 下一篇: 洛谷 - P4390 [BOI2007]