[Codeforces Round #625][Codeforces 1320C/1321E. World of Darkraft: Battle for Azathoth]

题目链接:1320C - World of Darkraft: Battle for Azathoth/1321E - World of Darkraft: Battle for Azathoth

Title effect: YES \ (n-\) sword species, each with a corresponding attack values and prices, \ (m \) shield species, each with a corresponding price and defense value. And \ (p \) a monster, each monster has a corresponding attack value, defense value, the number of coins to defeat it gets. Required to buy exactly sword - a shield, seeking maximum benefit (or negative) can beat after beat all the monsters, which defeated a monster of necessary and sufficient conditions are: sword attack value strictly greater than the defense value of the monster, and shield defense value strictly greater than the value of the monster attack.

Solution: Consider buying enumeration in the first \ (i \) maximum benefit under the prerequisite kinds sword, then only defense is less than its monster attack is likely to be defeated. These monsters into a collection within the equivalent of several months monster, seeking to buy a shield optimal solution (monster gains ATK less than the value of the defense shield and - the maximum price shield).

   So we can sort of sword attack value, this enumeration \ (i \) when the monster collection to meet the conditions must be monotonically increasing. After each monster will join a collection, you can update the revenue of all defense shield values ​​greater than the value of the attack. Obviously, this can be solved with a maximum range of plus tree line and queries the global defense shield by discrete values, but because I am too weak and not very good at writing segment tree, forced to use the approach block. But to pay up after the discovery actual running time only 374ms, even faster than the tree line part of the practice run, so come to share with you _ (: з "∠) _

   The shield sorted by Defensive, which is divided into blocks, provided for each block size \ (B \), to maintain each of maximum benefit, wherein the first \ (I \) a shield return is \ (F_i \), initially set to \ (- cb_i \). Each time a monster is added to the collection, assuming that the current value of the monster's attack is \ (w \), the reward is \ (v \), then all meet \ (b_i> w \) is \ (f_i \) are to be added on \ (v \). So we can \ (upper \ text {_} bound \) can find the first beat of the monster shield \ (b_k \), modification operations.

   To \ block (k \) where we can direct violence modify \ (f_k \) and updates the value of the block where the answers. For each block of modified later, we can hit the mark by the way, the current record revenue in the region are all added much, do not modify the values in the block, as long as additional plus when seeking gains after each point tag value like. And each time changes, we can return the maximum value of the current real-time updates, do not operate the additional questioning.

   Obviously \ (\ sqrt {m} \) Optimal when \ (B \) to take, the time complexity is \ (O (p \ sqrt {m}) \).

 

#include<bits/stdc++.h>
using namespace std;
#define N 200001
int n,m,p,B,nb,cur,ans,f[N],mx[N],bel[N],tag[N];
struct rua
{
    int w,v,w2;
    void read(){scanf("%d%d",&w,&v);}
    void read2(){scanf("%d%d%d",&w,&w2,&v);}
    bool operator <(const rua &t)const{return w<t.w;}
}a[N],b[N],c[N];
void Build()
{
    int l=1,r=min(m,B);
    while(l<=m)
      {
      mx[++nb]=ans;
      for(int i=l;i<=r;i++)
        {
        bel[i]=nb;
        f[i]=-b[i].v;
        mx[nb]=max(mx[nb],f[i]);
        }
      cur=max(cur,mx[nb]);
      l=r+1,r=min(m,l+B-1);
      }
}
void add(int x)
{
    int w=c[x].w2,v=c[x].v;
    int k=upper_bound(b+1,b+m+1,(rua){w,v,233})-b;
    while(k<=m && bel[k]==bel[k-1])
      {
      f[k]+=v;
      mx[bel[k]]=max(mx[bel[k]],f[k]+tag[bel[k]]);
      cur=max(cur,mx[bel[k]]);
      k++;
      }
    if(k>m)return;
    for(int i=bel[k];i<=nb;i++)
      tag[i]+=v,mx[i]+=v,cur=max(cur,mx[i]);
}
int main()
{
    B=sqrt(N)+1;
    cur=ans=-2147483647;
    scanf("%d%d%d",&n,&m,&p);
    for(int i=1;i<=n;i++)a[i].read();
    for(int i=1;i<=m;i++)b[i].read();
    for(int i=1;i<=p;i++)c[i].read2();
    sort(a+1,a+n+1);
    sort(b+1,b+m+1);
    sort(c+1,c+p+1);
    Build();
    int k=1;
    for(int i=1;i<=n;i++)
      {
      while(k<=p && c[k].w<a[i].w)add(k++);
      ans=max(ans,-a[i].v+cur);
      }
    printf("%d\n",ans);
}
View Code

Feel old ... if placed two years ago, some things are twenty-three four lines will be directly pressed into line _ (: з "∠) _

Guess you like

Origin www.cnblogs.com/DeaphetS/p/12393494.html