hdu 5517 (三元組)二維樹狀陣列
阿新 • • 發佈:2018-12-15
思路: 第一眼看還覺得沒法處理,但是我們可以發現他要求top三元組,所以對於二元組 a b 對於每個b 我只需要保留他的最大的a就可以了。生成的新的三元組最多就100000 個。 這樣的話,就直接 二維樹狀陣列求就可以了。
程式碼:
#include<bits/stdc++.h> using namespace std; typedef long long ll; int B[100005],cntb[100005]; ll tr[1005][1005]; int n,m; int tot; struct node { int a,c,d; int cnt; }san[100005],tmp[100005]; void add(int x, int y, ll z){ //將點(x, y)加上z int memo_y = y; while(x <= 1002){ y = memo_y; while(y <= 1002) tr[x][y] += z, y += y & -y; x += x & -x; } } ll ask(int x, int y){//求左上角為(1,1)右下角為(x,y) 的矩陣和 ll res = 0, memo_y = y; while(x){ y = memo_y; while(y) res += tr[x][y], y -= y & -y; x -= x & -x; } return res; } void range_add(int xa, int ya, int xb, int yb, int z){ add(xa, ya, z); add(xa, yb + 1, -z); add(xb + 1, ya, -z); add(xb + 1, yb + 1, z); } int ask(int x,int y,int x2,int y2) // 查詢 x+1 y+1 到 x2 y2 { ll c1=ask(x,y); ll c2=ask(x2,y); ll c3=ask(x,y2); ll c4=ask(x2,y2); return c4-c2-c3+c1; } bool cmp(node x,node y) { if(x.a==y.a&&x.c==y.c) return x.d>y.d; if(x.a==y.a) return x.c>y.c; return x.a>y.a; } int buxiangdeng(int i,int j) { if(san[i].a!=san[j].a||san[i].c!=san[j].c||san[i].d!=san[j].d) return 1; return 0; } int main() { int T; int x,y,z; scanf("%d",&T); int kk=0; while(T--) { for(int i=0;i<=100002;i++) { B[i]=cntb[i]=0; } for(int i=0;i<=1002;i++) { for(int j=0;j<=1002;j++) { tr[i][j]=0; } } scanf("%d %d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d %d",&x,&y); if(B[y]==0||x>B[y]) { B[y]=x; cntb[y]=1; } else if(x==B[y]) { cntb[y]++; } } tot=0; for(int i=1;i<=m;i++) { scanf("%d %d %d",&x,&y,&z); if(B[z]!=0) { san[++tot]=(node){B[z],x,y,cntb[z]}; } } sort(san+1,san+tot+1,cmp); int tot1=0; for(int i=1;i<=tot;i++) { if(buxiangdeng(i,i-1)) { tmp[++tot1]=san[i]; } else { tmp[tot1].cnt+=san[i].cnt; } } for(int i=1;i<=tot1;i++) { san[i]=tmp[i]; } tot=tot1; /* for(int i=1;i<=tot;i++) { printf("(%d %d %d) cnt %d \n",san[i].a,san[i].c,san[i].d,san[i].cnt); } */ ll ans=0; for(int i=1;i<=tot;i++) { int c,d; c=san[i].c; d=san[i].d; ll cnt=ask(c-1,d-1,1000,1000); //printf("** cnt %lld\n",cnt); if(cnt==0) { ans+=san[i].cnt; } add(c,d,1ll*san[i].cnt); } printf("Case #%d: %d\n",++kk,ans); } return 0; }