51Nod1674[区间的价值 V2]--分块

【链接】
51nod1674

【解题报告】

经典的分块题目。

因为对于一组数 a1,a2,a3,...,an 的按顺序 and 值是递减的, or 值是递增的,所以对于每个块最多有 2log2amax 种情况(还有需要注意的是处理一个新的块时要去重)。所以此题就可以用分块解决。

#include<cstdio>
#define LL long long
using namespace std;
const int maxn=100005,maxv=65,tt=1000000007;
int n,tot,ans,a[maxn];
struct wjd
{
    int a,b,l;
}c[maxn];
inline char nc()
{
    static char buf[100000],*l=buf,*r=buf;
    if (l==r) r=(l=buf)+fread(buf,1,100000,stdin);
    if (l==r) return EOF; return *l++;
}
inline int Read()
{
    int res=0; char ch=nc();
    while (ch<'0'||ch>'9') ch=nc();
    while (ch>='0'&&ch<='9') res=(res<<3)+(res<<1)+ch-48,ch=nc();
    return res;
}
void Updata(int x)
{
    c[++tot]=(wjd){a[x],a[x],1};
    for (int i=1; i<=tot; i++) c[i].a&=a[x],c[i].b|=a[x];
    int now=tot; tot=1;
    for (int i=2; i<=now; i++)
     if (c[i].a==c[tot].a&&c[i].b==c[tot].b) c[tot].l+=c[i].l; else c[++tot]=c[i];
    for (int i=1; i<=tot; i++) ans=(ans+(LL)c[i].a*c[i].b%tt*c[i].l%tt)%tt;
}
int main()
{
    freopen("1674.in","r",stdin);
    freopen("1674.out","w",stdout);
    n=Read(); ans=tot=0;
    for (int i=1; i<=n; i++) a[i]=Read();
    for (int i=n; i; i--) Updata(i);
    printf("%d\n",ans);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值