因为一个等号挂掉了10pts
发现每个黑色段一定对应了一段不可行的出发区间
检查是否存在所有黑色段的并集的补集即可
具体来说,我们对于每个黑色段计算出一个(有的是两个)区间 \([l, r]\) ,把它们全合并,看有没有剩下的位置
Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 500010
#define ll long long
//#define int long long
char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
int ans=0, f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
return ans*f;
}
int n; ll s, k;
ll a[N];
namespace force{
bool lim[N]; ll ento;
bool check(int b) {
ll now=b;
while (now<ento) {
now+=k;
if (lim[now]) return 0;
}
return 1;
}
void solve() {
ento=0;
memset(lim, 0, sizeof(lim));
for (int i=1; i<=n; ++i) {
//cout<<"goto lim: "<<ento+1<<' '<<ento+a[i]+s<<endl;
if (i&1) for (int j=ento+1; j<ento+a[i]+s; ++j) lim[j]=1;
ento+=a[i];
}
//cout<<"lim: "; for (int i=0; i<=ento; ++i) cout<<lim[i]<<' '; cout<<endl;
//cout<<"check: "; for (int i=0; i<=k; ++i) cout<<check(-i)<<' '; cout<<endl;
for (int i=0; i<=k; ++i)
if (check(-i)) {puts("TAK"); return ;}
puts("NIE");
}
}
namespace task1{
struct range{ll l, r; inline void build(ll a, ll b) {l=a; r=b;}}ran[N];
inline bool operator < (range a, range b) {return a.l<b.l;}
void solve() {
ll now=0; int top=0;
for (int i=1; i<=n; ++i) {
if (i&1) {
if (1ll*a[i]+s>k) {puts("NIE"); return ;}
//cout<<"limit: "<<now+1<<' '<<now+a[i]+s-1<<endl;
ll t1=ceil((1.0*now+1)/(1.0*k)+1e-8);
ran[++top].build(now+1-t1*k, now+a[i]+s-1-t1*k);
if (now+a[i]+s-1-t1*k>=0) {
++t1;
ran[++top].build(now+1-t1*k, now+a[i]+s-1-t1*k);
}
}
now+=a[i];
}
sort(ran+1, ran+top+1);
//cout<<"ran: "<<endl; for (int i=1; i<=top; ++i) cout<<ran[i].l<<' '<<ran[i].r<<endl;
for (int i=2; i<=top; ++i) {
//cout<<"i: "<<i<<endl;
if (ran[i-1].r+1>=ran[i].l) ran[i].l=ran[i-1].l, ran[i].r=max(ran[i-1].r, ran[i].r);
else {puts("TAK"); return ;}
}
//assert(top==1);
//cout<<"top: "<<ran[top].l<<' '<<ran[top].r<<endl;
if ((ran[top].l>=-k&&ran[top].r<-1) || (ran[top].l>-k&&ran[top].r<0)) {puts("TAK"); return ;}
puts("NIE");
}
}
signed main()
{
int T;
T=read();
while (T--) {
s=read(); k=read(); n=read();
for (int i=1; i<=n; ++i) a[i]=read();
//force::solve();
task1::solve();
}
return 0;
}