Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <cstdlib>
- #include <cmath>
- #include <sstream>
- #include <string>
- #include <cstring>
- #include <vector>
- #include <queue>
- #include <set>
- #include <map>
- #include <sstream>
- #include <algorithm>
- #define f first
- #define s second
- #define pb push_back
- #define mp make_pair
- #define inf 2000000000
- #define Min(a,b) (a<b?a:b)
- #define Max(a,b) (a>b?a:b)
- #define For(i,s,n) for (int i=s;i<=n;i++)
- #define FOR(i,s,n) for (int i=s;i<n;i++)
- #define Ford(i,s,n) for (int i=s;i>=n;i--)
- #pragma comment(linker, "/STACK:16777216")
- #define MD 1000000007
- typedef long long ll;
- using namespace std;
- pair <pair <int,int>,int > a[50009];
- int T,N,t[200105],ans,FIRST_BLACK,FIRST_WHITE,LAST_WHITE,ADDRESS[50009],M;
- bool used[50009];
- bool cmp(pair <pair <int,int>,int > A,pair <pair <int,int>,int > B){
- swap(A.f.f,A.f.s);
- swap(B.f.f,B.f.s);
- return A.f<B.f;
- }
- void build(int v,int tl,int tr){
- if (tl==tr) t[v]=0;else{
- int tm=(tl+tr)/2;
- build(2*v,tl,tm);
- build(2*v+1,tm+1,tr);
- t[v]=t[2*v]+t[2*v+1];
- }
- }
- void update(int v,int tl,int tr,int pos){
- if (tl==tr)
- t[v]++;else{
- int tm=(tl+tr)/2;
- if (pos<=tm) update(2*v,tl,tm,pos);else update(2*v+1,tm+1,tr,pos);
- t[v]=t[2*v]+t[2*v+1];
- }
- }
- int get_sum(int v,int tl,int tr,int l,int r){
- if (l>r) return 0;
- if (l==tl&&r==tr) return t[v];
- int tm=(tl+tr)/2;
- return get_sum(2*v,tl,tm,l,Min(r,tm))+get_sum(2*v+1,tm+1,tr,Max(l,tm+1),r);
- }
- int main(){
- freopen("i.txt","r",stdin);freopen("o.txt","w",stdout);
- scanf("%d",&T);
- while(T){
- scanf("%d",&N);
- M=0;
- For(i,1,N){
- scanf("%d%d%d",&a[i].f.f,&a[i].f.s,&a[i].s);
- a[i].f.f++;a[i].f.s++;
- M=Max(M,a[i].f.s);
- }
- ans=0;
- build(1,1,M);
- For(i,0,M) used[i]=false;
- sort(a+1,a+N+1,cmp);
- For(i,1,N){
- int C=get_sum(1,1,M,a[i].f.f,a[i].f.s);
- if (C>=a[i].s) continue;
- a[i].s-=C;
- ans+=a[i].s;
- int j=a[i].f.s;
- FIRST_BLACK=j;
- while(1){
- if (used[j]) j=ADDRESS[j];
- FIRST_WHITE=j;
- used[j]=true;
- //cout<<j<<endl;
- a[i].s--;
- j--;
- LAST_WHITE=j;
- while(a[i].s){
- if (used[j]){
- //LAST_WHITE=j+1;
- break;
- }
- used[j]=true;
- //cout<<j<<endl;
- j--;
- LAST_WHITE=j;
- a[i].s--;
- }
- For(k,LAST_WHITE+1,FIRST_WHITE) update(1,1,M,k);
- //update(1,1,M,LAST_WHITE+1,FIRST_WHITE);
- //update(LAST_WHITE+1,FIRST_WHITE-LAST_WHITE);
- //update(FIRST_WHITE+1,-(FIRST_WHITE-LAST_WHITE));
- if (!a[i].s) break;
- }
- ADDRESS[FIRST_BLACK]=j;
- }
- T--;
- printf("%d\n",ans);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment