玲珑学院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$,问从各个节 ...
随机推荐
- IO之DataStream数据流举例
import java.io.*; public class TestDataStream { public static void main(String[] args) { ByteArrayOu ...
- Python 1-3区分Python文件的两种用途和模块的搜索路径
区分Python文件的两种用途 run.py文件: import m1 m1.py文件: def f1(): print('f1') def f2(): print('f2') #当文件被执行时__n ...
- Reparameterization Trick
目录 Sample() is not differentiable Reparameterization trick Too Complex Sample() is not differentiabl ...
- OpenCV+Python实现视频文件裁剪功能
Python编程实现对视频文件进行剪切的功能.截取指定长度的视频并保存,运行后首先选择要裁剪的视频,然后输入开始时间点和停止时间点即可.将剪切后的视频保存为output.avi文件 所属网站分类: 资 ...
- String类的概述和构造方法
StringDemo.java /* * String:字符串类 * 由多个字符组成的一串数据 * 字符串其本质就是一个字符数组 * * 构造方法: * String(String original) ...
- Dijkstra算法C++实现总结
问题描述 求无负权图中点s到点t的最短凝聚力 备注 标准说法中,"缩短"/"松弛"(relax)操作是对边进行的.下面为了行文方便,将其拓展到点.即以下操作,其 ...
- 易接SDK ios9以上无法弹出充值界面的一种情况
充值需要用到http请求: 打开info.plist, 在app tansport security setting 这个项 , 加入 NSAllowsArbitraryLoads YES
- api安全认证
三.auth自定义授权 客户端代码: import requests import hashlib import time current_time = time.time() #自意义的字符串app ...
- node.js里的buffer常见操作,copy,concat等实例讲解
//通过长度构建的buffer内容是随机的 var buffer=new Buffer(100); console.log(buffer); //手动清空buffer,一般创建buffer不会清空 b ...
- tyvj1045 最大的算式
描述 题目很简单,给出N个数字,不改变它们的相对位置,在中间加入K个乘号和N-K-1个加号,(括号随便加)使最终结果尽量大.因为乘号和加号一共就是N-1个了,所以恰好每两个相邻数字之间都有一个符号.例 ...