分析: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. 在springBoot的控制台打印sql语句

    在springBoot+Mybatis日志显示SQL的执行情况的最简单方法就是在properties新增: properties形式 logging.level.com.eth.wallet.mapp ...

  2. kubernetes 知识点及常用命令

    一.附上一个Deployment文件 apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment spec: selec ...

  3. c++基础_特殊的数字

    #include <iostream> #include <math.h> using namespace std; int main(){ ;i<;i++){ ; )% ...

  4. ArcGIS:Hello World Maps

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...

  5. Quartz--Trigger

    TriggerQuartz中的触发器,用来告诉调度程序什么时候触发,即Trigger对象是用来触发Job的 触发器通用属性 JobKey StartTime EndTimeJobKey表示job实例的 ...

  6. //……关于promise

    什么是promise? promise 翻译成中文的意思是 "承诺" ,一个承诺说出去了说明他是进行中的,承诺兑现了代表成功,没有兑现代表失败了. promise 对象的状态一旦发 ...

  7. Notepad++ 连接远程 FTP 进行文件编辑

    一.下载安装 Notepad++ 1.下载 Notepad++ : https://pan.baidu.com/s/1o7VrS4y 密码 : ck8a 2.安装 Notepad++ 2.1.勾选所有 ...

  8. 判断List集合为空

    package org.springframework.util; CollectionUtils.isEmpty(list)

  9. 2017-10-03-afternoon

    P100 zhx 竞赛时间:????年??月??日??:??-??:?? 题目名称 a b c 名称 a b c 输入 a.in b.in c.in 输出 a.out b.out c.out 每个测试 ...

  10. Java高并发实战,锁的优化

    锁优化 这里的锁优化主要是指 JVM 对 synchronized 的优化. 自旋锁 互斥同步进入阻塞状态的开销都很大,应该尽量避免.在许多应用中,共享数据的锁定状态只会持续很短的一段时间.自旋锁的思 ...