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 115 116
| #include<iostream> #include<cstdio> #include<algorithm> #define fir first #define sec second using namespace std; using ll=long long; using PII=pair<int,int>; const int N=1e6+10,INF=1e9; template<class T> inline void read(T &x) { x=0;bool f=0; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();} while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); if(f)x=~x+1; } template<class T,class ...T1> inline void read(T &x,T1 &...x1) { read(x),read(x1...); } int T,n,m; struct Lim{int l,r,v;}a[N]; int v[N],low[N]; int tot,c[N]; struct SegmentTree { struct Node { int l,r; PII v; int tag; }tr[N<<2]; void pushup(int x){tr[x].v=min(tr[x<<1].v,tr[x<<1|1].v);} void update(int x,int k){tr[x].v.fir+=k,tr[x].tag+=k;} void pushdown(int x) { if(!tr[x].tag)return ; update(x<<1,tr[x].tag),update(x<<1|1,tr[x].tag); tr[x].tag=0; } void build(int l,int r,int x=1) { tr[x].l=l,tr[x].r=r,tr[x].tag=0; if(l==r)return tr[x].v={0,l},void(); int mid=l+r>>1; build(l,mid,x<<1),build(mid+1,r,x<<1|1); pushup(x); } void modify(int l,int r,int k,int x=1) { if(l>r)return ; if(l<=tr[x].l&&tr[x].r<=r)return update(x,k); pushdown(x); int mid=tr[x].l+tr[x].r>>1; if(l<=mid)modify(l,r,k,x<<1); if(r>mid)modify(l,r,k,x<<1|1); pushup(x); } PII query(int l,int r,int x=1) { if(l<=tr[x].l&&tr[x].r<=r)return tr[x].v; pushdown(x); int mid=tr[x].l+tr[x].r>>1; PII res={INF,INF}; if(l<=mid)res=min(res,query(l,r,x<<1)); if(r>mid)res=min(res,query(l,r,x<<1|1)); return res; } }seg; inline void solve() { read(n,m); for(int i=1;i<=n;i++)v[i]=low[i]=-1; tot=0; for(int i=1;i<=m;i++) { read(a[i].l,a[i].r,a[i].v); c[++tot]=a[i].v; } sort(c+1,c+1+tot); tot=unique(c+1,c+1+tot)-c-1; for(int i=1;i<=m;i++) { a[i].v=lower_bound(c+1,c+1+tot,a[i].v)-c; v[a[i].l]=a[i].v; for(int j=a[i].l;j<=a[i].r;j++)low[j]=a[i].v; } seg.build(0,tot); for(int i=1;i<=n;i++)if(~v[i])seg.modify(v[i]+1,tot,1); for(int i=1;i<=n;i++) { if(~v[i])seg.modify(low[i]+1,tot,-1); else v[i]=seg.query(low[i],tot).sec; seg.modify(0,v[i]-1,1); } seg.build(0,tot); ll ans=0; for(int i=1;i<=n;i++) { ans+=seg.query(v[i],v[i]).fir; seg.modify(0,v[i]-1,1); } printf("%lld\n",ans); } int main() { freopen("bubble.in","r",stdin); freopen("bubble.out","w",stdout); read(T); while(T--)solve(); return 0; }
|