分析:a^b+2(a&b)=a+b  so->a^(-b)+2(a&(-b))=a-b

然后树状数组分类讨论即可

链接:http://www.ifrog.cc/acm/problem/1023

吐槽:这个题本来是mod(2^40),明显要用快速乘啊,但是用了以后狂T,不用反而过了,不懂出题人

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 1e5+;
const LL mod = (1ll<<);
int T,n,m,a[N],b[N],p[N],cnt,kase;
LL c[][N],sum[];
inline void init()
{
for(int i=; i<; ++i)
for(int j=;j<=cnt;++j)c[i][j]=;
sum[]=sum[]=sum[]=sum[]=;
}
inline void up(LL &x,LL t)
{
x+=t;
if(x>=mod)x-=mod;
}
inline void add(int pos,int x,LL t)
{
for(int i=x; i<=cnt; i+=i&(-i))up(c[pos][i],t);
}
inline LL ask(int pos,int x)
{
LL ret=;
for(int i=x; i; i-=i&(-i))up(ret,c[pos][i]);
return ret;
}
LL ksc(LL x,LL y)
{
LL ret=;
while(y)
{
if(y&)up(ret,x);
y>>=;
up(x,x);
}
return ret;
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=; i<=n; ++i)scanf("%d",&a[i]);
for(int i=; i<=m; ++i)scanf("%d",&b[i]),p[i]=b[i];
sort(p+,p++m);
cnt = unique(p+,p++m)-p-;
int ptr=;
LL ret=;
init();
for(int i=; i<=n; ++i)
{
int pos;
for(; ptr<=m&&ptr<i; ++ptr)
{
pos = lower_bound(p+,p++cnt,b[ptr])-p;
add(,pos,);++sum[];
add(,pos,ptr);up(sum[],ptr);
add(,pos,b[ptr]);up(sum[],b[ptr]);
add(,pos,1ll*b[ptr]*ptr%mod);up(sum[],1ll*b[ptr]*ptr%mod);
}
/**j<i,b[j]<a[i]**/
pos = lower_bound(p+,p++cnt,a[i])-p;
--pos;
LL tmp =ask(,pos);
if(tmp!=)
{
tmp = 1ll*i*a[i]%mod*tmp%mod;up(ret,tmp);
//up(ret,ksc(1ll*i*a[i]%mod,tmp));
tmp = -(ask(,pos)*a[i]%mod);
//tmp = -ksc(ask(1,pos),a[i]);
up(tmp,mod);
up(ret,tmp);
tmp = -(ask(,pos)*i%mod);
//tmp = -ksc(ask(2,pos),i);
up(tmp,mod);
up(ret,tmp);
up(ret,ask(,pos));
}
/*********/
/**j<i,b[j]>a[i]**/
pos = upper_bound(p+,p++cnt,a[i])-p;
if(pos==cnt+)continue;
--pos;
tmp = sum[]-ask(,pos);
if(tmp==)continue;
tmp = -(1ll*i*a[i]%mod*tmp%mod);
//tmp= -ksc(1ll*i*a[i]%mod,tmp);
up(tmp,mod);
up(ret,tmp);
tmp = (sum[]-ask(,pos)+mod)%mod;
tmp = tmp*a[i]%mod;
//tmp = ksc(tmp,a[i]);
up(ret,tmp);
tmp = (sum[]-ask(,pos)+mod)%mod;
tmp = tmp*i%mod;
//tmp = ksc(tmp,i);
up(ret,tmp);
tmp = (sum[]-ask(,pos)+mod)%mod;
tmp = (mod-tmp)%mod;
up(ret,tmp);
/*********/
}
init();
ptr=m;
for(int i=n; i; --i)
{
int pos;
for(; ptr>i&&ptr; --ptr)
{
pos = lower_bound(p+,p++cnt,b[ptr])-p;
add(,pos,);++sum[];
add(,pos,ptr);up(sum[],ptr);
add(,pos,b[ptr]);up(sum[],b[ptr]);
add(,pos,1ll*b[ptr]*ptr%mod);up(sum[],1ll*b[ptr]*ptr%mod);
}
/**j>i,b[j]>a[i]**/
pos = upper_bound(p+,p++cnt,a[i])-p;
--pos;
if(pos!=cnt)
{
LL tmp = sum[]-ask(,pos);
if(tmp!=)
{
tmp = 1ll*i*a[i]%mod*tmp%mod;up(ret,tmp);
//up(ret,ksc(1ll*i*a[i]%mod,tmp));
tmp = (sum[]-ask(,pos)+mod)%mod;
tmp = -(tmp*a[i]%mod);
//tmp = -ksc(tmp,a[i]);
up(tmp,mod);
up(ret,tmp);
tmp = (sum[]-ask(,pos)+mod)%mod;
tmp = -(tmp*i%mod);
//tmp = -ksc(tmp,i);
up(tmp,mod);
up(ret,tmp);
tmp = (sum[]-ask(,pos)+mod)%mod;
up(ret,tmp);
}
}
/*********/
/**j>i,b[j]<a[i]**/
pos = lower_bound(p+,p++cnt,a[i])-p;
--pos;
LL tmp = ask(,pos);
if(tmp==)continue;
tmp =-(1ll*i*a[i]%mod*tmp%mod);
//tmp= -ksc(1ll*i*a[i]%mod,tmp);
up(tmp,mod);
up(ret,tmp);
tmp = ask(,pos);
tmp = tmp*a[i]%mod;
//tmp = ksc(tmp,a[i]);
up(ret,tmp);
tmp = ask(,pos);
tmp = tmp*i%mod;
//tmp = ksc(tmp,i);
up(ret,tmp);
tmp = ask(,pos);
tmp = (mod-tmp)%mod;
up(ret,tmp);
/*********/
}
printf("Case #%d: %lld\n",++kase,ret);
}
return ;
}

