跳到正文

剪贴板

剪贴板 fm6c7wd4

原文
剪贴板信息

剪贴板信息

收录
更新

[NOIP 2022] T4 比赛 题解 代码。

C++
#include <bits/stdc++.h>
#define foru(i, l, r) for(int i=(l); i<=(r); i++)
#define ford(i, l, r) for(int i=(l); i>=(r); i--)
typedef long long ll;
typedef unsigned long long ull;
namespace io {
	inline char gc();
	template<class T> void read(T &x);
	template<class T> void write(T x);
	void reads(char *x);
	struct Fio {
		template<class T> inline Fio& operator>> (T &x) { read(x); return *this; }
		template<class T> inline Fio& operator<< (const T &x) { write(x); return *this; }
		inline Fio& operator>> (char &x) { do { x = gc(); } while (isspace(x)); return *this; }
		inline Fio& operator>> (char *x) { reads(x); return *this; }
		inline Fio& operator<< (const char x) { putchar(x); return *this; }
		inline Fio& operator<< (const char *x) { while (*x) putchar(*(x++)); return *this; }
		inline Fio& operator<< (char *x) { while (*x) putchar(*(x++)); return *this; }
	} fio;
}
using io::fio;
const int N=2.5e5;
int testId, n, q;
struct DataAB {
	int val, pre;
} arr[N+10], brr[N+10];

void getPre(DataAB data[]) {
	static int val[N+10], id[N+10];
	int cnt = 0;
	foru (i, 1, n) {
		while (cnt && val[cnt] < data[i].val) --cnt;
		data[i].pre = id[cnt];
		++cnt;
		val[cnt] = data[i].val, id[cnt] = i;
	}
}

struct Tag {
	ull a, b;
	ull c, ca, cb, cl;
	inline bool empty() const {
		return !(a || b || c || ca || cb || cl);
	}
	inline void clear() {
		a = b = 0;
		c = ca = cb = cl = 0;
	}
	inline void add(const Tag &t) {
		if (this->empty()) return (void) (*this = t);
		// matrix 1
		const ull a1s = a ? 0 : b, b1s = b ? 0 : a;
		const ull a1a = a ? 0 : 1, b1b = b ? 0 : 1;
		const ull l1s = a*b, l1a = a, l1b = b;
		const ull s1c = c, a1c = ca, b1c = cb, l1c = cl;
		// matrix 2
		const ull s2c = t.c, a2c = t.ca, b2c = t.cb, l2c = t.cl;
		// matrix result
		const ull sc =                                       s1c*1;
		const ull ac = a1s*s2c + a1a*a2c                   + a1c*1;
		const ull bc = b1s*s2c           + b1b*b2c         + b1c*1;
		const ull lc = l1s*s2c + l1a*a2c + l1b*b2c + 1*l2c + l1c*1;
		// assign
		c = sc, ca = ac, cb = bc, cl = lc;
		if (t.a) a = t.a;
		if (t.b) b = t.b;
	}
};
struct Node {
	ull s, sa, sb, c;
	Tag tag;
} tree[N*4+10];

inline void push_up(int p) {
	Node &self = tree[p], &ls = tree[p<<1], &rs = tree[p<<1|1];
	self.s = ls.s + rs.s;
	self.sa = ls.sa + rs.sa;
	self.sb = ls.sb + rs.sb;
	self.c = ls.c + rs.c;
}

inline void push_down(int p, int l, int r);

inline void set_val(int p, int l, int r, const Tag &tag) {
	Node &self = tree[p];
	const ull len = r - l + 1;
	const ull s = self.s, sa = self.sa, sb = self.sb;
	if (tag.a && tag.b) {
		self.s = tag.a * tag.b * len;
		self.sa = tag.a * len;
		self.sb = tag.b * len;
	} else if (tag.a) {
		self.s = tag.a * sb;
		self.sa = tag.a * len;
	} else if (tag.b) {
		self.s = tag.b * sa;
		self.sb = tag.b * len;
	}
	self.c += tag.c * s + tag.ca * sa + tag.cb * sb + tag.cl * len;
	self.tag.add(tag);
	// if (l < r) push_down(p, l, r);
	// tree[p].tag = tag;
}

inline void push_down(int p, int l, int r) {
	if (tree[p].tag.empty()) return;
	int mid = (l + r) >> 1;
	set_val(p<<1, l, mid, tree[p].tag);
	set_val(p<<1|1, mid+1, r, tree[p].tag);
	tree[p].tag.clear();
}

void update(int p, int l, int r, int x, int y, const Tag &tag) {
	if (x <= l && r <= y) return (void) set_val(p, l, r, tag);
	push_down(p, l, r);
	int mid = (l + r) >> 1;
	if (x <= mid) update(p<<1, l, mid, x, y, tag);
	if (y > mid) update(p<<1|1, mid+1, r, x, y, tag);
	push_up(p);
}

ull query(int p, int l, int r, int x, int y, int gt) {
	if (x <= l && r <= y) return tree[p].c + (gt + 1) * tree[p].s;
	push_down(p, l, r);
	int mid = (l + r) >> 1;
	if (y <= mid) return query(p<<1, l, mid, x, y, gt);
	if (x > mid) return query(p<<1|1, mid+1, r, x, y, gt);
	return query(p<<1, l, mid, x, y, gt) + query(p<<1|1, mid+1, r, x, y, gt);
}

