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

来源:互联网 发布:无主之地2怎么连接网络 编辑:程序博客网 时间:2024/06/04 00:58

【链接】
51nod1674

【解题报告】

经典的分块题目。

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

#include<cstdio>#define LL long longusing 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;}
原创粉丝点击