BZOJ.2738.矩阵乘法(整体二分 二维树状数组)

题目链接 BZOJ
洛谷

整体二分。把求序列第K小的树状数组改成二维树状数组就行了。
初始答案区间有点大,离散化一下。

因为这题是一开始给点,之后询问,so可以先处理该区间值在l~mid的修改,再处理询问。即二分标准可以直接用点的标号。
结构体的赋值可以改为赋值操作的编号。(这样内存没那么连续?想多了你)

改了半下午,优化了500ms。。

//6980kb    10584ms 好慢啊QAQ
//4208ms    7.59MB
#include <cstdio>
#include <cctype>
#include <algorithm>
#define lb(x) ((x)&-(x))
//#define gc() getchar()
#define MAXIN 500000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
const int N=505,M=60005;int n,m,Ans[M],q[M],q1[M],q2[M];
char IN[MAXIN],*SS=IN,*TT=IN;
struct Point
{int x,y,val;Point() {}Point(int x,int y,int val):x(x),y(y),val(val) {}bool operator <(const Point &a)const{return val<a.val;}
}pt[N*N];
inline int read();
struct Operation//Query
{int K,x1,y1,x2,y2;inline void Input(){x1=read(),y1=read(),x2=read(),y2=read(),K=read();}
}op[M];inline int read()
{int now=0;register char c=gc();for(;!isdigit(c);c=gc());for(;isdigit(c);now=now*10+c-'0',c=gc());return now;
}
namespace T
{int n,t[N][N];inline void Modify(int x,int y,int v){for(int i=x; i<=n; i+=lb(i))for(int j=y; j<=n; j+=lb(j)) t[i][j]+=v;}inline void Clear(int x,int y){for(int i=x; i<=n; i+=lb(i))for(int j=y; j<=n; j+=lb(j))if(t[i][j]) t[i][j]=0; else break;}inline int Query(int x,int y){int res=0;for(int i=x; i; i^=lb(i))for(int j=y; j; j^=lb(j)) res+=t[i][j];return res;}inline int Query_Area(Operation q){//prefix sumreturn Query(q.x2,q.y2)-Query(q.x1-1,q.y2)-Query(q.x2,q.y1-1)+Query(q.x1-1,q.y1-1);}
}
void Solve(int l,int r,int h,int t)
{if(h>t) return;if(l==r){for(int i=h; i<=t; ++i) Ans[q[i]]/*[op[q[i]].pos]*/=pt[l].val;return;}int mid=l+r>>1, t1=0, t2=0;for(int i=l; i<=mid; ++i) T::Modify(pt[i].x,pt[i].y,1);for(int now,tmp,i=h; i<=t; ++i){now=q[i], tmp=T::Query_Area(op[now]);if(tmp>=op[now].K) q1[t1++]=now;else op[now].K-=tmp, q2[t2++]=now;}for(int i=l; i<=mid; ++i) T::Clear(pt[i].x,pt[i].y);for(int i=0; i<t1; ++i) q[h+i]=q1[i];for(int i=0; i<t2; ++i) q[h+t1+i]=q2[i];Solve(l,mid,h,h+t1-1), Solve(mid+1,r,h+t1,t);
}int main()
{T::n=n=read(), m=read();int tot=0;for(int i=1; i<=n; ++i)for(int j=1; j<=n; ++j) pt[++tot]=Point(i,j,read());std::sort(pt+1,pt+1+tot);for(int i=1; i<=m; ++i) q[i]=i, op[i].Input();Solve(1,tot,1,m);for(int i=1; i<=m; ++i) printf("%d\n",Ans[i]);return 0;
}

优化前:

//4680ms    17.75MB
#include <cstdio>
#include <cctype>
#include <algorithm>
#define lb(x) ((x)&-(x))
//#define gc() getchar()
#define MAXIN 60000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
const int N=505,M=60005+N*N;int n,m,Q,cnt,A[N*N],Ans[60005];
char IN[MAXIN],*SS=IN,*TT=IN;
struct Operation
{int K,x1,y1,x2,y2,pos;//K=0: Modify (x1,y1):=posOperation() {}Operation(int K,int x1,int y1,int x2,int y2,int pos):K(K),x1(x1),y1(y1),x2(x2),y2(y2),pos(pos) {}
}q[M],q1[M],q2[M];inline int read()
{int now=0;register char c=gc();for(;!isdigit(c);c=gc());for(;isdigit(c);now=now*10+c-'0',c=gc());return now;
}
namespace T
{int n,t[N][N];inline void Modify(int x,int y,int v){for(int i=x; i<=n; i+=lb(i))for(int j=y; j<=n; j+=lb(j)) t[i][j]+=v;}inline void Clear(int x,int y){for(int i=x; i<=n; i+=lb(i))for(int j=y; j<=n; j+=lb(j))if(t[i][j]) t[i][j]=0; else break;}inline int Query(int x,int y){int res=0;for(int i=x; i; i^=lb(i))for(int j=y; j; j^=lb(j)) res+=t[i][j];return res;}inline int Query_Area(Operation q){//prefix sumreturn Query(q.x2,q.y2)-Query(q.x1-1,q.y2)-Query(q.x2,q.y1-1)+Query(q.x1-1,q.y1-1);}
}
void Solve(int l,int r,int h,int t)
{if(h>t) return;if(l==r){for(int i=h; i<=t; ++i) if(q[i].K) Ans[q[i].pos]=A[l];return;}bool goon=0;for(int i=h; i<=t; ++i) if(q[i].K) {goon=1; break;}if(!goon) return;int mid=l+r>>1, midV=A[mid], t1=0, t2=0;for(int i=h; i<=t; ++i)if(q[i].K){int tmp=T::Query_Area(q[i]);//这样好像少做几次加法!但是多copy两个int。。(你够了→_→)if(tmp>=q[i].K) q1[t1++]=q[i];else q[i].K-=tmp, q2[t2++]=q[i];}else{if(q[i].pos<=midV) T::Modify(q[i].x1,q[i].y1,1), q1[t1++]=q[i];else q2[t2++]=q[i];}for(int i=0; i<t1; ++i) if(!q1[i].K) T::Clear(q1[i].x1,q1[i].y1);for(int i=0; i<t1; ++i) q[h+i]=q1[i];for(int i=0; i<t2; ++i) q[h+t1+i]=q2[i];Solve(l,mid,h,h+t1-1), Solve(mid+1,r,h+t1,t);
}int main()
{T::n=n=read(), m=read(), Q=0;for(int i=1; i<=n; ++i)for(int j=1; j<=n; ++j) q[++Q]=Operation(0,i,j,0,0,A[Q]=read());std::sort(A+1,A+1+Q), cnt=1;for(int i=2; i<=Q; ++i) if(A[i]!=A[i-1]) A[++cnt]=A[i];for(int x1,y1,x2,y2,i=1; i<=m; ++i)x1=read(),y1=read(),x2=read(),y2=read(),q[++Q]=Operation(read(),x1,y1,x2,y2,i);Solve(1,cnt,1,Q);for(int i=1; i<=m; ++i) printf("%d\n",Ans[i]);return 0;
}

转载于:https://www.cnblogs.com/SovietPower/p/9234335.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/389356.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

从数据库里读值往TEXT文本里写

/// <summary> /// 把预定内容导入到Text文档 /// </summary> private void ChangeDbToText() { this.RecNum.Visibletrue; //建立文件&#xff0c;并打开 string oneLine ""; string filename "Storage Card/YD" DateTime.Now.…

DengAI —如何应对数据科学竞赛? (EDA)

了解机器学习 (Understanding ML) This article is based on my entry into DengAI competition on the DrivenData platform. I’ve managed to score within 0.2% (14/9069 as on 02 Jun 2020). Some of the ideas presented here are strictly designed for competitions li…

Pytorch模型层简单介绍

模型层layers 深度学习模型一般由各种模型层组合而成。 torch.nn中内置了非常丰富的各种模型层。它们都属于nn.Module的子类&#xff0c;具备参数管理功能。 例如&#xff1a; nn.Linear, nn.Flatten, nn.Dropout, nn.BatchNorm2d nn.Conv2d,nn.AvgPool2d,nn.Conv1d,nn.Co…

有效沟通的技能有哪些_如何有效地展示您的数据科学或软件工程技能

有效沟通的技能有哪些What is the most important thing to do after you got your skills to be a data scientist? It has to be to show off your skills. Otherwise, there is no use of your skills. If you want to get a job or freelance or start a start-up, you ha…

java.net.SocketException: Software caused connection abort: socket write erro

场景&#xff1a;接口测试 编辑器&#xff1a;eclipse 版本&#xff1a;Version: 2018-09 (4.9.0) testng版本&#xff1a;TestNG version 6.14.0 执行testng.xml时报错信息&#xff1a; 出现此报错原因之一&#xff1a;网上有人说是testng版本与eclipse版本不一致造成的&#…

[博客..配置?]博客园美化

博客园搞定时间 -> 18年6月27日 [让我歇会儿 搞这个费脑子 代码一个都看不懂] 转载于:https://www.cnblogs.com/Steinway/p/9235437.html

使用K-Means对美因河畔法兰克福的社区进行聚类

介绍 (Introduction) This blog post summarizes the results of the Capstone Project in the IBM Data Science Specialization on Coursera. Within the project, the districts of Frankfurt am Main in Germany shall be clustered according to their venue data using t…

Pytorch损失函数losses简介

一般来说&#xff0c;监督学习的目标函数由损失函数和正则化项组成。(Objective Loss Regularization) Pytorch中的损失函数一般在训练模型时候指定。 注意Pytorch中内置的损失函数的参数和tensorflow不同&#xff0c;是y_pred在前&#xff0c;y_true在后&#xff0c;而Ten…

读取Mc1000的 唯一 ID 机器号

先引用Symbol.ResourceCoordination 然后引用命名空间 using System;using System.Security.Cryptography;using System.IO; 以下为类程序 /// <summary> /// 获取设备id /// </summary> /// <returns></returns> public static string GetDevi…

样本均值的抽样分布_抽样分布样本均值

样本均值的抽样分布One of the most important concepts discussed in the context of inferential data analysis is the idea of sampling distributions. Understanding sampling distributions helps us better comprehend and interpret results from our descriptive as …

玩转ceph性能测试---对象存储(一)

笔者最近在工作中需要测试ceph的rgw&#xff0c;于是边测试边学习。首先工具采用的intel的一个开源工具cosbench&#xff0c;这也是业界主流的对象存储测试工具。 1、cosbench的安装&#xff0c;启动下载最新的cosbench包wget https://github.com/intel-cloud/cosbench/release…

[BZOJ 4300]绝世好题

Description 题库链接 给定一个长度为 \(n\) 的数列 \(a_i\) &#xff0c;求 \(a_i\) 的子序列 \(b_i\) 的最长长度&#xff0c;满足 \(b_i\wedge b_{i-1}\neq 0\) &#xff08; \(\wedge\) 表示按位与&#xff09; \(1\leq n\leq 100000\) Solution 令 \(f_i\) 为二进制第 \(i…

因果关系和相关关系 大数据_数据科学中的相关性与因果关系

因果关系和相关关系 大数据Let’s jump into it right away.让我们马上进入。 相关性 (Correlation) Correlation means relationship and association to another variable. For example, a movement in one variable associates with the movement in another variable. For…

Pytorch构建模型的3种方法

这个地方一直是我思考的地方&#xff01;因为学的代码太多了&#xff0c;构建的模型各有不同&#xff0c;这里记录一下&#xff01; 可以使用以下3种方式构建模型&#xff1a; 1&#xff0c;继承nn.Module基类构建自定义模型。 2&#xff0c;使用nn.Sequential按层顺序构建模…

vue取数据第一个数据_我作为数据科学家的第一个月

vue取数据第一个数据A lot.很多。 I landed my first job as a Data Scientist at the beginning of August, and like any new job, there’s a lot of information to take in at once.我于8月初找到了数据科学家的第一份工作&#xff0c;并且像任何新工作一样&#xff0c;一…

Flask-SocketIO 简单使用指南

Flask-SocketIO 使 Flask 应用程序能够访问客户端和服务器之间的低延迟双向通信。客户端应用程序可以使用 Javascript&#xff0c;C &#xff0c;Java 和 Swift 中的任何 SocketIO 官方客户端库或任何兼容的客户端来建立与服务器的永久连接。 安装 直接使用 pip 来安装&#xf…

STL-开篇

基本概念 STL&#xff1a; Standard Template Library&#xff0c;标准模板库 定义&#xff1a; c引入的一个标准类库 特点&#xff1a;1&#xff09;数据结构和算法的 c实现&#xff08; 采用模板类和模板函数&#xff09;2&#xff09;数据的存储和算法的分离3&#xff09;高…

Symbol Mc1000 声音的设置以及播放

首先引用Symbol.Audio 加一命名空间using Symbol.Audio; /声音设备的设置 //Select Device from device list Symbol.Audio.Device MyDevice (Symbol.Audio.Device)Symbol.StandardForms.SelectDevice.Select( Symbol.Audio.Controller.Title, Symbol.Audio.Devic…

/bin/bash^M: 坏的解释器: 没有那个文件或目录

在win下编辑的时候&#xff0c;换行结尾是\n\r &#xff0c; 而在linux下 是\n&#xff0c;所以会多出来一个\r&#xff0c;这样会出现错误 此时执行 sed -i s/\r$// file.sh 将file.sh中的\r都替换为空白&#xff0c;问题解决转载于:https://www.cnblogs.com/zzdbullet/p/9890…

rcp rapido_为什么气流非常适合Rapido

rcp rapidoBack in 2019, when we were building our data platform, we started building the data platform with Hadoop 2.8 and Apache Hive, managing our own HDFS. The need for managing workflows whether it’s data pipelines, i.e. ETL’s, machine learning predi…