Guest User

Untitled

a guest
Nov 6th, 2013
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <sstream>
  6. #include <string>
  7. #include <cstring>
  8. #include <vector>
  9. #include <queue>
  10. #include <set>
  11. #include <map>
  12. #include <sstream>
  13. #include <algorithm>
  14. #define f first
  15. #define s second
  16. #define pb push_back
  17. #define mp make_pair
  18. #define inf 2000000000
  19. #define Min(a,b) (a<b?a:b)
  20. #define Max(a,b) (a>b?a:b)
  21. #define For(i,s,n) for (int i=s;i<=n;i++)
  22. #define FOR(i,s,n) for (int i=s;i<n;i++)
  23. #define Ford(i,s,n) for (int i=s;i>=n;i--)
  24. #pragma comment(linker, "/STACK:16777216")
  25.  
  26.  
  27. #define MD 1000000007
  28.  
  29.  
  30. typedef long long ll;
  31.  
  32. using namespace std;
  33.  
  34.  
  35. pair <pair <int,int>,int > a[50009];
  36. int T,N,t[200105],ans,FIRST_BLACK,FIRST_WHITE,LAST_WHITE,ADDRESS[50009],M;
  37. bool used[50009];
  38.  
  39.  
  40.  
  41. bool cmp(pair <pair <int,int>,int > A,pair <pair <int,int>,int > B){
  42.     swap(A.f.f,A.f.s);
  43.     swap(B.f.f,B.f.s);
  44.     return A.f<B.f;
  45. }
  46.  
  47.  
  48. void build(int v,int tl,int tr){
  49.     if (tl==tr) t[v]=0;else{
  50.         int tm=(tl+tr)/2;
  51.         build(2*v,tl,tm);
  52.         build(2*v+1,tm+1,tr);
  53.         t[v]=t[2*v]+t[2*v+1];
  54.     }
  55. }
  56.  
  57.  
  58. void update(int v,int tl,int tr,int pos){
  59.     if (tl==tr)
  60.         t[v]++;else{
  61.             int tm=(tl+tr)/2;
  62.             if (pos<=tm) update(2*v,tl,tm,pos);else update(2*v+1,tm+1,tr,pos);
  63.             t[v]=t[2*v]+t[2*v+1];
  64.     }
  65. }
  66.  
  67.  
  68.  
  69. int get_sum(int v,int tl,int tr,int l,int r){
  70.     if (l>r) return 0;
  71.     if (l==tl&&r==tr) return t[v];
  72.     int tm=(tl+tr)/2;
  73.     return get_sum(2*v,tl,tm,l,Min(r,tm))+get_sum(2*v+1,tm+1,tr,Max(l,tm+1),r);
  74. }
  75.  
  76.  
  77. int main(){
  78.     freopen("i.txt","r",stdin);freopen("o.txt","w",stdout);
  79.     scanf("%d",&T);
  80.     while(T){
  81.         scanf("%d",&N);
  82.         M=0;
  83.         For(i,1,N){
  84.             scanf("%d%d%d",&a[i].f.f,&a[i].f.s,&a[i].s);
  85.             a[i].f.f++;a[i].f.s++;
  86.             M=Max(M,a[i].f.s);
  87.         }
  88.         ans=0;
  89.         build(1,1,M);
  90.         For(i,0,M) used[i]=false;
  91.         sort(a+1,a+N+1,cmp);
  92.         For(i,1,N){
  93.             int C=get_sum(1,1,M,a[i].f.f,a[i].f.s);
  94.             if (C>=a[i].s) continue;
  95.             a[i].s-=C;
  96.             ans+=a[i].s;
  97.             int j=a[i].f.s;
  98.             FIRST_BLACK=j;
  99.             while(1){
  100.                 if (used[j]) j=ADDRESS[j];
  101.                 FIRST_WHITE=j;
  102.                 used[j]=true;
  103.                 //cout<<j<<endl;
  104.                 a[i].s--;
  105.                 j--;
  106.                 LAST_WHITE=j;
  107.                 while(a[i].s){
  108.                     if (used[j]){
  109.                         //LAST_WHITE=j+1;
  110.                         break;
  111.                     }
  112.                     used[j]=true;
  113.                     //cout<<j<<endl;
  114.                     j--;
  115.                     LAST_WHITE=j;
  116.                     a[i].s--;
  117.                 }
  118.                 For(k,LAST_WHITE+1,FIRST_WHITE) update(1,1,M,k);
  119.                 //update(1,1,M,LAST_WHITE+1,FIRST_WHITE);
  120.                 //update(LAST_WHITE+1,FIRST_WHITE-LAST_WHITE);
  121.                 //update(FIRST_WHITE+1,-(FIRST_WHITE-LAST_WHITE));
  122.                 if (!a[i].s) break;
  123.             }
  124.             ADDRESS[FIRST_BLACK]=j;
  125.         }
  126.         T--;
  127.         printf("%d\n",ans);
  128.     }
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment