玲珑学院OJ 1023 - Magic boy Bi Luo with his excited math problem 树状数组暴力
分析: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 树状数组暴力的更多相关文章
- 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 ...
- 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: ...
- 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree
// 树形DP CCPC网络赛 HDU5834 Magic boy Bi Luo with his excited tree // 题意:n个点的树,每个节点有权值为正,只能用一次,每条边有负权,可以 ...
- 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 ...
- 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 ...
- 动态规划(树形DP):HDU 5834 Magic boy Bi Luo with his excited tree
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA8UAAAJbCAIAAABCS6G8AAAgAElEQVR4nOy9fXQcxZ0uXH/hc8i5N+
- 【树形动规】HDU 5834 Magic boy Bi Luo with his excited tree
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5834 题目大意: 一棵N个点的有根树,每个节点有价值ci,每条树边有费用di,节点的值只能取一次,边 ...
- HDU 5834 Magic boy Bi Luo with his excited tree
树形dp. 先dfs一次处理子树上的最优解,记录一下回到这个点和不回到这个点的最优解. 然后从上到下可以推出所有答案.细节较多,很容易写错. #pragma comment(linker, " ...
- HDU 5834 Magic boy Bi Luo with his excited tree(树形dp)
http://acm.hdu.edu.cn/showproblem.php?pid=5834 题意: 一棵树上每个节点有一个价值$Vi$,每个节点只能获得一次,每走一次一条边要花费$Ci$,问从各个节 ...
随机推荐
- Bootstrap 12 栅格系统
栅格系统简介 Bootstrap 提供了一套响应式.移动设备优先的流式栅格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多 12 列.它包含了易于使用的预定义类,还有强大的mix ...
- SQL中带有NOT IN 子查询改写
报表程序中的一段SQL运行很慢,代码如下: 优化前: 耗时:1337s INSERT INTO PER_LTE_ZIB_PB_COMMISSION_07 SELECT P.TOPACTUALID, Q ...
- 13Spring通过注解配置Bean(1)
配置Bean的形式:基于XML文件的方式:基于注解的方式(基于注解配置Bean:基于注解来装配Bean的属性) 下面介绍基于注解的方式来配置Bean. ——组件扫描(component scannin ...
- Adversarial Auto-Encoders
目录 Another Approach: q(z)->p(z) Intuitively comprehend KL(p|q) Minimize KL Divergence How to comp ...
- STM32F407 跑马灯 寄存器版 个人笔记
更多原理请参考跑马灯 库函数版 个人笔记 步骤 使能IO口时钟.配置相关寄存器寄存器RCC->AHB1ENR 初始化IO口模式.配置四个配置寄存器 GPIOx_MODER/ GPIOx_OTYP ...
- BNUOJ 7697 Information Disturbing
Information Disturbing Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on HDU. ...
- HDU 1525 Euclid Game
题目大意: 给定2个数a , b,假定b>=a总是从b中取走一个a的整数倍,也就是让 b-k*a(k*a<=b) 每人执行一步这个操作,最后得到0的人胜利结束游戏 (0,a)是一个终止态P ...
- [luoguP1944] 最长括号匹配_NOI导刊2009提高(1)
传送门 非常傻的DP. f[i]表示末尾是i的最长的字串 #include <cstdio> #include <cstring> #define N 1000001 int ...
- [luoguP1472] 奶牛家谱 Cow Pedigrees(DP)
传送门 一个深度为i的树可以由一个根节点外加两个深度为i-1的树组成,这就决定了DP该怎么写. 然而我真的没有想到. f[i][j]表示深度为i节点数为j的个数 sum[i][j]表示深度小于等于i节 ...
- android studio配置so文件路径
将一个项目从eclipse上移植到android studio时,发现总是加载不成功库文件,so库文件放在了main/src/libs下的目录. 参考网上资料,studio默认的库文件路径是main/ ...