A sequence a=[a1,a2,…,al]a=[a1,a2,…,al] of length ll has an ascent if there exists a pair of indices (i,j)(i,j) such that 1≤i<j≤l1≤i<j≤l and ai<ajai<aj. For example, the sequence [0,2,0,2,0][0,2,0,2,0] has an ascent because of the pair (1,4)(1,4), but the sequence [4,3,3,3,1][4,3,3,3,1] doesn’t have an ascent.

Let’s call a concatenation of sequences pp and qq the sequence that is obtained by writing down sequences pp and qq one right after another without changing the order. For example, the concatenation of the [0,2,0,2,0][0,2,0,2,0] and [4,3,3,3,1][4,3,3,3,1] is the sequence [0,2,0,2,0,4,3,3,3,1][0,2,0,2,0,4,3,3,3,1]. The concatenation of sequences pp and qq is denoted as p+qp+q.

Gyeonggeun thinks that sequences with ascents bring luck. Therefore, he wants to make many such sequences for the new year. Gyeonggeun has nn sequences s1,s2,…,sns1,s2,…,sn which may have different lengths.

Gyeonggeun will consider all n2n2 pairs of sequences sxsx and sysy (1≤x,y≤n1≤x,y≤n), and will check if its concatenation sx+sysx+sy has an ascent. Note that he may select the same sequence twice, and the order of selection matters.

Please count the number of pairs (x,yx,y) of sequences s1,s2,…,sns1,s2,…,sn whose concatenation sx+sysx+sy contains an ascent.

Input
The first line contains the number nn (1≤n≤1000001≤n≤100000) denoting the number of sequences.

The next nn lines contain the number lili (1≤li1≤li) denoting the length of sisi, followed by lili integers si,1,si,2,…,si,lisi,1,si,2,…,si,li (0≤si,j≤1060≤si,j≤106) denoting the sequence sisi.

It is guaranteed that the sum of all lili does not exceed 100000100000.

Output
Print a single integer, the number of pairs of sequences whose concatenation has an ascent.

Examples
Input
5
1 1
1 1
1 2
1 4
1 3
Output
9
Input
3
4 2 0 2 0
6 9 9 8 8 7 7
1 6
Output
7
Input
10
3 62 24 39
1 17
1 99
1 60
1 64
1 30
2 79 29
2 20 73
2 85 37
1 100
Output
72
Note
For the first example, the following 99 arrays have an ascent: [1,2],[1,2],[1,3],[1,3],[1,4],[1,4],[2,3],[2,4],[3,4][1,2],[1,2],[1,3],[1,3],[1,4],[1,4],[2,3],[2,4],[3,4]. Arrays with the same contents are counted as their occurences.
题意:给定n个数组,按照数组加法那样把任意两个数组拼接到一起。如果有1≤i<j≤l且ai <aj这样的,就是一个题目要求的连接串,也可以自己和自己连接。问一共可以有多少个连接串。
思路:如果数组本身就是这样的连接串,那么它和任意一个数组都可以组成题目要求的连接串。如果数组ai不是题目中的连接串,并且这个数组的最大值比其余数组aj的最小值还要小的话,这样就不能组成连接串了(ai+aj不行,但是aj+ai可以)。那么我们可以把所有的不是题目要求的连接串中的最大值最小值求出来,然后对最小值数组排序,之后把最大值数组去最小值数组中二分,找出不符合的,最后用n*n减去就行。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;const int maxx=1e5+100;
vector<int> p[maxx];
int l[maxx],r[maxx],vis[maxx];
int n;int main()
{scanf("%d",&n);int x,y;for(int i=1;i<=n;i++){scanf("%d",&x);for(int j=0;j<x;j++){scanf("%d",&y);p[i].push_back(y);}}memset(vis,0,sizeof(vis));for(int i=1;i<=n;i++){for(int j=0;j<p[i].size();j++){for(int k=j+1;k<p[i].size();k++){if(p[i][j]<p[i][k]){vis[i]=1;break;}}if(vis[i]) break;}}int cnt=0;for(int i=1;i<=n;i++){if(!vis[i]){sort(p[i].begin(),p[i].end());l[++cnt]=p[i][0],r[cnt]=p[i][p[i].size()-1];}}ll num=0;sort(l+1,l+1+cnt);for(int i=1;i<=cnt;i++){int pos=lower_bound(l+1,l+1+cnt,r[i])-l-1;num+=cnt-pos;}cout<<((ll)n*(ll)n)-num<<endl;//注意用long long
}

