QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#159132#7108. Couleurucup-team088#AC ✓2337ms64280kbC++1710.9kb2023-09-02 17:31:162023-09-02 17:31:17

Judging History

你现在查看的是最新测评结果

  • [2023-09-02 17:31:17]
  • 评测
  • 测评结果:AC
  • 用时:2337ms
  • 内存:64280kb
  • [2023-09-02 17:31:16]
  • 提交

answer

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
#include<chrono>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
//ll mod = 1;
constexpr ll mod = 998244353;
//constexpr ll mod = 1000000007;
const int mod17 = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;

#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;

using ld = double;
typedef pair<ld, ld> LDP;
const ld eps = 1e-10;
const ld pi = acosl(-1.0);

template<typename T>
void chmin(T& a, T b) {
	a = min(a, b);
}
template<typename T>
void chmax(T& a, T b) {
	a = max(a, b);
}
template<typename T>
vector<T> vmerge(vector<T>& a, vector<T>& b) {
	vector<T> res;
	int ida = 0, idb = 0;
	while (ida < a.size() || idb < b.size()) {
		if (idb == b.size()) {
			res.push_back(a[ida]); ida++;
		}
		else if (ida == a.size()) {
			res.push_back(b[idb]); idb++;
		}
		else {
			if (a[ida] < b[idb]) {
				res.push_back(a[ida]); ida++;
			}
			else {
				res.push_back(b[idb]); idb++;
			}
		}
	}
	return res;
}
template<typename T>
void cinarray(vector<T>& v) {
	rep(i, v.size())cin >> v[i];
}
template<typename T>
void coutarray(vector<T>& v) {
	rep(i, v.size()) {
		if (i > 0)cout << " "; cout << v[i];
	}
	cout << "\n";
}
ll mod_pow(ll x, ll n, ll m = mod) {
	if (n < 0) {
		ll res = mod_pow(x, -n, m);
		return mod_pow(res, m - 2, m);
	}
	if (abs(x) >= m)x %= m;
	if (x < 0)x += m;
	//if (x == 0)return 0;
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
//mod should be <2^31
struct modint {
	int n;
	modint() :n(0) { ; }
	modint(ll m) {
		if (m < 0 || mod <= m) {
			m %= mod; if (m < 0)m += mod;
		}
		n = m;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
bool operator<(modint a, modint b) { return a.n < b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= (int)mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += (int)mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }
modint operator/=(modint& a, modint b) { a = a / b; return a; }
const int max_n = 1 << 20;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}
modint combP(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
template<typename T>
void addv(vector<T>& v, int loc, T val) {
	if (loc >= v.size())v.resize(loc + 1, 0);
	v[loc] += val;
}
/*const int mn = 2000005;
bool isp[mn];
vector<int> ps;
void init() {
	fill(isp + 2, isp + mn, true);
	for (int i = 2; i < mn; i++) {
		if (!isp[i])continue;
		ps.push_back(i);
		for (int j = 2 * i; j < mn; j += i) {
			isp[j] = false;
		}
	}
}*/

//[,val)
template<typename T>
auto prev_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	if (res == st.begin())return st.end();
	res--; return res;
}

//[val,)
template<typename T>
auto next_itr(set<T>& st, T val) {
	auto res = st.lower_bound(val);
	return res;
}
using mP = pair<modint, modint>;
mP operator+(mP a, mP b) {
	return { a.first + b.first,a.second + b.second };
}
mP operator+=(mP& a, mP b) {
	a = a + b; return a;
}
mP operator-(mP a, mP b) {
	return { a.first - b.first,a.second - b.second };
}
mP operator-=(mP& a, mP b) {
	a = a - b; return a;
}
LP operator+(LP a, LP b) {
	return { a.first + b.first,a.second + b.second };
}
LP operator+=(LP& a, LP b) {
	a = a + b; return a;
}
LP operator-(LP a, LP b) {
	return { a.first - b.first,a.second - b.second };
}
LP operator-=(LP& a, LP b) {
	a = a - b; return a;
}

mt19937 mt(time(0));

const string drul = "DRUL";
string senw = "SENW";
//DRUL,or SENW
//int dx[4] = { 1,0,-1,0 };
//int dy[4] = { 0,1,0,-1 };

//------------------------------------

const int sz = 17;
struct wavelet_matrix {
private:
	vector<bool> b[sz];
	vector<int> cntone[sz];
	vector<int> nex[sz];

	vector<P> le[sz], ri[sz];

	vector<int> las;
public:
	void init(vector<int> v) {
		int n = v.size();

		per(i, sz) {
			vector<P> v0, v1;
			rep(j, n) {
				cntone[i][j + 1] = cntone[i][j];
				if (v[j] & (1 << i)) {
					b[i][j] = true;
					cntone[i][j + 1]++;
					v1.push_back({ v[j],j });
				}
				else {
					b[i][j] = false;
					v0.push_back({ v[j],j });
				}
			}

			P pre = { 0,0 };
			rep(j, n) {
				le[i][j] = pre;
				if (b[i][j])pre.second = j;
				else pre.first = j;
			}
			le[i][n] = pre;
			pre = { n,n };
			per(j, n) {
				if (b[i][j])pre.second = j;
				else pre.first = j;
				ri[i][j] = pre;
			}
			v.clear();
			rep(j, v0.size()) {
				nex[i][v0[j].second] = j; v.push_back(v0[j].first);
			}
			rep(j, v1.size()) {
				nex[i][v1[j].second] = j + v0.size();
				v.push_back(v1[j].first);
			}
		}
		las = v;
	}
	wavelet_matrix(vector<int> v) {
		int n = v.size();
		rep(i, sz) {
			b[i].resize(n);
			cntone[i].resize(n + 1);
			nex[i].resize(n);
			le[i].resize(n + 1);
			ri[i].resize(n);
		}

		per(i, sz) {
			vector<P> v0, v1;
			rep(j, n) {
				cntone[i][j + 1] = cntone[i][j];
				if (v[j] & (1 << i)) {
					b[i][j] = true;
					cntone[i][j + 1]++;
					v1.push_back({ v[j],j });
				}
				else {
					b[i][j] = false;
					v0.push_back({ v[j],j });
				}
			}

			P pre = { 0,0 };
			rep(j, n) {
				le[i][j] = pre;
				if (b[i][j])pre.second = j;
				else pre.first = j;
			}
			le[i][n] = pre;
			pre = { n,n };
			per(j, n) {
				if (b[i][j])pre.second = j;
				else pre.first = j;
				ri[i][j] = pre;
			}
			v.clear();
			rep(j, v0.size()) {
				nex[i][v0[j].second] = j; v.push_back(v0[j].first);
			}
			rep(j, v1.size()) {
				nex[i][v1[j].second] = j + v0.size();
				v.push_back(v1[j].first);
			}
		}
		las = v;
	}
	//[l,r)
	//0-indexed
	int take_kth(int k, int l, int r) {
		k++;
		per(i, sz) {
			int c = cntone[i][r] - cntone[i][l];
			if (r - l - c < k) {
				k -= r - l - c;
				l = nex[i][ri[i][l].second];
				r = nex[i][le[i][r].second] + 1;
			}
			else {
				l = nex[i][ri[i][l].first];
				r = nex[i][le[i][r].first] + 1;
			}
		}
		return las[l];
	}

	//number of >=k in [l,r)
	int number_upk(int k, int l, int r) {
		int res = 0;
		per(i, sz) {
			if (k & (1 << i)) {
				l = ri[i][l].second;
				r = le[i][r].second;
				if (l > r)break;
				l = nex[i][l];
				r = nex[i][r] + 1;
			}
			else {
				res += cntone[i][r] - cntone[i][l];
				l = ri[i][l].first;
				r = le[i][r].first;
				if (l > r)break;
				l = nex[i][l];
				r = nex[i][r] + 1;
			}
		}
		if (l < r)res += r - l;
		return res;
	}
	//number of <=k in [l,r)
	int number_dwk(int k, int l, int r) {
		int res = 0;
		per(i, sz) {
			if (k & (1 << i)) {
				res += r - l;
				res -= cntone[i][r] - cntone[i][l];
				l = ri[i][l].second;
				r = le[i][r].second;
				if (l > r)break;
				l = nex[i][l];
				r = nex[i][r] + 1;
			}
			else {
				l = ri[i][l].first;
				r = le[i][r].first;
				if (l > r)break;
				l = nex[i][l];
				r = nex[i][r] + 1;
			}
		}
		if (l < r)res += r - l;
		return res;
	}
};
template<typename T>
struct BIT {
private:
	vector<T> node; int n;
public:
	BIT(int n_) {
		n = n_; node.resize(n, 0);
	}
	//0-indexed
	void add(int a, T w) {
		for (int i = a; i < n; i |= i + 1)node[i] += w;
	}
	//[0,a)
	T sum(int a) {
		T ret = 0;
		for (int i = a - 1; i >= 0; i = (i & (i + 1)) - 1)ret += node[i];
		return ret;
	}
	//[a,b)
	T sum(int a, int b) {
		return sum(b) - sum(a);
	}
};

void solve() {
	int n; cin >> n;
	vector<int> a(n);
	rep(i, n) {
		cin >> a[i]; a[i]--;
	}
	BIT<int> bt(n);
	wavelet_matrix wm(a);
	vector<ll> memval(n+1);
	set<int> st;
	st.insert(-1);
	st.insert(n);
	multiset<ll> stval;
	ll ssum = 0;
	rep(i, n) {
		ssum += bt.sum(a[i]+1, n);
		bt.add(a[i], 1);
	}
	stval.insert(ssum);
	memval[0] = ssum;
	per(i, n)bt.add(a[i], -1);
	ll las = ssum;
	vector<ll> anss;
	rep(i, n) {
		anss.push_back(las);
		//cout << "?? " << las << "\n";
		//cout << las << "\n";
		ll z; cin >> z;
		int x = z ^ las;
		x--;
		//cout << "!! " << x << "\n";
		int le = *prev_itr(st, x);
		int ri = *next_itr(st, x);
		st.insert(x);
		le++;
		stval.erase(stval.find(memval[le]));
		ll cur = memval[le];
		//[le,x),[x+1,ri)
		if (x - le < ri - (x + 1)) {
			Rep(i, le, x+1) {
				if (a[i] > 0) {
					cur -= wm.number_dwk(a[i] - 1, i + 1, ri);
				}
			}
			ll nw = 0;
			Rep(i, le, x) {
				nw += bt.sum(a[i] + 1, n);
				bt.add(a[i], 1);
			}
			Rep(i, le, x) {
				bt.add(a[i], -1);
			}
			stval.insert(nw); stval.insert(cur);
			memval[le] = nw;
			memval[x + 1] = cur;
		}
		else {
			for (int i = ri - 1; i >= x; i--) {
				cur -= wm.number_upk(a[i] + 1, le, i);
			}
			ll nw = 0;
			Rep(i, x + 1, ri) {
				nw += bt.sum(a[i] + 1, n);
				bt.add(a[i], 1);
			}
			Rep(i, x + 1, ri) {
				bt.add(a[i], -1);
			}
			stval.insert(cur);
			stval.insert(nw);
			memval[le] = cur;
			memval[x+1] = nw;
		}
		las = *--stval.end();
	}
	coutarray(anss);
}




signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed<<setprecision(10);
	//init_f();
	//init();
	//init2();
	//while(true)
	//expr();
	int t; cin >> t; rep(i, t)
		solve();
	return 0;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 5ms
memory: 11920kb

input:

3
5
4 3 1 1 1
5 4 5 3 1
10
9 7 1 4 7 8 5 7 4 8
21 8 15 5 9 2 4 5 10 6
15
4 8 8 1 12 1 10 14 7 14 2 9 13 10 3
37 19 23 15 7 2 10 15 2 13 4 5 8 7 10

output:

7 0 0 0 0
20 11 7 2 0 0 0 0 0 0
42 31 21 14 14 4 1 1 1 0 0 0 0 0 0

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 2337ms
memory: 64280kb

input:

11116
10
10 5 10 3 6 4 8 5 9 8
31 27 24 11 12 3 0 2 3 1
10
8 2 7 2 8 10 1 10 9 10
6 5 2 13 2 1 0 1 3 1
10
7 10 7 6 1 3 10 6 7 9
21 18 10 1 6 5 4 8 9 10
10
2 10 4 8 8 5 7 2 6 7
20 10 9 1 15 0 4 2 9 7
10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
10
1 2 3 4 5 6 7 8 9 10
6 3 5 2 7 10 9 1 4 8
10
1 10 1 3...

output:

21 18 16 12 10 6 4 1 1 0
12 12 10 10 4 4 4 2 1 0
20 16 9 5 3 3 3 0 0 0
22 14 8 8 5 5 2 1 1 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
19 12 7 4 4 2 2 1 0 0
20 18 8 3 1 1 0 0 0 0
45 21 21 10 3 3 3 0 0 0
17 11 8 2 1 1 1 0 0 0
13 4 1 0 0 0 0 0 0 0
29 27 22 15 9 7 4 3 1 0
26 16 9 2 1 1 1 1 1 0
0 0 0 0 0 ...

result:

ok 11116 lines

Extra Test:

score: 0
Extra Test Passed