HihoCoder - 1391(离线树状数组)

1391 : Countrie

时间限制:1000ms
单点时限:1000ms
内存限制:256MB
描述
There are two antagonistic countries, country A and country B. They are in a war, and keep launching missiles towards each other.

It is known that country A will launch N missiles. The i-th missile will be launched at time Tai. It flies uniformly and take time Taci from one country to the other. Its damage capability is Dai.

It is known that country B will launch M missiles. The i-th missile will be launched at time Tbi.

It flies uniformly and takes time Tbci from one country to the other. Its damage capability is Dbi.

Both of the countries can activate their own defending system.

The defending system of country A can last for time TA, while The defending system of country B can last for time TB.

When the defending system is activated, all missiles reaching the country will turn around and fly back at the same speed as they come.

At other time, the missiles reaching the country will do damages to the country.
(Note that the defending system is still considered active at the exact moment it fails)

Country B will activate its defending system at time X.

When is the best time for country A to activate its defending system? Please calculate the minimal damage country A will suffer.

输入
There are no more than 50 test cases.

For each test case:

The first line contains two integers TA and TB, indicating the lasting time of the defending system of two countries.

The second line contains one integer X, indicating the time that country B will active its defending system.

The third line contains two integers N and M, indicating the number of missiles country A and country B will launch.

Then N lines follow. Each line contains three integers Tai, Taci and Dai, indicating the launching time, flying time and damage capability of the i-th missiles country A launches.

Then M lines follow. Each line contains three integers Tbi, Tbci and Dbi, indicating the launching time, flying time and damage capability of the i-th missiles country B launches.

**0 <= TA, TB, X, Tai, Tbi<= 100000000
1 <= Taci, Tbci <= 100000000
0 <= N, M <= 10000
1 <= Dai, Dbi <= 10000**

输出
For each test case, output the minimal damage country A will suffer.

提示
In the first case, country A should active its defending system at time 3.

Time 1: the missile is launched by country A.

Time 2: the missile reaches country B, and country B actives its defending system, then the missile turns around.

Time 3: the missile reaches country A, and country A actives its defending system, then the missile turn around.

Time 4: the missile reaches country B and turns around.

Time 5: the missile reaches country A and turns around.

Time 6: the missile reaches country B, causes damages to country B.

样例输入
2 2
2
1 0
1 1 10
4 5
3
2 2
1 2 10
1 5 7
1 3 2
0 4 8
样例输出
0
17

题意:
A和B两个国家互射导弹,每个国家都有一个防御系统,在防御系统开启的时间内可以将到达本国的导弹反弹回去(掉头,防御系统不能开开关关)。

  现在已知:Ta、Tb为A、B两国导弹防御能开启的持续时间,X为B国开启导弹防御系统的时刻(持续时间为[X,Tb+X],包含端点)

  A向B发射N枚导弹,B向A发射M枚导弹。每枚导弹有3个值:发射时间,从A到B或从B到A的飞行时间,伤害值。

  现在A可以在任意时刻开启防御系统,求A所受到的最小伤害值。

思路:
我们可以知道每颗导弹到达A的时间(到不了的可以直接忽略掉).这样就有一种暴力思路,枚举每个导弹到来的时候开启防御,然后处理每个导弹会不会打在A上,这样复杂度是N^2的.
我们可以预处理出每个导弹到来时开启防御至少需要多长时间的防御罩才能挡住导弹.
然后我们可以按照上面暴力思路,此时问题变为在这段防御时间里能挡住多少导弹,也就是我们刚刚处理的每个导弹需要的截止时间有多少在这个范围内,当然到达时间必须在我们当前枚举的导弹之后.
此时问题就变为了 给你n个数,不断询问某个区间内小于等于某个数的数有多少个.
按照导弹需要的截止时间排序,用树状数组处理.

代码:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5+5;

struct node
{
    int at,tt,d,pos;
    node(){}
    node(int at,int tt,int d):at(at),tt(tt),d(d){}
    friend bool operator < (node x,node y)
    {
        return x.tt< y.tt;
    }
} e[maxn];

int ta,tb,n,m,xt,et;
int cnt;
int q[maxn],c[maxn];

void add(int la,int fa,int da)
{   
    int tmp = 0,nowtime = la;
    if(nowtime+fa> et||nowtime+fa< xt)
        e[++cnt] = node(la,la+tmp,da);
    else
    {
        tmp+= fa;
        nowtime+= fa;

        int num = (et-nowtime)/(fa+fa);
        tmp+= num*(fa+fa);
        tmp+= fa;
        e[++cnt] = node(la,la+tmp,da);
    }
    return ;
}

int lowbit(int x)
{
    return x&(-x);
}

void add(int x,int val)
{
    for(int i = x;i<= cnt;i+= lowbit(i))
        c[i]+= val;
    return ;
}

int query(int l,int r)
{
    int ans = 0;
    for(int i = r;i> 0;i-= lowbit(i))
        ans+= c[i];
    for(int i = l-1;i> 0;i-= lowbit(i))
        ans-= c[i];
    return ans;
}

bool cmp(node x,node y)
{
    return x.at< y.at;
}

int main()
{
    while(~scanf("%d %d",&ta,&tb))
    {
        mem(c,0);
        cnt = 0;
        int sum = 0;
        scanf("%d %d %d",&xt,&n,&m);
        et = xt+tb;
        for(int i = 1,x,y,z;i<= n;i++)
        {
            scanf("%d %d %d",&x,&y,&z);
            if(x+y>= xt&&x+y<= et)
            {
                add(x+y+y,y,z);
                sum+= z;
            }
        }
        for(int i = 1,x,y,z;i<= m;i++)
        {
            scanf("%d %d %d",&x,&y,&z);
            add(x+y,y,z);
            sum+= z;
        }

        sort(e+1,e+cnt+1,cmp);
        for(int i = 1;i<= cnt;i++)
        {
            e[i].pos = i;
            q[i] = e[i].at+ta;
        }
        sort(e+1,e+cnt+1);

        int ans = 0,l = 1;
        for(int i = 1;i<= cnt;i++)
        {
            while(e[l].tt<= q[i]&&l<= cnt)
                add(e[l].pos,e[l].d),l++;
            ans = max(ans,query(i,cnt));
        }

        printf("%d\n",sum-ans);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值