天天看點

HDU 6018 Game Arrangement

Problem Description

Luras is fond of playing games. Once she gets free, she might try playing games such as dato and cf.

However, if you have read the past problems, you will know that luras is very busy even if she skips many classes.

As a result, she wants to arrange her time for playing more rounds of games during her free time.

It is known that luras has m types of games which she is willing to play, and for each game type, there is a time segment,

luras will only try playing one game type during its limited time segment (and ofc in the class-skipping time).

Besides, you are told of the needed time for each round of every game type.

Be attention, once luras started a game, she needs to finish it without any break during the limited time.

Now please help luras arrange her time in order to play more rounds of games.

Input

The first line is an integer T which indicates the case number.

and as for each case,

the first line are two integer n and m which indicate the number of time segment of skipping the class and the game types luras wants to try.

then there are n lines, for the i-th line, there are 2 integers L[i] and R[i], which mean that luras could skip the class for games at the R[i] - L[i] + 1 time point { L[i], L[i] + 1, ..., R[i] - 1, R[i] }.

then there are m lines, for the i-th line, there are 3 integers l[i], r[i] and d[i], which mean that only during the r[i] - l[i] + 1 time point { l[i], l[i] + 1, ..., r[i] - 1, r[i] }, could luras play the i-th type game.

once luras spends a d[i] continuous time segment during [ l[i], r[i] ], she could finish 1 round of the i-th game.

At last please note luras could only play at most 1 round game at a time point, she is a cut noob : ) .

It is guaranteed that——

1 <= T <= 1000
 

 1 <= L[1] <= R[1] < L[2] <= R[2] < ... < L[n] <= R[n] <= 10^9
 

 1 <= l[] <= r[] <= 10^9
 

 1 <= d[] <= 10^9
 

 for 99% cases,1 <= n, m <= 100
 

 for 100% cases,1 <= n, m <= 10000      

Output

As for each case, you need to output a single line.

there should be 1 integer in the line which represents the most rounds of games luras can play if she arrange in the best way.

Sample Input

4
2 2
1 1
2 5
1 3 1
4 5 2

2 2
1 1
3 4
1 3 1
4 5 2

3 1
1 1
3 3
5 5
1 5 2

1 1
1 10
3 5 2      

Sample Output

1

#include<map> 
#include<set>
#include<ctime>  
#include<cmath>      
#include<queue>   
#include<string>  
#include<vector>  
#include<cstdio>      
#include<cstring>    
#include<iostream>  
#include<algorithm>      
#include<functional>  
using namespace std;
#define ms(x,y) memset(x,y,sizeof(x))      
#define rep(i,j,k) for(int i=j;i<=k;i++)      
#define per(i,j,k) for(int i=j;i>=k;i--)      
#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])      
#define inone(x) scanf("%d",&x)      
#define intwo(x,y) scanf("%d%d",&x,&y)      
#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)    
#define infou(x,y,z,p) scanf("%d%d%d%d",&x,&y,&z,&p)   
#define lson x<<1,l,mid  
#define rson x<<1|1,mid+1,r  
#define mp(i,j) make_pair(i,j)  
#define ft first  
#define sd second  
typedef long long LL;
typedef pair<int, int> pii;
const int low(int x) { return x&-x; }
const int INF = 0x7FFFFFFF;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const double eps = 1e-10;
int T, n, m, cnt, x, y, z;
pii a[N], b[N];
multiset<int> p;

int main()
{
    for (inone(T); T--;)
    {
        intwo(n, m);
        a[cnt = 0].sd = -1;
        rep(i, 1, n)
        {
            intwo(a[i].ft, a[i].sd);
            if (a[i].ft == a[cnt].sd + 1) a[cnt].sd = a[i].sd;
            else a[++cnt] = a[i];
        }
        n = cnt; cnt = 0;
        rep(i, 1, m)
        {
            inthr(x, y, z);
            if (y - z + 1 < x) continue;
            b[++cnt] = mp(x, z);
            b[++cnt] = mp(y - z + 2, -z);
        }
        rep(i, 1, n) b[++cnt] = mp(a[i].sd + 1, INF);
        m = cnt;
        sort(b + 1, b + m + 1);
        int ans = 0; p.clear();
        for (int i = 1, j = 1; i <= n; i++)
        {
            for (; j <= m&&b[j].ft <= a[i].ft; j++)
            {
                if (b[j].sd < 0) p.erase(p.find(-b[j].sd));
                else p.insert(b[j].sd);
            }
            int last = INF, bef = a[i].ft;
            for (int k; j <= m && b[j].ft <= a[i].sd + 1; j = k)
            {
                int L = b[j].ft - bef; bef = b[j].ft;
                if (last != INF)
                {
                    int g = min(last, L);
                    L -= g; last -= g;
                    if (last == 0) ++ans, last = INF;
                }
                if (last == INF && !p.empty())
                {
                    int now = *p.begin();
                    ans += L / now;
                    last = L % now;
                    if (last) last = now - last; else last = INF;
                }
                for (k = j; k <= m && b[k].ft == b[j].ft; k++)
                {
                    if (b[k].sd < 0) p.erase(p.find(-b[k].sd));
                    else p.insert(b[k].sd);
                }
                if (!p.empty()) last = min(last, *p.begin());
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}