1. 程式人生 > >1391(離線樹狀陣列)

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;
}