1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| #include<bits/stdc++.h> using namespace std; #ifdef LOCAL #define debug(...) fprintf(stdout, ##__VA_ARGS__) #else #define debug(...) void(0) #endif #define int long long inline int read(){int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+ (x<<3)+(ch^48);ch=getchar();}return x*f;} #define Z(x) (x)*(x) #define pb push_back #define fi first #define se second
#define mo (int)(1e9+7) #define N 100010 int pw(int a, int b) { int ans=1; while(b) { if(b&1) ans*=a; a*=a; b>>=1; ans%=mo; a%=mo; } return ans; } void Mod(int &a) { if(a>=mo || a<=-mo) a%=mo; if(a<0) a+=mo; } void Add(int &a, int b) { a+=b; Mod(a); } void Mul(int &a, int b) { Mod(b); a*=b; Mod(a); } struct Binary_tree { int cnt[N]; void mem() { memset(cnt, 0, sizeof(cnt)); } void add(int x) { while(x<N) cnt[x]++, x+=x&-x; } int que(int x) { int ans=0; while(x) ans+=cnt[x], x-=x&-x; return ans; } }Bin; struct node { int l, r; }a1[N], a2[N]; int n, m, i, j, k, T, p[N], ans, sum;
struct Merge_tree { int i, j, k, b[N]; #define par pair<int, int> priority_queue<par, vector<par>, greater<par> >q[N];
#undef par void init() { for(i=1; i<=n; ++i) b[i]=i; } void add(int x, int i, int y) { q[b[x]].push({y, i}); } void merge(int x, int y) {
int p = y; x = b[x]; y = b[y]; if(q[x].empty() && q[y].empty()) return ; if(q[x].size() > q[y].size()) swap(x, y);
pair<int, int> t; while(!q[x].empty()) t = q[x].top(), q[x].pop(), q[y].push(t); debug("Merging 3 ... %d \n", q[y].size()); b[p]=y; } pair<int, int> query(int x) { int p = x; x = b[x]; debug("query %d : %d\n", p, q[x].size()); if(q[x].empty()) return {-1, -1}; auto t = q[x].top(); q[x].pop(); if(p > t.fi) return {-1, -1}; return t; } };
int calc(node *a) { Merge_tree Mer; Mer.init(); for(i=1; i<=n; ++i) Mer.add(a[i].l, i, a[i].r); sum=1; for(i=1; i<=n; ++i) Mul(sum, a[i].r-a[i].l+1); debug("sum : %lld\n", sum); for(i=1; i<=n; ++i) { auto t = Mer.query(i); if(t.fi == -1) return 0; debug("Right Point : %lld\n", t.fi); p[t.se] = i; Mer.merge(i, t.fi+1); } Bin.mem(); ans=0; for(i=1; i<=n; ++i) ans+=Bin.que(p[i]), Bin.add(p[i]), debug("%lld ", ans); ; debug("\n"); for(i=1; i<=n; ++i) debug("%lld ", p[i]); debug("\n"); debug("NXD : %lld\n", ans); if(ans&1) ans=-1; else ans=1; debug("ans : %lld\n", ans); return ans * pw(sum, mo-2) % mo; }
signed main() { freopen("math.in", "r", stdin); freopen("math.out", "w", stdout); #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); #endif
T=read(); while(T--) { n=read(); for(i=1; i<=n; ++i) { a1[i].l=read(); a1[i].r=read(); a2[i].l=read(); a2[i].r=read(); } int s1 = calc(a1), s2 = calc(a2); debug("%lld %lld\n", s1, s2); ans = s1 * s2; Mod(ans); printf("%lld\n", ans); } return 0; }
|