After the
determines the starting and direction, the process of travel determines that the amount of oil that reaches the I point between the <= and I points is the limiting condition of the topic.
because it is a circle problem, we can copy the sequence into a sequence of problems.
Set SUM[J] sum[j] indicates that the amount of oil obtained before J J Dis[j] Dis[j] represents the distance between 1 and J J, and for the start I, each j<=i+n J is guaranteed:
sum[j]−sum[i]>=dis[j]−dis[ I] sum[j]-sum[i]>=dis[j]-dis[i]
move to get: Sum[j]−dis[j]>=sum[i]−dis[i] sum[j]-dis[j]>=sum[i]-dis[i].
then just ensure that the minimum value of sum[j]−dis[j] sum[j]-dis[j] satisfies the condition. The
problem is transformed in order to find the interval most value problem, according to the movement rule of the starting point, we can determine that the range of J J moves forward only 1, then we can find the most value by the monotone queue, thus get the answer
#include <cstdio> #include <cstring> #include <algorithm> #define LL long long using namespace std;
const int m=2e6+5;
int d[m],p[m],n,q[m],id[m];
ll Sum[m],dis[m];
BOOL F[m];
inline Void Rd (int &res) {Res=0;char C;
while (C=getchar (), c<48);
Do res= (res<<1) + (res<<3) + (c^48);
while (C=getchar (), c>=48);
} void solve (int s) {int i,j,k;
sum[0]=0; for (i=1;i<=n*2;i++) {Sum[i]=sum[i-1]+p[i-1];//sum[i] represents the amount of oil that can be obtained from 1 to I, excluding the distance I dis[i]=dis[i-1]+d[i+s];//from 1 to I Away and}/* for I j->[i+1,i+n] sum[j]-sum[i]>=dis[j]-dis[i] Sum[j]-dis[j
]>=sum[i]-dis[i].
Min (Sum[j]-dis[j]) >=sum[i]-dis[i] Push K, pop val[j]>=val[k] */int l=0,r=-1;
for (i=2;i<=n+1;i++) {while (r>=l&&sum[q[r]]-dis[q[r]]>=sum[i]-dis[i]) r--;
Q[++r]=i; } for (i=1;i<=n;i++) {//To determine if I point is feasible while (l<=r&&q[l]< i+1) l++;
if (l<=r) {k=q[l];
if (sum[k]-dis[k]>=sum[i]-dis[i]) f[id[i]]=true;
} while (r>=l&&sum[q[r]]-dis[q[r]]>=sum[i+n+1]-dis[i+n+1]) r--;
q[++r]=i+n+1;
}} int main () {int i,j,k;
RD (N);
for (i=1;i<=n;i++) {id[i]=i;
RD (P[i]), RD (D[i]);
P[i+n]=p[i];
D[i+n]=d[i];
} Solve (-1);
for (i=1;i<=n;i++) {swap (d[i],d[2*n-i+1]);
Swap (p[i],p[2*n-i+1]);
id[i]=n-i+1;
} solve (0);
for (i=1;i<=n;i++) {if (F[i]) puts ("TAK");
Else puts ("NIE");
} return 0; }