YJJ's SalesmanTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1737 Accepted Submission(s): 644 Problem Description YJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination. Input The first line of the input contains an integer T (1≤T≤10) ,which is the number of test cases. Output The maximum of dollars YJJ can get. Sample Input 1 3 1 1 1 1 2 2 3 3 1 Sample Output 3 Source |
主要倒着更新,没想到... ...
#include <bits/stdc++.h>
#define mid ((tr[d].l + tr[d].r)>>1)
#define lc (d << 1)
#define rc (d << 1|1)
using namespace std;
const int maxn = 1e5+5;
struct Tr{
int l,r,mx;
}tr[maxn*4];
struct Node{
int x,y,w;
friend operator < (Node a,Node b){ //倒着更新,类似背包,否则会对后面操作产生影响
if(a.x!=b.x)
return a.x<b.x;
else
return a.y>b.y;
}
}node[maxn];
int T,n;
int hash_x[maxn],hash_y[maxn];
int n1,n2;
void build(int d,int l,int r){
tr[d].l = l,tr[d].r = r,tr[d].mx = 0;
if (tr[d].l == tr[d].r){
return;
}
build(lc,l,mid);
build(rc,mid+1,r);
}
int query(int d,int l,int r){
if (tr[d].l == l && tr[d].r == r){
return tr[d].mx;
}
if (mid >= r) return query(lc,l,r);
else if (mid < l) return (rc,l,r);
else return max(query(lc,l,mid),query(rc,mid+1,r));
}
void update(int d,int pos,int w){
if (tr[d].l == tr[d].r && tr[d].r == pos) {
tr[d].mx = max(w,tr[d].mx);
return;
}
if (mid >= pos) update(lc,pos,w);
else update(rc,pos,w);
tr[d].mx = max(tr[lc].mx,tr[rc].mx);
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for (int i = 0;i < n;++i){
scanf("%d%d%d",&node[i].x,&node[i].y,&node[i].w);
hash_x[i] = node[i].x,hash_y[i] = node[i].y;
}
sort(hash_x,hash_x+n);
sort(hash_y,hash_y+n);
n2 = unique(hash_y,hash_y+n) - hash_y;
for (int i = 0;i < n;++i){
node[i].y = lower_bound(hash_y,hash_y+n2,node[i].y) - hash_y;
}
sort(node,node+n);
build(1,0,n);
int ans = 0;
for (int i = 0;i < n;++i){
int tmp;
if (node[i].y == 0) tmp = node[i].w;
else tmp = query(1,0,node[i].y-1) + node[i].w;
update(1,node[i].y,tmp);
ans = max(ans,tmp);
}
printf("%d\n",ans);
}
return 0;
}