ZOJ2019年1月月赛

A

找规律
就是二进制展开权重

#include 

using namespace std;

using ll = long long;

ll KSM(ll a, ll k) {
	ll ret = 1;
	for (; k ; k >>= 1) {
		if (k & 1) 
			ret = ret * a;
		a = a * a;
	}
	return ret;
}

ll calc(ll n) {
	if (n == 0) 
		return 0;
	return calc(n - KSM(2, log2(n))) + 1;
}

int main() {
	// static int a[200][200];
	// a[1][1] = 1;
	// for (int i = 2; i <= 100; ++i)
	//     for (int j = 1; j <= i; ++j)
	//         a[i][j] = a[i - 1][j] + a[i - 1][j - 1],
	//         a[i][j] %= 2;
	// for (int i = 1; i <= 100; ++i) {
	//     // bitset<1000> v(i);
	//     // cout << v.count() << " : ";
	//     int sum = 0;
	//     for (int j = 1; j <= i; ++j)
	//         sum += a[i][j];
	//     cout << KSM(2, calc(i - 1)) << ' ';
	//     cout << sum << '\n';
	// }
	ios::sync_with_stdio(0);
	int T;
	cin >> T;
	while (T--) {
		ll n;
		cin >> n;
		cout << KSM(2, calc(n - 1)) << '\n';
	}
	return 0;
}

B

最开始没有发现u,v也是1到N,于是大力上了线段树。然而T的无法自理,ZOJ还讨厌我的读入挂,加了就不给我提交。
但是如果发现了u,v也是1到N的,那么注意到答案点的y坐标随着x的增加是单调的,所以直接扫一遍就完事了

AC代码

#include
#include
#include
using namespace std;

using ll = long long;

inline ll read() {
	ll d = 0, f = 1; 
	char s = getchar();
	while (s < '0' || s > '9') {
		if (s == '-')
			f = -1;
		s = getchar();
	}
	while (s >= '0' && s <= '9') {
		d = d * 10 + s - '0';
		s = getchar();
	}
	return d * f;
}

int const N = 1e5 + 5;

struct edge {
	int y, next;
}a[N], totx[N];
int lasta[N], ne, lastx[N], xne;
int n, m;

void add(int x, int y) {
	a[++ne].y = y;
	a[ne].next = lasta[x]; 
	lasta[x] = ne;
}

void addtot(int y, int x) {
	totx[++xne].y = x;
	totx[xne].next = lastx[y];
	lastx[y] = xne;
}

int solve(ll s) {
	for (int i = 1; i <= n; ++i)
		lastx[i] = 0;
	xne = 0;
	int ret = 0, ansy = n;
	ll sumx = 0, sumy = 0, sump = 0;
	for (int i = 1; i <= n; ++i) {
		for (int j = lasta[i]; j != 0; j = a[j].next) {
			int y = a[j].y;
			if (y <= ansy) {
				sumx += i;
				sumy += y;
				++sump;
				addtot(y, i);
			}
		}

		while (sump * (i + ansy) - sumx - sumy > s) {
			for (int j = lastx[ansy]; j != 0; j = totx[j].next) {
				int x = totx[j].y;
				sumx -= x;
				--sump;
				sumy -= ansy;
			}
			--ansy;
		}

		if (sump * (i + ansy) - sumx - sumy == s)
			++ret;
	}
	return ret;
}

int main() {
	int T = read();
	while (T--) {
		n = read(), m = read();
		for (int i = 1; i <= n; ++i)
			lasta[i] = 0;
		ne = 0;

		for (int i = 1; i <= m; ++i) {
			int x = read(), y = read();
			add(x, y);
		}

		int Q = read();
		while (Q--) {
			ll x = read();
			printf("%d", solve(x));
			if (Q == 0) 
				putchar('\n');
			else 
				putchar(' ');
		}
	}
}

如果和我最开始一样傻逼没注意到u和v的范围怎么办呢?那么虽然还是单调的,可是初始的y坐标可以非常巨大。
那么我们分成三部分统计

  • 枚举x,线段树上二分确定y的位置来计算
  • 统计答案点如果大于所有的x和所有的y的情况的点数,注意到如果在n+1这个位置的答案的y坐标不是一个整数,那么在整个需要统计的区域都不会是整数(因为x每+1,y就需要-1,所以小数部分永远是相同的)
  • 最后枚举y,统计有多少个对应的答案点的x是大于n的、

这样时间复杂度就是O(nqlogn)了
代码如下(因为不加读入挂就是T,加了应该还是,常数有点大,所以不能知道正确性)


/* ***********************************************
Author        :BPM136
Created Time  :1/19/2019 1:56:52 PM
File Name     :B.cpp
************************************************ */

#include
#include
#include
#include
#include
#include
#include

using namespace std;

using ll = long long;

inline ll read() {
	ll d = 0, f = 1; 
	char s = getchar();
	while (s < '0' || s > '9') {
		if (s == '-')
			f = -1;
		s = getchar();
	}
	while (s >= '0' && s <= '9') {
		d = d * 10 + s - '0';
		s = getchar();
	}
	return d * f;
}

int const N = 1e5 + 5;

ll sum[N << 2], sumx[N << 2];
int pn[N << 2];
int p[N][2];
int n, m;

struct edge {
	int y, next;
}a[N];
int lasta[N], ne;

void pushup(int k) {
	pn[k] = pn[k << 1] + pn[k << 1 | 1];
	sum[k] = sum[k << 1] + sum[k << 1 | 1];
	sumx[k] = sumx[k << 1] + sumx[k << 1 | 1];
}

