Skip to content

泉州一中信息学Blog

信息学奥赛技术分享博客

  • 登录
  • 小学oj
  • 中学oj
  • 测试页面1
  • Toggle search form

NAN DAY 3

Posted on 2024年11月12日 By 张, 高畅 NAN DAY 3无评论

其实本来有很多能写的但是现在时间不够了(大悲

因此以下内容全部都将简述.

[T1]

注意到 a_i \leq 100 ,因此可以使用 DP 来做。

但是如果这题值域很大的话,就没有办法了。

LXL给的算法是采用分治/扫描线(其实不太像)

具体地,因为对于每一个数 x,最多只会有 \log x 级别的 \gcd 个数,因此用队列(数组)维护这 \log x 不同的数,并记录答案。

DP 的复杂度为 O(100n),代码如下

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5,M=105,K=100;
int n,res,f[2][M],st[M];
int gcd(int a,int b)
{
    if(!b)return a;
    else return gcd(b,a%b);
}
int main()
{
    freopen("set.in","r",stdin);
    freopen("set.out","w",stdout);
    scanf("%d",&n);
    for(int i=1,x;i<=n;i++)
    {
        scanf("%d",&x);
        for(int j=1;j<=K;j++)
        {
            f[i&1][j]=0;
            if(f[i-1&1][j])
            {
                st[j]=1;
                f[i&1][gcd(j,x)]=1;
            }
        }
        f[i&1][x]=1;
    }
    for(int i=1;i<=K;i++)
    {
        if(st[i]||f[n&1][i])res++;
    }
    printf("%d",res);
    return 0;
}

[T2]

找规律题。

对于 y坐标轴右侧呈现了一个上下震荡式的东西,y轴左侧则是正常逆时针分布。

代码如下:

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
typedef long long LL;
int n,m;
LL ABS(LL x)
{
    if(x>=0)return x;
    else return -x;
}
int main()
{
    freopen("number.in","r",stdin);
    freopen("number.out","w",stdout);
    scanf("%d%d",&n,&m);
    while(n--)
    {
        LL x,y;
        scanf("%lld%lld",&x,&y);
        if(!x&&!y)
        {
            puts("0");
            continue;
        }
        LL round=ABS(x)+ABS(y);
        LL cnt=(round-1)*round*2;
        if(x>0)
        {
            if(!y)cnt++;
            else
            {
                if(y>0)cnt+=(round-x)*2;
                else cnt+=(round-x)*2+1;
            }
        }
        else 
        {
            if(y>=0)cnt+=2*round+ABS(x);
            else cnt+=4*round-ABS(x);
        }
        printf("%lld\n",cnt); 
    }
    while(m--)
    {
        LL p;
        scanf("%lld",&p);
        if(!p)
        {
            puts("0 0");
            continue;
        }
        LL round=(sqrt(2*p)-1)/2;
        round++;
        p-=(round-1)*round*2;
        if(p<=2*round)
        {
            if(p==1)printf("%lld %lld\n",round,0);
            else if(p&1)printf("%lld %lld\n",round-(p/2),-(p/2));
            else printf("%lld %lld\n",round-(p/2),(p/2));
        }
        else
        {
            p-=2*round;
            if(p<=round)printf("%lld %lld\n",-p,round-p);
            else
            {
                p-=round;
                printf("%lld %lld\n",p-round,-p);
            }
        }
    }
    return 0;
}

[T3]

最难泵的一题,出题人甚至把读入文件输入错了。

但是这题本身也挺难泵的。

首先考虑普通贪心。

将 a_i-b_i 后我们来看怎么选。

对于正数,我们肯定要按照 ABBA 这样选最优,

对于负数与 0 ,我们则需要交替取最优。

然后考虑怎么办这东西扔到数据结构上维护。

考虑建立两棵值域线段树。

对于正数,按排名模 4 余 0/1/2/3 分类求和。
对于负数与 0,按排名模 2 余 0/1 分类求和。

最终答案一定是一定是正数树上的0/3减去上1/2,再加/减负数树上的 0 减去 负数树上的 1。

具体细节见代码,注意由于负数树上还需要维护零因此要注意值域。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5,D=1e5;
typedef long long LL;
struct node
{
    LL w[4];
    int Size;
}tr[2][N<<2];
int n,m;
LL w[N];
void pushup(int op,node &u,node &l,node &r)
{
    u.Size=l.Size+r.Size;
    int S=op?2:4;
    for(int i=0;i<S;i++)u.w[i]=l.w[i]+r.w[((i-l.Size)%S+S)%S];
}
void modify(int op,int u,int l,int r,int pos,int w)
{
    if(l==r)
    {
        tr[op][u].Size+=w;
        int S=op?2:4;
        for(int i=0;i<S;i++)tr[op][u].w[i]=(tr[op][u].Size/S)*pos;
        for(int i=0;i<tr[op][u].Size%S;i++)tr[op][u].w[i]+=pos;
        return;
    }
    int mid=l+r>>1;
    if(pos<=mid)modify(op,u<<1,l,mid,pos,w);
    else modify(op,u<<1|1,mid+1,r,pos,w);
    if(!op)pushup(op,tr[op][u],tr[op][u<<1|1],tr[op][u<<1]);
    else pushup(op,tr[op][u],tr[op][u<<1],tr[op][u<<1|1]);
}
int main()
{
    freopen("xiaovid.in","r",stdin);
    freopen("xiaovid.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        LL a,b;
        scanf("%lld%lld",&a,&b);
        w[i]=a-b;
        if(w[i]>0)modify(0,1,1,D,w[i],1);
        else modify(1,1,0,D,-w[i],1);
    }
    while(m--)
    {
        int x;
        LL y,z;
        scanf("%d%lld%lld",&x,&y,&z);
        if(w[x]>0)modify(0,1,1,D,w[x],-1);
        else modify(1,1,0,D,-w[x],-1);
        w[x]=y-z;
        if(w[x]>0)modify(0,1,1,D,w[x],1);
        else modify(1,1,0,D,-w[x],1);
        node POS=tr[0][1],NEG=tr[1][1]; 
        LL res=POS.w[0]+POS.w[3]-POS.w[1]-POS.w[2];
        if(POS.Size&1)res+=NEG.w[0]-NEG.w[1];
        else res+=NEG.w[1]-NEG.w[0];
        printf("%lld\n",res);
    }
    return 0;
}

[T4]

最讨厌线段树的一集。

首先可用并查集乱搞得到 45pts。

考虑在以上基础上优化,比如用hash来快速判断是否相等。

我们发现对于操作二可行了,但是操作一怎么维护呢?

考虑类似并查集式的按大小合并,我们发现复杂度是启发式合并的 n\log n 级别。

然而每次我们寻找哪些点要被改也需要 \log n 级别的二分。

最终复杂度 O(n \log n)

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N=1e5+3,Base=131,mod=1e9+7;
int n,m,par[N];
struct node
{
    int l,r;
    LL w[2];
}tr[N<<2];
LL power[N];
inline void pushup(node &u,node &l,node &r)
{
    u.l=l.l;
    u.r=r.r;
    u.w[0]=(l.w[0]*power[r.r-r.l+1]%mod+r.w[0])%mod;
    u.w[1]=(r.w[1]*power[l.r-l.l+1]%mod+l.w[1])%mod;
}
void build(int u,int l,int r)
{
    tr[u]={l,r};
    if(l==r)
    {
        tr[u].w[0]=tr[u].w[1]=l;
        return;
    }
    int mid=l+r>>1;
    build(u<<1,l,mid);
    build(u<<1|1,mid+1,r);
    pushup(tr[u],tr[u<<1],tr[u<<1|1]);
}
void modify(int u,int pos,int w)
{
    if(tr[u].l==tr[u].r)
    {
        tr[u].w[0]=tr[u].w[1]=w;
        return;
    }
    int mid=tr[u].l+tr[u].r>>1;
    if(pos<=mid)modify(u<<1,pos,w);
    else modify(u<<1|1,pos,w);
    pushup(tr[u],tr[u<<1],tr[u<<1|1]);
}
node query(int u,int l,int r)
{
    if(l<=tr[u].l&&tr[u].r<=r)return tr[u];
    int mid=tr[u].l+tr[u].r>>1;
    node res={0,0,0},left,right;
    if(l<=mid&&mid<r)
    {
        left=query(u<<1,l,r),right=query(u<<1|1,l,r);
        pushup(res,left,right);
        return res;
    }
    else if(l<=mid)return query(u<<1,l,r);
    else if(mid<r)return query(u<<1|1,l,r);
    else return res;
}
vector<int>S[N];
void merge(int s,int t)
{
    if(s==t)return;
    if(S[s].size()>S[t].size())
    {
        while(S[t].size())
        {
            S[s].push_back(S[t].back());
            modify(1,S[t].back(),s);
            par[S[t].back()]=s;
            S[t].pop_back();
        }
    }
    else
    {
        while(S[s].size())
        {
            S[t].push_back(S[s].back());
            modify(1,S[s].back(),t);
            par[S[s].back()]=t;
            S[s].pop_back();
        }
    }
}
void work(int x,int y)
{
    int L=x+y>>1,R=x+y>>1,len=0;
    if((x+y)&1)R++;
    do
    {
        int l=len+1,r=(y-x+2)/2;
        while(l<r)
        {
            int mid=l+r>>1;
            if(query(1,L-mid+1,L).w[0]!=query(1,R,R+mid-1).w[1])r=mid;
            else l=mid+1;
        }
        merge(par[L-l+1],par[R+l-1]);
        len=l;
    }
    while(len<(y-x+2)/2);
}
int main()
{
    freopen("tiao.in","r",stdin);
    freopen("tiao.out","w",stdout);
    scanf("%d%d",&n,&m);
    power[0]=1;
    for(int i=1;i<=n;i++)
    {
        power[i]=power[i-1]*Base%mod;
        par[i]=i;
        S[i].push_back(i);
    }
    build(1,1,n);
    while(m--)
    {
        int op,l,r,x,y;
        scanf("%d%d%d",&op,&l,&r);
        if(op==1)work(l,r);
        else if(op==2)
        {
            scanf("%d%d",&x,&y);
            if(y-x+1!=r-l+1)puts("Not equal");
            else puts(query(1,l,r).w[0]==query(1,x,y).w[0]?"Equal":"Unknown");
        }
    }
    return 0;
 }
训练日志

文章导航

Previous Post: 南安一中DAY3
Next Post: NAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYZ DAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY4

发表回复 取消回复

要发表评论,您必须先登录。

2025年 6月
一 二 三 四 五 六 日
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« 2月    

2024常州 Class Classic OI Problems Contest cqr的长乐集训2023 CZYZ LOC New Game NOI NOIP Password Protected PM_PK Preview Problems Retrospect Selfmade Qusetion STL The end Training Uneasy Problem 蒟蒻 通报

  • 训练日志
  • 链表
  • 入门
  • 模拟
  • dfs序
  • 并查集
  • spfa
  • 最小割
  • 矩阵树定理
  • 仙人掌
  • BSGS
  • 凸包
  • 回文自动机
  • 递推与动归
  • 堆
  • 莫队算法
  • ST表
  • Treap
  • 树套树
  • 可持久化线段树
  • 初赛
  • 搜索
  • 贪心
  • 深度优先搜索
  • 欧拉图
  • dijkstra
  • 费用流
  • 哈夫曼树
  • kruskual
  • 置换
  • 旋转卡壳
  • KMP
  • 区间动归
  • STL
  • 链表
  • 可并堆
  • sply
  • 主席树
  • 可持久化字典树
  • 算法
  • 动态规划
  • 构造
  • 广度优先搜索
  • 最短路
  • floyd
  • 最大流
  • 虚树
  • prim
  • 筛法
  • 半平面交
  • 字典树
  • 背包动归
  • 基础数据结构
  • 分块
  • 线段树
  • 替罪羊树
  • K-DTree
  • 图论
  • 二分法
  • 迭代搜索
  • 拓扑排序
  • 有上下界网络流
  • 生成树
  • 快速幂
  • 后缀数组
  • 树形动归
  • 哈希表
  • 中级数据结构
  • 平衡树
  • 可持久化数据结构
  • 数据结构
  • 三分法
  • 启发式搜索
  • 图的连通
  • 点分治
  • 博弈论
  • AC自动机
  • 状压动归
  • 单调栈
  • 树状数组
  • 高级数据结构
  • OI资料
  • 数学
  • 高精度
  • 差分约束
  • 树上倍增
  • 素数测试
  • 后缀自动机
  • 数位动归
  • 单调队列
  • 新闻
  • 几何
  • 随机化
  • 二分图染色
  • 树链剖分
  • 欧拉函数
  • manacher
  • 斜率优化
  • 离线处理
  • 信息学奥赛学长风采
  • 字符串
  • 二分图匹配
  • prufer编码
  • 卡特兰数
  • 密码学
  • 决策单调
  • 赛后总结
  • 其他
  • 2-SAT
  • 最近公共祖先
  • 矩阵乘法
  • 记忆化搜索
  • 网络流
  • Link cut tree
  • 排列组合
  • 树
  • 高斯消元
  • 乘法逆元
  • 容斥原理
  • 调和级数
  • 概率与期望
  • 模线性方程组
  • 莫比乌斯反演
  • 快速傅里叶变换
  • 扩展欧几里德
  • 最大公约数与最小公倍数

近期文章

  • DP杂题
  • 2025年2月13日模拟赛
  • HLOJ-TEST ROUND 4-T1/T2(构造)- 3
  • HLOJ-TEST ROUND 4-T1/T2(构造)- 2
  • HLOJ-TEST ROUND 4-T1/T2(构造)- 1

近期评论

归档

  • 2025年2月
  • 2025年1月
  • 2024年11月
  • 2024年10月
  • 2024年9月
  • 2024年8月
  • 2024年7月
  • 2024年3月
  • 2024年2月
  • 2024年1月
  • 2023年12月
  • 2023年11月
  • 2023年10月
  • 2023年9月
  • 2023年8月
  • 2023年7月
  • 2023年3月
  • 2023年2月
  • 2023年1月
  • 2022年12月

Copyright © 2025 泉州一中信息学Blog.

Powered by PressBook WordPress theme