1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
| #include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #include<queue> #define ll long long using namespace std;
const ll N=1e5;
ll n,g,tot,ans;
ll uq[N+5];
struct sgt{ ll l,r,ma; bool chg; #define l(x) tree[x].l #define r(x) tree[x].r #define ma(x) tree[x].ma #define chg(x) tree[x].chg }tree[N*4+5];
struct node{ ll date,id,dt; bool operator < (const node& rhs) const{ return date<rhs.date; } }q[N+5];
void build(ll p,ll l,ll r) { l(p)=l;r(p)=r;ma(p)=g; if(l==r) return; ll mid=(l+r)>>1; build(p<<1,l,mid);build(p<<1|1,mid+1,r); }
void add(ll p,ll x,ll k) { if(l(p)==r(p)) { ma(p)+=k;return; } ll mid=(l(p)+r(p))>>1; ll tmplma=ma(p<<1),tmprma=ma(p<<1|1),tmpma=ma(p); if(x<=mid) add(p<<1,x,k); if(x>mid) add(p<<1|1,x,k); ma(p)=max(ma(p<<1),ma(p<<1|1)); if(tmplma==tmpma) { if(ma(p<<1)<ma(p)) chg(p)=1; else if(chg(p<<1)) chg(p)=1; } if(tmplma!=tmpma) {if(ma(p<<1)==ma(p)) chg(p)=1;} if(tmprma==tmpma) { if(ma(p<<1|1)<ma(p)) chg(p)=1; else if(chg(p<<1|1)) chg(p)=1; } if(tmprma!=tmpma) {if(ma(p<<1|1)==ma(p)) chg(p)=1;} chg(p<<1)=0;chg(p<<1|1)=0; }
void clear(ll p) { chg(p)=0;if(l(p)==r(p)) return; clear(p<<1);clear(p<<1|1); }
inline ll read() { ll ret=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') f=-f;ch=getchar();} while(ch>='0'&&ch<='9') {ret=(ret<<3)+(ret<<1)+ch-'0';ch=getchar();} return ret*f; }
inline void write(ll x) { static char buf[22];static ll len=-1; if(x>=0) { do{buf[++len]=x%10+48;x/=10;}while(x); } else { putchar('-'); do{buf[++len]=-(x%10)+48;x/=10;}while(x); } while(len>=0) putchar(buf[len--]); }
int main() {
n=read();g=read();
for(ll i=1;i<=n;i++) { q[i].date=read(); q[i].id=read();q[i].dt=read(); uq[i]=q[i].id; }
sort(q+1,q+n+1); sort(uq+1,uq+n+1);tot=unique(uq+1,uq+n+1)-uq-1;
for(ll i=1;i<=n;i++) { q[i].id=lower_bound(uq+1,uq+tot+1,q[i].id)-uq; }
build(1,1,tot+1);
for(ll i=1;i<=n;i++) { add(1,q[i].id,q[i].dt); ans+=chg(1);chg(1)=0; }
write(ans);
return 0; }
|