【USACO 2017FEB】 Why Did the Cow Cross the Road III
2024-08-27 17:58:35
【题目链接】
【算法】
树状数组
【代码】
#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 ; }
最新文章
- 【IScroll深入学习】解决IScroll疑难杂症
- Thenao tutorial – indexing
- Visual C#两分钟搭建BHO IE钩子(转)
- CoderForces 280B(记忆化搜索)
- Educational Codeforces Round 3 E (609E) Minimum spanning tree for each edge
- [Buffalo]MVC架构模式
- EF FluentAPI映射一对多 关系时候报错
- 破译情报-NOIP2016提高组复赛模拟试题
- 了解 : 怎么处理jobbox status drop down list roll back
- ActiveReports 6:如何升级旧版本的项目
- ElasticSearch之 控制相关度原理讲解
- Linux的eth0,eth1,eth2,lo详解
- mysql 通过测试&#39;for update&#39;,深入了解行锁、表锁、索引
- random-----随机数
- 32.Mysql Cluster
- 【HDU5831】Rikka with Parenthesis II(括号)
- 在react+redux+axios项目中使用async/await
- winform 与百度搜索智能提示
- JVM调优——之CMS GC日志分析
- Python中如何获取类属性的列表
热门文章
- JavaScript中的特殊数据类型
- Java ExecutorService四种线程池的例子与说明(转发)
- Orcad中较好的习惯背景
- 关于meta标签的使用,属性的说明
- 九度oj 题目1077:最大序列和
- [K/3Cloud] 创建一个单据转换插件
- android开发里跳过的坑-android studio 错误 Could not find junit:junit:4.12
- Linux下汇编语言学习笔记21 ---
- hdu - 2645 find the nearest station (bfs水)
- POJ 3686_The Windy&#39;s