void SegAdd(int k, int l, int r, int x, int val) {
	if (l == r) {
		sum[k] += x;
		sumx[k] += val;
		++pn[k];
		return;
	}
	int mid = (l + r) / 2;
	if (x <= mid) 
		SegAdd(k << 1, l, mid, x, val);
	else
		SegAdd(k << 1 | 1, mid + 1, r, x, val);
	pushup(k);
}

ll sum_n, sum_x, sum_y;
ll SegEFin(int k, int l, int r, ll x, ll val) {
	if (l == r) {
		sum_n += pn[k];
		sum_y += sum[k];
		sum_x += sumx[k];
		if ((l + x) * sum_n - sum_x - sum_y == val)
			return l;
		else
			return 0;
	}
	int mid = (l + r) / 2;
	if ((mid + x) * (sum_n + pn[k << 1]) - (sum_x + sumx[k << 1]) - (sum_y + sum[k << 1]) >= val) 
		return SegEFin(k << 1, l, mid, x, val);
	else {
		sum_n += pn[k << 1];
		sum_x += sumx[k << 1];
		sum_y += sum[k << 1];
		return SegEFin(k << 1 | 1, mid + 1, r, x, val);
	}
}

ll SegEF(int x, ll val) {
	if (val < 0)
		return 0;
	sum_n = 0;
	sum_x = 0;
	sum_y = 0;
	return SegEFin(1, 1, n, x, val);
}

ll SegSum(int k, int l, int r, int _l, int _r) {
	if (l == _l && r == _r) 
		return sum[k];
	int mid = (l + r) / 2;
	if (_r <= mid) 
		return SegSum(k << 1, l, mid, _l, _r);
	if (_l > mid)
		return SegSum(k << 1 | 1, mid + 1, r, _l, _r);
	return SegSum(k << 1, l, mid, _l, mid) + SegSum(k << 1 | 1, mid + 1, r, mid + 1, _r);
}

ll q[20], Q, ans[20];

void solve() {
	for (int i = 0; i <= n * 4; ++i)
		pn[i] = sum[i] = sumx[i] = 0;
	memset(ans, 0, sizeof(ans));
	for (int i = 1; i <= n; ++i) {
		for (int j = lasta[i]; j != 0; j = a[j].next) {
			int x = a[j].y;
			SegAdd(1, 1, n, x, i);
		}
		for (int j = 1; j <= Q; ++j)
			ans[j] += SegEF(i, q[j]) > 0 ? 1 : 0;
	}
	ll _sumx = 0;
	for (int i = 1; i <= m; ++i) 
		_sumx += n + 1 - p[i][0];
	for (int i = 1; i <= Q; ++i) {
		ll posy = SegEF(n + 1, q[i] - _sumx);
		if (posy > n) 
			ans[i] += posy - n + 1;
	}
	for (int i = n - 1; i >= 1; --i) {
		for (int j = 1; j <= Q; ++j) {
			ll tmp = q[j] - (SegSum(1, 1, n, 1, i) + _sumx);
			ll td = tmp / n;
			if (tmp >= 0 && td * n == tmp)
				++ans[j];
		}
	}
}

void add(int x, int y) {
	a[++ne].y = y;
	a[ne].next = lasta[x]; 
	lasta[x] = ne;
}

int main() {
	ios::sync_with_stdio(0);
	int T = read();
	while (T--) {
		n = read(), m = read();
		for (int i = 0; i <= n; ++i)
			lasta[i] = 0;
		ne = 0;

		for (int i = 1; i <= m; ++i) {
			int x = read(), y = read();
			p[i][0] = x, p[i][1] = y;
			add(x, y);
		}

		Q = read();
		for (int i = 1; i <= Q; ++i)
			q[i] = read();
		solve();
		printf("%lld", ans[1]);
		for (int i = 2; i <= Q; ++i)
			printf(" %lld", ans[i]);
		putchar('\n');
	}
	return 0;
}

最后,那我们怎么去掉这个log呢?
注意到我们刚刚统计x和y大于所有点的时候,如果n+1不是整数那么全部都不是整数
同理,假如我们对应了一个x,那么到下一个x的时候,假如不是整数,那么同样可以直接扔掉
否则下降到下一个x或者y的这一段,都将是答案
当x超出了之后,再次枚举y,同理维护即可
这样时间复杂度就是O(nq)了

E

XD
我们枚举每一位,然后统计每一位的贡献就好了

#include 

using namespace std;

using ll = long long;

ll max(ll a, ll b) {
	if (a > b) 
		return a;
	else 
		return b;
}

int main() {
	ios::sync_with_stdio(0);
	// cerr << calc(1000000088888880ll, 10000001ll) << '\n';
	// return 0;
	int T;
	cin >> T;
	while (T--) {
		int k, m;
		cin >> k >> m;
		ll ans = 0, i;
		for (i = 1; i <= k; i *= 10) 
			ans += k / i - i + 1;
		--ans;
		if (ans >= m || (k - (i / 10) == 0 && ans < m - 1))
			cout << 0 << '\n';
		else {
			ll n = k;
			for (i = k - i / 10; ans < m - 1; n *= 10) {
				i *= 10;
				ans += i;
			}
			ll anss = n - ans + m - 2;
			cout << max(anss, k) << '\n';
		}
	}
	return 0;
}

I

你可能感兴趣的:(ACM补题,ZOJ月赛)