性感代码,在线求调教
查看原帖
性感代码,在线求调教
997374
liangxinyue楼主2024/7/11 20:20
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<bitset>
namespace fast_IO {
#define FASTIO
#define IOSIZE 100000
	char ibuf[IOSIZE], obuf[IOSIZE];
	char *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#ifdef ONLINE_JUDGE
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#endif//fread in OJ, stdio in local
	
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
	template<typename T> inline T read() {
		T s = 0;
		int w = 1;
		char ch;
		while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1;
		if (ch == EOF) return false;
		while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar();
		return s * w;
	}
	template<typename T> inline bool read(T &s) {
		s = 0;
		int w = 1;
		char ch;
		while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1;
		if (ch == EOF) return false;
		while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar();
		return s *= w, true;
	}
	inline bool read(char &s) {
		while (s = getchar(), isspace(s));
		return true;
	}
	inline bool read(char *s) {
		char ch;
		while (ch = getchar(), isspace(ch));
		if (ch == EOF) return false;
		while (!isspace(ch)) *s++ = ch, ch = getchar();
		*s = '\000';
		return true;
	}
	template<typename T> inline void print(T x) {
		if (x < 0) putchar('-'), x = -x;
		if (x > 9) print(x / 10);
		putchar(x % 10 + 48);
	}
	inline void print(char x) {
		putchar(x);
	}
	inline void print(char *x) {
		while (*x) putchar(*x++);
	}
	inline void print(const char *x) {
		for (int i = 0; x[i]; i++) putchar(x[i]);
	}
#ifdef _GLIBCXX_STRING
	inline bool read(std::string& s) {
		s = "";
		char ch;
		while (ch = getchar(), isspace(ch));
		if (ch == EOF) return false;
		while (!isspace(ch)) s += ch, ch = getchar();
		return true;
	}
	inline void print(std::string x) {
		for (int i = 0, n = x.size(); i < n; i++)
			putchar(x[i]);
	}
#endif//string
	template<typename T, typename... T1> inline int read(T& a, T1&... other) {
		return read(a) + read(other...);
	}
	template<typename T, typename... T1> inline void print(T a, T1... other) {
		print(a);
		print(other...);
	}
	
