首页 > 其他分享 >小米 Online Judge TCO 预选赛 Rectangle [离散化+二维前缀和]

小米 Online Judge TCO 预选赛 Rectangle [离散化+二维前缀和]

时间:2023-05-31 10:06:25浏览次数:41  
标签:int ll ans long TCO Online Judge mx mod


题目链接:https://code.mi.com/problem/list/view?id=151&cid=13

 

解题思路:

首先将x轴和y轴坐标离散化,然后就可以用二维前缀和求得每个格子被覆盖了几次,然后就可以求出每个格子的贡献,最后将总的贡献和乘以总的方案数的逆元即可。

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int,int> pa; 
const int mx = 1e3 + 10;
const int mod = 1000000007;
int n,x[mx],y[mx],f[mx][mx];
pa a[mx];
ll qpow(ll x,ll y){
	ll ans = 1;
	while(y){
		if(y&1) ans = ans*x%mod;
		x = x*x%mod;
		y >>= 1;
	}
	return ans;
}
int main(){
	scanf("%d",&n);
	int r , c;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&a[i].x,&a[i].y);	
		x[i] = a[i].x,y[i] = a[i].y;
	}
	sort(x+1,x+1+n);sort(y+1,y+1+n);
	r = unique(x+1,x+1+n) - x;
	c = unique(y+1,y+1+n) - y;
	for(int i=1;i<=n;i++){
		a[i].x = lower_bound(x+1,x+r,a[i].x) - x;
		a[i].y = lower_bound(y+1,y+c,a[i].y) - y;
	}
	for(int i=1;i<=n;i++){
		for(int j=i+1;j<=n;j++){
			int x1 = min(a[i].x,a[j].x);
			int x2 = max(a[i].x,a[j].x);
			int y1 = min(a[i].y,a[j].y);
			int y2 = max(a[i].y,a[j].y);
			f[x1][y1]++,f[x2][y2]++;
			f[x1][y2]--,f[x2][y1]--;
		}
	}
	ll ans = 0;
	for(int i=1;i<r-1;i++){
		for(int j=1;j<c-1;j++){
			f[i][j] += f[i-1][j] + f[i][j-1] - f[i-1][j-1];
			ll ret = 1ll*f[i][j]*(f[i][j]-1)/2%mod;
			ans += ret*(x[i+1]-x[i])%mod*(y[j+1]-y[j])%mod;
			ans %= mod;
		}
	}
	ll ret = n*(n-1)/2;
	printf("%lld\n",ans*qpow(ret*(ret-1)/2%mod,mod-2)%mod);
	return 0;
}

 

标签:int,ll,ans,long,TCO,Online,Judge,mx,mod
From: https://blog.51cto.com/u_12468613/6384501

相关文章

  • AtCoder Beginner Contest 258 Ex Odd Steps
    洛谷传送门AtCoder传送门不错的矩阵快速幂优化dp练习题。考虑一个朴素dp,\(f_i\)为组成和为\(i\)且用到的数都是奇数的方案数。有转移:\[f_i=\begin{cases}\sum\limits_{j=0}^{i-1}[i\bmod2\nej\bmod2]f_j&i\notinA\\0&i\inA\end{cases}\]考虑前......
  • AtCoder Beginner Contest 289(E,F)
    AtCoderBeginnerContest289(E,F)E(dijkstra)E这个题的大意就是有两个人,一个人在点\(1\),一个人在点\(n\),现在两个人要同时走(题目给出了点和边,还有每一个点的颜色),但是他们的下一步要到的点需要是颜色不同的,问\(1\)点出发的和\(n\)点出发的同时达到对方的起点的最短路径时哪......
  • AtCoder Beginner Contest 303
    A-SimilarString(abc303a)题目大意给定两个字符串,问这两个字符串是否相似。两个字符串相似,需要每个字母,要么完全相同,要么一个是1一个是l,要么一个是0一个是o解题思路按照题意模拟即可。可以将全部1换成l,全部0换成o,再判断相等。神奇的代码#include<bits/stdc++.h>us......
  • AtCoder Regular Contest 153 D Sum of Sum of Digits
    洛谷传送门AtCoder传送门又浪费一道好题我首先想的是,\(x\)显然最优取某些\(a_i\)往前进位时的值。然后就误以为\(x\)的取值是\(O(n\log_{10}V)\)的了……既然没发现什么性质,那就直接dp吧!设\(f_{d,i}\)为从低到高前\(d\)位,所有\(a_i\)进位之和为\(i\)。然......
  • leetcode 693. Binary Number with Alternating Bits
    Givenapositiveinteger,checkwhetherithasalternatingbits:namely,iftwoadjacentbitswillalwayshavedifferentvalues.Example1:Input:5Output:TrueExplanation:Thebinaryrepresentationof5is:101Example2:Input:7Output:FalseExplanation......
  • leetcode 637. Average of Levels in Binary Tree
    Givenanon-emptybinarytree,returntheaveragevalueofthenodesoneachlevelintheformofanarray.Example1:Input:3/\920/\157Output:[3,14.5,11]Explanation:Theaveragevalueofnodesonlevel0is3,onlevel......
  • leetcode 496. Next Greater Element I
    Youaregiventwoarrays(withoutduplicates)nums1andnums2wherenums1’selementsaresubsetofnums2.Findallthenextgreaternumbersfornums1'selementsinthecorrespondingplacesofnums2.TheNextGreaterNumberofanumberxinnums1isth......
  • leetcode 463. Island Perimeter
    Youaregivenamapinformofatwo-dimensionalintegergridwhere1representslandand0representswater.Gridcellsareconnectedhorizontally/vertically(notdiagonally).Thegridiscompletelysurroundedbywater,andthereisexactlyoneisland(i......
  • leetcode 682. Baseball Game
    You'renowabaseballgamepointrecorder.Givenalistofstrings,eachstringcanbeoneofthe4followingtypes:Integer(oneround'sscore):Directlyrepresentsthenumberofpointsyougetinthisround."+"(oneround'sscor......
  • leetcode 566. Reshape the Matrix
    InMATLAB,thereisaveryusefulfunctioncalled'reshape',whichcanreshapeamatrixintoanewonewithdifferentsizebutkeepitsoriginaldata.You'regivenamatrixrepresentedbyatwo-dimensionalarray,andtwopositiveintegersr......