【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]$交集非空的区间个数 用上面维护 ...
随机推荐
- mysql数据库导出导入
MYSQL的命令行模式设置: 我的电脑->属性->高级系统设置->环境变量->系统变量-> 选择Path,在后面添加“;path\mysql\bin;”其中path为MY ...
- LeetCode(70) Climbing Stairs
题目 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cli ...
- UVa 210 并行程序模拟(deque)
题意: 模拟n个程序运行 格式一共有5种:var = constant(赋值):print var(打印):lock:unlock:end, 上述5种语句分别需要t1.t2.t3.t4.t5单位时间 ...
- JSONArray 遍历方式
第一种(java8):遍历JSONArray 拼接字符串 public static void main(String[] args) { JSONArray jSONArray = new JSON ...
- Light oj-1002 - Country Roads,迪杰斯特拉变形,不错不错~~
1002 - Co ...
- hdu - 1068 Girls and Boys (二分图最大独立集+拆点)
http://acm.hdu.edu.cn/showproblem.php?pid=1068 因为没有指定性别,所以要拆点,把i拆分i和i’ 那么U=V-M (M是最大匹配,U最大独立集,V是顶点数) ...
- FTPUtil工具类
package com.xxx.common.util; import java.io.File; import java.io.FileOutputStream; import java.io.IO ...
- 洛谷 P1457 城堡 The Castle
P1457 城堡 The Castle 题目描述 我们憨厚的USACO主人公农夫约翰(Farmer John)以无法想象的运气,在他生日那天收到了一份特别的礼物:一张“幸运爱尔兰”(一种彩票).结果这 ...
- MongoDB小结20 - find【查询条件$size】
size可以获得指定数组长度的文档 db.user.find({"fruit":{"$size":3}},{"_id":0}) { &quo ...
- storm ——Understanding the Parallelism of a Storm Topology
http://www.michael-noll.com/blog/2012/10/16/understanding-the-parallelism-of-a-storm-topology/ 这篇文章好 ...