/*
Copyright 2013 Jakub "Cubix651" Cisło
Interval tree (interval - interval): (+, max)
Add value for interval and ask about maximum value of interval
Complexity: O(z log n)
*/
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAX = 131074;
int n, m, z, p, k, l, W[MAX], M[MAX], S;
int greaterPow2(int x)
{
int res = 1;
while(res < x)
res <<= 1;
return res;
}
void insert(int a, int b, int c)
{
a += S;
b += S;
W[a] += c;
M[a] += c;
if(a != b)
{
W[b] += c;
M[b] += c;
}
while(a>>1 != b>>1)
{
if(!(a&1))
{
W[a+1] += c;
M[a+1] += c;
}
if(b&1)
{
W[b-1] += c;
M[b-1] += c;
}
a>>=1;
b>>=1;
M[a] = max(M[a<<1], M[(a<<1)+1]) + W[a];
M[b] = max(M[b<<1], M[(b<<1)+1]) + W[b];
}
while(a != 1)
{
a>>=1;
M[a] = max(M[a<<1], M[(a<<1)+1]) + W[a];
}
}
int x, y;
int _query(int a, int b, int v, int sum)
{
if(x <= a && b <= y)
return sum + M[v];
if(x > b || y < a)
return 0;
return max(
_query(a, (a+b)/2, 2*v, sum+W[v]),
_query((a+b)/2 + 1, b, 2*v+1, sum+W[v])
);
}
int query(int a, int b)
{
x = a;
y = b;
return _query(0, S-1, 1, 0);
}
void printTree()
{
for(int i = 1; i <= 2*S; ++i)
printf("(%d %d) ", W[i], M[i]);
printf("\n\n");
}
/* Example: Task "Rails" (IX OI) */
int main()
{
scanf("%d%d%d", &n, &m, &z);
S = greaterPow2(n-1);
while(z--)
{
scanf("%d%d%d", &p, &k, &l);
--k;
//printTree();
if(query(--p, --k) + l <= m)
{
insert(p, k, l);
printf("T\n");
}
else
{
printf("N\n");
}
}
return 0;
}
LyogCiAgIENvcHlyaWdodCAyMDEzIEpha3ViICJDdWJpeDY1MSIgQ2lzxYJvCiAgIEludGVydmFsIHRyZWUgKGludGVydmFsIC0gaW50ZXJ2YWwpOiAoKywgbWF4KQogICBBZGQgdmFsdWUgZm9yIGludGVydmFsIGFuZCBhc2sgYWJvdXQ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