当前位置:网站首页>2022.07.19 Logu p6588 『 jroi-1 』 vector
2022.07.19 Logu p6588 『 jroi-1 』 vector
2022-07-20 21:42:00 【Yingye Zhu(HPXXZYY)】
P6588 [JROI-1] Vector \color{green}{\texttt{P6588 [JROI-1] }\text{Vector}} P6588 [JROI-1] Vector
[Problem] \color{blue}{\texttt{[Problem]}} [Problem]
You have now n n n Vector a i → = ( x i , y i ) \overrightarrow{a_{i}}=(x_{i},y_{i}) ai=(xi,yi), You need to do something 5 5 5 Operations in total m m m Time :
- Put the vector a i → \overrightarrow{a_{i}} ai Add the given directional quantity ( Δ x , Δ y ) (\Delta x, \Delta y) (Δx,Δy)
- Put the vector a i → \overrightarrow{a_{i}} ai Subtract the given amount ( Δ x , Δ y ) (\Delta x,\Delta y) (Δx,Δy)
- Put the vector a i → \overrightarrow{a_{i}} ai With a given integer k k k Perform multiplication , Namely the a i → \overrightarrow{a_{i}} ai Turn into ( k × x i , k × y i ) (k \times x_{i},k \times y_{i}) (k×xi,k×yi)
- inquiry ∑ i = l r − 1 ∑ j = i + 1 r a i → ⋅ a j → \sum\limits_{i=l}^{r-1} \sum\limits_{j=i+1}^{r} \overrightarrow{a_{i}} \cdot \overrightarrow{a_{j}} i=l∑r−1j=i+1∑rai⋅aj( That is to find the sum of dot multiplication )
- inquiry ∑ i = 1 r − 1 ∑ j = i + 1 r a i → ⊕ a j → \sum\limits_{i=1}^{r-1} \sum\limits_{j=i+1}^{r} \overrightarrow{a_{i}} \oplus \overrightarrow{a_{j}} i=1∑r−1j=i+1∑rai⊕aj( That is to find the sum of cross products )
( As for what each operation means , Please do the basic operation of Baidu vector by yourself )
1 ≤ n , m ≤ 1 × 1 0 5 1 \leq n,m \leq 1 \times 10^{5} 1≤n,m≤1×105. Guarantee any vector at any time a i → \overrightarrow{a_{i}} ai The absolute value of the horizontal and vertical coordinates of is not greater than 1000 1000 1000.
[Analysis] \color{blue}{\texttt{[Analysis]}} [Analysis]
The first three operations are the basic operations of the segment tree , Focus on the fourth and fifth operations .
Operation 4 \text{Operation 4} Operation 4
Because dot multiplication has Commutative law 1 and Distributive law 2, therefore :
∑ i = l r − 1 ∑ j = i + 1 r a i → ⋅ a j → = ( s l , r → ) 2 − ∑ i = l r ( a i → ) 2 2 \sum\limits_{i=l}^{r-1}\sum\limits_{j=i+1}^{r}\overrightarrow{a_{i}} \cdot \overrightarrow{a_{j}}=\dfrac{\left ( \overrightarrow{s_{l,r}} \right )^{2}-\sum\limits_{i=l}^{r} \left ( \overrightarrow{a_{i}} \right )^{2}}{2} i=l∑r−1j=i+1∑rai⋅aj=2(sl,r)2−i=l∑r(ai)2
among :
s l , r = ∑ i = l r a i → s_{l,r}=\sum\limits_{i=l}^{r} \overrightarrow{a_{i}} sl,r=i=l∑rai
It's equivalent to multiplying all vectors once , Subtract yourself by yourself , Divided by 2 2 2( because a → \overrightarrow{a} a and b → \overrightarrow{b} b Will multiply twice ).
Operation 5 \text{Operation 5} Operation 5
unfortunately , Cross multiplication has no commutative law 3, Only the law of distribution .
We have a new way , remember :
Cross l , r = ∑ i = 1 r − 1 ∑ j = i + 1 r a i → ⊕ a j → \text{Cross}_{l,r}=\sum\limits_{i=1}^{r-1} \sum\limits_{j=i+1}^{r} \overrightarrow{a_{i}} \oplus \overrightarrow{a_{j}} Crossl,r=i=1∑r−1j=i+1∑rai⊕aj
Sx l , r = ∑ i = l r x i \text{Sx}_{l,r}=\sum\limits_{i=l}^{r}x_{i} Sxl,r=i=l∑rxi
namely Sx l , r \text{Sx}_{l,r} Sxl,r Vector a l ⋯ r → \overrightarrow{a_{l \cdots r}} al⋯r The sum of abscissa of , Sy l , r \text{Sy}_{l,r} Syl,r similar , Is the sum of the ordinates .
take ∀ mid s.t. l ≤ mid ≤ r \forall \text{mid} \quad \texttt{s.t.} \quad l \leq \text{mid} \leq r ∀mids.t.l≤mid≤r, be :
Cross l , r = Cross l , mid + Cross mid + 1 , r + Sx l , mid × Sy mid + 1 , r − Sy l , mid × Sx mid + 1 , r \text{Cross}_{l,r}=\text{Cross}_{l,\text{mid}}+\text{Cross}_{\text{mid}+1,r}+\text{Sx}_{l,\text{mid}} \times \text{Sy}_{\text{mid}+1,r}-\text{Sy}_{l,\text{mid}} \times \text{Sx}_{\text{mid}+1,r} Crossl,r=Crossl,mid+Crossmid+1,r+Sxl,mid×Symid+1,r−Syl,mid×Sxmid+1,r
According to this nature , We can use the segment tree to maintain .
[code] \color{blue}{\texttt{[code]}} [code]
typedef long long ll;
const int N=1e5+100,M=N<<2;
ll x[M],y[M],xs[M],ys[M],Cross[M];
pair<ll,ll> add(pair<ll,ll> a,pair<ll,ll> b){
return make_pair(a.first+b.first,a.second+b.second);
}
inline void maintain(int o){
xs[o]=x[o]*x[o];
ys[o]=y[o]*y[o];
}//maintain the leaves
pair<int,int> a[N];
inline void pushup(int o){
x[o]=x[o<<1]+x[o<<1|1];
y[o]=y[o<<1]+y[o<<1|1];
xs[o]=xs[o<<1]+xs[o<<1|1];
ys[o]=ys[o<<1]+ys[o<<1|1];
Cross[o]=Cross[o<<1]+Cross[o<<1|1]+x[o<<1]*y[o<<1|1]-y[o<<1]*x[o<<1|1];
}
void build(int o,int l,int r){
if (l==r){
x[o]=a[l].first;
y[o]=a[r].second;
maintain(o);
return;
}
int mid=(l+r)>>1;
build(o<<1,l,mid);
build(o<<1|1,mid+1,r);
pushup(o);return;
}
void modify(int o,int l,int r,int p,int vx,int vy){
if (l==r){
x[o]+=vx;y[o]+=vy;
maintain(o);
return;
}
int mid=(l+r)>>1;
if (p<=mid) modify(o<<1,l,mid,p,vx,vy);
else modify(o<<1|1,mid+1,r,p,vx,vy);
pushup(o);return;
}//add or minus ( operation 1,2)
void modify(int o,int l,int r,int p,int v){
if (l==r){
x[o]*=v;y[o]*=v;
maintain(o);
return;
}
int mid=(l+r)>>1;
if (p<=mid) modify(o<<1,l,mid,p,v);
else modify(o<<1|1,mid+1,r,p,v);
pushup(o);return;
}//times ( operation 3)
pair<ll,ll> query(int o,int l,int r,int p,int q){
if (p<=l&&r<=q) return make_pair(x[o],y[o]);
int mid=(l+r)>>1;
pair<ll,ll> ret=make_pair(0ll,0ll);
if (p<=mid) ret=add(ret,query(o<<1,l,mid,p,q));
if (mid<q) ret=add(ret,query(o<<1|1,mid+1,r,p,q));
return ret;
}//the sum of ai ( Sum up )
ll Query(int o,int l,int r,int p,int q){
if (p<=l&&r<=q) return xs[o]+ys[o];
int mid=(l+r)>>1;ll ans=0;
if (p<=mid) ans+=Query(o<<1,l,mid,p,q);
if (mid<q) ans+=Query(o<<1|1,mid+1,r,p,q);
return ans;
}//the sum of ai dot ai ( Point multiplication )
pair<ll,pair<ll,ll> > QUERY(int o,int l,int r,int p,int q){
if (p<=l&&r<=q) return make_pair(Cross[o],make_pair(x[o],y[o]));
int mid=(l+r)>>1;
pair<ll,pair<ll,ll> > L,R;
L=make_pair(0ll,make_pair(0ll,0ll));
R=make_pair(0ll,make_pair(0ll,0ll));
if (p<=mid) L=QUERY(o<<1,l,mid,p,q);
if (mid<q) R=QUERY(o<<1|1,mid+1,r,p,q);
ll ans=L.first+R.first+L.second.first*R.second.second-L.second.second*R.second.first;
return make_pair(ans,make_pair(L.second.first+R.second.first,L.second.second+R.second.second));
}//the sum of ai cross ai ( Cross riding )
int n,m;
int main(){
n=read();m=read();
for(int i=1;i<=n;i++){
int u=read(),v=read();
a[i]=make_pair(u,v);
}
build(1,1,n);
for(int i=1,opt,l,r,vx,vy;i<=m;i++){
opt=read();l=read();
if (opt==1){
vx=read();vy=read();
modify(1,1,n,l,vx,vy);
}
else if (opt==2){
vx=read();vy=read();
modify(1,1,n,l,-vx,-vy);
}
else if (opt==3){
vx=read();
modify(1,1,n,l,vx);
}
else if (opt==4){
r=read();
pair<ll,ll> tmp=query(1,1,n,l,r);
ll Minus=Query(1,1,n,l,r);
printf("%lld\n",(tmp.first*tmp.first+tmp.second*tmp.second-Minus)/2);
}// Don't forget to divide by 2
else{
r=read();
pair<ll,pair<ll,ll> > ans=QUERY(1,1,n,l,r);
printf("%lld\n",ans.first);
}
}
return 0;
}
Commutative law refers to a → ⋅ b → = b → ⋅ a → \overrightarrow{a} \cdot \overrightarrow{b}=\overrightarrow{b}\cdot \overrightarrow{a} a⋅b=b⋅a︎
Distributive law refers to a → ⋅ ( b → + c → ) = a → ⋅ b → + a → ⋅ c → \overrightarrow{a} \cdot (\overrightarrow{b}+\overrightarrow{c})=\overrightarrow{a} \cdot \overrightarrow{b}+\overrightarrow{a} \cdot \overrightarrow{c} a⋅(b+c)=a⋅b+a⋅c︎
in fact , According to the geometric meaning of cross multiplication , a → ⊕ b → = − b → ⊕ a → \overrightarrow{a} \oplus \overrightarrow{b}=-\overrightarrow{b} \oplus \overrightarrow{a} a⊕b=−b⊕a︎
边栏推荐
- Mysql8.0 new feature - persistence of self increasing variables
- LeetCode 69:爬楼梯 / 跳台阶
- Unity3d learning note 9 - loading textures
- 記錄一下十三届藍橋杯嵌入式省賽題目
- STM32 porting RT thread nano to realize full steps of fish
- To do difficult work
- Cannot make QOpenGLContext current in a different thread : PyQt多线程崩溃的解决方法
- Project knowledge points
- Skywalking full link monitoring cluster and dynamic deployment
- 基于ssh大学生党建网站系统
猜你喜欢
随机推荐
金仓数据库 KingbaseES SQL 语言参考手册 (3.3. 类型转换表、3.4. 常量)
KubeSphere 3.3.0 离线安装教程
牛客多校第一场i题 Chiitoitsu 期望+概率DP
STM32 Hal library serial port sends and receives at the same time, and the receiving is stuck?
Kingbasees SQL language reference manual of Jincang database (3.2. data type comparison rules)
適合送禮的藍牙耳機有哪些?2022藍牙耳機排行榜10强
STM32 porting RT thread nano to realize full steps of fish
LVGL 8.2 Textarea
谈谈指针!
Redis is awesome. If you don't understand the usage specification, it will be ruined
项目知识点
How to implement recursive function in wechat game making tool
Source code of short video system, loading order of main files in uni app project
【每日一题】731. 我的日程安排表Ⅱ
当元宇宙的发展开始越来越多地呈现互联网的样式时,我们需要警惕
Is it safe for Dongguan securities to buy shares and open an account?
Code source du système vidéo court, séquence de chargement des fichiers principaux dans le projet uni app
树的定义和基本术语
短視頻系統源碼,uni-app項目中主要文件的加載順序
To do difficult work