【BZOJ 2738】矩阵乘法

题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2738

这个题,真的是妙啊!
整体二分看起来很好用的样子!

#include<bits/stdc++.h>
#define LL long long
#define abs(x) ((x)>0?(x):-(x))
using namespace std;

const int N = 500+9;
const int M = 250000+9;
const int Q = 60000+9;

struct Point{int val,x,y;inline bool operator < (const Point &B) const {return val < B.val;}}p[M];
struct Query{int k,x1,x2,y1,y2,id;}q[Q],buf[Q];

int n,m,vout[Q];

inline int read(){
	char c=getchar(); int ret=0,f=1;
	while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
	while (c<='9'&&c>='0') ret=ret*10+c-'0',c=getchar();
	return ret*f;
}

namespace Fenwick_Tree{
	#define BIT Fenwick_Tree
	#define lowbit(x) ((x)&-(x))
	int sum[N][N];
	
	inline void modify(int x, int y, int delta) {
		if (x <= 0 || y <= 0) return;
		for (int j=y;j<=n;j+=lowbit(j)) for (int i=x;i<=n;i+=lowbit(i))
			sum[i][j] += delta;
	}
	
	inline int query(int x, int y){
		if (x <= 0 || y <= 0) return 0;
		int ret = 0;
		for (int j=y;j;j-=lowbit(j)) for (int i=x;i;i-=lowbit(i))
			ret += sum[i][j];
		return ret;
	}
};

void solve(int l, int r, int L, int R) {
	if (l == r) for (int i=L;i<=R;i++) vout[q[i].id] = p[l].val;
	else {
		int mid = l + r + 1 >> 1,ls=L-1,rs=R+1;
		for (int i=l;i<=mid-1;i++) BIT::modify(p[i].x,p[i].y,1);
		for (int i=L,tmp;i<=R;i++) {
			tmp = BIT::query(q[i].x1-1,q[i].y1-1);
			tmp += BIT::query(q[i].x2,q[i].y2);
			tmp -= BIT::query(q[i].x1-1,q[i].y2);
			tmp -= BIT::query(q[i].x2,q[i].y1-1);
			if (tmp >= q[i].k) buf[++ls] = q[i];
			else q[i].k -= tmp, buf[--rs] = q[i];
		}
		memcpy(q+L,buf+L,sizeof(buf[0])*(R-L+1));
		for (int i=l;i<=mid-1;i++) BIT::modify(p[i].x,p[i].y,-1);
		if (L <= ls) solve(l,mid-1,L,ls);
		if (rs <= R) solve(mid,r,rs,R);
	}
} 

int main(){
	n = read(); m = read();
	for (int j=1,t=1;j<=n;j++) for (int i=1;i<=n;i++,t++) 
		p[t].x = i, p[t].y = j, p[t].val = read();
	for (int i=1,a,b,c,d,e,f;i<=m;i++) 
 		q[i].y1 = read(), q[i].x1 = read(),
		q[i].y2 = read(), q[i].x2 = read(),
		q[i].k = read(), q[i].id = i;
	sort(p+1,p+1+n*n);
	solve(1,n*n,1,m);
	for (int i=1;i<=m;i++) printf("%d\n",vout[i]);
	return 0;
}

27 thoughts to “【BZOJ 2738】矩阵乘法”

  1. An outstanding share! I’ve just forwarded this onto a coworker who
    had been conducting a little homework on this.
    And he in fact ordered me dinner because I stumbled upon it
    for him… lol. So let me reword this…. Thank YOU
    for the meal!! But yeah, thanks for spending the time to
    discuss this issue here on your web site.

  2. I blog often and I really thank you for your content.

    This article has really peaked my interest. I’m going to book mark your site and keep checking for new details about once a week.
    I opted in for your Feed as well.

  3. you are really a excellent webmaster. The web site loading velocity is incredible.
    It kind of feels that you are doing any unique trick.
    Also, The contents are masterpiece. you’ve performed a wonderful job in this topic!

  4. Just wish to say your article is as astonishing. The clarity on your submit is just cool and that i could
    suppose you are a professional on this subject.
    Well together with your permission allow me to seize
    your RSS feed to stay updated with impending post.
    Thanks a million and please carry on the gratifying work.

  5. My spouse and I stumbled over here from a different web address and
    thought I should check things out. I like
    what I see so now i’m following you. Look forward to going over your
    web page yet again.

  6. Hi there this is somewhat of off topic but I was wanting to know if blogs use WYSIWYG editors or
    if you have to manually code with HTML. I’m starting a blog soon but have no coding expertise so I wanted
    to get advice from someone with experience. Any help would be greatly appreciated!

  7. Whats Going down i am new to this, I stumbled upon this I’ve found It positively useful and it has aided me out loads. I’m hoping to contribute & help different users like its aided me. Good job.

  8. That is really fascinating, You’re a very skilled blogger.
    I have joined your feed and stay up for
    seeking more of your magnificent post. Additionally, I’ve shared your web site in my social networks

  9. Hey! I’m at work browsing your blog from my new iphone! Just wanted to say I love
    reading your blog and look forward to all your posts!
    Carry on the superb work!

  10. Do you mind if I quote a couple of your articles as long as I provide credit and sources back to your blog?
    My blog site is in the exact same niche as yours and my visitors would genuinely benefit from some of the information you present here.
    Please let me know if this ok with you. Appreciate it!

Leave a Reply

Your email address will not be published. Required fields are marked *