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.pswp.cn/news/389356.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389356.shtml
英文地址,請注明出處:http://en.pswp.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…