void debug(int p, int l, int r, std::function<void(const Node &, int, int)> cb) {
	cb(tree[p], l, r);
	if (l == r) return;
	push_down(p, l, r);
	int mid = (l + r) >> 1;
	debug(p<<1, l, mid, cb);
	debug(p<<1|1, mid+1, r, cb);
}

void debug(int gt) {
	fprintf(stderr, "gt=%d\n", gt);
	fprintf(stderr, "tree: ");
	debug(1, 1, n, [&](const Node &self, int l, int r) {
		if (l != r) return;
		fprintf(stderr, "%llu:(c=%lld) ",
			self.c + (gt + 1) * self.s,
			(ll) self.c
		);
	});
	fprintf(stderr, "\n");
	debug(1, 1, n, [&](const Node &self, int l, int r) {
		fprintf(stderr, "  [%d-%d]: s=%llu, c=%lld\n",
			l, r,
			self.s, (ll) self.c
		);
	});
	fprintf(stderr, "\n");
}
/*
template<int sz>
struct Matrix {
	ll val[sz][sz];
	Matrix () { memset(val, 0, sizeof val); }
	Matrix (int x) {
		memset(val, 0, sizeof val);
		ford (i, sz-1, 0) val[i][i] = x;
	}
	Matrix operator* (const Matrix &t) const {
		Matrix ret;
		ford (i, sz-1, 0) {
			ford (k, sz-1, 0) {
				ford (j, sz-1, 0) {
					ret.val[i][j] += val[i][k] * t.val[k][j];
				}
			}
		}
		return ret;
	}
	template<class TIO>
	void println(TIO &io) const {
		foru (j, 0, sz-1) {
			foru (i, 0, sz-1) {
				io << val[i][j] << ' ';
			}
			io << '\n';
		}
	}
};

namespace Test {
	using std::cerr;
	Matrix<4> getMatrixA(int a) {
		Matrix<4> ret;
		ret.val[2][0] = a;
		ret.val[3][1] = a;
		ret.val[2][2] = 1;
		ret.val[3][3] = 1;
		return ret;
	}
	Matrix<4> getMatrixB(int b) {
		Matrix<4> ret;
		ret.val[1][0] = b;
		ret.val[3][2] = b;
		ret.val[1][1] = 1;
		ret.val[3][3] = 1;
		return ret;
	}

	int test() {
		Matrix<4> mt1 = getMatrixA(2);
		Matrix<4> mt2 = getMatrixB(3);
		mt1.println(cerr), cerr << std::endl;
		mt2.println(cerr), cerr << std::endl;
		(mt1 * mt2).println(cerr), cerr << std::endl;
		return 0;
	}

	int _ = test();
}
*/
int main() {
	using std::cerr;
	fio >> testId >> n;
	foru (i, 1, n) fio >> arr[i].val;
	foru (i, 1, n) fio >> brr[i].val;
	fio >> q;
	getPre(arr), getPre(brr);
	static struct Ques { int id, l, r; } ques[N+10];
	static ull ans[N+10];
	foru (i, 1, q) {
		ques[i].id = i;
		fio >> ques[i].l >> ques[i].r;
	}
	std::sort(ques+1, ques+1+q, [](const Ques &a, const Ques &b) {
		return a.r < b.r;
	});
	int cntR = 0;
	// debug(0);
	foru (i, 1, q) {
		while (cntR < ques[i].r) {
			++cntR;
			ull a = arr[cntR].val, b = brr[cntR].val, gt = cntR;
			update(1, 1, n, arr[cntR].pre + 1, cntR, Tag { a, 0, gt, 0, -gt*a, 0 });
			update(1, 1, n, brr[cntR].pre + 1, cntR, Tag { 0, b, gt, -gt*b, 0, 0 });
			// debug(gt);
		}
		ans[ques[i].id] = query(1, 1, n, ques[i].l, ques[i].r, cntR);
	}
	foru (i, 1, q) fio << ans[i] << '\n';

	return 0;
}

namespace io {
#ifdef LOCAL
	inline char gc() { return getchar(); }
#else
	inline char gc() {
		static const int SZ=1e6;
		static char buf[SZ+10], *p1=buf, *p2=buf;
		if (p1 == p2) {
			p1 = buf;
			p2 = buf + fread(buf, 1, SZ, stdin);
			if (p1 == p2) return EOF;
		}
		return *(p1++);
	}
#endif

	template<class T>
	void read(T &x) {
		x = 0; char c = gc(); bool fu = 0;
		while (c != EOF && !isdigit(c)) { if (c == '-') fu = 1; c = gc(); }
		while (isdigit(c)) x = x*10 + (c-'0'), c = gc();
		if (fu) x *= -1;
	}

	template<class T>
	void write(T x) {
	    static char opt[101];
	    char *p = opt; bool fu = x < 0;
	    if (fu) *(p++) = '0' - (x % 10);
	    else *(p++) = '0' + (x % 10);
	    x /= 10;
	    if (fu) x *= -1, putchar('-');
	    while (x) *(p++) = x % 10 + '0', x /= 10;
	    while (p != opt) putchar(*(--p));
	}

	void reads(char *x) {
		do { *x = gc(); } while (isspace(*x));
		do { *(++x) = gc(); } while (*x != EOF && !isspace(*x));
		*x = '\0';
	}
}