【USACO 2017FEB】 Why Did the Cow Cross the Road III
【题目链接】
【算法】
树状数组
【代码】
#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010 int n,i,x;
long long ans;
int pos[MAXN];
long long c[MAXN]; inline int lowbit(int x)
{
return x & (-x);
}
inline void modify(int pos,long long val)
{
int i;
for (i = pos; i <= * n; i += lowbit(i)) c[i] += val;
}
inline long long query(int pos)
{
int i;
long long ans = ;
for (i = pos; i; i -= lowbit(i)) ans += c[i];
return ans;
}
inline long long query(int l,int r)
{
return query(r) - query(l-);
} int main() { scanf("%d",&n);
for (i = ; i <= * n; i++)
{
scanf("%d",&x);
if (!pos[x])
{
pos[x] = i;
modify(i,);
} else
{
ans += query(pos[x]+,i-);
modify(pos[x],-);
}
}
printf("%lld\n",ans); return ; }
【USACO 2017FEB】 Why Did the Cow Cross the Road III的更多相关文章
- 【USACO 2017Feb】 Why Did the Cow Cross the Road
[题目链接] 点击打开链接 [算法] dist[i][j][k]表示当前走到(i,j),走的步数除以3的余数为k的最小花费 spfa即可 [代码] #include<bits/stdc++.h& ...
- [ USACO 2017 FEB ] Why Did the Cow Cross the Road III (Gold)
\(\\\) \(Description\) 给定长度为\(2N\)的序列,\(1\text ~N\)各出现过\(2\)次,\(i\)第一次出现位置记为\(a_i\),第二次记为\(b_i\),求满足 ...
- Why Did the Cow Cross the Road III(树状数组)
Why Did the Cow Cross the Road III 时间限制: 1 Sec 内存限制: 128 MB提交: 65 解决: 28[提交][状态][讨论版] 题目描述 The lay ...
- 洛谷 P3663 [USACO17FEB]Why Did the Cow Cross the Road III S
P3663 [USACO17FEB]Why Did the Cow Cross the Road III S 题目描述 Why did the cow cross the road? Well, on ...
- [USACO17FEB]Why Did the Cow Cross the Road III P
[USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...
- [USACO17FEB]Why Did the Cow Cross the Road III S
题目描述 Why did the cow cross the road? Well, one reason is that Farmer John's farm simply has a lot of ...
- 洛谷 P3660 [USACO17FEB]Why Did the Cow Cross the Road III G(树状数组)
题目背景 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题目描述 The layout of Farmer ...
- 【题解】洛谷P3660 [USACO17FEB]Why Did the Cow Cross the Road III
题目地址 又是一道奶牛题 从左到右扫描,树状数组维护[左端点出现而右端点未出现]的数字的个数.记录每个数字第一次出现的位置. 若是第二次出现,那么删除第一次的影响. #include <cstd ...
- P3660 【[USACO17FEB]Why Did the Cow Cross the Road III G】
题外话:维护区间交集子集的小套路 开两个树状数组,一个维护进入区间,一个维护退出区间 $Query:$ 给定询问区间$l,r$和一些其他区间,求其他区间中与$[l,r]$交集非空的区间个数 用上面维护 ...
随机推荐
- 用ffmpeg切割音频文件
ffmpeg -i audio.wav -f segment -segment_time -c copy audio%02d.wav "-segment_time 60" 表示每6 ...
- Spider-scrapy 中的 xpath 语法与调试
把setting中的机器人过滤设为False ROBOTSTXT_OBEY = False 1 语法 artcile 选取所有子节点 /article 选取根元素 artile article/a 选 ...
- spark streaming 踩过的那些坑
系统背景 spark streaming + Kafka高级API receiver 目前资源分配(现在系统比较稳定的资源分配),独立集群 --driver-memory 50G --exec ...
- [luoguP1042] 乒乓球(模拟)
传送门 终于过了这sb题了. 当初我连这道题都A不了(╯▔皿▔)╯ 代码 #include <cstdio> #include <iostream> #define N 100 ...
- org.osgi.framework.BundleException: Exception in org.eclipse.core.resources.ResourcesPlugin.start()
http://freestyle21.cn 不知什么时候,启动eclipse的时候就一直不行,说是an error ..我查了下log 报错:org.osgi.framework.BundleExce ...
- linux ftp服务器搭建
作为服务器的机器IP:192.168.124.129 主机名:Centos 操作系统:CentOS 5.5 需求:匿名用户可以下载公共目录里边内容,本地用户登录有rwx权限 软件安装 1. 准备测 ...
- Ubuntu 12.04 之 LAMP
搭建LAMP环境 (1)更新软件列表: sudo apt-get update 结果报错: W: 无法下载 bzip2:/var/lib/apt/lists/partial/cn.archive.ub ...
- 静态区间第k大(主席树)
POJ 2104为例(主席树入门题) 思想: 可持久化线段树,也叫作函数式线段树,也叫主席树(高大上). 可持久化数据结构(Persistent data structure):利用函数式编程的思想使 ...
- Layui动画、按钮、表单
Layui动画.按钮.表单 在实用价值的前提之下,我们并没有内置过多花俏的动画.而他们同样在 layui 的许多交互元素中,发挥着重要的作用.layui 的动画全部采用 CSS3,因此不支持ie8和部 ...
- dtrace.org
http://dtrace.org/blogs/rm/2016/09/15/turtles-on-the-wire-understanding-how-the-os-uses-the-modern-n ...