Monkeying Around Gym - 101350F (容器原理)

When the monkey professor leaves his class for a short time, all the monkeys go bananas. N monkeys are lined up sitting side by side on their chairs. They each have the same joke book. Before the professor returns, M jokes were heard.

Each of the M jokes are said in the order given and have the following properties:

xi - position of the monkey who said it.

li – index of the joke in the book.

ki – volume the monkey says that joke.

When the monkey at position xi says the joke li, all monkeys at a distance less than or equal to ki from that monkey (including the monkey who said the joke) will fall off their chairs in laughter if they have never heard the joke li before.

If the joke li has been heard anytime during the past before, and the monkey hears it again, then he will sit back up in his chair.

A monkey can fall off his chair more than once (every time he hears a new joke), and if he is already on the ground and hears a new joke, he will stay on the ground.

Can you figure out how many monkeys will be in their seats by the time the professor comes back?

Input

The first line of input is T – the number of test cases.

The first line of each test case is NM (1 ≤ N ≤ 105(1 ≤ M ≤ 105) – the number of monkeys in the class, and the number of jokes said before the professor returns.

The next M lines contain the description of each joke: xi, li, ki (1 ≤ xi ≤ N(1 ≤ li ≤ 105(0 ≤ ki ≤ N).

Output

For each test case, output on a line a single integer - the number of monkeys in their seats after all jokes have been said.

Example

Input
1
10 7
3 11 0
3 11 2
5 12 1
8 13 2
7 11 2
10 12 1
9 12 0
Output
3

题意:
给了一个矩阵,里面有炸弹,求不含炸弹的子矩阵个数。
思路:
状压枚举每种状况,减去奇数个炸弹的情况,加上偶数个炸弹的情况。
计算情况种数的方法,就是计算出与当前点有关的区间重合的左上角和右上角,把左上角和右上角的个数相乘就行了。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 100086;
const int inf = 2.1e9;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);
ll ans=0;
int x[maxn],y[maxn];
int n,m;
void solve(int p){
    int k=0;
    int maxx=0,maxy=0;
    int minx=inf,miny=inf;
    int t=1;
    while(p){
        if(p&1){
            maxx=max(maxx,x[t]);
            maxy=max(maxy,y[t]);
            minx=min(minx,x[t]);
            miny=min(miny,y[t]);
            k++;
        }
        p>>=1;
        t++;
    }
    if(k&1){
        k=-1;
    }
    else{
        k=1;
    }
    ans+=1ll*k*(minx)*miny*(n-maxx+1)*(m-maxy+1);
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        int k;
        scanf("%d%d%d",&n,&m,&k);
        int tot=1<<k;
        ans=1ll*n*(n+1)*m*(m+1)/4;
        for(int i=1;i<=k;i++){
            scanf("%d%d",&x[i],&y[i]);
        }

        for(int i=1;i<tot;i++){
            solve(i);
        }
        printf("%lld\n",ans);
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/ZGQblogs/p/10662176.html