#include<bits/stdc++.h>
using namespace std;
int a,b,c,n,T;
int main () {
	scanf ("%d",&T);
	while (T--) {
		scanf ("%d %d %d %d",&a,&b,&c,&n);
		int k=max(a,max(b,c)),flag=0;
		if ((n-(k*3-a-b-c))%3==0)
		printf ("YES\n");
		else printf ("NO\n");
	}
	return 0;
}


#include <bits/stdc++.h>
#include<cmath>

using namespace std;

int main(){
	long long t, n, df;
	cin >> t;
	while(t--){
		long long arr[3];
        cin >> arr[0] >> arr[1] >> arr[2] >> n;
        sort(arr, arr + 3);
        df = arr[2] - arr[0];
        df += arr[2] - arr[1];

        if(n >= df) {
            n -= df;
            if(n % 3 == 0)
                cout << "YES" << endl;
            else
                cout << "NO" << endl;
        }
        else cout << "NO" << endl;
    }
	return 0;
}


#include<iostream>
#include<cmath>

using namespace std;

int main(){
	int a, b, c, n, t, d1, d2, ds, d3;
	cin >> t;
	while(t--){
		cin >> a >> b >> c >> n;
		//int largest = a > b ? (a > c? a : c) : (b > c ? b : c);
		if(a > b && a > c){
				d1 = a - b;
				d2 = a - c;
				ds = d1 + d2;
				d3 = n - ds;
				if(d3%3 == 0)
					cout << "YES" << endl;
				else
					cout << "NO" << endl;
		}
		else if(b > a && b > c){
					d1 = b - a;
					d2 = b - c;
					ds = d1 + d2;
					d3 = n - ds;
					if(d3%3 == 0)
						cout << "YES" << endl;
					else
						cout << "NO" << endl;
		}
		else if(c > a && c > b){
				d1 = c - a;
				d2 = c - b;
				ds = d1 + d2;
				d3 = n - ds;
				if(d3%3 == 0)
					cout << "YES" << endl;
				else
					cout << "NO" << endl;

		}
		else if ((a == b && a != c) || (b == a && b != c) || (c == a && c != b)){
			d3 = abs(a-b) + abs(b-c) + abs(c-a);
			if(d3%3 == 0)
				cout << "YES" << endl;
			else
				cout << "NO" << endl;
		}
		else if(a == b && b == c && c == a){
			if(n%3 == 0)
				cout << "YES" << endl;
			else
				cout << "NO" << endl;
		}
	}
	return 0;
}


#include<iostream>
#include<cmath>

using namespace std;

int main(){
	int a, b, c, n, t, d1, d2, ds, d3;
	cin >> t;
	while(t--){
		cin >> a >> b >> c >> n;
		//int largest = a > b ? (a > c? a : c) : (b > c ? b : c);
		if(a > b){
			if(a > c){
				d1 = a - b;
				d2 = a - c;
				ds = d1 + d2;
				d3 = n - ds;
				if(ds < n){
					cout << "YES" << endl;
				}
				else
					cout << "NO" << endl;
			}
		}
		if(b > a){
				if(b > c){
					d1 = b - a;
					d2 = b - c;
					ds = d1 + d2;
					d3 = n - ds;
					if(ds < n)
						cout << "YES" << endl;
					else
						cout << "NO" << endl;
				}
		}
		if(c > a){
			if(c > b){
				d1 = c - a;
				d2 = c - b;
				ds = d1 + d2;
				d3 = n - ds;
				if(ds < n)
					cout << "YES" << endl;
				else
					cout << "NO" << endl;
			}

		}
		if ((a == b && a != c) || (b == a && b != c) || (c == a && c != b)){
			ds = abs(a-b) + abs(b-c) + abs(c-a);
			if(ds < n)
				cout << "YES" << endl;
			else
				cout << "NO" << endl;
		}
		if(a == b && b == c && c == a){
			cout << "YES" << endl;
		}
	}
	return 0;
}


#include<iostream>
#include<cmath>

using namespace std;

int main(){
	int a, b, c, n, t, d1, d2, ds, d3;
	cin >> t;
	while(t--){
		cin >> a >> b >> c >> n;
		//int largest = a > b ? (a > c? a : c) : (b > c ? b : c);
		if(a > b && a > c){
				d1 = a - b;
				d2 = a - c;
				ds = d1 + d2;
				d3 = n - ds;
				if(d3%3 == 0)
					cout << "YES" << endl;
				else
					cout << "NO" << endl;
		}
		else if(b > a && b > c){
					d1 = b - a;
					d2 = b - c;
					ds = d1 + d2;
					d3 = n - ds;
					if(d3%3 == 0)
						cout << "YES" << endl;
					else
						cout << "NO" << endl;
		}
		else if(c > a && c > b){
				d1 = c - a;
				d2 = c - b;
				ds = d1 + d2;
				d3 = n - ds;
				if(d3%3 == 0)
					cout << "YES" << endl;
				else
					cout << "NO" << endl;

		}
		else if ((a == b && a != c) || (a != b && a == c)){
			ds = abs(a-b) + abs(b-c) + abs(c-a);
			d3 = n - ds;
			if(d3%3 == 0 || n % (ds/2) == 0)
				cout << "YES" << endl;
			else
				cout << "NO" << endl;
		}
		else if(a == b && b == c && c == a){
			if(n%3 == 0)
				cout << "YES" << endl;
			else
				cout << "NO" << endl;
		}
	}
	return 0;
}


#include<iostream>
#include<cmath>

using namespace std;

int main(){
	int a, b, c, n, t, d1, d2, ds, d3;
	cin >> t;
	while(t--){
		cin >> a >> b >> c >> n;
		//int largest = a > b ? (a > c? a : c) : (b > c ? b : c);
		if(a > b){
			if(a > c){
				d1 = a - b;
				d2 = a - c;
				ds = d1 + d2;
				d3 = n - ds;
				if(d3%3 == 0){
					cout << "YES" << endl;
				}
				else
					cout << "NO" << endl;
			}
		}
		if(b > a){
				if(b > c){
					d1 = b - a;
					d2 = b - c;
					ds = d1 + d2;
					d3 = n - ds;
					if(d3%3 == 0)
						cout << "YES" << endl;
					else
						cout << "NO" << endl;
				}
		}
		if(c > a){
			if(c > b){
				d1 = c - a;
				d2 = c - b;
				ds = d1 + d2;
				d3 = n - ds;
				if(d3%3 == 0)
					cout << "YES" << endl;
				else
					cout << "NO" << endl;
			}

		}
		if ((a == b && a != c) || (b == a && b != c) || (c == a && c != b)){
			d3 = abs(a-b) + abs(b-c) + abs(c-a);
			if(d3%3 == 0)
				cout << "YES" << endl;
			else
				cout << "NO" << endl;
		}
		if(a == b && b == c && c == a){
			cout << "YES" << endl;
		}
	}
	return 0;
}


#include<iostream>
#include<cmath>

using namespace std;

int main(){
	int a, b, c, n, t, d1, d2, ds, d3;
	cin >> t;
	while(t--){
		cin >> a >> b >> c >> n;
		//int largest = a > b ? (a > c? a : c) : (b > c ? b : c);
		if(a > b){
			if(a > c){
				d1 = a - b;
				d2 = a - c;
				ds = d1 + d2;
				d3 = n - ds;
				if(d3%3 == 0){
					cout << "YES" << endl;
				}
				else
					cout << "NO" << endl;
			}
		}
		if(b > a){
				if(b > c){
					d1 = b - a;
					d2 = b - c;
					ds = d1 + d2;
					d3 = n - ds;
					if(d3%3 == 0)
						cout << "YES" << endl;
					else
						cout << "NO" << endl;
				}
		}
		if(c > a){
			if(c > b){
				d1 = c - a;
				d2 = c - b;
				ds = d1 + d2;
				d3 = n - ds;
				if(d3%3 == 0)
					cout << "YES" << endl;
				else
					cout << "NO" << endl;
			}

		}
		if (a == b && b == c && a == c){
			d3 = abs(a-b) + abs(b-c) + abs(c-a);
			if(d3%3 == 0)
				cout << "YES" << endl;
			else
				cout << "NO" << endl;
		}
	}
	return 0;
}


#include<iostream>
#include<cmath>

using namespace std;

int main(){
	int a, b, c, n, t, d1, d2, ds, d3;
	cin >> t;
	while(t--){
		cin >> a >> b >> c >> n;
		//int largest = a > b ? (a > c? a : c) : (b > c ? b : c);
		if(a > b && a > c){
				d1 = a - b;
				d2 = a - c;
				ds = d1 + d2;
				d3 = n - ds;
				if(d3%3 == 0)
					cout << "YES" << endl;
				else
					cout << "NO" << endl;
		}
		else if(b > a && b > c){
					d1 = b - a;
					d2 = b - c;
					ds = d1 + d2;
					d3 = n - ds;
					if(d3%3 == 0)
						cout << "YES" << endl;
					else
						cout << "NO" << endl;
		}
		else if(c > a && c > b){
				d1 = c - a;
				d2 = c - b;
				ds = d1 + d2;
				d3 = n - ds;
				if(d3%3 == 0)
					cout << "YES" << endl;
				else
					cout << "NO" << endl;

		}
		else if ((a == b && a != c) || (b == a && b != c) || (c == a && c != b)){
			ds = abs(a-b) + abs(b-c) + abs(c-a);
			d3 = n - ds;
			if(d3%3 == 0)
				cout << "YES" << endl;
			else
				cout << "NO" << endl;
		}
		else if(a == b && b == c && c == a){
			if(n%3 == 0)
				cout << "YES" << endl;
			else
				cout << "NO" << endl;
		}
	}
	return 0;
}


#include<iostream>
#include<cmath>

using namespace std;

