QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#168273#7108. CouleurbocchiRE 2ms3548kbC++205.5kb2023-09-08 04:59:262023-09-08 04:59:28

Judging History

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

  • [2023-09-08 04:59:28]
  • 评测
  • 测评结果:RE
  • 用时:2ms
  • 内存:3548kb
  • [2023-09-08 04:59:26]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
//#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vi vector<ll>
#define vii vector<pair<int,int>>
#define ti tuple<ll,ll,ll>
#define ff first 
#define ss second
#define rep(i,a,b) for(ll i=(a); i<(b); i++)
#define repd(i,a,b) for(ll i=(a)-1; i!=(b)-1; i--)
#define all(x) x.begin(),x.end()
#define sz(a) ((int) (a).size())
#define nl '\n'
#define cma <<','<<
//ll mod = 1e9+7;
ll inf = 1e18;
const ll maxn = 2050;
ll mod = 998244353;

struct wavelet_tree{
    int low, high;
    wavelet_tree *l, *r;
    vector<int> freq;
    wavelet_tree(int* from, int* to, int x, int y)
    {
        low = x, high = y;
        if (from >= to)
            return;
        if (high == low) {
            freq.reserve(to - from + 1);
            freq.push_back(0);
            for (auto it = from; it != to; it++)
                freq.push_back(freq.back() + 1);
            return;
        }
        int mid = (low + high) / 2;
        auto lessThanMid = [mid](int x) {
            return x <= mid;
        };
        freq.reserve(to - from + 1);
        freq.push_back(0);
        for (auto it = from; it != to; it++)
            freq.push_back(freq.back() + lessThanMid(*it));       
        auto pivot = stable_partition(from, to, lessThanMid);
        l = new wavelet_tree(from, pivot, low, mid);
        r = new wavelet_tree(pivot, to, mid + 1, high);
    }
 	//no. of elements in [l, r] less than or equal to k
    int kOrLess(int l, int r, int k)
    {
        if (l > r or k < low)
            return 0;
        if (high <= k)
            return r - l + 1;
        int LtCount = freq[l - 1];
        int RtCount = freq[r];
        return (this->l->kOrLess(LtCount + 1, RtCount, k) +
             this->r->kOrLess(l - LtCount, r - RtCount, k));
    }
    int kOrMore(int l, int r, int k){
    	return r - l + 1 - kOrLess(l, r, k - 1);
	}
};

ll ft[maxn];

void up(ll x, ll v) {
	for (; x < maxn; x += x & -x) ft[x] += v;
}

ll qry(ll x) {
	ll sum = 0;
	for (; x; x -= x & -x) sum += ft[x];
	return sum;
}

ll qry(ll x, ll y){
	return qry(y) - qry(x-1);
}

int len(int l, int r){
	return r - l + 1;
}

void print(vector<ll> v){
	for(auto i: v) cout << i << " ";
	cout << nl;
}

void solve(int tc){
	int n; cin >> n;
	rep(i,0,n + 5) ft[i] = 0;
	vector<int> a(n);
	vector<ll> p(n);
	int wt[n];
	rep(i,0,n) cin >> a[i];
	rep(i,0,n) cin >> p[i];
	rep(i,0,n) wt[i] = a[i];
	wavelet_tree T(wt, wt + n, 1, n);
//	cout << T.kOrLess(3, 5, 0) << nl;
	
	ll curans = 0;
	for(int i = n - 1; i >= 0; i--){
		curans += qry(a[i] - 1);
		up(a[i], 1);
	}
	cout << curans << " ";
	
	vector<ll> ans(n);
	ans[0] = curans;
	set<pii> s;
	s.insert({0, n - 1});
	multiset<ll, greater<ll>> vals;
	vals.insert(ans[0]);
	
	auto getans = [&](){ 
		if(vals.empty()) return 0LL;
		return *vals.begin();	
	};
	
	auto kOrLess = [&](int l, int r, int k){
		return T.kOrLess(l + 1, r + 1, k);	
	};
	auto kOrMore = [&](int l, int r, int k){
		return T.kOrMore(l + 1, r + 1, k);
	};
	
	auto calc = [&](int l, int r) -> ll{
		ll res = 0;
		for(int i = l; i < r; i++){
			res += kOrLess(i + 1, r, a[i] - 1);
		}
		return res;
	};
	auto prtset = [&](){
		for(auto [a, b] : s){
			cout << a cma b << " ";
		}	
		cout << nl;
	};
	
	rep(i, 0, n - 1){
		ll x = p[i];
		x ^= curans;
		x--;

		auto it = --s.upper_bound({x, n + 1});
		auto [l, r] = *it;
		
		s.erase(it);
		auto itt = vals.lower_bound(ans[l]);
		vals.erase(itt);
		if(l == r){
			cout << curans << (i < n - 2 ? " " : "");
			continue;
		}
		if(x == l){
			ans[l + 1] = ans[l] - kOrLess(l + 1, r, a[x] - 1);
			vals.insert(ans[l + 1]);
			s.insert({l + 1, r});
//			prtset();
			curans = getans();
			cout << curans << (i < n - 2 ? " " : "");
			continue;
		}else if(x == r){
			ans[l] = ans[l] - kOrMore(l, r - 1, a[x] + 1);
			vals.insert(ans[l]);
			
			s.insert({l, r - 1});
//			prtset();
			curans = getans();
			cout << curans << (i < n - 2 ? " " : "");
			continue;
		}
		
		int l1 = l, r1 = x - 1, l2 = x + 1, r2 = r;
		ll tmp = kOrMore(l1, r1, a[x] + 1) + kOrLess(l2, r2, a[x] - 1);
//		cout << nl << "this is tmp: " << tmp;
		ans[l2] = ans[l] - tmp;
		ans[l1] = ans[l] - tmp;

		ll tot = 0;
		if(len(l1, r1) < len(l2, r2)){
			ans[l1] = calc(l1, r1);
			for(int j = l1; j <= r1; j++){
				ans[l2] -= kOrLess(l2, r2, a[j] - 1);
			}
			ans[l2] -= ans[l1]; 
		}else{
			ans[l2] = calc(l2, r2);
//			cout << nl << "this is ans l2: " << ans[l2] cma l2 cma r2;
			for(int j = l2; j <= r2; j++){
				ans[l1] -= kOrMore(l1, r1, a[j] + 1); 
			}
			ans[l1] -= ans[l2];
		}
		
		vals.insert(ans[l1]); vals.insert(ans[l2]);
		s.insert({l1, r1}); s.insert({l2, r2});
		
//		cout << nl;
//		print(ans);
		
		curans = getans();
		cout << curans << (i < n - 2 ? " " : "");
	}
	if(tc > 0) cout << nl;
	
}

int main(){
//	ifstream cin("r.txt");
//	ofstream cout("w1.txt");
	
	ios_base::sync_with_stdio(0);
	cin.tie(0);

//    std::cin.rdbuf(cin.rdbuf()); //redirect std::cin to in.txt!
//    std::cout.rdbuf(cout.rdbuf()); //redirect std::cout to out.txt!
	
	int t = 1; cin >> t;
	while(t--){
		solve(t);	
	}
	
}
/*
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

1
10
1 7 5 4 2 8 8 9 10 2 
6 2 8 4 7 9 3 1 10 5 

*/


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3548kb

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: -100
Runtime Error

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: