2017 Multi-University Training Contest 2 hdu 6047
思路:
AC代码:
#include "iostream" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set" #include "map" #include "algorithm" #include "stdio.h" #include "math.h" #pragma comment(linker, "/STACK:102400000,102400000") #define ll long long #define endl ("\n") #define bug(x) cout<<x<<" "<<"UUUUU"<<endl; #define mem(a,x) memset(a,x,sizeof(a)) #define mp(x,y) make_pair(x,y) #define pb(x) push_back(x) #define ft (frist) #define sd (second) #define lrt (rt<<1) #define rrt (rt<<1|1) using namespace std; const long long INF = 1e18+1LL; const int inf = 1e9+1e8; const int N=1e5+100; const ll mod=1e9+7; ///1003 struct Node{ int id,v; bool friend operator< (Node a, Node b){ return a.v<b.v; } }; Node a; int n,b[N]; priority_queue<Node> Q; int main(){ while(cin>>n){ while(!Q.empty()) Q.pop(); for(int i=1; i<=n; ++i){ scanf("%d",&a.v); a.v-=i; a.id=i; Q.push(a); } for(int i=1; i<=n; ++i){ scanf("%d",&b[i]); } sort(b+1,b+1+n); ll ans=0; for(int i=1; i<=n; ++i){ a=Q.top(); while(a.id<b[i]){ Q.pop(),a=Q.top();; } ans=(ans+a.v)%mod; a.id=n+i, a.v-=n+i; Q.push(a); } printf("%lld\n", (ans+mod)%mod); } return 0; } ///线段树写法 /* ll ma[N<<2],a[N<<2]; int n,b[N]; void push_up(int rt){ ma[rt]= max(ma[lrt], ma[rrt]); } void creat(int rt, int l, int r){ if(l==r){ ma[rt]=a[l]; return; } int mid=l+r>>1; creat(lrt, l, mid); creat(rrt, mid+1, r); push_up(rt); } void up(int rt, int L, int R, int p, ll w){ if(L==R){ ma[rt]=w; return; } int mid=L+R>>1; if(p<=mid) up(lrt, L, mid, p, w ); else if(p > mid) up(rrt, mid+1, R, p, w); push_up(rt); } ll query(int rt, int l, int r, int L, int R){ if(l==L && r==R){ return ma[rt]; } int mid=l+r>>1; if(R<=mid) return query(lrt, l, mid, L, R); else if(L>mid) return query(rrt, mid+1, r, L, R); else return max(query(lrt, l, mid, L, mid), query(rrt, mid+1, r, mid+1, R)); } int main(){ //ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); while(scanf("%d",&n)!=EOF){ mem(ma); for(int i=1; i<=n; ++i){ scanf("%lld",&a[i]); a[i]-=i; } for(int i=1; i<=n; ++i){ scanf("%d",&b[i]); } sort(b+1,b+1+n); creat(1,1,2*n); ll ans=0; for(int i=1; i<=n; ++i){ ll mm=query(1, 1, 2*n, b[i], n+i-1);//cout<<b[i]<<" "<<mm-n+i<<" "; bug(mm) ans=(ans+mm)%mod; up(1,1,2*n,n+i,mm-n-i); } cout<<(ans+mod)%mod<<"\n"; } return 0; } */