【题目链接】

点击打开链接

【算法】

树状数组

【代码】

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define MAXN 100010
  4.  
  5. int n,i,x;
  6. long long ans;
  7. int pos[MAXN];
  8. long long c[MAXN];
  9.  
  10. inline int lowbit(int x)
  11. {
  12. return x & (-x);
  13. }
  14. inline void modify(int pos,long long val)
  15. {
  16. int i;
  17. for (i = pos; i <= * n; i += lowbit(i)) c[i] += val;
  18. }
  19. inline long long query(int pos)
  20. {
  21. int i;
  22. long long ans = ;
  23. for (i = pos; i; i -= lowbit(i)) ans += c[i];
  24. return ans;
  25. }
  26. inline long long query(int l,int r)
  27. {
  28. return query(r) - query(l-);
  29. }
  30.  
  31. int main() {
  32.  
  33. scanf("%d",&n);
  34. for (i = ; i <= * n; i++)
  35. {
  36. scanf("%d",&x);
  37. if (!pos[x])
  38. {
  39. pos[x] = i;
  40. modify(i,);
  41. } else
  42. {
  43. ans += query(pos[x]+,i-);
  44. modify(pos[x],-);
  45. }
  46. }
  47. printf("%lld\n",ans);
  48.  
  49. return ;
  50.  
  51. }

【USACO 2017FEB】 Why Did the Cow Cross the Road III的更多相关文章

  1. 【USACO 2017Feb】 Why Did the Cow Cross the Road

    [题目链接] 点击打开链接 [算法] dist[i][j][k]表示当前走到(i,j),走的步数除以3的余数为k的最小花费 spfa即可 [代码] #include<bits/stdc++.h& ...

  2. [ USACO 2017 FEB ] Why Did the Cow Cross the Road III (Gold)

    \(\\\) \(Description\) 给定长度为\(2N\)的序列,\(1\text ~N\)各出现过\(2\)次,\(i\)第一次出现位置记为\(a_i\),第二次记为\(b_i\),求满足 ...

  3. Why Did the Cow Cross the Road III(树状数组)

    Why Did the Cow Cross the Road III 时间限制: 1 Sec  内存限制: 128 MB提交: 65  解决: 28[提交][状态][讨论版] 题目描述 The lay ...

  4. 洛谷 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 ...

  5. [USACO17FEB]Why Did the Cow Cross the Road III P

    [USACO17FEB]Why Did the Cow Cross the Road III P 考虑我们对每种颜色记录这样一个信息 \((x,y,z)\),即左边出现的位置,右边出现的位置,该颜色. ...

  6. [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 ...

  7. 洛谷 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 ...

  8. 【题解】洛谷P3660 [USACO17FEB]Why Did the Cow Cross the Road III

    题目地址 又是一道奶牛题 从左到右扫描,树状数组维护[左端点出现而右端点未出现]的数字的个数.记录每个数字第一次出现的位置. 若是第二次出现,那么删除第一次的影响. #include <cstd ...

  9. P3660 【[USACO17FEB]Why Did the Cow Cross the Road III G】

    题外话:维护区间交集子集的小套路 开两个树状数组,一个维护进入区间,一个维护退出区间 $Query:$ 给定询问区间$l,r$和一些其他区间,求其他区间中与$[l,r]$交集非空的区间个数 用上面维护 ...

随机推荐

  1. mysql数据库导出导入

    MYSQL的命令行模式设置: 我的电脑->属性->高级系统设置->环境变量->系统变量-> 选择Path,在后面添加“;path\mysql\bin;”其中path为MY ...

  2. 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 ...

  3. UVa 210 并行程序模拟(deque)

    题意: 模拟n个程序运行 格式一共有5种:var = constant(赋值):print var(打印):lock:unlock:end, 上述5种语句分别需要t1.t2.t3.t4.t5单位时间 ...

  4. JSONArray 遍历方式

    第一种(java8):遍历JSONArray 拼接字符串 public static void main(String[] args) { JSONArray jSONArray = new JSON ...

  5. Light oj-1002 - Country Roads,迪杰斯特拉变形,不错不错~~

                                                                                               1002 - Co ...

  6. hdu - 1068 Girls and Boys (二分图最大独立集+拆点)

    http://acm.hdu.edu.cn/showproblem.php?pid=1068 因为没有指定性别,所以要拆点,把i拆分i和i’ 那么U=V-M (M是最大匹配,U最大独立集,V是顶点数) ...

  7. FTPUtil工具类

    package com.xxx.common.util; import java.io.File; import java.io.FileOutputStream; import java.io.IO ...

  8. 洛谷 P1457 城堡 The Castle

    P1457 城堡 The Castle 题目描述 我们憨厚的USACO主人公农夫约翰(Farmer John)以无法想象的运气,在他生日那天收到了一份特别的礼物:一张“幸运爱尔兰”(一种彩票).结果这 ...

  9. MongoDB小结20 - find【查询条件$size】

    size可以获得指定数组长度的文档 db.user.find({"fruit":{"$size":3}},{"_id":0}) { &quo ...

  10. storm ——Understanding the Parallelism of a Storm Topology

    http://www.michael-noll.com/blog/2012/10/16/understanding-the-parallelism-of-a-storm-topology/ 这篇文章好 ...