Submission #334967

# Submission time Handle Problem Language Result Execution time Memory
334967 2020-12-10T13:34:46 Z Dymo Harbingers (CEOI09_harbingers) C++14
20 / 100
441 ms 65540 KB
#include<bits/stdc++.h>
using namespace std;


#define pb    push_back
#define eb   emplace_back
#define ll  long long
#define pll pair<ll,ll>
#define ff first
#define ss second
#define endl "\n"
const ll maxn=1e5+10;
const ll mod =1e9+7 ;
const ll base=3e18;
bool ktu=false;
struct Line {
	mutable ll k, m, p;
	bool operator<(const Line& o) const { return k > o.k; }
	bool operator<(ll x) const { return p < x; }
};

struct tk : multiset<Line, less<>> {
	static const ll inf = LLONG_MAX;
	ll div(ll a, ll b) {
		return a / b - ((a ^ b) < 0 && a % b); }
	bool isect(iterator x, iterator y) {
		if (y == end()) return x->p = inf, 0;
		if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
		else x->p = div(y->m - x->m, x->k - y->k);
		return x->p >= y->p;
	}
	void add(ll k, ll m) {
		auto z = insert({k, m, 0}), y = z++, x = y;
		while (isect(y, z)) z = erase(z);
		if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
		while ((y = x) != begin() && (--x)->p >= y->p)
			isect(x, erase(y));
	}
	ll query(ll x) {
		if (empty()) return base;
		auto l = *lower_bound(x);
		return l.k * x + l.m;
	}
};
vector<pll> adj[maxn];

ll dep[maxn];
ll siz[maxn];
tk st[4*maxn];
ll par[maxn];

ll get1(ll id,ll left,ll right,ll x,ll y,ll t )
{
    if (x>right||y<left) return base;
    if (x<=left&&y>=right)
    {
       /* if (ktu)
        {
            cout <<"WTF"<<" "<<id<<" "<<st[id].query(t)<<endl;
        }*/
        return st[id].query(t);
    }
    ll mid=(left+right)/2;
    return min(get1(id*2,left,mid, x, y,t ),get1(id*2+1,mid+1, right, x, y,t ));
}
void update(ll id,ll left,ll right,ll x,ll y,ll pos)
{
    if (pos>right||pos<left) return ;
   /* if (pos==1)
    {
        cout <<left<<" "<<right<<" "<<x<<" "<<y<<" "<<id<<endl;
    }*/
    st[id].add(x,y);
    if (left==right) return;
    ll mid=(left+right)/2;
    if (mid>=pos) update(id*2,left,mid,x, y, pos);
    else update(id*2+1, mid+1, right, x, y, pos);
}

void dfs(ll u,ll par1)
{
    siz[u]=1;

    for (auto p:adj[u])
    {
        ll h=p.ff;
        if (h==par1) continue;
        dep[h]=dep[u]+p.ss;

        par[h]=u;
        dfs(h,u);
        siz[u]+=siz[h];

    }
}


ll dp[maxn];
ll cntnw=0;
ll nchain=1;
ll chainhead[maxn];
ll chainid[maxn];
ll id[maxn];


void hld(ll u )
{
    if (!chainhead[nchain])
        chainhead[nchain]=u;
    cntnw++;
    chainid[u]=nchain;
    id[u]=cntnw;
    ll nxt=-1;
    for (auto p:adj[u])
    {
        ll to=p.ff;
        if (to==par[u]) continue;
        if (nxt==-1||siz[nxt]<siz[to])
        {
            nxt=to;

        }
    }
    if (nxt!=-1)
    {
        hld(nxt);

    }
    for (auto p:adj[u])
    {
        ll to= p.ff;
        if (to==par[u]) continue;
        if (to==nxt)
            continue;
        nchain++;
        hld(to);
    }


}

ll get(ll l,ll r,ll x)
{
   return get1(1,1,cntnw,l,r,x);
}
void dosth(ll u,ll a ,ll x)
{
    ll p=chainid[u];
    ll chk= chainid[a];
    ll p1=u;
    dp[p1]=base;
    while (1)
    {
        if (p==chk)
        {
            dp[p1]=min(dp[p1],get(id[a],id[u],x));
            break;
        }
         dp[p1]=min(dp[p1],get(id[chainhead[p]],id[u],x));
        u=par[chainhead[p]];
        p=chainid[u];
    }
}

ll s[maxn];
ll v[maxn];

void dfs1(ll u)
{
    if (u==1)
    {
        dp[u]=0;
    }
    else
    {
        dosth(u,1,-v[u]);
       /* if (u==2)
        {
            cout <<q[u]+dep[u]*p[u]<<" "<<dp[u]<<endl;
        }*/
        dp[u]=(dp[u]+v[u]*dep[u]+s[u]);
    }

    update(1,1,cntnw,dep[u],dp[u],id[u]);
 //   cout <<dp[u]<<" "<<u<<endl;
   for (auto p:adj[u])
   {
      ll to=p.ff;
      if (to==par[u]) continue;
      dfs1(to);
   }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    if (fopen("harbingers.in", "r"))
    {
        freopen("harbingers.in", "r", stdin);
        freopen("harbingers.out", "w", stdout);
    }
    ll n;
     cin>>n ;
     for (int i=1;i<=n-1;i++)
     {
         ll x, y, d;
          cin>> x>> y>> d;
          adj[x].pb(make_pair(y,d));
          adj[y].pb(make_pair(x,d));
     }
     dfs(1,0);
     hld(1);
     for (int i=2;i<=n;i++)
     {
         cin>>s[i]>>v[i];

     }
     dfs1(1);
     for (int i=2;i<=n;i++)
     {
         cout <<dp[i]<<" ";
     }





}

Compilation message

harbingers.cpp: In function 'int main()':
harbingers.cpp:200:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  200 |         freopen("harbingers.in", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
harbingers.cpp:201:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
  201 |         freopen("harbingers.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 15 ms 21484 KB Output is correct
2 Correct 21 ms 22636 KB Output is correct
3 Runtime error 200 ms 44268 KB Memory limit exceeded
4 Runtime error 339 ms 61164 KB Memory limit exceeded
5 Runtime error 344 ms 59752 KB Memory limit exceeded
6 Runtime error 441 ms 65540 KB Memory limit exceeded
7 Runtime error 298 ms 51052 KB Memory limit exceeded
8 Runtime error 235 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
9 Runtime error 253 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
10 Runtime error 202 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)