玲珑学院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$,问从各个节 ...
随机推荐
- PHP实现QQ第三方登录的方法
前言: PHP实现QQ快速登录,罗列了三种方法 方法一:面向过程,回调地址和首次触发登录写到了一个方法页面[因为有了if做判断], 方法二,三:面向对象 1.先调用登录方法,向腾讯发送请求,2.腾讯携 ...
- MySQL主从配置详解
一.mysql主从原理 1. 基本介绍 MySQL 内建的复制功能是构建大型,高性能应用程序的基础.将 MySQL 的 数亿分布到到多个系统上去,这种分步的机制,是通过将 MySQL 的某一台主机的数 ...
- CentOS 6, 编译安装lamp (php-fpm)
1 整体要求 php-fpm.httpd.mysql三者分别安装在三台虚拟机上: 第一台虚拟主机用于安装Mariadb,第二台虚拟主机安装php-fpm:第三台虚拟主机安装httpd.三台主机安装完之 ...
- ubuntu14.04 mysql-workbench Connecting to MySQL server ... Native table 'performance_schema'.'session_variables' has the wrong structure错误解决
使用的mysql版本: mysql Ver 14.14 Distrib 5.7.9, for Linux (x86_64) using EditLine wrapper 打开shell命令 1.输 ...
- Uva 839天平(二叉树dfs, 递归建树)
题意: 给定一个天平长度 输入格式为 wl dl wr dr 分别代表天平左边长度,左边重量, 右边长度, 右边重量. 如果重量为0, 说明下面还有一个天平, 递归给出. 样例输入:10 2 0 40 ...
- 在linux服务器上搭建Struts2项目运行环境
服务器上工作: 首先要先装java https://www.cnblogs.com/lamp01/p/8932740.html 然后装好tomcat https://www.cnblogs.com/y ...
- LaTex/Overleaf使用笔记
1. 添加网页引用 @misc{ r1, author = "Wikipedia", title = "Binary decision diagram --- { ...
- 7-14 电话聊天狂人(25 分)(Hash表基本操作)
7-14 电话聊天狂人(25 分) 给定大量手机用户通话记录,找出其中通话次数最多的聊天狂人. 输入格式: 输入首先给出正整数N(≤105),为通话记录条数.随后N行,每行给出一条通话记录.简单 ...
- HDU 4465 递推与double的精确性
题目大意不多说了 这里用dp[i][0] 代表取完第一个盒子后第二个盒子剩 i 个的概率,对应期望就是dp[i][0] *i dp[i][1] 就代表取完第二个盒子后第一个盒子剩 i 个的概率 dp[ ...
- hdu 5073 推公式相邻质心转换
#include<stdio.h> #include<stdlib.h> #include<string.h> #define N 51000 int cmp(co ...