http://www.tsinsen.com/A1333

题意:……

思路:和之前的第k小几乎一样,只不过把一维BIT换成二维BIT而已。注意二维BIT写法QAQ

 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <stack>
using namespace std;
#define INF 0x3f3f3f3f
#define N 350000
typedef long long LL;
struct P {
int x1, x2, y1, y2, val, id;
P () {}
P (int x1, int y1, int x2, int y2, int val, int id) : x1(x1), y1(y1), x2(x2), y2(y2), val(val), id(id) {}
} q[N], lq[N], rq[N];
int bit[][], ans[N], n; int lowbit(int x) { return x & (-x); } void update(int x, int y, int w) {
for(int i = x; i <= n; i += lowbit(i))
for(int j = y; j <= n; j += lowbit(j)) bit[i][j] += w;
} int query(int x, int y) {
int ans = ;
for(int i = x; i; i -= lowbit(i))
for(int j = y; j; j -= lowbit(j)) ans += bit[i][j];
return ans;
} void Solve(int lask, int rask, int l, int r) {
if(lask > rask || l > r) return ;
if(l == r) {
for(int i = lask; i <= rask; i++) if(q[i].id) ans[q[i].id] = l;
return ;
}
int mid = (l + r) >> , lcnt = , rcnt = ;
for(int i = lask; i <= rask; i++) {
if(!q[i].id) {
if(q[i].val <= mid) {
update(q[i].x1, q[i].y1, );
lq[++lcnt] = q[i];
} else rq[++rcnt] = q[i];
} else {
int num = query(q[i].x2, q[i].y2) - query(q[i].x1 - , q[i].y2) - query(q[i].x2, q[i].y1 - ) + query(q[i].x1 - , q[i].y1 - );
if(num >= q[i].val) lq[++lcnt] = q[i];
else {
q[i].val -= num;
rq[++rcnt] = q[i];
}
}
}
for(int i = ; i <= lcnt; i++) if(!lq[i].id) update(lq[i].x1, lq[i].y1, -);
for(int i = ; i <= lcnt; i++) q[lask+i-] = lq[i];
for(int i = ; i <= rcnt; i++) q[lask+lcnt+i-] = rq[i];
Solve(lask, lask + lcnt - , l, mid);
Solve(lask + lcnt, rask, mid + , r);
} int main() {
int m, cnt = , a;
scanf("%d%d", &n, &m);
memset(bit, , sizeof(bit));
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++) {
scanf("%d", &a); q[++cnt] = P(i, j, , , a, );
}
for(int i = ; i <= m; i++) {
++cnt; q[cnt].id = i;
scanf("%d%d%d%d%d", &q[cnt].x1, &q[cnt].y1, &q[cnt].x2, &q[cnt].y2, &q[cnt].val);
}
Solve(, cnt, , INF);
for(int i = ; i <= m; i++) printf("%d\n", ans[i]);
}

最新文章

  1. [windows]win10家庭版切换到管理员账户
  2. PacificA中的租约与失效检测解读
  3. 廖雪峰js教程笔记7 基本类型和包装类型
  4. cmd 打开gitshell
  5. arp:地址解析协议(Address Resolution Protocol)(来自维基百科)
  6. 我眼中的go的语法特点
  7. ubuntu14.04启动提示set_sw_state failed
  8. C语言基础学习基本数据类型-变量的命名
  9. NicEdit - WYSIWYG Content Editor, Inline Rich Text Application
  10. 获取本机IP(适用于Linux系统)
  11. [Swift]LeetCode703. 数据流中的第K大元素 | Kth Largest Element in a Stream
  12. Cocos Creator cc.Button (脚本事件内容)
  13. apache 基本vhost配置 【目的及过程】
  14. UVa 11059 - Maximum Product 最大乘积【暴力】
  15. Can’t call setState (or forceUpdate) on an unmounted component 警告处理方法
  16. 用MFC(C++)实现拼音搜索
  17. office系列调节背景主题
  18. 安卓APP动态调试-IDA实用攻略
  19. Android -- SDcard文件读取和保存
  20. PHP基本的语法的小结

热门文章

  1. 剑指offer替换空格
  2. zf-关于差旅报销的excel表单填写
  3. 词链(link)
  4. Android开源项目收集
  5. FZU Problem 2213 Common Tangents
  6. FZU Problem 2029 买票问题(树状数组)
  7. Python之路: socket篇
  8. Android5.1图库Gallery2代码分析数据加载流程
  9. HDU 1828 POJ 1177 Picture
  10. DEDECMS模板中dede标签使用php和if判断语句的方法