记录编号 136224 评测结果 AAAAAAAAAA
题目名称 [NOIP 2013]火柴排队 最终得分 100
用户昵称 GravatarAsm.Def 是否通过 通过
代码语言 C++ 运行时间 0.076 s
提交时间 2014-11-02 17:30:51 内存使用 2.20 MiB
显示代码纯文本
#include <cstdio>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>

#if defined DEBUG
FILE *in = fopen("temp", "r");
#define out stdout
#else
FILE *in = fopen("MatchNOIP2013.in", "r");
FILE *out = fopen("MatchNOIP2013.out", "w");
#endif

inline void getint(int &x){
	char c = fgetc(in);
	while(!isdigit(c))c = fgetc(in);
	x = c - '0';
	while(isdigit(c = fgetc(in)))x = x * 10 - '0' + c;
}
typedef long long LL;
using namespace std;
inline int lowbit(int x){return x & -x;}
/*=====================================*/
const int maxn = 100000 + 5, mod = 99999997;
int n, A[maxn], B[maxn], t1[maxn], t2[maxn], ans = 0;

inline bool tmpa(int a, int b){return A[a] < A[b];}
inline bool tmpb(int a, int b){return B[a] < B[b];}

//namespace Fenwick{
	int Arr[maxn] = {0};
	inline void insert(int x){
		++Arr[x];
		int i = x + lowbit(x);
		while(i <= n){
			++Arr[i];
			i += lowbit(i);
		}
	}
	inline int getS(int x){
		int ans = Arr[x], i = x ^ lowbit(x);
		while(i){
			ans += Arr[i];
			i ^= lowbit(i);
		}
		return ans;
	}
//}//Fenwick
//using Fenwick::getS;
//using Fenwick::insert;

inline void work(){
	getint(n);
	int i, ord[maxn];
	for(i = 1;i <= n;++i)
		getint(A[i]), t1[i] = i;
	for(i = 1;i <= n;++i)
		getint(B[i]), t2[i] = i;
	sort(t1 + 1, t1 + n + 1, tmpa);
	sort(t2 + 1, t2 + n + 1, tmpb);
	for(i = 1;i <= n;++i)
		ord[t1[i]] = t2[i];
	for(i = n;i;--i){
		ans = (ans + getS(ord[i])) % mod;
		insert(ord[i]);
	}
	fprintf(out, "%d\n", (ans + mod) % mod);
}

int main(){
	
	work();
	
	return 0;
}