【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]$交集非空的区间个数 用上面维护 ...
随机推荐
- assert.doesNotThrow()
assert.doesNotThrow(block[, error][, message]) 断言 block 函数不会抛出错误.查阅 assert.throws() 了解更多详情. 当调用 asse ...
- 读书笔记:《人有人的用处》------N.维纳. (2016.12.28)
读书笔记:<人有人的用处>------N.维纳 ·某些系统可以依其总能量而和其他系统区别开来. ·在某些情况下,一个系统如果保持足够长时间的运转,那它就会遍历一切与其能量相容的位置和动量的 ...
- 83-MACD 移动平均汇总/分离指标.(2015.7.3)
MACD 移动平均汇总/分离指标 ~计算: · EMA(12,t)=EMA(12,t-1) * 11/13 + Close * 2/13 · EMA(26,t)=EMA(26,t-1) * 25/27 ...
- UVA 10652 凸包问题
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> # ...
- 【数轴涂色+并查集路径压缩+加速】C. String Reconstruction
http://codeforces.com/contest/828/problem/C [题意] [思路] 因为题目保证一定有解,所有优化时间复杂度的关键就是不要重复染色,所以我们可以用并查集维护区间 ...
- [NOIP2005] 提高组 洛谷P1051 谁拿了最多奖学金
题目描述 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同: 1) 院士奖学金,每人8000元,期末平均成绩高于80分(>80),并且在本学期内发表1篇或1 ...
- PHP_pear的安装和使用
-------------- 安装pear -------------- pear是PHP的扩展和应用程序库,包含了很多有用的类,安装好php5.0后,pear实际上并没有被安装 ...
- JVM(五):探究类加载过程-上
JVM(五):探究类加载过程-上 本文我们来研究一个Java字节码文件(Class文件)是如何加载入内存中的,在這個过程中涉及类加载过程中的加载,验证,准备,解析(连接),初始化,使用,销毁过程,并探 ...
- Redis集群方案之主从复制(待实践)
Redis有主从复制的功能,一台主可以有多台从,从还可以有多台从,但是从只能有一个主.并且在从写入的数据不会复制到主. 配置 在Redis中,要实现主从复制架构非常简单,只需要在从数据库的配置文件中加 ...
- html5 编辑
在html中想获得矢量图形可以用svg标签.该标签画出的图形全部用代码实现. 可以用在线html编辑工具来进行所见即所得编辑,然后到处源码. 比较好用的工具有http://editor.method. ...