思路:很容易就能想到统计没两对点之间的未匹配点的个数。 在想怎么用数据结构维护这个东西,

没有想到用树状数组能很巧妙地维护出来, 就写了个莫队。。。

莫队:暴力维护就好了。

#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的更多相关文章

  1. [BZOJ 1106] [POI2007] 立方体大作战tet 【树状数组】

    题目链接:BZOJ - 1106 题目分析 从1到2n枚举每一个位置. 如果枚举到某一个数,这个数已经是第二次出现,那么就看它和第一次出现的位置之间有多少数还没有被匹配,有多少没有匹配的就要进行多少次 ...

  2. BZOJ 1106: [POI2007]立方体大作战tet

    1106: [POI2007]立方体大作战tet Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 682  Solved: 496[Submit][St ...

  3. BZOJ 1106 [POI2007]立方体大作战tet(树状数组)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1106 [题目大意] 给定玩家一个有2n个元素的栈,元素一个叠一个地放置. 这些元素拥有 ...

  4. BZOJ 1106 立方体大作战

    BIT. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm&g ...

  5. bzoj 1106 [POI2007]立方体大作战tet 树状数组优化

    [POI2007]立方体大作战tet Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 821  Solved: 601[Submit][Status][ ...

  6. BZOJ 1106: [POI2007]立方体大作战tet 树状数组 + 贪心

    Description 一个叫做立方体大作战的游戏风靡整个Byteotia.这个游戏的规则是相当复杂的,所以我们只介绍他的简单规 则:给定玩家一个有2n个元素的栈,元素一个叠一个地放置.这些元素拥有n ...

  7. [原博客] POI系列(2)

    正规.严谨.精妙. -POI bzoj 1098 : [POI2007]办公楼biu 如果把互相有手机号的建边得到一个无向图,那么这个图的补图的连通分量个数就是答案了.因为互相没手机号的必然在同一个连 ...

  8. 【BZOJ】1106: [POI2007]立方体大作战tet

    题意 给定一个长度为\(2n(1 \le n \le 500000)\)的序列,\(1\)~\(n\)各出现两次,可以交换相邻两项,两个同样的数放在一起会对消,求把所有数对消的最小交换次数. 分析 如 ...

  9. BZOJ 2127: happiness [最小割]

    2127: happiness Time Limit: 51 Sec  Memory Limit: 259 MBSubmit: 1815  Solved: 878[Submit][Status][Di ...

随机推荐

  1. python 获取外网地址

    def get_ip(): try: url = "http://cn.bing.com/search?q=ip&go=%E6%8F%90%E4%BA%A4&qs=n& ...

  2. 基于javaWeb阶段下的Cookie和Session总结

    1. 会话技术   就是用户在使用浏览器浏览界面的时候,去访问多个页面后一次性关闭浏览器,这个过程叫会话,学习会话技术就是在客户端与服务器进行交互的时候为了能更好的保存数据.在java中会话技术只有C ...

  3. Oracle把本地的dmp备份文件导入到本地的Oracle数据库中语句

    ----------------------------------------------------------------------------- 导入语法 imp usename/passw ...

  4. Oracl闪回数据命令。

    当数据库操作没有备份,并且误删数据.可闪回任何 当前闪回15分钟前数据库状态.  alter table BASE_APPOINT_LOG enable row movement;flashback  ...

  5. 51nod1471 小S的兴趣

    题目来源: CodeForces 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 320 小S喜欢有趣的事.但是,每个人的兴趣都是独特的.小S热衷于自问自答.有一天,小S想出了一个问题 ...

  6. .NET中的异常和异常处理

    .NET中的异常(Exception) .net中的中异常的父类是Exception,大多数异常一般继承自Exception. 可以通过编写一个继承自Exception的类的方式,自定义异常类! 异常 ...

  7. centOS7 vsftp ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS) 启动失败问题?

    [root@localhost c]# systemctl status vsftpd.service ● vsftpd.service - Vsftpd ftp daemon Loaded: loa ...

  8. [POI 2008&洛谷P3467]PLA-Postering 题解(单调栈)

    [POI 2008&洛谷P3467]PLA-Postering Description Byteburg市东边的建筑都是以旧结构形式建造的:建筑互相紧挨着,之间没有空间.它们共同形成了一条长长 ...

  9. POJ 3734 Blocks (矩阵快速幂)

    题目链接 Description Panda has received an assignment of painting a line of blocks. Since Panda is such ...

  10. HDU 1081 To The Max (dp)

    题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...