USACO18FEB

Posted scx117

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了USACO18FEB相关的知识,希望对你有一定的参考价值。

SlingShot

 1 #include<bits/stdc++.h>
 2 #define P pair<ll,ll>
 3 #define fir first
 4 #define sec second
 5 using namespace std;
 6 typedef long long ll;
 7 int read()
 8 {
 9     int x=0;char ch=getchar();
10     while (ch<0||ch>9) ch=getchar();
11     while (0<=ch&&ch<=9) x=(x<<1)+(x<<3)+ch-0,ch=getchar();
12     return x;
13 }
14 const int N=1e9,T=6e6,M=1e5+5;
15 const ll inf=1ll<<60;
16 P Min[T];
17 int ls[T],rs[T],A[M],x,y,sc,n,rt0[M],rt1[M],m;
18 struct node{ll a,b,c;}p[M];
19 bool cmp(const node &A,const node &B) {return A.a<B.a;}
20 P fmin(P a,P b){
21     return P(min(a.fir,b.fir),min(a.sec,b.sec));
22 };
23 void ins(int &k,int pk,int l,int r,int x,P y)
24 {
25     k=++sc;ls[k]=ls[pk];rs[k]=rs[pk];Min[k]=Min[pk];
26     if (l==r) {Min[k]=fmin(Min[k],y);return;}
27     int mid=(l+r)>>1;
28     if (x<=mid) ins(ls[k],ls[pk],l,mid,x,y);
29     else ins(rs[k],rs[pk],mid+1,r,x,y);
30     Min[k]=fmin(Min[ls[k]],Min[rs[k]]);
31 }
32 P qry(int k,int l,int r,int L,int R)
33 {
34     if (!k) return P(inf,inf);
35     if (L<=l&&r<=R) return Min[k];
36     int mid=(l+r)>>1;P mn=P(inf,inf);
37     if (L<=mid) mn=fmin(mn,qry(ls[k],l,mid,L,R));
38     if (R>mid) mn=fmin(mn,qry(rs[k],mid+1,r,L,R));
39     return mn;
40 }
41 int main()
42 {
43     n=read();m=read();Min[0]=P(inf,inf);
44     for (int i=1;i<=n;i++)
45       p[i].a=read(),p[i].b=read(),p[i].c=read(),A[i]=p[i].a;
46     sort(p+1,p+n+1,cmp);
47     sort(A+1,A+n+1);//寻址数组 
48     for (int i=n;i>=1;i--)
49         ins(rt0[i],rt0[i+1],0,N,p[i].b,P(p[i].a-p[i].b+p[i].c,p[i].a+p[i].b+p[i].c));
50     for (int i=1;i<=n;i++)
51         ins(rt1[i],rt1[i-1],0,N,p[i].b,P(-p[i].a-p[i].b+p[i].c,-p[i].a+p[i].b+p[i].c));
52     while (m--)
53     {
54         x=read();y=read();
55         ll ans=abs(x-y);
56         int k=upper_bound(A+1,A+n+1,x)-A-1; 
57         ans=min(ans,qry(rt0[k+1],1,N,1,y).fir-x+y);
58         ans=min(ans,qry(rt1[k],1,N,1,y).fir+x+y);
59         ans=min(ans,qry(rt0[k+1],1,N,y,N).sec-x-y);
60         ans=min(ans,qry(rt1[k],1,N,y,N).sec+x-y);
61         printf("%lld
",ans);
62     }
63     return 0;
64 }

 

以上是关于USACO18FEB的主要内容,如果未能解决你的问题,请参考以下文章

P4265 [USACO18FEB]Snow Boots S

[luogu4264][USACO18FEB]Teleportation

P4269 [USACO18FEB]Snow Boots G

题解[USACO18FEB]New Barns

[USACO18FEB]Snow Boots S

luogu4088 [USACO18FEB]Slingshot