整体二分 WA 0 pts 求调
查看原帖
整体二分 WA 0 pts 求调
754310
Pepsee楼主2024/3/4 21:51

感觉没有什么问题的说qwq

#include <bits/stdc++.h>
#define ls k<<1
#define rs k<<1|1
using namespace std;
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;
}
void write(int x){
	if(x<0){putchar('-');x=-x;}
	if(x>9) write(x/10);
	putchar(x%10+'0');
}
int n,m,tot;
struct query{
	int x,y,x_,y_,k,id;
}que[60005],tmpl[60005],tmpr[60005];
int tr[505][505],ans[60005];
struct node{
	int v,x,y;
}a[250005];
bool cmp(node a,node b){
	return a.v < b.v;
}
void add(int x,int y,int a)
{
	for (; x <= 505; x += x&-x)
		for (; y <= 505; y += y&-y)
			tr[x][y] += a;
}
int query(int x,int y)
{
	int res = 0;
	for (; x; x -= x&-x)
		for (; y; y -= y&-y)
			res += tr[x][y];
	return res;
}
int get_sum(int x,int y,int x_,int y_){
	return query(x_,y_)-query(x_,y-1)-query(x-1,y_)+query(x-1,y-1);
}
void solve(int l,int r,int ql,int qr)
{
	if (l>r || ql>qr) return ;
	if (l == r){
		for (int i = ql; i <= qr; i++)
			ans[que[i].id] = a[l].v;
		return ;
	}
	int mid = (l+r) >> 1,totl = 0,totr = 0;
	for (int i = l; i <= mid; i++)
		add(a[i].x,a[i].y,1);
	for (int i = ql; i <= qr; i++)
	{
		int res = get_sum(que[i].x,que[i].y,que[i].x_,que[i].y_);
		if (que[i].k > res) que[i].k -= res,tmpr[++totr] = que[i];
		else tmpl[++totl] = que[i];
	}
	for (int i = 1; i <= totl; i++)
		que[ql+i-1] = tmpl[i];
	for (int i = 1; i <= totr; i++)
		que[ql+totl+i-1] = tmpr[i];
	for (int i = l; i <= mid; i++)
		add(a[i].x,a[i].y,-1);
	solve(l,mid,ql,ql+totl-1); solve(mid+1,r,ql+totl,qr);
}
signed main()
{
	n = read(); m = read();
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
		{
			++tot;
			a[tot].v = read()+1;
			a[tot].x = i; a[tot].y = j;			
		}
	sort(a+1,a+tot+1,cmp);
	for (int i = 1; i <= m; i++)
	{
		que[i].x = read(); que[i].y = read();
		que[i].x_ = read(); que[i].y_ = read();
		que[i].k = read(); que[i].id = i;
	}
	solve(1,tot,1,m);
	for (int i = 1; i <= m; i++)
		write(ans[i]-1),puts("");
	return 0;
}
2024/3/4 21:51
加载中...