玲珑学院OJ 1023 - Magic boy Bi Luo with his excited math problem 树状数组暴力的更多相关文章

  1. 2016中国大学生程序设计竞赛 - 网络选拔赛 C. Magic boy Bi Luo with his excited tree

    Magic boy Bi Luo with his excited tree Problem Description Bi Luo is a magic boy, he also has a migi ...

  2. hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)

    题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: ...

  3. 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree

    // 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以 ...

  4. hdu 5834 Magic boy Bi Luo with his excited tree 树形dp+转移

    Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 13107 ...

  5. HDU5834 Magic boy Bi Luo with his excited tree(树形DP)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5834 Description Bi Luo is a magic boy, he also ...

  6. 动态规划(树形DP):HDU 5834 Magic boy Bi Luo with his excited tree

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8UAAAJbCAIAAABCS6G8AAAgAElEQVR4nOy9fXQcxZ0uXH/hc8i5N+

  7. 【树形动规】HDU 5834 Magic boy Bi Luo with his excited tree

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5834 题目大意: 一棵N个点的有根树,每个节点有价值ci,每条树边有费用di,节点的值只能取一次,边 ...

  8. HDU 5834 Magic boy Bi Luo with his excited tree

    树形dp. 先dfs一次处理子树上的最优解,记录一下回到这个点和不回到这个点的最优解. 然后从上到下可以推出所有答案.细节较多,很容易写错. #pragma comment(linker, " ...

  9. HDU 5834 Magic boy Bi Luo with his excited tree(树形dp)

    http://acm.hdu.edu.cn/showproblem.php?pid=5834 题意: 一棵树上每个节点有一个价值$Vi$,每个节点只能获得一次,每走一次一条边要花费$Ci$,问从各个节 ...

随机推荐

  1. nodejs学习(一) ---- nodejs + express应用生成器 快速创建应用

    1.node安装及环境配置(自行百度) 2.express安装及配置 (自行百度) 3.通过应用生成器工具 express 快速创建应用骨架   全局安装应用生成器 : npm install exp ...

  2. Zabbix微信告警

    Zabbix微信告警 摘要 Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越多的企业开始使用zabbix结合微信作为主要的告警方式,这样可以及时有效的把告警信 ...

  3. Buffer.from(arrayBuffer[, byteOffset[, length]])

    Buffer.from(arrayBuffer[, byteOffset[, length]]) arrayBuffer - 一个 TypedArray 或 new ArrayBuffer() 的 . ...

  4. PHP:验证邮箱合法性

    文章来源:http://www.cnblogs.com/hello-tl/p/7592304.html /** * [verifyPhone description] 效验邮箱号合法性 * @para ...

  5. 集训第五周动态规划 F题 最大子矩阵和

    Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...

  6. 集训第五周动态规划 D题 LCS

    Description In a few months the European Currency Union will become a reality. However, to join the ...

  7. BNUOJ 6719 Simpsons’ Hidden Talents

    Simpsons’ Hidden Talents Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU ...

  8. NYOJ-768移位密码,最简单的代替密码;

    移位密码 时间限制:1000 ms  |  内存限制:65535 KB 难度:0 ->   Link   <- 还有1个半小时考信息安全导论,昨晚心血来潮在oj上看到这几个题,简直就是水啊 ...

  9. 【Eclipse】eclipse中设置tomcat启动时候的JVM参数

    主要通过以下的几个jvm参数来设置堆内存的: -Xmx512m 最大总堆内存,一般设置为物理内存的1/4 -Xms512m 初始总堆内存,一般将它设置的和最大堆内存一样大,这样就不需要根据当前堆使用情 ...

  10. CURL不可以读写文件

    最近在学ES(elastic search),参考http://www.learnes.net/里面翻译的官方权威指南(后面发现官网已经推出了中文版文档了).里面有的例子把访问ES的命令做了简化如下: ...