P3431 [POI2005]AUT-The Bus(二维偏序、树状数组)

https://www.luogu.org/problem/P3431

二维偏序经典题,树状数组维护下前缀最大值,dp思想。

 1 #define bug(x) cout<<#x<<" is "<<x<<endl
 2 #define IO std::ios::sync_with_stdio(0)
 3 #define ull unsigned long long
 4 #include <bits/stdc++.h>
 5 #define iter ::iterator
 6 #define pa pair<int,ll>
 7 #define pp pair<int,pa>
 8 using namespace  std;
 9 #define ll long long
10 #define mk make_pair
11 #define pb push_back
12 #define se second
13 #define fi first
14 #define ls o<<1
15 #define rs o<<1|1
16 const int N=1e5+5;
17 int n,m,q;
18 int mx=1e9;
19 pp p[N];
20 int c[N];
21 int low(ll x){
22     return x&(-x);
23 }
24 void add(int x,int y){
25     while(x<=q){
26         c[x]=max(c[x],y);
27         x+=low(x);
28     }
29 }
30 int qu(int x){
31     int res=0;
32     while(x){
33         res=max(res,c[x]);
34         x-=low(x);
35     }
36     return res;
37 }
38 int a[N];
39 int main(){
40     IO;
41     cin>>n>>m>>q;
42     for(int i=1;i<=q;i++){
43         cin>>p[i].fi>>p[i].se.fi>>p[i].se.se;
44         a[i]=p[i].se.fi;
45     }
46     sort(a+1,a+1+q);
47     int na=unique(a+1,a+1+q)-a-1;
48     sort(p+1,p+1+q);
49     ll ans=p[1].se.se;
50     int x=lower_bound(a+1,a+1+na,p[1].se.fi)-a;
51     add(x,ans);
52     for(int i=2;i<=q;i++){
53         int x=lower_bound(a+1,a+1+na,p[i].se.fi)-a;
54         ll res=qu(x)+p[i].se.se;
55         ans=max(ans,res);
56         add(x,res);
57     }
58     printf("%lld\n",ans);
59 }

猜你喜欢

转载自www.cnblogs.com/ccsu-kid/p/11548118.html