답안 #96601

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
96601 2019-02-10T11:46:56 Z psmao Transport (COCI19_transport) C++14
13 / 130
88 ms 7032 KB
#include <bits/stdc++.h>
using namespace std;

#define fo(i,s,t) for(int i = s; i <= t; ++ i)
#define fd(i,s,t) for(int i = s; i >= t; -- i)
#define bf(i,s) for(int i = head[s]; i; i = e[i].next)
#define mp make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define VI vector<int>
#define sf scanf
#define pf printf
#define fp freopen
#define SZ(x) ((int)(x).size())
#ifdef MPS
#define D(x...) printf(x)
#else
#define D(x...)
#endif
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
const int inf = 1<<30;
const ll INF = 1ll<<60;
const db Inf = 1e20;
const db eps = 1e-9;

void gmax(int &a,int b){a = (a > b ? a : b);}
void gmin(int &a,int b){a = (a < b ? a : b);}

const int maxn = 100050;

int n, a[maxn], nxt[maxn], l[maxn], r[maxn];
vector<pii> adj[maxn];
ll ans;

void explore(int u, int fa, int cur)
{
	if(cur < 0) return;
	if(fa) ++ ans;
	for(auto p : adj[u])
		if(p.fi != fa)
			explore(p.fi, u, cur + a[u] - p.se);
}
int main()
{
	#ifdef MPS	
		fp("1.in","r",stdin);
		fp("1.out","w",stdout);
	#endif
	sf("%d",&n);
	fo(i,1,n) sf("%d",&a[i]);
	bool chk = true;
	fo(i,2,n) 
	{
		int u, v, w; sf("%d%d%d",&u,&v,&w); 
		if(u > v) swap(u, v);
		chk &= (abs(u-v)==1);
		nxt[u] = w;
		adj[u].pb(mp(v,w)); adj[v].pb(mp(u,w));
	}
	if(n <= 5000) 
	{
		fo(i,1,n) explore(i,0,0);
		pf("%lld\n",ans);
	}
	else if(chk) 
	{
		fo(i,1,n) l[i] = r[i] = i;
		int cur = 0;
		fo(i,1,n) 
		{
			if(cur + a[i] - nxt[i] >= 0)
			{
				cur = cur + a[i] - nxt[i];
				l[i+1] = l[i];
			}
			else cur = 0;
		}
		cur = 0;
		fd(i,n-1,1)
		{
			if(cur + a[i+1] - nxt[i] >= 0)
			{
				cur = cur + a[i+1] - nxt[i];
				r[i] = r[i+1];
			}else cur = 0;
		}
		fo(i,1,n) ans += (r[i] - l[i]);
		pf("%lld\n",ans);
	}
	return 0;
}

Compilation message

transport.cpp: In function 'int main()':
transport.cpp:53:4: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  sf("%d",&n);
    ^
transport.cpp:54:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  fo(i,1,n) sf("%d",&a[i]);
              ^
transport.cpp:58:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int u, v, w; sf("%d%d%d",&u,&v,&w); 
                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 2808 KB Output is correct
2 Correct 15 ms 2812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 65 ms 2968 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 28 ms 4728 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 39 ms 5496 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 50 ms 6520 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 23 ms 3804 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 33 ms 4856 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 51 ms 5340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 63 ms 6172 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 88 ms 7032 KB Output isn't correct
2 Halted 0 ms 0 KB -