	struct Fast_IO {
		~Fast_IO() {
			fwrite(obuf, p3 - obuf, 1, stdout);
		}
	} io;
	template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) {
		return read(b), io;
	}
	template<typename T> Fast_IO& operator << (Fast_IO &io, T b) {
		return print(b), io;
	}
}
using namespace fast_IO;
typedef long long ll;
using namespace std;
const int N = 5e4, inf = 0x7fffffff / 2;
ll a[N + 10];
double mid;
ll k;
int cnt_hash;
namespace TA{
	int c[N * 2 + 10];
	inline int lowbit(int i){
		return i & (-i);
	}
	inline void modify(int i, int add){
		for(; i <= cnt_hash; i += lowbit(i))
			c[i] += add;
	}
	inline int getsum(int i){
		int sum = 0;
		for(; i; i -= lowbit(i))
			sum += c[i];
		return sum;
	}
}
namespace TD{
	struct edge{
		int nxt, to;
		ll val;
	} e[N * 2 + 10];
	int head[N + 10], cnt;
	inline void add(int u, int v, ll w){
		e[++cnt] = (edge){head[u], v, w};
		head[u] = cnt;
	}
	int size[N + 10], f[N + 10];
	bool tag[N + 10];
	void init(int u, int fa){
		size[u] = 1;
		for(int i = head[u]; i; i = e[i].nxt){
			int v = e[i].to;
			if(v == fa | tag[v])
				continue;
			init(v, u);
			size[u] += size[v];
		}
	}
	int root;
	void heavy(int u, int fa, int rt){
		f[u] = size[rt] - size[u];
		for(int i = head[u]; i; i = e[i].nxt){
			int v = e[i].to;
			if(v == fa | tag[v])
				continue;
			heavy(v, u, rt);
			f[u] = max(f[u], size[v]);
		}
		if(f[root] > f[u])
			root = u;
	}
	int stk[N + 10], top;
	pair<ll, int> dis[N + 10];
	void modify(int u, int fa){
		stk[++top] = u;
		for(int i = head[u]; i; i = e[i].nxt){
			int v = e[i].to;
			if(v == fa | tag[v])
				continue;
			dis[v] = make_pair(dis[u].first + e[i].val, dis[u].second + 1);
			modify(v, u);
		}
	}
	ll ans;
	struct quest{
		double x, y;
		int op;
	} val[N * 2 + 10];
	double hash[N * 2 + 10];
	inline void update(int l, int r, int op){
		//(k-mid)*dis[x].second-dis[x].first<=(mid-k)*dis[y].second+dis[y].first, dis[x].first-(k+mid)*dis[x].second<=(k+mid)*dis[y].second-dis[y].first
		
		//(k-mid)*dis[x].second-dis[x].first<=-((k-mid)*dis[y].second-dis[y].first), dis[x].first-(k+mid)*dis[x].second<=-(dis[y].first-(k+mid)*dis[y].second)
		
		//A[i] = (k-mid)*dis[i].second-dis[i].first
		//B[i] = dis[i].first-(k+mid)*dis[i].second
		
		//A[x]<=-A[y]且B[x]<=-B[y]
		int cnt_val = 0;
		cnt_hash = 0;
		for(int i = l; i <= r; i++){
			double x = (k - mid) * dis[stk[i]].second - dis[stk[i]].first, y = dis[stk[i]].first - (k + mid) * dis[stk[i]].second;
			val[++cnt_val] = (quest){x, y, 1};
			val[++cnt_val] = (quest){-x, -y, 0};
			hash[++cnt_hash] = y, hash[++cnt_hash] = -y;
		}
		sort(hash + 1, hash + cnt_hash + 1);
		cnt_hash = unique(hash + 1, hash + cnt_hash + 1) - hash - 1;
		for(int i = 1; i <= cnt_val; i++)
			val[i].y = lower_bound(hash + 1, hash + cnt_hash + 1, val[i].y) - hash;
		//val[x].first <= -val[y].first && val[x].second <= -val[y].second
		sort(val + 1, val + cnt_val + 1, [](const quest &x, const quest &y){
			if(x.x != y.x)
				return x.x < y.x;
			return x.op > y.op;
		});
		for(int i = 1; i <= cnt_val; i++){
			if(val[i].op)
				TA::modify(val[i].y, 1);
			else
				ans += TA::getsum(val[i].y) * op;
		}
		for(int i = 1; i <= cnt_val; i++)
			if(val[i].op)
				TA::modify(val[i].y, -1);
	}
	bool build(int u, int fa){
		root = top = 0;
		init(u, fa);
		heavy(u, fa, u);
		tag[root] = 1;
		for(int i = head[root]; i; i = e[i].nxt){
			int v = e[i].to;
			if(tag[v])
				continue;
			dis[v] = make_pair(e[i].val, 1);
			int lst = top + 1;
			modify(v, root);
			update(lst, top, -1);
		}
		update(1, top, 1);
		if(ans)
			return true;
		for(int i = head[root]; i; i = e[i].nxt){
			int v = e[i].to;
			if(tag[v])
				continue;
			if(build(v, root))
				return true;
		}
		return false;
	}
}
namespace furina{
	int n;
	ll maxn, ans = 0x7fffffffffffffff / 2;
	int main(){
		io >> n >> k;
		for(int i = 1, u, v; i < n; i++){
			ll w;
			io >> u >> v >> w;
			TD::add(u, v, w), TD::add(v, u, w);
			ans = min(ans, abs(w - k));
			maxn = max(maxn, w);
		}
		double L = 0, R = maxn;
		while(R - L >= 0.3){
			mid = (L + R) / 2;
			TD::ans = 0;
			for(int i = 1; i <= n; i++)
				TD::tag[i] = 0;
			TD::f[0] = inf;
			if(TD::build(1, 0))
				R = mid;
			else
				L = mid;
		}
		io << min(ans, (ll)(R));
		return 0;
	}
}
signed main(){
	return furina::main();
}

爸❤妈❤不❤在❤家

妹❤妹❤一❤个❤人❤在❤家

寂❤寞❤无❤聊❤求❤调❤教

TLE 60 死活过不去

2024/7/11 20:20
加载中...