[信息學奧賽一本通oj1741]電子速度 題解

 

 

 

對於$100 \%$的數據,$1≤n,m≤1e6 \ \ \ 0<=x_i,y_i<20170927 \ \ \ 1≤l_i,r_i≤n $ios

 

 

 

$Solution:$

一開始沒看懂題。後來大體理解了一下,所謂的v是一個二維向量,有x和y兩個參數。那個$\times$是叉乘,即$(x_i y_j-x_j y_i)$。git

因此題意就是給你一個x序列和y序列,對於每次詢問的區間$[l,r]$,求$\sum \limits _{l\leq i<j \leq r}(x_iy_j-x_jy_i)^2$。帶修。數組

點對的形式比較麻煩,嘗試化成單點的柿子:spa

先拆平方:blog

$\sum \limits _{l\leq i<j \leq r}x_i^2y_j^2-2x_ix_jy_iy_j+x_j^2y_i^2$get

大力化簡:string

$\large \begin{array}{ll} ans &=& \sum \limits_{i=l}^{r} \sum \limits_{j=i+1}^r (x_i^2y_j^2+x_j^2y_i^2-2x_iy_ix_jy_j) \\ &=& \sum \limits_{i=l}^r \sum \limits_{j=i+1}^r x_i^2y_j^2 + \sum \limits_{i=l}^r \sum \limits_{j=i+1}^r x_j^2y_i^2 - \sum \limits_{i=l}^r \sum \limits_{j=i+1}^r 2x_iy_ix_jy_j \\ &=& \sum \limits_{i=l}^r \sum \limits_{j=l}^r [i \neq j]*x_i^2y_j^2 - \sum \limits_{i=l}^r \sum \limits_{j=l}^r [i \neq j]*x_iy_ix_jy_j \\ &=& \sum \limits_{i=l}^r x_i^2 (\sum \limits_{j=l}^r y_j^2 -y_i^2) - (\sum \limits_{i=l}^r x_iy_i (\sum \limits_{j=l}^r x_jy_j - x_iy_i)) \\ &=& \sum \limits_{i=l}^r x_i^2 \sum \limits_{j=l}^r y_j^2 - \sum \limits_{i=l}^r x_i^2y_i^2 - (\sum \limits_{i=l}^r x_iy_i \sum \limits_{j=l}^r x_jy_j - \sum \limits_{i=l}^r x_i^2 y_i^2) \\ &=& \sum \limits_{i=l}^r x_i^2* \sum \limits_{i=l}^ry_i^2 - (\sum \limits_{i=l}^r x_iy_i)^2\end{array}$it

而後開三個樹狀數組分別維護${x_i}^2,{y_i}^2,x_i y_i$便可。io

 

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
#define pa pair<ll,ll>
typedef long long ll;
const ll mod=20170927;
ll read()
{
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return x*f;
}
const int N=1e6+5;
int n,m;
ll c[3][N];
pa v[N];
int lb(int x){return x&-x;}
void add(int x,ll val,int id)
{
    for( ;x<=n;x+=lb(x))
        c[id][x]+=val,(c[id][x]+=mod)%=mod;
}
ll query(int x,int id)
{
    ll res=0;
    for( ;x;x-=lb(x))
        (res+=c[id][x])%=mod;
    return (res+mod)%mod;
}
ll ask(int l,int r,int id)
{
    return (query(r,id)-query(l-1,id)+mod)%mod;
}
int main()
{
    n=read();m=read();
    for(int i=1;i<=n;i++)
    {
        v[i].first=read(),v[i].second=read();
        add(i,v[i].first*v[i].first,0);
        add(i,v[i].second*v[i].second,1);
        add(i,v[i].first*v[i].second,2);
    }

    while(m--)
    {
        int op=read();
        if(op==1)
        {
            int pos=read();
            ll x=read(),y=read();
            add(pos,x*x-v[pos].first*v[pos].first,0);
            add(pos,y*y-v[pos].second*v[pos].second,1);
            add(pos,x*y-v[pos].first*v[pos].second,2);
            v[pos].first=x;v[pos].second=y;

        }
        if(op==2)
        {
            int l=read(),r=read();
            ll res=ask(l,r,0)*ask(l,r,1)%mod,res1=ask(l,r,2);
            res=(res-(res1*res1%mod)+mod)%mod;
            printf("%lld\n",res);
        }
    }
    return 0;
}
相關文章
相關標籤/搜索