4822: [Cqoi2017]老C的任务

每次统计一个矩形内的点权和

拆成四个小矩形作差分

扫描线+树状数组即可

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
 
const int maxn = 1E5 + 10;
const int Max = ~0U>>1;
const int Min = -(1LL << 31LL);
typedef long long LL;
 
struct Query{
    int y,Num,typ; Query(){}
    Query(int y,int Num,int typ):
        y(y),Num(Num),typ(typ){}
};
 
int n,m,cx,cy,tx,ty,ax[maxn],ay[maxn],px[maxn],py[maxn],p[maxn];
LL Ans[maxn],sum[maxn];
 
vector <Query> Q[maxn];
vector <pair<int,int> > P[maxn];
 
inline int getint()
{
    char ch = getchar(); int ret = 0,a = 1;
    while (ch < '0' || '9' < ch)
    {
        if (ch == '-') a = -1;
        ch = getchar();
    }
    while ('0' <= ch && ch <= '9')
        ret = ret * 10 + ch - '0',ch = getchar();
    return ret * a;
}
 
inline void Add(int x,int y,int Num,int typ)
{
    int nx = lower_bound(ax + 1,ax + cx + 1,x) - ax;
    int ny = lower_bound(ay + 1,ay + cy + 1,y) - ay;
    if (ax[nx] > x) --nx; if (ay[ny] > y) --ny;
    Q[nx].push_back(Query(ny,Num,typ));
}
 
void Pre_Work()
{
    for (int i = 1; i <= n; i++)
    {
        ax[i] = px[i] = getint();
        ay[i] = py[i] = getint();
        p[i] = getint();
    }
    cx = 1; ax[++tx] = Min; ax[++tx] = Max;
    cy = 1; ay[++ty] = Min; ay[++ty] = Max;
    sort(ax + 1,ax + tx + 1);
    for (int i = 2; i <= tx; i++)
        if (ax[i] != ax[i - 1]) ax[++cx] = ax[i];
    sort(ay + 1,ay + ty + 1);
    for (int i = 2; i <= ty; i++)
        if (ay[i] != ay[i - 1]) ay[++cy] = ay[i];
    for (int i = 1; i <= n; i++)
    {
        px[i] = lower_bound(ax + 1,ax + cx + 1,px[i]) - ax;
        py[i] = lower_bound(ay + 1,ay + cy + 1,py[i]) - ay;
        P[px[i]].push_back(make_pair(py[i],p[i]));
    }
    for (int i = 1; i <= m; i++)
    {
        int xa,ya,xb,yb;
        xa = getint(); ya = getint();
        xb = getint(); yb = getint(); Add(xb,yb,i,1);
        if (xa > Min) Add(xa - 1,yb,i,-1);
        if (ya > Min) Add(xb,ya - 1,i,-1);
        if (xa > Min && ya > Min) Add(xa - 1,ya - 1,i,1);
    }
}
 
int main()
{
    #ifdef DMC
        freopen("DMC.txt","r",stdin);
    #endif
     
    tx = ty = n = getint();
    m = getint(); Pre_Work();
    for (int i = 1; i <= cx; i++)
    {
        for (int j = 0; j < P[i].size(); j++)
            for (int k = P[i][j].first; k <= cy; k += k&-k)
                sum[k] += 1LL * P[i][j].second;
        for (int j = 0; j < Q[i].size(); j++)
        {
            Query now = Q[i][j]; LL tot = 0;
            for (int k = now.y; k > 0; k -= k&-k) tot += sum[k];
            Ans[now.Num] += 1LL * now.typ * tot;
        }
    }
    for (int i = 1; i <= m; i++) printf("%lld\n",Ans[i]);
    return 0;
}

根据引用所述,交错序列是一个仅由0和1构成的序列,其中没有相邻的1(可以有相邻的0)。特征值定义为x^ay^b,其中x和y分别表示0和1出现的次数。长度为n的交错序列可能有多个。问题要求计算所有长度为n的交错序列特征值的和除以m的余数。 根据引用所述,输入文件包含一个行,该行包含三个整数n、a、b和m。其中,1≤n≤10000000,0≤a、b≤45,m<100000000。 为了解决这个问题,可以使用动态规划和矩阵快速幂优化的方法,具体实现可以参考引用提到的相关算法。算法的思路是通过计算长度为n的交错序列的特征值,然后将所有特征值求和并对m取余数。 具体步骤如下: 1. 使用动态规划计算长度为n的所有交错序列的特征值,将结果保存在一个矩阵中。 2. 使用矩阵快速幂优化,将动态规划的过程进行优化。 3. 对优化后的结果进行求和,并对m取余数。 4. 输出结果。 参考引用给出的博客中的代码实现,可以帮助你更好地理解和实现该算法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [BZOJ5298 CQOI2018 交错序列 【DP+矩阵快速幂优化】*](https://blog.csdn.net/weixin_30892987/article/details/99470493)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值