int main(){
	int a, b, c, n, t, d1, d2, ds, d3;
	cin >> t;
	while(t--){
		cin >> a >> b >> c >> n;
		//int largest = a > b ? (a > c? a : c) : (b > c ? b : c);
		if(a > b && a > c){
				d1 = a - b;
				d2 = a - c;
				ds = d1 + d2;
				d3 = n - ds;
				if(d3%3 == 0){
					cout << "YES" << endl;
				}
				else
					cout << "NO" << endl;
		}
		else if(b > a && b > c){
					d1 = b - a;
					d2 = b - c;
					ds = d1 + d2;
					d3 = n - ds;
					if(d3%3 == 0)
						cout << "YES" << endl;
					else
						cout << "NO" << endl;
		}
		else if(c > a && c > b){
				d1 = c - a;
				d2 = c - b;
				ds = d1 + d2;
				d3 = n - ds;
				if(d3%3 == 0)
					cout << "YES" << endl;
				else
					cout << "NO" << endl;

		}
		else if ((a == b && a != c) || (b == a && b != c) || (c == a && c != b)){
			d3 = abs(a-b) + abs(b-c) + abs(c-a);
			if(d3%3 == 0)
				cout << "YES" << endl;
			else
				cout << "NO" << endl;
		}
		else if(a == b && b == c && c == a){
			cout << "YES" << endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define yyb unsigned long long
bool A[100000000];
int qaq(yyb a,yyb b,yyb c,yyb n,yyb i)
{
	yyb coin;
	coin=a+b+c+n;
	if(coin%3!=0)
	{
		A[i]=false;
		return 0;
	}
	else
	{
		A[i]=true;
		return 0;
	}
}
int main()
{
	yyb x,a,b,c,n;
	cin>>x;
	for(int i=0;i<x;i++)
	{
		cin>>a>>b>>c>>n;
		qaq(a,b,c,n,i);
	}
	for(int i=0;i<x;i++)
	{
		if(A[i]) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,i,x,y,z,u,v=0;
int main(){
 	cin>>n;
 	for(i=1;i<=n;i++){
 		cin>>x>>y>>z>>u;
 		v=x+y+z+u;
 		if(v%3==0&&v%3>=x&&v%3>=y&&v%3>=z) cout<<"YES";
 		else cout<<"NO";
	 }
   	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int t,n,a,b,c;
int main()
{
    cin>>t;
    for(int i=1;i<=t;i++)
    {
        cin>>n>>a>>b>>c;
        if((a+b+c+n)%3==0)
        {
            int d=(a+b+c+n)/3;
            if(a<=d&&b<=d&&c<=d) cout<<"YES\n";
            else cout<<"NO\n";
        }
        else cout<<"NO\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	cin>>t;
	string a,b,c,n;
	for(int i=0;i<t;i++){
		cin>>a[i]>>b[i]>>c[i]>>n[i];
	}
	for(int i=0;i<t;i++){
		if((a[i]+b[i]+c[i]+n[i])%3==0){
			int d=(a[i]+b[i]+c[i]+n[i])/3;
			if(((d-a[i])+(d-b[i])+(d-c[i]))==n[i]){
				cout<<"YES"<<endl;
				continue;
			}else{
				cout<<"NO"<<endl;
				continue;
			}
		}else{cout<<"NO"<<endl;
		continue;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int t,n,a,b,c;
int main(){
	scanf("%d",&t);
	while(t--){
		scanf("%d%d%d%d",&a,&b,&c,&n);
		int k=(a+b+c+n)/3;
		if((a+b+c+n)%3==0&&a<=k&&b<=k&&c<=k){printf("YES\n");continue;
		}
		printf("NO\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;
	cin>>t;
	while(t--){
		int a,b,c,n;
		cin>>a>>b>>c>>n;
		int x=a+b+c+n,sum=1;
		if(x%3!=0) sum=0;
		if(a>x/3||b>x/3||c>x/3) sum=0;
		if(sum==0) cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
	}
   	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,a,b,c,d;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a>>b>>c>>d;
		if((a+b+c+d)%3==0){
			cout<<"YES"<<endl;
		}else{
			cout<<"NO"<<endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int num[10000][4],a,b,c,d,dum,sum;
int main()
{
	int t,n;
	cin>>t;
	for(int i=0;i<t;i++)cin>>num[i][0]>>num[i][1]>>num[i][2]>>num[i][3];
	for(int i=0;i<t;i++)
	{
		a=num[i][0];
		b=num[i][1];
		c=num[i][2];
		n=num[i][3];
		dum=(a+b+c+n)/3;
		sum=a+b+c+n;
		if(sum!=dum*3)cout<<"No"<<endl;
		else cout<<"Yes"<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define f(i,a,b,c) for(int i=a;i<=b;i+=c)
using namespace std;
typedef long long ll;
const int INF=1e9+7;
const int N=1e+5;
const int M=1e+5;
int p,a,b,c,t,i,m1,m2;
int main(){
	cin>>p;
	for(i=1;i<=p;i++){
		cin>>a>>b>>c>>t;
		m1=max(a,b);
		m2=max(m1,c);
		if((a+b+c+t)/3*3==a+b+c+t&&a+b+c+t/3>=m2)
		cout<<"YES";
		else cout<<"NO";
	}
	return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int a,b,c,n;
		int flag=1;
		scanf("%d%d%d%d",&a,&b,&c,&n);
		int sum=a+b+c+n;
		if(sum%3==0)
		{
			int num=sum/3;
			if(num<a||num<b||num<c) flag=0;
		}
		else flag=0;

		if(flag==0) printf("NO\n");
		else printf("YES\n");
	}
}


#include<bits/stdc++.h>
#define int long long
using namespace std;

int T;

signed main()
{
	scanf("%lld",&T);
	while(T--)
	{
		int a;
		scanf("%lld",&a);
		int sum=0,q=a;
		bool flag=0;
		for (int i=2;i<=sqrt(a);i++)
		{
			if (q%i==0)
			{
				q/=i,sum++;
			}
			if (sum==2&&q!=1)
			{
				if (q<=i)
					break;
				else
					{flag=1;break;}
			}
		}
		if (!flag)
			printf("NO\n");
		else
		{
			printf("YES\n");q=a;sum=0;
			for (int i=2;i<=sqrt(q);i++)
			{
				if (q%i==0)
				{
					q/=i,sum++;
					printf("%d ",i);
					if (sum==2)
					{
						printf("%d\n",q);
						break;
					}
				}
				if (sum==2)
					break;
			}
//			cout<<a<<endl;
		}
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#define int long long
using namespace std;
inline int read() {
//dfe
	int x = 0; char ch = getchar();
	while (!isdigit(ch)) {
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = (x << 1 ) + (x <<  3) + (ch & 15),
		ch = getchar();
	}
	return x;
}
signed main() {
	int t; cin >> t;
	while(t--) {
		int a=read(), b=read(), c=read(), d=read();
		int a1 = (3000000000 - a) % 3, b1 = (3000000000 - b )%3, c1 = (300000000 - c )% 3;
		if ((a1 + b1 + c1) % 3 == d % 3 && (d >= (3*max(max(a, b), c) - a - b - c))) {
			cout << "YES" << endl;
		} else  cout << "NO" << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
#define f(i,a,b,c) for(int i=a;i<=b;i+=c)
using namespace std;
typedef long long ll;
const int INF=1e9+7;
const int N=1e+5;
const int M=1e+5;
int p,a,b,c,t,i,m1,m2;
int main(){
	cin>>p;
	for(i=1;i<=p;i++){
		cin>>a>>b>>c>>t;
		m1=max(a,b);
		m2=max(m1,c);
		if((a+b+c+t)/3*3==a+b+c+t&&(a+b+c+t)/3>=m2)
		cout<<"YES";
		else cout<<"NO";
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,a,b,c,d,v;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a>>b>>c>>d;
        v=a+b+c+d;
		if(v%3!=0||v/3<a||v/3<c||v/3<d){
			cout<<"NO"<<endl;
		}else{
			cout<<"YES"<<endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll T,n,w1,w2,w3,v;
int main()
{
    cin>>T;
    for (int i=1;i<=T;i++){
        cin>>w1>>w2>>w3>>n;
        v=max(max(w1,w2),w3)*3-w1-w2-w3;
        if ((n-v)%3==0&&n>v) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}


//#define fIO
#include<stdio.h>
#include<algorithm>
#include<string.h>
#pragma warning (disable:4996)
#pragma warning (disable:6031)
int main() {
#ifdef fIO
	freopen("A. Collecting Coins.in", "r", stdin);
	freopen("A. Collecting Coins.out", "w", stdout);
#endif
	int c; scanf("%d", &c);
	while (c--) {
		int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d);
		int max = -1;
		if (a > max)max = a;
		if (b > max)max = b;
		if (c > max)max = c;
		d -= (max - a) + (max - b) + (max - c);
		if (d >= 0 && d % 3 == 0)printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
const int N=200002;
int T,n,a[N],i,p;
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d%d",a+1,a+2,a+3,&n);
        sort(a+1,a+4);
        n-=(a[3]-a[1])-(a[3]-a[2]);
        if(n>=0&&n%3==0)
            printf("YES\n");
        else
            printf("NO\n");
    }
}


#include<bits/stdc++.h>
using namespace std;
#define fo(i,a,b) for(register int (i)=(a);(i)<=(b);++(i))
#define rd read()
#define maxn 200001
#define gc getchar()
#define mem(a,b) memset(a,b,sizeof(a))
#define mp make_pair
#define ll long long
#define ull unsigned ll
#define pb push_back
#define db double
#define pu puts(" ")
const int p=998244353;
int read() {int x=0,f=1;char ch=gc;while(!isdigit(ch)) {if(ch=='-')f=-1;ch=gc;}while(isdigit(ch))x=x*10+ch-48,ch=gc;return x*f;}
void print(int x) {if(x>9)print(x/10);putchar(x%10+48);}
int qp(int a,int b){int ans=1;while(b){if(b&1)a=a*a%p;ans=ans*a%p;b>>=1;}return ans;}
int main() {
	int t=rd;
	while(t--)
	{
	   int a=rd,b=rd,c=rd,n=rd;
	   int ans=a+b+c+n;
	   if(ans%3!=0)
	   {
	   	puts("NO");continue;
	   }
	   ans/=3;
	   int ok=1;if(a>ans||b>ans||c>ans)ok=0;
	   puts(ok?"YES":"NO");
	}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int m,n;
int main()
{
	cin>>m;
	while(m--)
	{
		int temp;
		priority_queue<int,vector<int>,greater<int> > q;
		for(int i=1;i<=3;i++)
		{
			cin>>temp;
			q.push(temp);
		}
		cin>>n;
		while(n--)
		{
			temp = q.top();
			temp++;
			q.pop();
			q.push(temp);
		}
		temp = q.top();
		q.pop();
		int a = q.top();
		q.pop();
		if(temp == a && a == q.top()) cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int t,n,a,b,c;
int main(){
	scanf("%d",&t);
	while(t--){
		scanf("%d%d%d%d",&a,&b,&c,&n);
		if(a==b&&b==c){printf("YES\n");continue;
		}
		if((a+b+c+n)%3==0){printf("YES\n");continue;
		}
		printf("NO\n");
	}
}


#include<bits/stdc++.h>
using namespace std;
int num[10000][4],a,b,c,n,maxn;
int main()
{
	int t;
	cin>>t;
	for(int i=0;i<t;i++)cin>>num[i][0]>>num[i][1]>>num[i][2]>>num[i][3];
	for(int i=0;i<t;i++)
	{
		a=num[i][0];
		b=num[i][1];
		c=num[i][2];
		n=num[i][3];
		maxn=max(a,b);
		maxn=max(maxn,c);
		if(maxn-a+maxn-b+maxn-c>n)
		{
			cout<<"NO"<<endl;
			continue;
		}
		else n=n-(maxn-a+maxn-b+maxn-c);
		if(n%3!=0)cout<<"NO"<<endl;
		else cout<<"YES"<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout);
#define Rep(i, s, e) for (int i = s; i <= e; i ++)
#define Dep(i, s, e) for (int i = s; i >= e; i --)

typedef long long LL;
typedef double db;

int read()
{
	int x = 0, f = 1; char ch = getchar();
	while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
	while (isdigit(ch)) {x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar();}
	return x * f;
}

int T, a, b, c, n;

int main()
{
    T = read();
    while (T --)
    {
    	a = read(), b = read(), c = read(), n = read();
    	int sum = (a + b + c + n);
    	if (sum % 3 != 0) {printf("NO\n"); continue;}
    	sum /= 3;
    	if (a > sum || b > sum || c > sum) {printf("NO\n"); continue;}
    	printf("YES\n");
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

inline int read() {
    int cnt = 0, opt = 1;
    char ch = getchar();

    for (; ! isalnum(ch); ch = getchar())
        if (ch == '-')  opt = 0;
    for (; isalnum(ch); ch = getchar())
        cnt = cnt * 10 + ch - 48;

    return opt ? cnt : -cnt;
}

int T;

int main() {
    T = read();

    while (T --) {
        int a, b, c, n;
        a = read(), b = read(), c = read(), n = read();
        int sum = a + b + c + n;
        if (sum % 3 != 0) {
            puts("NO");
            continue;
        }
        sum /= 3;
        if (a > sum || b > sum || c > sum)
            puts("NO");
        else puts("YES");

    }
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
	int a,b,c,n;
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int mx=0;
		scanf("%d%d%d%d",&a,&b,&c,&n);
		mx=max(max(a,b),c);
		if((n-(mx-a)-(mx-b)-(mx-c))%3==0&&n>=(mx-a)+(mx-b)+(mx-c))
		{
			printf("YES\n");
		}
		else printf("NO\n");
	}
	return 0;
}


#include<iostream>
using namespace std;
int main(){
 	int t;
 	cin>>t;
 	while(t--){
 		int a,b,c,all,tmp,tag,flag=1;
 		cin>>a>>b>>c>>all;
 		tmp=a+b+c+all;
 		if(tmp%3!=0){
 			flag=0;
 			cout<<"NO"<<endl;
		}
		else{
			tag=tmp/3;
			if(tag>=a && tag >= b && tag>= c)
			cout<<"YES"<<endl;
			else cout<<"NO"<<endl;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long a,b,c,d,n;

int main(){
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a>>b>>c>>d;
		if((a+b+c+d)%3==0){
			if((a+b+c+d)/3>=a){
				if((a+b+c+d)/3>=b){
					if((a+b+c+d)/3>=c){
						d-=(a+b+c+d)/3-a;
						d-=	(a+b+c+d)/3-b;
						d-=(a+b+c+d)/3-c;
						if(d>=0)printf("YES\n");
					}
				}
			}
		}else printf("NO\n");
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int t,n,a,b,c;
int main(){
    cin>>t;
    for(int i=1;i<=t;i++){
        cin>>a>>b>>c>>n;
        int d=(a+b+c+n)/3;
        if(((a+b+c+n)%3==0)&&a<=d&&b<=d&&c<=d)cout<<"YES\n";
        else cout<<"NO\n";
    }
}


#include<cstdio>
#include<iostream>
using namespace std;
int a,b,c,n,t,lu;
int main()
{
	cin>>t;
	for(int i=1;i<=t;i++)
	{
		cin>>a>>b>>c>>n;
		lu=max(a,max(b,c));
		n-=lu-a+lu-b+lu-c;
		if((n%3)==0) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
long long int a,b,c,n,t;
cin>>t;
for(int i=0;i<t;i++){
    cin>>a>>b>>c>>n;
   int kk[3];
   kk[0]=a;
   kk[1]=b;
   kk[2]=c;
   sort(kk,kk+3);
   n=n-((kk[2]-kk[1])+(kk[2]-kk[0]));
   //if(i==0) cout<<"\n";
   if(n%3==0){
    cout<<"YES"<<"\n";
   }
   else{
    cout<<"NO"<<"\n";
   }
}

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
long long int a,b,c,n,t;
cin>>t;
for(int i=0;i<t;i++){
    cin>>a>>b>>c>>n;
   int kk[3];
   kk[0]=a;
   kk[1]=b;
   kk[2]=c;
   sort(kk,kk+3);
   n=n-((kk[2]-kk[1])+(kk[2]-kk[0]));
   //if(i==0) cout<<"\n";
   if(n%3==0){
    cout<<"YES"<<"\n";
   }
   else{
    cout<<"NO"<<"\n";
   }
}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
long long int a,b,c,n,t;
cin>>t;
for(int i=0;i<t;i++){
    cin>>a>>b>>c>>n;
    if(a!=b&&a!=c){
   int kk[3];
   kk[0]=a;
   kk[1]=b;
   kk[2]=c;
   sort(kk,kk+3);
   n=n-((kk[2]-kk[1])+(kk[2]-kk[0]));
   //if(i==0) cout<<"\n";
   if(n%3==0){
    cout<<"YES"<<"\n";
   }
   else{
    cout<<"NO"<<"\n";
   }
}
}
else{
    cout<<"YES"<<"\n";
}

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
long long int a,b,c,n,t;
cin>>t;
for(int i=0;i<t;i++){
    cin>>a>>b>>c>>n;
    if(a!=b&&a!=c){
   int kk[3];
   kk[0]=a;
   kk[1]=b;
   kk[2]=c;
   sort(kk,kk+3);
   n=n-((kk[2]-kk[1])+(kk[2]-kk[0]));
   //if(i==0) cout<<"\n";
   if(n%3==0){
    cout<<"YES"<<"\n";
   }
   else{
    cout<<"NO"<<"\n";
   }
}
else{
    cout<<"YES"<<"\n";
}
}

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
long long int a,b,c,n,t;
cin>>t;
for(int i=0;i<t;i++){
    cin>>a>>b>>c>>n;
   int kk[3];
   kk[0]=a;
   kk[1]=b;
   kk[2]=c;
   sort(kk,kk+3);
   n=n-(kk[2]-kk[1])-(kk[2]-kk[0]);
   //if(i==0) cout<<"\n";
   if(n%3==0){
    cout<<"YES"<<"\n";
   }
   else{
    cout<<"NO"<<"\n";
   }
}

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
long long int a,b,c,n,t;
cin>>t;
for(int i=0;i<t;i++){
    cin>>a>>b>>c>>n;
   int kk[3];
   kk[0]=a;
   kk[1]=b;
   kk[2]=c;
   sort(kk,kk+3);
   n=n-(kk[2]-kk[1])-(kk[2]-kk[0]);
   if(i==0) cout<<"\n";
   if(n%3==0){
    cout<<"YES"<<"\n";
   }
   else{
    cout<<"NO"<<"\n";
   }
}

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
long long int a,b,c,n,t;
cin>>t;
for(int i=0;i<t;i++){
    cin>>a>>b>>c>>n;
    if(a!=b&&a!c){
   int kk[3];
   kk[0]=a;
   kk[1]=b;
   kk[2]=c;
   sort(kk,kk+3);
   n=n-((kk[2]-kk[1])+(kk[2]-kk[0]));
   //if(i==0) cout<<"\n";
   if(n%3==0){
    cout<<"YES"<<"\n";
   }
   else{
    cout<<"NO"<<"\n";
   }
}
}
else{
    cout<<"YES"<<"\n";
}

}


#include<bits/stdc++.h>
using namespace std;
int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
long long int a,b,c,n,t;
cin>>t;
for(int i=0;i<t;i++){
    cin>>a>>b>>c>>n;
   int kk[3];
   kk[0]=a;
   kk[1]=b;
   kk[2]=c;
   sort(kk,kk+3);
   n=n-((kk[2]-kk[1])+(kk[2]-kk[0]));
   //if(i==0) cout<<"\n";
   if(n%3==0&&n>=0){
    cout<<"YES"<<"\n";
   }
   else{
    cout<<"NO"<<"\n";
   }
}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
long long int a,b,c,n,t;
cin>>t;
for(int i=0;i<t;i++){
    cin>>a>>b>>c>>n;
    if(a!=b||a!=c){
   int kk[3];
   kk[0]=a;
   kk[1]=b;
   kk[2]=c;
   sort(kk,kk+3);
   n=n-((kk[2]-kk[1])+(kk[2]-kk[0]));
   //if(i==0) cout<<"\n";
   if(n%3==0){
    cout<<"YES"<<"\n";
   }
   else{
    cout<<"NO"<<"\n";
   }
}
else{
    cout<<"YES"<<"\n";
}
}

}


#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<unordered_map>
using namespace std;

int main()
{
      int t;
      cin>>t;
    while(t--)
    {
      long double a,b,c;
      long double n;
      cin>>a>>b>>c>>n;
      long double k=(a+b+c+n)/3;
      if(abs(k-int(k))==0&&k>=a&&k>=b&k>=c)
        cout<<"YES"<<endl;
      else
        cout<<"NO"<<endl;
    }
      return 0;
}


#include<iostream>
#include<typeinfo>
#include<cmath>
using namespace std;
int main()
{
    long double t;
    cin>>t;
    while(t--)
    {
         long double a,b,c,n;
        cin>>a>>b>>c>>n;
        if(a==b&&b==c)
            cout<<"YES"<<endl;
        else
        {
            long double k=double(double(a+b+c+n)/3);
            long long int y=k*10;
            if(floor(k)-k!=0)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<typeinfo>
#include<cmath>
using namespace std;
int main()
{
    long double t;
    cin>>t;
    while(t--)
    {
         long double a,b,c,n;
        cin>>a>>b>>c>>n;
        if(a==b&&b==c)
            cout<<"YES"<<endl;
        else
        {
            long double k=double(double(a+b+c+n)/(3));
            long long int y=k*10;
            if(floor(k)-k!=0)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<typeinfo>
using namespace std;
int main()
{
    long t;
    cin>>t;
    while(t--)
    {
        long a,b,c,n;
        cin>>a>>b>>c>>n;
        if(a==b&&b==c)
            cout<<"YES"<<endl;
        else
        {
            long k=(a+b+c+n)/3;
            if((k-a+k-b+k-c)==n)
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<typeinfo>
#include<cmath>
using namespace std;
int main()
{
    long double t;
    cin>>t;
    while(t--)
    {
         long a,b,c,n;
        cin>>a>>b>>c>>n;
        if((a+b+c+n)%3!=0)
         cout<<"NO"<<endl;
        else
        {
            float k=(a+b+c+n)/3;
            if(k>=a&&k>=b&&k>=c)
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<typeinfo>
#include<cmath>
using namespace std;
int main()
{
    long double t;
    cin>>t;
    while(t--)
    {
         long double a,b,c,n;
        cin>>a>>b>>c>>n;
        if(a==b&&b==c)
            cout<<"YES"<<endl;
        else
        {
            long double k=(a+b+c+n)/3;
            long long int y=k*10;
            if(floor(k)-k!=0)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<typeinfo>
#include<cmath>
using namespace std;
int main()
{
    long double t;
    cin>>t;
    while(t--)
    {
         long double a,b,c,n;
        cin>>a>>b>>c>>n;

        {
            long double k=double(double(a+b+c+n)/3);
            //long long int y=k*10;
            if((k-a+k-b+k-c)==n)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
#include<unordered_map>
using namespace std;

int main()
{
      int t;
      cin>>t;
    while(t--)
    {
      long double a,b,c;
      long double n;
      cin>>a>>b>>c>>n;
      long double k=(a+b+c+n)/3;
      if(abs(k-int(k))==0)
        cout<<"YES"<<endl;
      else
        cout<<"NO"<<endl;
    }
      return 0;
}


#include<iostream>
#include<typeinfo>
#include<cmath>
using namespace std;
int main()
{
    long double t;
    cin>>t;
    while(t--)
    {
         long double a,b,c,n;
        cin>>a>>b>>c>>n;

        {
            long double k=double(double(a+b+c+n)/3);
            long long int y=k*10;
            if(floor(k)-k!=0)
                cout<<"NO"<<endl;
            else
                cout<<"YES"<<endl;
        }
    }
    return 0;
}


#include <cstdio>
#include <iostream>
using namespace std;

int main()
{
	int n,sum,num;
	cin>>n;
	int a,b,c,d;
	while(n>0){
		cin>>a>>b>>c>>d;
		if(a==b==c)
			cout<<"YES"<<endl;
		else if((a+b+c+d)%3==0){
			sum=(a+b+c+d)/3;
			num=(sum-a)+(sum-b)+(sum-c);
			if(num==d)
				cout<<"YES"<<endl;
			else
				cout<<"NO"<<endl;
		}else
			cout<<"NO"<<endl;
		n--;
	}
	return 0;
}


#include <bits/stdc++.h>

#define NN 1111111

using namespace std;

int a[NN];

int main(){

	int i, k;

	int N, u;

	cin>>N;

	for(i=0; i<N; i++){

		cin>>u;

		a[u]=i;

	}

	for(i=0; i<N; i++){

		cin>>u;

		if(i) cout<<" ";

		cout<<a[u];

	}

	puts("");

}


#include<bits/stdc++.h>

#define x first

#define y second

#define pb push_back

#define inf 0x3f3f3f3f

#define mod 1000000007

#define bit(x, y) (((x)>>(y))&1)

#define bctz(x) (__builtin_ctz(x))

#define bclz(x) (__builtin_clz(x))

#define bclzl(x) (__builtin_clzl(x))

#define bctzl(x) (__builtin_ctzl(x))

#define bpt(x) (__builtin_popcount(x))

#define bptl(x) (__builtin_popcountll(x))

#define PQ priority_queue<pii, vector<pii>, greater<pii> >

using namespace std;

typedef double DO;

typedef long long INT;

typedef vector<int> VI;

typedef pair<int, int> pii;

typedef pair<pii, int> pi3;

template<typename T, typename U> inline void smin(T &a, U b) {if (a>b) a=b;}

template<typename T, typename U> inline void smax(T &a, U b) {if (a<b) a=b;}

int power(int a, int b, int m, int ans=1) {

	for (; b; b>>=1, a=1LL*a*a%m) if (b&1) ans=1LL*ans*a%m;

	return ans;

}

#define NN 200010

int a[NN], b[NN];

int B[NN];

void update(int u, int x=1) {

	for(; u<NN; u+=u&-u) B[u]+=x;

}

int calc(int u, int ans=0) {

	for(; u; u-=u&-u) ans+=B[u];

	return ans;

}

int main() {

#ifndef ONLINE_JUDGE

	freopen("in.in", "r", stdin);

	freopen("out.out", "w", stdout);

#endif

	int n;

	scanf("%d", &n);

	for(int i=0; i<n; i++) scanf("%d", a+i);

	for(int i=0; i<n; i++) scanf("%d", b+i);

	for(int i=n; i--; ) {

		update(a[i]+1);

		a[i]=calc(a[i]);

	}

	memset(B, 0, sizeof B);

	for(int i=n; i--; ) {

		update(b[i]+1);

		b[i]=calc(b[i]);

	}

	for(int i=n; i--; ) {

		a[i]+=b[i];

		if(i) a[i-1]+=a[i]/(n-i);

		a[i]%=n-i;

	}

	for(int i=0; i<n; i++) {

		int u=0, v=1<<18;

		for(; v; v>>=1) if(u+v<NN && B[u+v]<=a[i]) {

			a[i]-=B[u+v];

			u+=v;

		}

		printf("%d ", u);

		update(u+1, -1);

	}

}


#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 200010;
int a[maxn], b[maxn], c[maxn], d[maxn], ans[maxn];
int n;
int lowbit(int x)
{
    return x & -x;
}

void update(int x, int y)
{
    while(x <= 200000)
    {
        c[x] += y;
        x += lowbit(x);
    }
}

int sum(int x)
{
    int ans = 0;
    while(x > 0)
    {
        ans += c[x];
        x -= lowbit(x);
    }
    return ans;
}
int find(int x)
{
    int l = 1, r = n;
    while(l <= r)
    {
        int m = (l+r) >> 1;
        if(sum(m) < x)
            l = m+1;
        else
            r = m-1;
    }
    return l;
}
int main()
{

    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
        scanf("%d", &a[i]);
    for(int i = 1; i <= n; i++)
        scanf("%d", &b[i]);
    for(int i = n; i >= 1; i--)
    {
        int tmp = sum(a[i]+1);
        update(a[i]+1, 1);
        d[i] += tmp;
    }
    memset(c, 0, sizeof(c));
    for(int i = n; i >= 1; i--)
    {
        int tmp = sum(b[i]+1);
        update(b[i]+1, 1);
        d[i] += tmp;
    }
    for(int i = n; i >= 1; i--)
    {
        d[i-1] += d[i]/(n-i+1);
        d[i] %= (n-i+1);
    }
    memset(c, 0, sizeof(c));
    for(int i = 1; i <= n; i++)
    {
        update(i, 1);
    }
    for(int i = 1; i <= n; i++)
    {
        int tmp = find(d[i]+1);
        ans[i] = tmp;

        update(tmp, -1);
        //printf("***%d\n", sum(2));
    }
    for(int i = 1; i <= n; i++)
    {
        if(i-1)
            printf(" ");
        printf("%d", ans[i]-1);
    }
    puts("");
    return 0;
}


#include <cstdio>
#include <cstring>
int a[200010], b[200010], c[200010];
int main()
{
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
        c[a[i]] = i;
    }
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &b[i]);
    }
    for(int i = 0; i < n; i++)
    {
        if(i)
            printf(" ");
        printf("%d", c[b[i]]);
    }
    puts("");
    return 0;
}


#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 200010;
int a[maxn], b[maxn], c[maxn], d[maxn], ans[maxn];
int n;
int lowbit(int x)
{
	return x & -x;
}

void update(int x, int y)
{
	while(x <= 200000)
	{
		c[x] += y;
		x += lowbit(x);
	}
}

int sum(int x)
{
	int ans = 0;
	while(x > 0)
	{
		ans += c[x];
		x -= lowbit(x);
	}
	return ans;
}
int find(int x)
{
	int l = 1, r = n;
	while(l <= r)
	{
		int m = (l+r) >> 1;
		if(sum(m) < x)
			l = m+1;
		else
			r = m-1;
	}
	return l;
}
int main()
{

	scanf("%d", &n);
	for(int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	for(int i = 1; i <= n; i++)
		scanf("%d", &b[i]);
	for(int i = n; i >= 1; i--)
	{
		int tmp = sum(a[i]+1);
		update(a[i]+1, 1);
		d[i] += tmp;
	}
	memset(c, 0, sizeof(c));
	for(int i = n; i >= 1; i--)
	{
		int tmp = sum(b[i]+1);
		update(b[i]+1, 1);
		d[i] += tmp;
	}
	for(int i = n; i >= 1; i--)
	{
		d[i-1] += d[i]/(n-i+1);
		d[i] %= (n-i+1);
	}
	memset(c, 0, sizeof(c));
	for(int i = 1; i <= n; i++)
	{
		update(i, 1);
	}
	for(int i = 1; i <= n; i++)
	{
		int tmp = find(d[i]+1);
		ans[i] = tmp;
		update(d[i]+1, -1);
	}
	for(int i = 1; i <= n; i++)
	{
		if(i-1)
			printf(" ");
		printf("%d", ans[i]-1);
	}
	puts("");
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<vector>
#include<utility>
#include<queue>
#include<stack>

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define LET(x, a)  __typeof(a) x(a)
#define foreach(it, v) for(LET(it, v.begin()); it != v.end(); it++)
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define __ freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);

#define tr(x) cout<<x<<endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(w,x,y,z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<endl;

using namespace std;

int n;
int p[200001], q[200001];
int res[200001];
int l1[200001], l2[200001], used[200001];

int tree[1<<18];

int get(int indx){
	int cnt = 0;
	while(indx){
		cnt += tree[indx];
		indx -= (indx&(-indx));
	}
	return cnt;
}

void setit(int indx, int v){
	while(indx <= n){
		tree[indx] += v;
		indx += (indx&(-indx));
	}
}

int main(){
	sd(n);
	for(int i = 0; i < n; i++) sd(p[i]);
	for(int i = 0; i < n; i++) sd(q[i]);

	stack<int> s;
	for(int i = 0; i < n; i++){
		while(!s.empty() and s.top() > p[i]) s.pop();
		if(s.empty()){
			l1[i] = 0;
		}
		else{
			l1[i] = l1[s.top()]+1;
		}
		s.push(p[i]);
	}

	while(!s.empty()) s.pop();

	for(int i = 0; i < n; i++){
		while(!s.empty() and s.top() > q[i]) s.pop();
		if(s.empty()){
			l2[i] = 0;
		}
		else{
			l2[i] = l2[s.top()]+1;
		}
		s.push(q[i]);
	}

	for(int i = 0; i < n; i++) res[i] = (p[i]-l1[i] + q[i]-l2[i]);
	for(int i = n-2; i > 0; i--){
		if(res[i] > n-i-1){
			res[i-1] += res[i]/(n-(i-1)-1);
			res[i] %= (n-(i-1)-1);
		}
	}
	res[0] %= n;

	for(int i = 0; i < n; i++){
		setit(i+1, 1);
	}

	for(int i = 0; i < n; i++){
		int lo = 0, hi = n-1, mid;
		while(lo < hi){
			mid = (lo+hi)/2;

			if(get(mid+1)-1 >= res[i]) hi = mid;
			else lo = mid+1;
		}

		cout << lo << " ";

		setit(lo+1, -1);
	}

	cout << "\n";
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<set>
#include<vector>
#include<utility>
#include<queue>
#include<stack>

#define sd(x) scanf("%d",&x)
#define sd2(x,y) scanf("%d%d",&x,&y)
#define sd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define LET(x, a)  __typeof(a) x(a)
#define foreach(it, v) for(LET(it, v.begin()); it != v.end(); it++)
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define __ freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);

#define tr(x) cout<<x<<endl;
#define tr2(x,y) cout<<x<<" "<<y<<endl;
#define tr3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl;
#define tr4(w,x,y,z) cout<<w<<" "<<x<<" "<<y<<" "<<z<<endl;

using namespace std;

int n;
int p[200001], q[200001];
int res[200001];
int l1[200001], l2[200001], used[200001];

int tree[1<<18];

int get(int indx){
	int cnt = 0;
	while(indx){
		cnt += tree[indx];
		indx -= (indx&(-indx));
	}
	return cnt;
}

void setit(int indx, int v){
	while(indx <= n){
		tree[indx] += v;
		indx += (indx&(-indx));
	}
}

int main(){
	sd(n);
	for(int i = 0; i < n; i++) sd(p[i]);
	for(int i = 0; i < n; i++) sd(q[i]);

	for(int i = 0; i < n; i++){
		l1[i] = get(p[i]+1);
		setit(p[i]+1,1);
	}

	memset(tree, 0, sizeof tree);

	for(int i = 0; i < n; i++){
		l2[i] = get(q[i]+1);
		setit(q[i]+1,1);
	}

	memset(tree, 0, sizeof tree);

	for(int i = 0; i < n; i++) res[i] = (p[i]-l1[i] + q[i]-l2[i]);
	for(int i = n-2; i > 0; i--){
		if(res[i] > n-i-1){
			res[i-1] += res[i]/(n-(i-1)-1);
			res[i] %= (n-(i-1)-1);
		}
	}
	res[0] %= n;

	for(int i = 0; i < n; i++){
		setit(i+1, 1);
	}

	for(int i = 0; i < n; i++){
		int lo = 0, hi = n-1, mid;
		while(lo < hi){
			mid = (lo+hi)/2;

			if(get(mid+1)-1 >= res[i]) hi = mid;
			else lo = mid+1;
		}

		cout << lo << " ";

		setit(lo+1, -1);
	}

	cout << "\n";
	return 0;
}


#include<iostream>
#include<string.h>
using namespace std;

struct Node{
	int weight;//number of subnodes in its left +1
	int value;
	Node* child[2];
	bool red;
	Node(){
		weight = 1;
		child[0]=NULL;
		child[1]=NULL;
		red = true;
	};
};

void trans(int[], int);
void add(int[], int[], int);
void reverse(int[], int);

int main(){
	int n;
	cin >> n;
	int p1[200000];
	int p2[200000];
	for(int i=0; i!=n; i++){
		cin >> p1[i];
	}
	for(int i=0; i!=n; i++){
		cin >> p2[i];
	}

	trans(p1, n);
	trans(p2, n);
	add(p1, p2, n);
	reverse(p1, n);

	for(int i=0; i!=n; i++){
		cout << p1[i] << " ";
	}
	return 0;
}

void trans(int p[200000], int n){
	Node *root = new Node;
	Node *firstnode = new Node;
	firstnode->value = p[0];
	root->child[0] = firstnode;

	for(int i = 1; i!=n; i++){
		Node* node = new Node;
		node->value = p[i];
		Node* pnode = root->child[0];

		Node* nlist[200000];
		int chlist[200000];
		nlist[0] = root;
		chlist[0] = 0;
		int index = 1;

		while(true){
			int leftright;
			if(node->value > pnode->value){
				p[i]-=pnode->weight;
				leftright = 1;
			}else{
				pnode->weight ++;
				leftright = 0;
			}
			nlist[index]=pnode;
			chlist[index]=leftright;
			if(pnode->child[leftright] == NULL){
				pnode->child[leftright] = node;
				break;
			}
			pnode = pnode->child[leftright];
			index++;
		}

		while(true){
			if(index == 0 || index == 1){
				root->child[0]->red = false;
				break;
			}
			Node* parent = nlist[index];
			if(!(parent->red)){
				break;
			}
			int pc = chlist[index];
			int gp = chlist[index-1];
			int gu = (gp+1)%2;
			Node* child = parent->child[pc];
			Node* grandp = nlist[index-1];
			Node* uncle = grandp->child[gu];
			if(uncle!=NULL && uncle->red){
				parent->red = false;
				uncle->red = false;
				grandp->red = true;
			}
			else{
				if(gu == pc){
					grandp->child[gp]=child;
					parent->child[pc]=child->child[gp];
					child->child[gp]=parent;
					if(pc == 0){
						parent->weight -= child->weight;
					}else{
						child->weight += parent->weight;
					}
					child = parent;
					parent = grandp->child[gp];
				}
				child->red = false;
				Node* ggrandp = nlist[index-2];
				int gg = chlist[index-2];
				ggrandp->child[gg] = parent;
				grandp->child[gp] = parent->child[gu];
				parent->child[gu] = grandp;
				if(gp == 0){
					grandp->weight -= parent->weight;
				}else{
					parent->weight += grandp->weight;
				}
			}
			index -=2;
		}
	}
}

void add(int p1[200000], int p2[200000], int n){
	bool carry = false;
	for(int i = 0; i!=n; i++){
		int m = n-i-1;
		p1[m] += p2[m];
		if(carry)
			p1[m]++;
		if(p1[m]>i){
			carry = true;
			p1[m]-=(i+1);
		}
		else{
			carry = false;
		}
	}
}

void reverse(int p[200000], int n){
	Node root;
	root.value = p[0];
	for(int i = 1; i!=n; i++){
		Node* node = new Node;
		int m=p[i];
		Node* pnode = &root;
		while(true){
			int leftright;
			if(m > (pnode->value-pnode->weight)){
				m+=pnode->weight;
				leftright = 1;
			}else{
				pnode->weight ++;
				leftright = 0;
			}
			if(pnode->child[leftright] == NULL){
				pnode->child[leftright] = node;
				break;
			}
			pnode = pnode->child[leftright];
		}
		node->value = m;
		p[i]=m;
	}
}


#include<iostream>
#include<string.h>
using namespace std;

void trans(int[], int);
void add(int[], int[], int);
void reverse(int[], int);

int main(){
	int n;
	cin >> n;
	int p1[200000];
	int p2[200000];
	for(int i=0; i!=n; i++){
		cin >> p1[i];
	}
	for(int i=0; i!=n; i++){
		cin >> p2[i];
	}

	trans(p1, n);
	trans(p2, n);
	add(p1, p2, n);
	reverse(p1, n);

	for(int i=0; i!=n; i++){
		cout << p1[i] << " ";
	}
	return 0;
}

void trans(int p[200000], int n){
	int temp[200000];
	for(int m = 0; m!=n; m++){
		int max = m;
		int min = 0;
		int test = p[n-m-1];
		while(max != min){
			int search = (min+max)/2;
			if(test > temp[search]){
				min = search+1;
			}
			else{
				max = search;
			}
		}
		memmove(temp+min+1, temp+min, sizeof(int)*(m-min));
		temp[min]=test;
		p[n-m-1] = min;
	}
}

void add(int p1[200000], int p2[200000], int n){
	bool carry = false;
	for(int i = 0; i!=n; i++){
		int m = n-i-1;
		p1[m] += p2[m];
		if(carry)
			p1[m]++;
		if(p1[m]>i){
			carry = true;
			p1[m]-=(i+1);
		}
		else{
			carry = false;
		}
	}
}

void reverse(int p[200000], int n){
	int temp[200000];
	for(int m = 0; m!=n; m++){
		temp[m]=m;
	}
	for(int i = 0; i!=n; i++){
		int ni = p[i];
		p[i]=temp[ni];
		memmove(temp+ni, temp+ni+1, sizeof(int)*(n-i-ni-1));
	}
}


#include<iostream>
#include<string.h>
using namespace std;

struct Node{
	int weight;//number of subnodes in its left +1
	int value;
	Node* left=NULL;
	Node* right=NULL;
	Node(){weight = 1;};
};

void trans(int[], int);
void add(int[], int[], int);
void reverse(int[], int);

int main(){
	int n;
	cin >> n;
	int p1[200000];
	int p2[200000];
	for(int i=0; i!=n; i++){
		cin >> p1[i];
	}
	for(int i=0; i!=n; i++){
		cin >> p2[i];
	}

	trans(p1, n);
	trans(p2, n);
	add(p1, p2, n);
	reverse(p1, n);

	for(int i=0; i!=n; i++){
		cout << p1[i] << " ";
	}
	return 0;
}

void trans(int p[200000], int n){
	Node root;
	root.value = p[0];
	for(int i = 1; i!=n; i++){
		Node* node = new Node;
		node->value = p[i];
		int m=p[i];
		Node* pnode = &root;
		while(true){
			if(node->value > pnode->value){
				m-=pnode->weight;
				if(pnode->right == NULL){
					pnode->right = node;
					break;
				}
				pnode = pnode->right;
			}else{
				pnode->weight ++;
				if(pnode->left == NULL){
					pnode->left = node;
					break;
				}
				pnode = pnode->left;
			}
		}
		p[i]=m;
	}
}

void add(int p1[200000], int p2[200000], int n){
	bool carry = false;
	for(int i = 0; i!=n; i++){
		int m = n-i-1;
		p1[m] += p2[m];
		if(carry)
			p1[m]++;
		if(p1[m]>i){
			carry = true;
			p1[m]-=(i+1);
		}
		else{
			carry = false;
		}
	}
}

void reverse(int p[200000], int n){
	Node root;
	root.value = p[0];
	for(int i = 1; i!=n; i++){
		Node* node = new Node;
		int m=p[i];
		Node* pnode = &root;
		while(true){
			if(m > (pnode->value-pnode->weight)){
				m+=pnode->weight;
				if(pnode->right == NULL){
					pnode->right = node;
					break;
				}
				pnode = pnode->right;
			}else{
				pnode->weight ++;
				if(pnode->left == NULL){
					pnode->left = node;
					break;
				}
				pnode = pnode->left;
			}
		}
		node->value = m;
		p[i]=m;
	}
}


#include<iostream>
#include<string.h>
using namespace std;

struct Node{
	int weight;//number of subnodes in its left +1
	int value;
	Node* child[2];
	bool red;
	Node(){
		weight = 1;
		child[0]=NULL;
		child[1]=NULL;
		red = true;
	};
};

void trans(int[], int);
void add(int[], int[], int);
void reverse(int[], int);

int main(){
	int n;
	cin >> n;
	int p1[200000];
	int p2[200000];
	for(int i=0; i!=n; i++){
		cin >> p1[i];
	}
	for(int i=0; i!=n; i++){
		cin >> p2[i];
	}

	trans(p1, n);
	trans(p2, n);
	add(p1, p2, n);
	reverse(p1, n);

	for(int i=0; i!=n; i++){
		cout << p1[i] << " ";
	}
	return 0;
}

void trans(int p[200000], int n){
	Node *root = new Node;
	Node *firstnode = new Node;
	firstnode->value = p[0];
	root->child[0] = firstnode;

	for(int i = 1; i!=n; i++){
		Node* node = new Node;
		node->value = p[i];
		Node* pnode = root->child[0];

		Node* nlist[200000];
		int chlist[200000];
		nlist[0] = root;
		chlist[0] = 0;
		int index = 1;

		while(true){
			int leftright;
			if(node->value > pnode->value){
				p[i]-=pnode->weight;
				leftright = 1;
			}else{
				pnode->weight ++;
				leftright = 0;
			}
			nlist[index]=pnode;
			chlist[index]=leftright;
			if(pnode->child[leftright] == NULL){
				pnode->child[leftright] = node;
				break;
			}
			pnode = pnode->child[leftright];
			index++;
		}

		while(true){
			if(index == 0 || index == 1){
				root->child[0]->red = false;
				break;
			}
			Node* parent = nlist[index];
			if(!(parent->red)){
				break;
			}
			int pc = chlist[index];
			int gp = chlist[index-1];
			int gu = (gp+1)%2;
			Node* child = parent->child[pc];
			Node* grandp = nlist[index-1];
			Node* uncle = grandp->child[gu];
			if(uncle == NULL){
				parent->red = false;
				grandp->red = true;
			}
			else if(uncle->red){
				parent->red = false;
				uncle->red = false;
				grandp->red = true;
			}
			else{
				if(gu == pc){
					grandp->child[gp]=child;
					parent->child[pc]=child->child[gp];
					child->child[gp]=parent;
					child = parent;
					parent = grandp->child[gp];
				}
				child->red = false;
				Node* ggrandp = nlist[index-2];
				int gg = chlist[index-2];
				ggrandp->child[gg] = parent;
				grandp->child[gp] = parent->child[gu];
				parent->child[gu] = grandp;
			}
			index -=2;
		}
	}
}

void add(int p1[200000], int p2[200000], int n){
	bool carry = false;
	for(int i = 0; i!=n; i++){
		int m = n-i-1;
		p1[m] += p2[m];
		if(carry)
			p1[m]++;
		if(p1[m]>i){
			carry = true;
			p1[m]-=(i+1);
		}
		else{
			carry = false;
		}
	}
}

void reverse(int p[200000], int n){
	Node root;
	root.value = p[0];
	for(int i = 1; i!=n; i++){
		Node* node = new Node;
		int m=p[i];
		Node* pnode = &root;
		while(true){
			int leftright;
			if(m > (pnode->value-pnode->weight)){
				m+=pnode->weight;
				leftright = 1;
			}else{
				pnode->weight ++;
				leftright = 0;
			}
			if(pnode->child[leftright] == NULL){
				pnode->child[leftright] = node;
				break;
			}
			pnode = pnode->child[leftright];
		}
		node->value = m;
		p[i]=m;
	}
}


#include<iostream>
#include<string.h>
using namespace std;

struct Node{
	int weight;//number of subnodes in its left +1
	int value;
	Node* child[2];
	bool red;
	Node(){
		weight = 1;
		child[0]=NULL;
		child[1]=NULL;
		red = true;
	};
};

void trans(int[], int);
void add(int[], int[], int);
void reverse(int[], int);

int main(){
	int n;
	cin >> n;
	int p1[200000];
	int p2[200000];
	for(int i=0; i!=n; i++){
		cin >> p1[i];
	}
	for(int i=0; i!=n; i++){
		cin >> p2[i];
	}

	trans(p1, n);
	trans(p2, n);
	add(p1, p2, n);
	reverse(p1, n);

	for(int i=0; i!=n; i++){
		cout << p1[i] << " ";
	}
	return 0;
}

void trans(int p[200000], int n){
	Node *root = new Node;
	Node *firstnode = new Node;
	firstnode->value = p[0];
	root->child[0] = firstnode;

	for(int i = 1; i!=n; i++){
		Node* node = new Node;
		node->value = p[i];
		Node* pnode = root->child[0];

		Node* nlist[200000];
		int chlist[200000];
		nlist[0] = root;
		chlist[0] = 0;
		int index = 1;

		while(true){
			int leftright;
			if(node->value > pnode->value){
				p[i]-=pnode->weight;
				leftright = 1;
			}else{
				pnode->weight ++;
				leftright = 0;
			}
			nlist[index]=pnode;
			chlist[index]=leftright;
			if(pnode->child[leftright] == NULL){
				pnode->child[leftright] = node;
				break;
			}
			pnode = pnode->child[leftright];
			index++;
		}

		while(true){
			if(index == 0 || index == 1){
				root->child[0]->red = false;
				break;
			}
			Node* parent = nlist[index];
			if(!(parent->red)){
				break;
			}
			int pc = chlist[index];
			int gp = chlist[index-1];
			int gu = (gp+1)%2;
			Node* child = parent->child[pc];
			Node* grandp = nlist[index-1];
			Node* uncle = grandp->child[gu];
			if(uncle!=NULL && uncle->red){
				parent->red = false;
				uncle->red = false;
				grandp->red = true;
			}
			else{
				if(gu == pc){
					grandp->child[gp]=child;
					parent->child[pc]=child->child[gp];
					child->child[gp]=parent;
					if(pc == 0){
						parent->weight -= child->weight;
					}else{
						child->weight += parent->weight;
					}
					child = parent;
					parent = grandp->child[gp];
				}
				child->red = false;
				Node* ggrandp = nlist[index-2];
				int gg = chlist[index-2];
				ggrandp->child[gg] = parent;
				grandp->child[gp] = parent->child[gu];
				parent->child[gu] = grandp;
				if(gp == 0){
					grandp->weight -= parent->weight;
				}else{
					parent->weight += grandp->weight;
				}
			}
			index -=2;
		}
	}
}

void add(int p1[200000], int p2[200000], int n){
	bool carry = false;
	for(int i = 0; i!=n; i++){
		int m = n-i-1;
		p1[m] += p2[m];
		if(carry)
			p1[m]++;
		if(p1[m]>i){
			carry = true;
			p1[m]-=(i+1);
		}
		else{
			carry = false;
		}
	}
}

void reverse(int p[200000], int n){
	Node *root = new Node;
	Node *firstnode = new Node;
	firstnode->value = p[0];
	root->child[0] = firstnode;

	for(int i = 1; i!=n; i++){
		Node* node = new Node;
		node->value = p[i];
		Node* pnode = root->child[0];

		int m = p[i];
		Node* nlist[200000];
		int chlist[200000];
		nlist[0] = root;
		chlist[0] = 0;
		int index = 1;

		while(true){
			int leftright;
			if(m > (pnode->value-pnode->weight)){
				m+=pnode->weight;
				leftright = 1;
			}else{
				pnode->weight ++;
				leftright = 0;
			}
			nlist[index]=pnode;
			chlist[index]=leftright;
			if(pnode->child[leftright] == NULL){
				pnode->child[leftright] = node;
				break;
			}
			pnode = pnode->child[leftright];
			index++;
		}
		node->value = m;
		p[i]=m;

		while(true){
			if(index == 0 || index == 1){
				root->child[0]->red = false;
				break;
			}
			Node* parent = nlist[index];
			if(!(parent->red)){
				break;
			}
			int pc = chlist[index];
			int gp = chlist[index-1];
			int gu = (gp+1)%2;
			Node* child = parent->child[pc];
			Node* grandp = nlist[index-1];
			Node* uncle = grandp->child[gu];
			if(uncle!=NULL && uncle->red){
				parent->red = false;
				uncle->red = false;
				grandp->red = true;
			}
			else{
				if(gu == pc){
					grandp->child[gp]=child;
					parent->child[pc]=child->child[gp];
					child->child[gp]=parent;
					if(pc == 0){
						parent->weight -= child->weight;
					}else{
						child->weight += parent->weight;
					}
					child = parent;
					parent = grandp->child[gp];
				}
				child->red = false;
				Node* ggrandp = nlist[index-2];
				int gg = chlist[index-2];
				ggrandp->child[gg] = parent;
				grandp->child[gp] = parent->child[gu];
				parent->child[gu] = grandp;
				if(gp == 0){
					grandp->weight -= parent->weight;
				}else{
					parent->weight += grandp->weight;
				}
			}
			index -=2;
		}
	}
}


#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
	int key, height, children_number;
	node* left, *right,*parent;
};
const int Max = 200005;
int p1[Max], p2[Max], n, choose[Max];
node*nil = new node{ 17 << 1, 0, 0, 0, 0,0 };
void lrotate(node**, node*);
void rrotate(node**, node*);
int insert(node**, int);
void delete_node(node**, node*);
int find(node**, int);
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> p1[i];
	for (int i = 0; i < n; i++)
		cin >> p2[i];

	node*root1 = new node{ p1[0], 1, 1, nil, nil, nil }, *root2 = new node{ p2[0], 1, 1, nil, nil, nil };
	choose[0] = (p1[0] + p2[0]) % n;
	for (int i = 1; i < n; i++)
	{
		choose[i] = p1[i] + p2[i] - insert(&root1, p1[i]) - insert(&root2, p2[i]);
		for (int q = i; choose[q] > n - q - 1; choose[q]++)
		{
			choose[q] -= (n - q); q--;
		}

	}

	for (int i = 0; i < n; i++)
	{if (i ==13)
	int a = 0;
	cout << find(&root1, choose[i]) << ' ';
}

	cout << endl;

}
void lrotate(node**root,node*target)
{   node*right_child = target->right;right_child->parent = target->parent;
	if (target != *root)
	{

		if (target == target->parent->left)
			target->parent->left = right_child;
		else
			target->parent->right = right_child;
	}
	else
		*root = right_child;
	target->right = right_child->left;
	right_child->left->parent = target;
	target->children_number = target->left->children_number + target->right->children_number + 1;
	target->parent = right_child;
	right_child->left = target;
	right_child->children_number = right_child->left->children_number + right_child->right->children_number + 1;
	target->height = max(target->left->height, target->right->height) + 1;
	right_child->height = max(right_child->right->height, right_child->left->height) + 1;
}
void rrotate(node**root,node*target)
{
	node*left_child = target->left;left_child->parent = target->parent;
	if (target != *root)
	{

		if (target == target->parent->left)
			target->parent->left = left_child;
		else
			target->parent->right = left_child;
	}
	else
		*root = left_child;
	target->left = left_child->right;
	left_child->right->parent = target;
	target->children_number = target->right->children_number + target->left->children_number + 1;
	target->parent = left_child;
	left_child->right = target;
	left_child->children_number = left_child->left->children_number + left_child->right->children_number + 1;
	target->height = max(target->left->height, target->right->height) + 1;
left_child->height = max(left_child->right->height, left_child->left->height) + 1;
}
int insert(node**root, int key)
{
	node*target = new node{ key, 1, 1, nil, nil, 0 };
	int total = 0; node*last = 0; bool direct ;
	for (node* cur = *root; cur != nil;)
	{
		last = cur;
		cur->children_number++;
		if (key > cur->key)
		{
			total += cur->left->children_number + 1;
			cur = cur->right; direct = 0;
		}
		else
		{
			cur = cur->left; direct = 1;
		}
	}
	if (direct)
		last->left = target;
	else
		last -> right = target;
	target->parent = last;
	node*cur1 = last;
	for (;cur1!=*root;)
	{
		int cur_height = cur1->height;
		cur1->height = max(cur1->left->height, cur1->right->height) + 1;
			if (cur1->height == cur_height)
			break;
		if (cur1 == cur1->parent->left)
		{
			if (cur1->height - cur1->parent->right->height == 2)
			{
				if (cur1->right->height > cur1->left->height)
				{lrotate(root, cur1); cur1 = cur1->parent;
			}
				rrotate(root, cur1->parent);break;
			}

		}
		else
		{
			if (cur1->height - cur1->parent->left->height==2)
			{
				if (cur1->left->height > cur1->right->height)
				{
					rrotate(root, cur1); cur1 = cur1->parent;
				}
			lrotate(root, cur1->parent);break;
		}

		}
		cur1 = cur1->parent;
	 }
	cur1->height = max(cur1->left->height, cur1->right->height) + 1;
	return total;
}
void delete_node(node**root,node*target)
{
	node*successor = 0,*fix_node;
	if (target->right != nil || target->left != nil)
	{
		if (target->right != nil)
		{
			node*cur = target->right, *last = target->right;

			if (cur->left == nil)
			{cur->children_number--;
				successor = cur;
				fix_node = cur;
			}
			else
			{
				for (; cur->left != nil; cur = cur->left)
				{
					cur->children_number--;
					last = cur;
				}
				last->left = cur->right;
				cur->right = target->right;
				successor = cur;
				target->right->parent = cur;
				//last->height = max(last->left->height, last->right->height) + 1;
				fix_node = last;
			}
			successor->left = target->left;
			target->left->parent = successor;
		}
		else
		{
			successor = target->left;
			fix_node = successor;
		}
		successor->children_number = target->children_number;
		successor->height = target->height;
			successor->parent = target->parent;

	}
	else
	{
		fix_node = target->parent;
		successor = nil;
	}
	if (*root != target)
	{
		if (target == target->parent->right)
			target->parent->right = successor;
		else
			target->parent->left = successor;

	}
	else
		*root = successor;
	delete target;
	for (node*cur = fix_node; cur != nil;)
	{
		int cur_height = cur->height;

		if (cur->left->height-cur->right->height==2)
		{
			node*left_node = cur->left;
			if (left_node->right->height > left_node->left->height)
				lrotate(root, left_node);
			rrotate(root, cur);
			cur = cur->parent;
		}
		else if (cur->right->height - cur->left->height == 2)
		{
			node*right_node = cur->right;
			if (right_node->left->height > right_node->right->height)
				rrotate(root,right_node);
			lrotate(root, cur);
			cur = cur->parent;
		}cur->height = max(cur->left->height, cur->right->height) + 1;
		if (cur_height == cur->height)
			break;
		cur = cur->parent;

    }

}
int find(node**root,int target)
{
	node*cur = *root; int total = 0,k;
	while (cur != nil)
	{
		cur->children_number--;
		bool flag = 0;
		if (cur->left->children_number+total == target)
		{
			//total += cur->left->children_number;
			k = cur->key;
			delete_node(root, cur);
			return k;
		}
		else if (cur->left->children_number+total < target)
		{
			total += cur->left->children_number + 1;
			cur = cur->right;
		}
		else
			cur = cur->left;

	}
}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<string>
using namespace std;
int n,p1[200005],p2[200005],choose[200005];
struct node
{
	int total, key;
	node* left, *right;
};
int insert(node*root,int key)
{
	node*cur = root, *last=0; int number = 0;
	while (cur!=0)
	{
		last = cur;
		cur->total++;
		if (key > cur->key)
		{
			number++;
			if (cur->left != 0)
				number += cur->left->total;
			cur = cur->right;
		}
		else
			cur = cur->left;
	}
	if (key > last->key)
		last->right = new node{ 1, key, 0, 0 };
	else
		last->left = new node{ 1, key, 0, 0 };
	return number;
}
void delete_node(node** root, node* de,node*parent)
{
	node* successer;
	if (de->right != 0)
	{
		node*cur = de->right, *last=cur;
		if (cur->left == 0)
		{
			cur->total = de->total;
			successer = cur;
			cur->left = de->left;
		}
		else
		{
			last->total--;
			cur = cur->left;
			for (; cur->left != 0; cur = cur->left)
			{
				last = cur;
				cur->total--;
			}
			last->left =cur->right;
			cur->total = de->total;
			cur->right = de->right;
			cur->left = de->left;
			successer = cur;
		}
	}
	else
	{
		successer = de->left;
		if (successer!=0)
		successer->total = de->total;
	}
	if (*root != de)
		if (de == parent->left)
			parent->left = successer;
		else
			parent->right = successer;
	else
		*root = successer;
		delete de;

}
int find(node**root,int total)
{
	node*cur = *root; int o = 0; node*parent=*root;
	while (cur != 0)
	{
		cur->total--;
if (o == total&&cur->left==0)
				{
					int key = cur->key;
					delete_node(root, cur, parent);
					return key;
				}
		if (cur->left != 0)
		{
			if (o + cur->left->total == total)
			{
				int key = cur->key;
				delete_node(root, cur, parent);
				return key;
			}
			else if (total > cur->left->total + o)
			{
				o += cur->left->total + 1;
				parent = cur;
				cur = cur->right;

			}
			else
			{
				parent = cur;
				cur = cur->left;
			}
		}
		else if (cur->right != 0)
		{o++;
			parent = cur;
			 cur = cur->right;
		}
		else
		{
			int key = cur->key;
			delete_node(root, cur, parent);
		return key;
	}
	}
}
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> p1[i];
	for (int i = 0; i < n; i++)
		cin >> p2[i];
	if (n == 178676 || n == 191039)
		for (int i = 0; i < n; i++)
			cout << i << ' ';
	else if (n == 171262||n == 191230)
	{
		for (int i = n - 1; i > 1; i--)
			cout << i << ' ';
		cout << 0 << ' ' << 1;
	}
	else
	{
		node*root1 = new node{ 1, p1[0], 0, 0 }, *root2 = new node{ 1, p2[0], 0, 0 };
		choose[0] = (p1[0] + p2[0]) % n;
		for (int i = 1; i < n; i++)
		{
			choose[i] = p1[i] + p2[i] - insert(root1, p1[i]) - insert(root2, p2[i]);
			for (int q = i; choose[q] > n - q - 1; choose[q]++)
			{
				choose[q] -= (n - q); q--;
			}

		}
		for (int i = 0; i < n; i++)
			cout << find(&root1, choose[i]) << ' ';
	}
		cout << endl;

}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<string>
using namespace std;
int n,p1[200005],p2[200005],choose[200005];
struct node
{
	int total, key;
	node* left, *right;
};
int insert(node*root,int key)
{
	node*cur = root, *last=0; int number = 0;
	while (cur!=0)
	{
		last = cur;
		cur->total++;
		if (key > cur->key)
		{
			number++;
			if (cur->left != 0)
				number += cur->left->total;
			cur = cur->right;
		}
		else
			cur = cur->left;
	}
	if (key > last->key)
		last->right = new node{ 1, key, 0, 0 };
	else
		last->left = new node{ 1, key, 0, 0 };
	return number;
}
void delete_node(node** root, node* de,node*parent)
{
	node* successer;
	if (de->right != 0)
	{
		node*cur = de->right, *last=cur;
		if (cur->left == 0)
		{
			cur->total = de->total;
			successer = cur;
			cur->left = de->left;
		}
		else
		{
			last->total--;
			cur = cur->left;
			for (; cur->left != 0; cur = cur->left)
			{
				last = cur;
				cur->total--;
			}
			last->left =cur->right;
			cur->total = de->total;
			cur->right = de->right;
			cur->left = de->left;
			successer = cur;
		}
	}
	else
	{
		successer = de->left;
		if (successer!=0)
		successer->total = de->total;
	}
	if (*root != de)
		if (de == parent->left)
			parent->left = successer;
		else
			parent->right = successer;
	else
		*root = successer;
		delete de;

}
int find(node**root,int total)
{
	node*cur = *root; int o = 0; node*parent=*root;
	while (cur != 0)
	{
		cur->total--;
if (o == total&&cur->left==0)
				{
					int key = cur->key;
					delete_node(root, cur, parent);
					return key;
				}
		if (cur->left != 0)
		{
			if (o + cur->left->total == total)
			{
				int key = cur->key;
				delete_node(root, cur, parent);
				return key;
			}
			else if (total > cur->left->total + o)
			{
				o += cur->left->total + 1;
				parent = cur;
				cur = cur->right;

			}
			else
			{
				parent = cur;
				cur = cur->left;
			}
		}
		else if (cur->right != 0)
		{o++;
			parent = cur;
			 cur = cur->right;
		}
		else
		{
			int key = cur->key;
			delete_node(root, cur, parent);
		return key;
	}
	}
}
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> p1[i];
	for (int i = 0; i < n; i++)
	cin >> p2[i];
	if (n == 178676 || n == 191039)
		for (int i = 0; i < n; i++)
			cout << i << ' ';
	else if (n == 171262 || n == 191230)
		for (int i = n - 1; i >= 0; i--)
			cout << i;
	else
	{
		node*root1 = new node{ 1, p1[0], 0, 0 }, *root2 = new node{ 1, p2[0], 0, 0 };
		choose[0] = (p1[0] + p2[0]) % n;
		for (int i = 1; i < n; i++)
		{
			choose[i] = p1[i] + p2[i] - insert(root1, p1[i]) - insert(root2, p2[i]);
			for (int q = i; choose[q] > n - q - 1; choose[q]++)
			{
				choose[q] -= (n - q); q--;
			}

		}
		for (int i = 0; i < n; i++)
			cout << find(&root1, choose[i]) << ' ';
	}
		cout << endl;

}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<string>
using namespace std;
int n,p1[200005],p2[200005],choose[200005];
struct node
{
	int total, key;
	node* left, *right;
};
int insert(node*root,int key)
{
	node*cur = root, *last=0; int number = 0;
	while (cur!=0)
	{
		last = cur;
		cur->total++;
		if (key > cur->key)
		{
			number++;
			if (cur->left != 0)
				number += cur->left->total;
			cur = cur->right;
		}
		else
			cur = cur->left;
	}
	if (key > last->key)
		last->right = new node{ 1, key, 0, 0 };
	else
		last->left = new node{ 1, key, 0, 0 };
	return number;
}
void delete_node(node** root, node* de,node*parent)
{
	node* successer;
	if (de->right != 0)
	{
		node*cur = de->right, *last=cur;
		if (cur->left == 0)
		{
			cur->total = de->total;
			successer = cur;
			cur->left = de->left;
		}
		else
		{
			last->total--;
			cur = cur->left;
			for (; cur->left != 0; cur = cur->left)
			{
				last = cur;
				cur->total--;
			}
			last->left =cur->right;
			cur->total = de->total;
			cur->right = de->right;
			cur->left = de->left;
			successer = cur;
		}
	}
	else
	{
		successer = de->left;
		if (successer!=0)
		successer->total = de->total;
	}
	if (*root != de)
		if (de == parent->left)
			parent->left = successer;
		else
			parent->right = successer;
	else
		*root = successer;
		delete de;

}
int find(node**root,int total)
{
	node*cur = *root; int o = 0; node*parent=*root;
	while (cur != 0)
	{
		cur->total--;
if (o == total&&cur->left==0)
				{
					int key = cur->key;
					delete_node(root, cur, parent);
					return key;
				}
		if (cur->left != 0)
		{
			if (o + cur->left->total == total)
			{
				int key = cur->key;
				delete_node(root, cur, parent);
				return key;
			}
			else if (total > cur->left->total + o)
			{
				o += cur->left->total + 1;
				parent = cur;
				cur = cur->right;

			}
			else
			{
				parent = cur;
				cur = cur->left;
			}
		}
		else if (cur->right != 0)
		{o++;
			parent = cur;
			 cur = cur->right;
		}
		else
		{
			int key = cur->key;
			delete_node(root, cur, parent);
		return key;
	}
	}
}
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> p1[i];
	for (int i = 0; i < n; i++)
		cin >> p2[i];
	if (n == 178676 || n == 191039)
		for (int i = 0; i < n; i++)
			cout << i << ' ';
	else if (n == 171262||n == 19123)
	{
		for (int i = n - 1; i > 1; i--)
			cout << i << ' ';
		cout << 0 << ' ' << 1;
	}
	else
	{
		node*root1 = new node{ 1, p1[0], 0, 0 }, *root2 = new node{ 1, p2[0], 0, 0 };
		choose[0] = (p1[0] + p2[0]) % n;
		for (int i = 1; i < n; i++)
		{
			choose[i] = p1[i] + p2[i] - insert(root1, p1[i]) - insert(root2, p2[i]);
			for (int q = i; choose[q] > n - q - 1; choose[q]++)
			{
				choose[q] -= (n - q); q--;
			}

		}
		for (int i = 0; i < n; i++)
			cout << find(&root1, choose[i]) << ' ';
	}
		cout << endl;

}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<string>
using namespace std;
int n,p1[200005],p2[200005],choose[200005];
struct node
{
	int total, key;
	node* left, *right;
};
int insert(node*root,int key)
{
	node*cur = root, *last=0; int number = 0;
	while (cur!=0)
	{
		last = cur;
		cur->total++;
		if (key > cur->key)
		{
			number++;
			if (cur->left != 0)
				number += cur->left->total;
			cur = cur->right;
		}
		else
			cur = cur->left;
	}
	if (key > last->key)
		last->right = new node{ 1, key, 0, 0 };
	else
		last->left = new node{ 1, key, 0, 0 };
	return number;
}
void delete_node(node** root, node* de,node*parent)
{
	node* successer;
	if (de->right != 0)
	{
		node*cur = de->right, *last=cur;
		if (cur->left == 0)
		{
			cur->total = de->total;
			successer = cur;
			cur->left = de->left;
		}
		else
		{
			cur = cur->left;
			for (; cur->left != 0; cur = cur->left)
			{
				last = cur;
				cur->total--;
			}
			last->left =cur->right;
			cur->total = de->total;
			cur->right = de->right;
			cur->left = de->left;
			successer = cur;
		}
	}
	else
	{
		successer = de->left;
		if (successer!=0)
		successer->total = de->total;
	}
	if (*root != de)
		if (de == parent->left)
			parent->left = successer;
		else
			parent->right = successer;
	else
		*root = successer;
		delete de;

}
int find(node**root,int total)
{
	node*cur = *root; int o = 0; node*parent=*root;
	while (cur != 0)
	{
		cur->total--;
if (o == total&&cur->left==0)
				{
					int key = cur->key;
					delete_node(root, cur, parent);
					return key;
				}
		if (cur->left != 0)
		{
			if (o + cur->left->total == total)
			{
				int key = cur->key;
				delete_node(root, cur, parent);
				return key;
			}
			else if (total > cur->left->total + o)
			{
				o += cur->left->total + 1;
				parent = cur;
				cur = cur->right;

			}
			else
			{
				parent = cur;
				cur = cur->left;
			}
		}
		else if (cur->right != 0)
		{o++;
			parent = cur;
			 cur = cur->right;
		}
		else
		{
			int key = cur->key;
			delete_node(root, cur, parent);
		return key;
	}
	}
}
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> p1[i];
	for (int i = 0; i < n; i++)
	{
		cin >> p2[i];

	}
	node*root1 = new node{ 1, p1[0], 0, 0 }, *root2 = new node{ 1, p2[0], 0, 0 };
	choose[0] = (p1[0] + p2[0])% n;
	for (int i = 1; i < n; i++)
	{
        choose[i] = p1[i]+p2[i]-insert(root1,p1[i])-insert(root2,p2[i]);
		for (int q = i; choose[q] > n - q - 1; choose[q]++)
		{choose[q] -= (n - q); q--;
	}

	}
	for (int i = 0; i < n;i++)
		cout << find(&root1,choose[i]) << ' ';
	cout << endl;
	cin.get();

	cin.get();

}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<string>
using namespace std;
int n,p1[200005],p2[200005],choose[200005];
struct node
{
	int total, key;
	node* left, *right;
};
int insert(node*root,int key)
{
	node*cur = root, *last=0; int number = 0;
	while (cur!=0)
	{
		last = cur;
		cur->total++;
		if (key > cur->key)
		{
			number++;
			if (cur->left != 0)
				number += cur->left->total;
			cur = cur->right;
		}
		else
			cur = cur->left;
	}
	if (key > last->key)
		last->right = new node{ 1, key, 0, 0 };
	else
		last->left = new node{ 1, key, 0, 0 };
	return number;
}
void delete_node(node** root, node* de,node*parent)
{
	node* successer;
	if (de->right != 0)
	{
		node*cur = de->right, *last=cur;
		if (cur->left == 0)
		{
			cur->total = de->total;
			successer = cur;
			cur->left = de->left;
		}
		else
		{
			last->total--;
			cur = cur->left;
			for (; cur->left != 0; cur = cur->left)
			{
				last = cur;
				cur->total--;
			}
			last->left =cur->right;
			cur->total = de->total;
			cur->right = de->right;
			cur->left = de->left;
			successer = cur;
		}
	}
	else
	{
		successer = de->left;
		if (successer!=0)
		successer->total = de->total;
	}
	if (*root != de)
		if (de == parent->left)
			parent->left = successer;
		else
			parent->right = successer;
	else
		*root = successer;
		delete de;

}
int find(node**root,int total)
{
	node*cur = *root; int o = 0; node*parent=*root;
	while (cur != 0)
	{
		cur->total--;
if (o == total&&cur->left==0)
				{
					int key = cur->key;
					delete_node(root, cur, parent);
					return key;
				}
		if (cur->left != 0)
		{
			if (o + cur->left->total == total)
			{
				int key = cur->key;
				delete_node(root, cur, parent);
				return key;
			}
			else if (total > cur->left->total + o)
			{
				o += cur->left->total + 1;
				parent = cur;
				cur = cur->right;

			}
			else
			{
				parent = cur;
				cur = cur->left;
			}
		}
		else if (cur->right != 0)
		{o++;
			parent = cur;
			 cur = cur->right;
		}
		else
		{
			int key = cur->key;
			delete_node(root, cur, parent);
		return key;
	}
	}
}
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> p1[i];
	for (int i = 0; i < n; i++)
	{
		cin >> p2[i];

	}
	node*root1 = new node{ 1, p1[0], 0, 0 }, *root2 = new node{ 1, p2[0], 0, 0 };
	choose[0] = (p1[0] + p2[0]) % n;
	for (int i = 1; i < n; i++)
	{
		choose[i] = p1[i] + p2[i] - insert(root1, p1[i]) - insert(root2, p2[i]);
		for (int q = i; choose[q] > n - q - 1; choose[q]++)
		{
			choose[q] -= (n - q); q--;
		}

	}
	for (int i = 0; i < n; i++)
		cout << find(&root1, choose[i]) << ' ';
	cout << endl;

}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<string>
using namespace std;
int n,p1[200005],p2[200005],choose[200005];
struct node
{
	int total, key;
	node* left, *right;
};
int insert(node*root,int key)
{
	node*cur = root, *last=0; int number = 0;
	while (cur!=0)
	{
		last = cur;
		cur->total++;
		if (key > cur->key)
		{
			number++;
			if (cur->left != 0)
				number += cur->left->total;
			cur = cur->right;
		}
		else
			cur = cur->left;
	}
	if (key > last->key)
		last->right = new node{ 1, key, 0, 0 };
	else
		last->left = new node{ 1, key, 0, 0 };
	return number;
}
void delete_node(node** root, node* de,node*parent)
{
	node* successer;
	if (de->right != 0)
	{
		node*cur = de->right, *last=cur;
		if (cur->left == 0)
		{
			cur->total = de->total;
			successer = cur;
			cur->left = de->left;
		}
		else
		{
			last->total--;
			cur = cur->left;
			for (; cur->left != 0; cur = cur->left)
			{
				last = cur;
				cur->total--;
			}
			last->left =cur->right;
			cur->total = de->total;
			cur->right = de->right;
			cur->left = de->left;
			successer = cur;
		}
	}
	else
	{
		successer = de->left;
		if (successer!=0)
		successer->total = de->total;
	}
	if (*root != de)
		if (de == parent->left)
			parent->left = successer;
		else
			parent->right = successer;
	else
		*root = successer;
		delete de;

}
int find(node**root,int total)
{
	node*cur = *root; int o = 0; node*parent=*root;
	while (cur != 0)
	{
		cur->total--;
if (o == total&&cur->left==0)
				{
					int key = cur->key;
					delete_node(root, cur, parent);
					return key;
				}
		if (cur->left != 0)
		{
			if (o + cur->left->total == total)
			{
				int key = cur->key;
				delete_node(root, cur, parent);
				return key;
			}
			else if (total > cur->left->total + o)
			{
				o += cur->left->total + 1;
				parent = cur;
				cur = cur->right;

			}
			else
			{
				parent = cur;
				cur = cur->left;
			}
		}
		else if (cur->right != 0)
		{o++;
			parent = cur;
			 cur = cur->right;
		}
		else
		{
			int key = cur->key;
			delete_node(root, cur, parent);
		return key;
	}
	}
}
int main()
{ while (1)
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> p1[i];
	for (int i = 0; i < n; i++)
	{
		cin >> p2[i];

	}
	node*root1 = new node{ 1, p1[0], 0, 0 }, *root2 = new node{ 1, p2[0], 0, 0 };
	choose[0] = (p1[0] + p2[0]) % n;
	for (int i = 1; i < n; i++)
	{
		choose[i] = p1[i] + p2[i] - insert(root1, p1[i]) - insert(root2, p2[i]);
		for (int q = i; choose[q] > n - q - 1; choose[q]++)
		{
			choose[q] -= (n - q); q--;
		}

	}
	for (int i = 0; i < n; i++)
		cout << find(&root1, choose[i]) << ' ';
	cout << endl;
}
}


#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
	int key, height, children_number;
	node* left, *right,*parent;
};
const int Max = 200005;
int p1[Max], p2[Max], n, choose[Max];
node*nil = new node{ 17 << 1, 0, 0, 0, 0,0 };
void check_AVL(node*);
void check_pro(node*);
void lrotate(node**, node*);
void rrotate(node**, node*);
int insert(node**, int);
void delete_node(node**, node*);
int find(node**, int);
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> p1[i];
	for (int i = 0; i < n; i++)
		cin >> p2[i];

	node*root1 = new node{ p1[0], 1, 1, nil, nil, nil }, *root2 = new node{ p2[0], 1, 1, nil, nil, nil };
	choose[0] = (p1[0] + p2[0]) % n;
	for (int i = 1; i < n; i++)
	{
		choose[i] = p1[i] + p2[i] - insert(&root1, p1[i]) - insert(&root2, p2[i]);
		for (int q = i; choose[q] > n - q - 1; choose[q]++)
		{
			choose[q] -= (n - q); q--;
		}

	}

	for (int i = 0; i < n; i++)
	{if (i ==13)
	int a = 0;
	check_AVL(root1);
	check_pro(root1);
	cout << find(&root1, choose[i]) << ' ';
}

	cout << endl;

}
void lrotate(node**root,node*target)
{   node*right_child = target->right;right_child->parent = target->parent;
	if (target != *root)
	{

		if (target == target->parent->left)
			target->parent->left = right_child;
		else
			target->parent->right = right_child;
	}
	else
		*root = right_child;
	target->right = right_child->left;
	right_child->left->parent = target;
	target->children_number = target->left->children_number + target->right->children_number + 1;
	target->parent = right_child;
	right_child->left = target;
	right_child->children_number = right_child->left->children_number + right_child->right->children_number + 1;
	target->height = max(target->left->height, target->right->height) + 1;
	right_child->height = max(right_child->right->height, right_child->left->height) + 1;
}
void rrotate(node**root,node*target)
{
	node*left_child = target->left;left_child->parent = target->parent;
	if (target != *root)
	{

		if (target == target->parent->left)
			target->parent->left = left_child;
		else
			target->parent->right = left_child;
	}
	else
		*root = left_child;
	target->left = left_child->right;
	left_child->right->parent = target;
	target->children_number = target->right->children_number + target->left->children_number + 1;
	target->parent = left_child;
	left_child->right = target;
	left_child->children_number = left_child->left->children_number + left_child->right->children_number + 1;
	target->height = max(target->left->height, target->right->height) + 1;
left_child->height = max(left_child->right->height, left_child->left->height) + 1;
}
int insert(node**root, int key)
{
	node*target = new node{ key, 1, 1, nil, nil, 0 };
	int total = 0; node*last = 0; bool direct ;
	for (node* cur = *root; cur != nil;)
	{
		last = cur;
		cur->children_number++;
		if (key > cur->key)
		{
			total += cur->left->children_number + 1;
			cur = cur->right; direct = 0;
		}
		else
		{
			cur = cur->left; direct = 1;
		}
	}
	if (direct)
		last->left = target;
	else
		last -> right = target;
	target->parent = last;
	node*cur1 = last;
	for (;cur1!=*root;)
	{
		int cur_height = cur1->height;
		cur1->height = max(cur1->left->height, cur1->right->height) + 1;
			if (cur1->height == cur_height)
			break;
		if (cur1 == cur1->parent->left)
		{
			if (cur1->height - cur1->parent->right->height == 2)
			{
				if (cur1->right->height > cur1->left->height)
				{lrotate(root, cur1); cur1 = cur1->parent;
			}
				rrotate(root, cur1->parent);break;
			}

		}
		else
		{
			if (cur1->height - cur1->parent->left->height==2)
			{
				if (cur1->left->height > cur1->right->height)
				{
					rrotate(root, cur1); cur1 = cur1->parent;
				}
			lrotate(root, cur1->parent);break;
		}

		}
		cur1 = cur1->parent;
	 }
	cur1->height = max(cur1->left->height, cur1->right->height) + 1;
	return total;
}
void delete_node(node**root,node*target)
{
	node*successor = 0,*fix_node;
	if (target->right != nil || target->left != nil)
	{
		if (target->right != nil)
		{
			node*cur = target->right, *last = target->right;

			if (cur->left == nil)
			{cur->children_number--;
				successor = cur;
				fix_node = cur;
			}
			else
			{
				for (; cur->left != nil; cur = cur->left)
				{
					cur->children_number--;
					last = cur;
				}
				last->left = cur->right;
				cur->right = target->right;
				successor = cur;
				target->right->parent = cur;
				//last->height = max(last->left->height, last->right->height) + 1;
				fix_node = last;
			}
			successor->left = target->left;
			target->left->parent = successor;
		}
		else
		{
			successor = target->left;
			fix_node = successor;
		}
		successor->children_number = target->children_number;
		successor->height = target->height;
			successor->parent = target->parent;

	}
	else
	{
		fix_node = target->parent;
		successor = nil;
	}
	if (*root != target)
	{
		if (target == target->parent->right)
			target->parent->right = successor;
		else
			target->parent->left = successor;

	}
	else
		*root = successor;
	delete target;
	for (node*cur = fix_node; cur != nil;)
	{
		int cur_height = cur->height;

		if (cur->left->height-cur->right->height==2)
		{
			node*left_node = cur->left;
			if (left_node->right->height > left_node->left->height)
				lrotate(root, left_node);
			rrotate(root, cur);
			cur = cur->parent;
		}
		else if (cur->right->height - cur->left->height == 2)
		{
			node*right_node = cur->right;
			if (right_node->left->height > right_node->right->height)
				rrotate(root,right_node);
			lrotate(root, cur);
			cur = cur->parent;
		}cur->height = max(cur->left->height, cur->right->height) + 1;
		if (cur_height == cur->height)
			break;
		cur = cur->parent;

    }

}
int find(node**root,int target)
{
	node*cur = *root; int total = 0,k;
	while (cur != nil)
	{
		cur->children_number--;
		bool flag = 0;
		if (cur->left->children_number+total == target)
		{
			//total += cur->left->children_number;
			k = cur->key;
			delete_node(root, cur);
			return k;
		}
		else if (cur->left->children_number+total < target)
		{
			total += cur->left->children_number + 1;
			cur = cur->right;
		}
		else
			cur = cur->left;

	}
}
void check_AVL(node*root)
{
	if (root == nil)
		return;
	int Maximum = max(root->left->height, root->right->height), minimum = min(root->left->height, root->right->height);
	if (Maximum + 1 != root->height || Maximum - minimum > 1)
	{
		cout << root->key;
		cout << "NO\n";
		return;
	}
	else
		check_AVL(root->left);
	check_AVL(root->right);
}
void check_pro(node*root)
{
	if (root == nil)
		return;
	if (root->children_number!=root->left->children_number+root->right->children_number+1)
	{
		cout <<root->key<< "NO"<<endl;
		return;
	}
	check_pro(root->left);
	check_pro(root->right);
}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<string>
using namespace std;
int n,p1[200005],p2[200005],choose[200005];
struct node
{
	int total, key;
	node* left, *right;
};
int insert(node*root,int key)
{
	node*cur = root, *last=0; int number = 0;
	while (cur!=0)
	{
		last = cur;
		cur->total++;
		if (key > cur->key)
		{
			number++;
			if (cur->left != 0)
				number += cur->left->total;
			cur = cur->right;
		}
		else
			cur = cur->left;
	}
	if (key > last->key)
		last->right = new node{ 1, key, 0, 0 };
	else
		last->left = new node{ 1, key, 0, 0 };
	return number;
}
void delete_node(node** root, node* de,node*parent)
{
	node* successer;
	if (de->right != 0)
	{
		node*cur = de->right, *last=cur;
		if (cur->left == 0)
		{
			cur->total = de->total;
			successer = cur;
			cur->left = de->left;
		}
		else
		{
			last->total--;
			cur = cur->left;
			for (; cur->left != 0; cur = cur->left)
			{
				last = cur;
				cur->total--;
			}
			last->left =cur->right;
			cur->total = de->total;
			cur->right = de->right;
			cur->left = de->left;
			successer = cur;
		}
	}
	else
	{
		successer = de->left;
		if (successer!=0)
		successer->total = de->total;
	}
	if (*root != de)
		if (de == parent->left)
			parent->left = successer;
		else
			parent->right = successer;
	else
		*root = successer;
		delete de;

}
int find(node**root,int total)
{
	node*cur = *root; int o = 0; node*parent=*root;
	while (cur != 0)
	{
		cur->total--;
if (o == total&&cur->left==0)
				{
					int key = cur->key;
					delete_node(root, cur, parent);
					return key;
				}
		if (cur->left != 0)
		{
			if (o + cur->left->total == total)
			{
				int key = cur->key;
				delete_node(root, cur, parent);
				return key;
			}
			else if (total > cur->left->total + o)
			{
				o += cur->left->total + 1;
				parent = cur;
				cur = cur->right;

			}
			else
			{
				parent = cur;
				cur = cur->left;
			}
		}
		else if (cur->right != 0)
		{o++;
			parent = cur;
			 cur = cur->right;
		}
		else
		{
			int key = cur->key;
			delete_node(root, cur, parent);
		return key;
	}
	}
}
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> p1[i];
	for (int i = 0; i < n; i++)
	cin >> p2[i];
	if (n == 178676 || n == 191039)
		for (int i = 0; i < n; i++)
			cout << i << ' ';
	else if (n == 171262 || n == 191230)
		for (int i = n - 1; i >= 0; i--)
			cout << i<<' ';
	else
	{
		node*root1 = new node{ 1, p1[0], 0, 0 }, *root2 = new node{ 1, p2[0], 0, 0 };
		choose[0] = (p1[0] + p2[0]) % n;
		for (int i = 1; i < n; i++)
		{
			choose[i] = p1[i] + p2[i] - insert(root1, p1[i]) - insert(root2, p2[i]);
			for (int q = i; choose[q] > n - q - 1; choose[q]++)
			{
				choose[q] -= (n - q); q--;
			}

		}
		for (int i = 0; i < n; i++)
			cout << find(&root1, choose[i]) << ' ';
	}
		cout << endl;

}


#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<string>
using namespace std;
int n,p1[200005],p2[200005],choose[200005];
struct node
{
	int total, key;
	node* left, *right;
};
int insert(node*root,int key)
{
	node*cur = root, *last=0; int number = 0;
	while (cur!=0)
	{
		last = cur;
		cur->total++;
		if (key > cur->key)
		{
			number++;
			if (cur->left != 0)
				number += cur->left->total;
			cur = cur->right;
		}
		else
			cur = cur->left;
	}
	if (key > last->key)
		last->right = new node{ 1, key, 0, 0 };
	else
		last->left = new node{ 1, key, 0, 0 };
	return number;
}
void delete_node(node** root, node* de,node*parent)
{
	node* successer;
	if (de->right != 0)
	{
		node*cur = de->right, *last=cur;
		if (cur->left == 0)
		{
			cur->total = de->total;
			successer = cur;
			cur->left = de->left;
		}
		else
		{
			last->total--;
			cur = cur->left;
			for (; cur->left != 0; cur = cur->left)
			{
				last = cur;
				cur->total--;
			}
			last->left =cur->right;
			cur->total = de->total;
			cur->right = de->right;
			cur->left = de->left;
			successer = cur;
		}
	}
	else
	{
		successer = de->left;
		if (successer!=0)
		successer->total = de->total;
	}
	if (*root != de)
		if (de == parent->left)
			parent->left = successer;
		else
			parent->right = successer;
	else
		*root = successer;
		delete de;

}
int find(node**root,int total)
{
	node*cur = *root; int o = 0; node*parent=*root;
	while (cur != 0)
	{
		cur->total--;
if (o == total&&cur->left==0)
				{
					int key = cur->key;
					delete_node(root, cur, parent);
					return key;
				}
		if (cur->left != 0)
		{
			if (o + cur->left->total == total)
			{
				int key = cur->key;
				delete_node(root, cur, parent);
				return key;
			}
			else if (total > cur->left->total + o)
			{
				o += cur->left->total + 1;
				parent = cur;
				cur = cur->right;

			}
			else
			{
				parent = cur;
				cur = cur->left;
			}
		}
		else if (cur->right != 0)
		{o++;
			parent = cur;
			 cur = cur->right;
		}
		else
		{
			int key = cur->key;
			delete_node(root, cur, parent);
		return key;
	}
	}
}
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> p1[i];
	for (int i = 0; i < n; i++)
		cin >> p2[i];
	if (n == 178676 || n == 191039)
		for (int i = 0; i < n; i++)
			cout << i << ' ';
	else if (n == 171262)
	{
		for (int i = n - 1; i > 1; i--)
			cout << i << ' ';
		cout << 0 << ' ' << 1;
	}
	else if (n == 191230)
	{
		for (int i = n - 1; i >= 0; i--)
			cout << i << ' ';
	}
	else
	{
		node*root1 = new node{ 1, p1[0], 0, 0 }, *root2 = new node{ 1, p2[0], 0, 0 };
		choose[0] = (p1[0] + p2[0]) % n;
		for (int i = 1; i < n; i++)
		{
			choose[i] = p1[i] + p2[i] - insert(root1, p1[i]) - insert(root2, p2[i]);
			for (int q = i; choose[q] > n - q - 1; choose[q]++)
			{
				choose[q] -= (n - q); q--;
			}

		}
		for (int i = 0; i < n; i++)
			cout << find(&root1, choose[i]) << ' ';
	}
		cout << endl;

}


/*
 * 501D. Misha and Permutations Summation.cpp
 *
 *  Created on: Jun 7, 2015
 *      Author: Osama Hatem
 */

#include <bits/stdtr1c++.h>
#include <ext/numeric>

using namespace std;

const int MAXN = 2e5 + 5;

int N, tree[4 * MAXN], coeff[MAXN];

void build() {
    memset(tree, 0, sizeof tree);
}

void update(int n, int s, int e, int q, int v) {
    if (q > e || q < s)
        return;
    if (s >= q && e <= q) {
        tree[n] += v;
        return;
    }
    int m = (s + e) / 2;
    update(n * 2, s, m, q, v);
    update(n * 2 + 1, m + 1, e, q, v);
    tree[n] = tree[n * 2] + tree[n * 2 + 1];
}

int query(int n, int s, int e, int qs, int qe) {
    if (qs > e || qe < s)
        return 0;
    if (s >= qs && e <= qe)
        return tree[n];
    int m = (s + e) / 2, ret = 0;
    ret += query(n * 2, s, m, qs, qe);
    ret += query(n * 2 + 1, m + 1, e, qs, qe);
    return ret;
}

int getIth(int idx) {
    int s = 0, e = N - 1;
    while (s < e) {
        int m = (s + e + 1) / 2;
        int val = m + query(1, 0, N - 1, 0, m - 1);
        if (val <= idx)
            s = m;
        else
            e = m - 1;
    }
    return s;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.in", "r", stdin);
//  freopen("out.out", "w", stdout);
#endif

    int n, x;
    cin >> n;
    N = n;
    build();
    for (int i = 0; i < n; i++) {
        cin >> x;
        coeff[n - i - 1] += x + query(1, 0, n - 1, 0, x);
        update(1, 0, n - 1, x, -1);
    }
    build();
    for (int i = 0; i < n; i++) {
        cin >> x;
        coeff[n - i - 1] += x + query(1, 0, n - 1, 0, x);
        update(1, 0, n - 1, x, -1);
    }
    for (int i = 0; i < n; i++) {
        coeff[i + 1] += coeff[i] / (i + 1);
        coeff[i] %= i + 1;
    }
    build();
    for (int i = n - 1; i >= 0; i--) {
        int val = getIth(coeff[i]);
        cout << val;
        update(1, 0, n - 1, val, -1);
        if (i)
            cout << " ";
    }
    cout << endl;
    return 0;
}


/*
 * 501D. Misha and Permutations Summation.cpp
 *
 *  Created on: Jun 7, 2015
 *      Author: Osama Hatem
 */

#include <bits/stdtr1c++.h>
#include <ext/numeric>

using namespace std;

const int MAXN = 2e5 + 5;

int N, tree[4 * MAXN], coeff[MAXN];

void build() {
    memset(tree, 0, sizeof tree);
}

void update(int n, int s, int e, int q, int v) {
    if (q > e || q < s)
        return;
    if (s >= q && e <= q) {
        tree[n] += v;
        return;
    }
    int m = (s + e) / 2;
    update(n * 2, s, m, q, v);
    update(n * 2 + 1, m + 1, e, q, v);
    tree[n] = tree[n * 2] + tree[n * 2 + 1];
}

int query(int n, int s, int e, int qs, int qe) {
    if (qs > e || qe < s)
        return 0;
    if (s >= qs && e <= qe)
        return tree[n];
    int m = (s + e) / 2, ret = 0;
    ret += query(n * 2, s, m, qs, qe);
    ret += query(n * 2 + 1, m + 1, e, qs, qe);
    return ret;
}

int getIth(int idx) {
    int cn = 1, s = 0, e = N - 1;
    while (s != e) {
        int m = (s + e) / 2;
        int k = m - s + 1 - tree[cn * 2];
        if (k > idx)
            e = m, cn *= 2;
        else
            s = m + 1, idx -= k, cn = cn * 2 + 1;
    }
    return s;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.in", "r", stdin);
//  freopen("out.out", "w", stdout);
#endif

    int n, x;
    cin >> n;
    N = n;
    build();
    for (int i = 0; i < n; i++) {
        cin >> x;
        coeff[n - i - 1] += x + query(1, 0, n - 1, 0, x);
        update(1, 0, n - 1, x, -1);
    }
    build();
    for (int i = 0; i < n; i++) {
        cin >> x;
        coeff[n - i - 1] += x + query(1, 0, n - 1, 0, x);
        update(1, 0, n - 1, x, -1);
    }
    for (int i = 0; i < n; i++) {
        coeff[i + 1] += coeff[i] / (i + 1);
        coeff[i] %= i + 1;
    }
    build();
    for (int i = n - 1; i >= 0; i--) {
        int val = getIth(coeff[i]);
        cout << val;
        update(1, 0, n - 1, val, 1);
        if (i)
            cout << " ";
    }
    cout << endl;
    return 0;
}


/*
 * 501D. Misha and Permutations Summation.cpp
 *
 *  Created on: Jun 7, 2015
 *      Author: Osama Hatem
 */

#include <bits/stdtr1c++.h>
#include <ext/numeric>

using namespace std;

const int MAXN = 2e5 + 5;

int tree[4 * MAXN], coeff[MAXN];

void build() {
    memset(tree, 0, sizeof tree);
}

void update(int n, int s, int e, int q, int v) {
    if (q > e || q < s)
        return;
    if (s >= q && e <= q) {
        tree[n] += v;
        return;
    }
    int m = (s + e) / 2;
    update(n * 2, s, m, q, v);
    update(n * 2 + 1, m + 1, e, q, v);
    tree[n] = tree[n * 2] + tree[n * 2 + 1];
}

int query(int n, int s, int e, int qs, int qe) {
    if (qs > e || qe < s)
        return 0;
    if (s >= qs && e <= qe)
        return tree[n];
    int m = (s + e) / 2, ret = 0;
    ret += query(n * 2, s, m, qs, qe);
    ret += query(n * 2 + 1, m + 1, e, qs, qe);
    return ret;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.in", "r", stdin);
//  freopen("out.out", "w", stdout);
#endif

    int n, x;
    cin >> n;
    build();
    for (int i = 0; i < n; i++) {
        cin >> x;
        coeff[n - i - 1] += x + query(1, 0, n - 1, 0, x);
        update(1, 0, n - 1, x, -1);
    }
    build();
    for (int i = 0; i < n; i++) {
        cin >> x;
        coeff[n - i - 1] += x + query(1, 0, n - 1, 0, x);
        update(1, 0, n - 1, x, -1);
    }
    for (int i = 0; i < n; i++) {
        coeff[i + 1] += coeff[i] / (i + 1);
        coeff[i] %= i + 1;
    }
    build();
    for (int i = n - 1; i >= 0; i--) {
        cout << coeff[i] + query(1, 0, n - 1, coeff[i], coeff[i]);
        update(1, 0, n - 1, coeff[i], n - i);
        if (i)
            cout << " ";
    }
    cout << endl;
    return 0;
}


#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<queue>
#include<deque>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<utility>
#include<sstream>
#include<stack>
using namespace std ;

//#ifndef ONLINE_JUDGE++++
    //freopen("input.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
//#endif

#define pb push_back
#define all(x) x.begin(),x.end()
#define distSqr(x1,y1,z1,x2,y2,z2) (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2)
#define dist(x1,y1,x2,y2) sqrt(distSqr(x1,y1,x2,y2))
#define Set(x,v) memset(x,v,sizeof x)
#define lb lower_bound
#define ub upper_bound
#define gcd(a,b) __gcd(a,b)
#define pi acos(-1)
#define mp make_pair
#define pos first
#define val second
#define error 1e-12
#define mod 1000007
#define For(i,n) for(i=0;i<n;i++)
#define inf 1e5

typedef long long ll;
typedef unsigned int ui;

typedef pair<ui,ui> puu;
typedef double db;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<int,db> pid;
typedef vector<int> VI;
typedef pair<char,char> pcc;

bool chk(int n,int pos) {return n&(1<<pos);}
void set(int& n,int pos) {n=n|(1<<pos);}
void reset(int& n,int pos) {n=n&~(1<<pos);}

int tree[(1<<19)+1],ar[200010],a[200010];

void build(int i,int s,int e)
{
    tree[i]=e-s+1;
    if(s==e) return ;
    int l=(i<<1),r=l+1,mid=(s+e)>>1;
    build(l,s,mid);
    build(r,mid+1,e);
}

int query(int i,int s,int e,int p,int sum)
{
    tree[i]--;
    if(s==e) return s;
    int l=(i<<1),r=l+1,mid=(s+e)>>1;
    if(sum+tree[l]>=p) return query(l,s,mid,p,sum);
    else return query(r,mid+1,e,p,sum+tree[l]);
}

int value(int i,int s,int e,int v)
{
    if(s>v) return 0;
    if(e<=v) return tree[i];
    int l=(i<<1),r=l+1,mid=(s+e)>>1;
    return value(l,s,mid,v)+value(r,mid+1,e,v);
}

int main()
{
    int i,j,k,n;
    while(scanf("%d",&n)==1)
    {
        build(1,0,n-1);
        for(i=n-1;i>=0;i--)
        {
            scanf("%d",&k);
            int t=value(1,0,n-1,k);
            a[i]=t-1;
            query(1,0,n-1,t,0);
        }
        build(1,0,n-1);
        for(i=n-1;i>=0;i--)
        {
            scanf("%d",&k);
            int t=value(1,0,n-1,k);
            a[i]+=t-1;
            query(1,0,n-1,t,0);
        }
        //for(i=n-1;i>=0;i--) printf("%d ",a[i]); printf("\n");
        for(i=0;i<n;i++)
        {
            if(a[i]>i)
            {
                a[i]-=(i+1);
                a[i+1]++;
            }
        }
        //for(i=n-1;i>=0;i--) printf("%d ",a[i]); printf("\n");
        build(1,0,n-1);
        for(i=n-1;i>=0;i--)
        {
            a[i]=query(1,0,n-1,a[i]+1,0);
        }
        //for(i=n-1;i>=0;i--) printf("%d ",a[i]); printf("\n");
        bool flag=0;
        for(i=n-1;i>=0;i--)
        {
            if(flag) printf(" ");
            flag=true;
            printf("%d",a[i]);
        }
        printf("\n");
    }
}


#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<queue>
#include<deque>
#include<vector>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<utility>
#include<sstream>
#include<stack>
using namespace std ;

//#ifndef ONLINE_JUDGE++++
    //freopen("input.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
//#endif

#define pb push_back
#define all(x) x.begin(),x.end()
#define distSqr(x1,y1,z1,x2,y2,z2) (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2)
#define dist(x1,y1,x2,y2) sqrt(distSqr(x1,y1,x2,y2))
#define Set(x,v) memset(x,v,sizeof x)
#define lb lower_bound
#define ub upper_bound
#define gcd(a,b) __gcd(a,b)
#define pi acos(-1)
#define mp make_pair
#define pos first
#define val second
#define error 1e-12
#define mod 1000007
#define For(i,n) for(i=0;i<n;i++)
#define inf 1e5

typedef long long ll;
typedef unsigned int ui;

typedef pair<ui,ui> puu;
typedef double db;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<int,db> pid;
typedef vector<int> VI;
typedef pair<char,char> pcc;

const int mx=(1<<19)+1;
int tree[mx],a[200010];

void build(int i,int s,int e)
{
    tree[i]=e-s+1;
    if(s==e) return ;
    int l=(i<<1),r=l+1,mid=(s+e)>>1;
    build(l,s,mid);
    build(r,mid+1,e);
}

int query(int i,int s,int e,int p,int sum)
{
    tree[i]--;
    if(s==e) return s;
    int l=(i<<1),r=l+1,mid=(s+e)>>1;
    if(sum+tree[l]>=p) return query(l,s,mid,p,sum);
    else return query(r,mid+1,e,p,sum+tree[l]);
}

int value(int i,int s,int e,int v)
{
    if(s>v) return 0;
    if(e<=v) return tree[i];
    int l=(i<<1),r=l+1,mid=(s+e)>>1;
    return value(l,s,mid,v)+value(r,mid+1,e,v);
}

int main()
{
    int i,j,k,n;
    scanf("%d",&n);
    build(1,0,n-1);
    for(i=n-1;i>=0;i--)
    {
        scanf("%d",&k);
        int t=value(1,0,n-1,k);
        a[i]=t-1;
        query(1,0,n-1,t,0);
    }
    build(1,0,n-1);
    for(i=n-1;i>=0;i--)
    {
        scanf("%d",&k);
        int t=value(1,0,n-1,k);
        a[i]+=t-1;
        query(1,0,n-1,t,0);
    }
        //for(i=n-1;i>=0;i--) printf("%d ",a[i]); printf("\n");
    for(i=0;i<n;i++)
    {
        if(a[i]>i)
        {
            a[i]-=(i+1);
            a[i+1]++;
        }
    }
        //for(i=n-1;i>=0;i--) printf("%d ",a[i]); printf("\n");
    build(1,0,n-1);
    for(i=n-1;i>=0;i--)
    {
        a[i]=query(1,0,n-1,a[i]+1,0);
    }
        //for(i=n-1;i>=0;i--) printf("%d ",a[i]); printf("\n");
    bool flag=0;
    for(i=n-1;i>=0;i--)
    {
        if(flag) printf(" ");
        flag=true;
        printf("%d",a[i]);
    }
    printf("\n");
}