COGS.1689.[HNOI2010]Bounce 弹飞绵羊(分块)

题目链接

/*
分块,维护从某位置跳到下一个块需要多少步 
*/
#include<cmath>
#include<cstdio>
#include<cctype>
using namespace std;
const int N=2e6+5;

int n,v[N],belong[N],size,tot,step[N]/*跳出这个块需要多少步*/,nxt[N]/*最后跳到这块的哪*/;

inline int read()
{
    int now=0,f=1;register char c=getchar();
    for(;!isdigit(c);c=getchar())
      if(c=='-') f=-1;
    for(;isdigit(c);now=now*10+c-'0',c=getchar());
    return now*f;
}

void Modify(int p)
{
    int val=read();
    v[p]=val;
//不能只改一个点!因为同一块中,前面是由后面得到的,且维护的nxt[aft],即aft跳到下一个块的位置,所以必须从pos往前再更新一遍 
    for(int i=p;i>(belong[p]-1)*size;--i)
    {
        int aft=i+v[i];
        if(aft>n)//可能会在边界 
            step[i]=1, nxt[i]=n+1;
        else if(belong[aft]!=belong[i])
            step[i]=1, nxt[i]=aft;
        else
            step[i]=step[aft]+1, nxt[i]=nxt[aft];
    }
//  printf("Modify  %d:v:%d aft:%d step:%d nxt:%d\n",p,v[p],aft,step[p],nxt[p]);
}
int Query(int p)
{
    int res=0;
    while(p<=n)
//      printf("pos:%d step:%d nxt:%d\n",p,step[p],nxt[p]),
        res+=step[p], p=nxt[p];
    return res;
}

int main()
{
//  freopen("bzoj_2002.in","r",stdin);
//  freopen("bzoj_2002.out","w",stdout);

    n=read();size=sqrt(n);
//  tot=size;//n/size = n/sqrt(n) = sqrt(n) = size
//  if(n%size) ++tot;
    for(int i=1;i<=n;++i)
        v[i]=read(), belong[i]=(i-1)/size+1;
    for(int i=n;i;--i)//逆序做,这样就能利用前边得到的值O(1)得到某结果 
    {
        int aft=i+v[i];
        if(aft>n)
            step[i]=1, nxt[i]=n+1;
        else if(belong[aft]!=belong[i])
            step[i]=1, nxt[i]=aft;
        else
            step[i]=step[aft]+1, nxt[i]=nxt[aft];
//      printf("%d:v:%d aft:%d step:%d nxt:%d\n",i,v[i],aft,step[i],nxt[i]);
    }
    int m=read(),opt,p;
    while(m--)
    {
        opt=read(),p=read()+1;
        if(opt==1)
            printf("%d\n",Query(p));
        else
            Modify(p);
    }

//  fclose(stdin);fclose(stdout);
    return 0;
}

转载于:https://www.cnblogs.com/SovietPower/p/8435089.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是将代码修改为cot平滑的方法: 1. 首先,需要使用边界角的cot权重计算每个顶点的权重。 2. 然后,使用cot权重对每个顶点的邻域点进行加权计算,得到平滑后的坐标。 3. 最后,根据平滑后的坐标更新每个顶点的位置。 修改后的代码如下: float smooth() { float err = -1; cogs.clear(); v_end = mesh.vertices_end(); //cot平滑 for (v_it = mesh.vertices_begin(); v_it != v_end; ++v_it) { cog[0] = cog[1] = cog[2] = weight_sum = 0.0; for (vv_it = mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it) { double cot_weight = 0.0; MyMesh::HalfedgeHandle heh = mesh.find_halfedge(*v_it, *vv_it); if (!mesh.is_boundary(heh)) { MyMesh::HalfedgeHandle prev_heh = mesh.prev_halfedge_handle(heh); MyMesh::HalfedgeHandle next_heh = mesh.next_halfedge_handle(heh); MyMesh::VertexHandle prev_vh = mesh.to_vertex_handle(prev_heh); MyMesh::VertexHandle next_vh = mesh.to_vertex_handle(next_heh); MyMesh::Point prev_p = mesh.point(prev_vh); MyMesh::Point curr_p = mesh.point(*v_it); MyMesh::Point next_p = mesh.point(next_vh); double cot_alpha = cot(prev_p - curr_p, next_p - curr_p); double cot_beta = cot(curr_p - prev_p, next_p - prev_p); cot_weight = cot_alpha + cot_beta; } cog += cot_weight * mesh.point(*vv_it); weight_sum += cot_weight; } cogs.push_back(cog / weight_sum); } for (v_it = mesh.vertices_begin(), cog_it = cogs.begin(); v_it != v_end; ++v_it, ++cog_it) { if (!mesh.is_boundary(*v_it)) { MyMesh::Point p = mesh.point(*v_it); err = max(err, (p - *cog_it).norm()); mesh.set_point(*v_it, *cog_it); } } return err; } 其中cot函数的定义如下: double cot(MyMesh::Point a, MyMesh::Point b) { return dot(a, b) / cross(a, b).norm(); } 注意,这里使用的是边界角的cot权重,因此在计算cot权重时需要判断当前边是否为边界。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值