努力加油a啊,(o)/~

New Year and Ascent Sequence(二分)相关推荐

  1. 1284B. New Year and Ascent Sequence

    B. New Year and Ascent Sequence:题目 小小思维题 #include <bits/stdc++.h> using namespace std; #define ...

  2. CodeForces - 1284B New Year and Ascent Sequence(乱搞)

    题目链接:点击查看 题目大意:规定一个数列中在不改变内部顺序的前提下,至少存在着一组(i,j)满足: i<j a[i]<a[j] 就称该数列为上升数列,现在给出n组数列,每两组数列可以进行 ...

  3. 【置顶】【PAT】PAT甲级题目及分类总结(持续更新ing)

    在2019年3月底,我决定考浙大计院,经过一个月还算凑合的学习,痛定思痛,决定整理整理自己的博客. 粗略估计,大概一个月的时间里我AC了31道题,大概用时40个小时上下,毕竟还要把大部分时间花在笔试上 ...

  4. HDU 6304 Chiaki Sequence Revisited(二分+找规律)

    题目链接 Chiaki Sequence Revisited Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

  5. 2021牛客暑期多校训练营7 K-xay loves sequence(主席树+二分)

    K-xay loves sequence 首先不考虑模kkk的限制,容易想到对原数组做一个差分得到di=ai−ai−1d_i=a_i-a_{i-1}di​=ai​−ai−1​,显然对于∀1≤i≤nai ...

  6. pat 1085 Perfect Sequence (25) 二分查找

    题意 给我们一个数串 让我们从中挑选数字 构成一个集合 使得集合中最小元素m和最大元素M 满足 m*p>=M 求出集合的最大数量 分析 看下条件就可以发现 m*p >=M 也就是在数串中找 ...

  7. HDU4080 Stammering Aliens(二分 + 后缀数组)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4080 Description Dr. Ellie Arroway has establish ...

  8. HDU2199,HDU2899,HDU1969,HDU2141--(简单二分)

    二分是一种很有效的减少时间开销的策略, 我觉得单列出二分专题有些不太合理, 二分应该作为一中优化方法来考虑 这几道题都是简单的使用了二分方法优化, 二分虽然看似很简单, 但一不注意就会犯错. 在写二分 ...

  9. poj3061尺取法/前缀和 二分(java)

    今天遇到这题因为以前没见到过,当时就是想着应该有着一个很简单的方法可以过但是奈何就是没思路.后来看了别人思路写了下来.学习了尺取法 poj3061 题目介绍: Description A sequen ...

最新文章

  1. 【Visual C++】游戏开发笔记十三 游戏输入消息处理(二) 鼠标消息处理
  2. Flutter 系列(一)安装与配置
  3. Eigen与Matlab语法及语义辞典
  4. idea代码样式模板_阿里p3c(代码规范,eclipse插件、模版,idea插件)
  5. java 1.7 linux rpm,linux下用rpm 安装jdk 7的jdk-7u79-linux-x64.rpm
  6. mysql查询三个月内的_如何在三个月内自学攻克雅思6.5分?
  7. 一种用javascript实现的比较兼容的回到顶部demo + 阻止事件冒泡
  8. 华为机试——字符串分隔
  9. 基于顺序存储结构的图书信息表的新图书的入库(C++)
  10. Linux (Ubuntu) 命令
  11. SpringBoot实战(七):替代if的优雅方案,提高程序扩展性
  12. android UI 标签
  13. 2022年安全员-A证操作证考试题模拟考试平台操作
  14. 时间触发协议(Time triggered protocol)概述
  15. 计算机毕业设计Java高原特色农产品网站设计(源码+系统+mysql数据库+Lw文档)
  16. 这样的萌妹,谁不爱呢?
  17. 5. Django templates模板 (一)
  18. Linux内核发布时间表
  19. p5405 [CTS2019]氪金手游
  20. Unsupported SQL of `create database xxx CHARACTER SET UTF8

热门文章

  1. java中不用impore导入的_java import机制(不用IDE)
  2. 依赖编译_开源项目的编译优化实践
  3. QStyleOptionGraphicsItem实现自绘按钮悬浮按下状态
  4. Android开发之自定义控件的基本介绍(附源码)
  5. NSLog中格式符列表
  6. linux怎么衡量负载大小,如何查看linux机器的平均负载
  7. tomcat的备份脚本
  8. oracle的SCN和Checkpoint_Change#的关系
  9. 平台系统表怎么修改为普通表
  10. 浅谈MVC设计模式和SSH框架的关系