bzoj 1106
思路:很容易就能想到统计没两对点之间的未匹配点的个数。 在想怎么用数据结构维护这个东西,
没有想到用树状数组能很巧妙地维护出来, 就写了个莫队。。。
莫队:暴力维护就好了。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int,int>
#define piii pair<int, pair<int,int> > using namespace std; const int N = 1e5 + ;
const int M = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const int B = ; int n, a[N], cnt[N], l, r, ret;
struct Qus {
int l, r;
bool operator < (const Qus &rhs) const {
if(l / == rhs.l / ) return r < rhs.r;
return l / < rhs.l / ;
}
} qus[N]; inline void update(int x) {
ret += cnt[a[x]] ? - : ;
cnt[a[x]] ^= ;
} int main() {
scanf("%d", &n);
n <<= ; for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
if(qus[a[i]].l) qus[a[i]].r = i - ;
else qus[a[i]].l = i + ;
} sort(qus + , qus + + n / );
l = , r = , ret = ;
LL ans = ; for(int i = ; i <= n / ; i++) {
int L = qus[i].l, R = qus[i].r;
if(L > R) continue; while(r < R) update(++r);
while(l > L) update(--l);
while(r > R) update(r--);
while(l < L) update(l++); ans += ret;
} printf("%lld\n", ans / );
return ;
}
/*
*/
树状数组:
对于第一次遇到的数a[ i ], 我们往 i 位置加1, 对于第二次遇到的数,我们往 pre[ a[ i ] ] 位置减1,然后统计区间( pre[ a[ i ] ], i )的值加到答案中。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int,int>
#define piii pair<int, pair<int,int> > using namespace std; const int N = 1e5 + ;
const int M = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, a[N], pre[N]; void modify(int x, int v) {
for(int i = x; i < N; i += i & -i)
a[i] += v;
} int query(int x) {
int ans = ;
for(int i = x; i; i -= i & -i)
ans += a[i];
return ans;
} int main() {
scanf("%d", &n);
LL ans = ;
for(int i = ; i <= * n; i++) {
int x; scanf("%d", &x);
if(!pre[x]) {
pre[x] = i;
modify(i, );
} else {
modify(pre[x], -);
ans += query(i) - query(pre[x]);
}
} printf("%lld\n", ans);
return ;
}
/* */
bzoj 1106的更多相关文章
- [BZOJ 1106] [POI2007] 立方体大作战tet 【树状数组】
题目链接:BZOJ - 1106 题目分析 从1到2n枚举每一个位置. 如果枚举到某一个数,这个数已经是第二次出现,那么就看它和第一次出现的位置之间有多少数还没有被匹配,有多少没有匹配的就要进行多少次 ...
- BZOJ 1106: [POI2007]立方体大作战tet
1106: [POI2007]立方体大作战tet Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 682 Solved: 496[Submit][St ...
- BZOJ 1106 [POI2007]立方体大作战tet(树状数组)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1106 [题目大意] 给定玩家一个有2n个元素的栈,元素一个叠一个地放置. 这些元素拥有 ...
- BZOJ 1106 立方体大作战
BIT. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm&g ...
- bzoj 1106 [POI2007]立方体大作战tet 树状数组优化
[POI2007]立方体大作战tet Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 821 Solved: 601[Submit][Status][ ...
- BZOJ 1106: [POI2007]立方体大作战tet 树状数组 + 贪心
Description 一个叫做立方体大作战的游戏风靡整个Byteotia.这个游戏的规则是相当复杂的,所以我们只介绍他的简单规 则:给定玩家一个有2n个元素的栈,元素一个叠一个地放置.这些元素拥有n ...
- [原博客] POI系列(2)
正规.严谨.精妙. -POI bzoj 1098 : [POI2007]办公楼biu 如果把互相有手机号的建边得到一个无向图,那么这个图的补图的连通分量个数就是答案了.因为互相没手机号的必然在同一个连 ...
- 【BZOJ】1106: [POI2007]立方体大作战tet
题意 给定一个长度为\(2n(1 \le n \le 500000)\)的序列,\(1\)~\(n\)各出现两次,可以交换相邻两项,两个同样的数放在一起会对消,求把所有数对消的最小交换次数. 分析 如 ...
- BZOJ 2127: happiness [最小割]
2127: happiness Time Limit: 51 Sec Memory Limit: 259 MBSubmit: 1815 Solved: 878[Submit][Status][Di ...
随机推荐
- angular2 获取到的数据无法实时更新的问题
在修改完组件数据之后调用下面两句: this.changeDetectorRef.markForCheck(); this.changeDetectorRef.detectChanges(); 注入到 ...
- springMVC参数的获取区别
在springMVC中我们一般使用注解的形式来完成web项目,但是如果不明白springmvc的对于不同注解的应用场景就会很容易犯错误 1.什么是restful形式: 什么是RESTful restf ...
- windows环境下封装条件wait和signal
linux 环境有提供好的pthread_cond_wait() 和 phread_signal().pthread_broadcast() windows需要自己封装,利用semophore控制线程 ...
- sqlserver 2008连接
初次安装使用SQL server 2008时,可能会遇到无法连接到(local)的情况.那么,如何解决此问题? 工具/原料 SQL server 2008 方法/步骤 1 打开SQL se ...
- Struts2版本更新报错:>>> ActionContextCleanUp <<< is deprecated! Please use the new filters!
因低版本Struts2存在漏洞,更新为较新的版本.启动时,报如下警告信息: ************************************************************** ...
- Hibernate + mysql 查询伪劣时:= 出现 Space is not allowed after parameter prefix ':' MySQL异常
需求: 要求查询一个站点在地市和省的排名信息出来. 效果图: 代码实现: 注释的部分是起初采用的hibernate 查询,但无论怎么写都会报 space is not allowed after pa ...
- Shiro实战教程(一)
Shiro完整架构图 Shiro认证过程 Shiro授权的内部处理机制 Shiro 支持三种方式的授权 1.编程式:通过写if/else 授权代码块完成: Subject subject = Secu ...
- 无聊,纯css写了个评分鼠标移入的效果
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- LintCode 391: Count Of Airplanes
LintCode 391: Count Of Airplanes 题目描述 给出飞机的起飞和降落时间的列表,用 interval 序列表示. 请计算出天上同时最多有多少架飞机? 样例 对于每架飞机的起 ...
- 微信小程序迁移到头条小程序工具
最近公司需要将微信小程序迁移到头条小程序,比较得知微信和头条小程序的写法类似,只有文件名称不同,相关的指令不同,以及头条在ttml绑定的数据不可使用function,于是就写了node脚本来实现这些重 ...