CodeForces 1284 B. New Year and Ascent Sequence

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

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

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

Gyeonggeun will consider all n2 pairs of sequences sx and sy (1≤x,y≤n), and will check if its concatenation sx+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,y) of sequences s1,s2,…,sn whose concatenation sx+sy contains an ascent.

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

The next n lines contain the number li (1≤li) denoting the length of si, followed by li integers si,1,si,2,…,si,li (0≤si,j≤106) denoting the sequence si.

It is guaranteed that the sum of all li does not exceed 100000.

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 9 arrays have an ascent: [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个li长度的串,问有多少个串结合满足结合后的串不是递减序列。

思路
我们首先模拟子段的结合,我们发现就只有两种可能:
本身可以的,他结合后也肯定是
本身不可以的,结合后与另一个本身不可以的,并且最大值小于之前最小的,这个是不可以的。
我们用数组记录每个串的max,min,他本身可以吗。
然后用前缀和来记录比这个数小的有几个,这样可以把o(N^2)降到o(N)

代码

#include <bits/stdc++.h>
typedef long long ll;
const ll mod = 998244353;
using namespace std;
namespace fastIO {
    inline void input(int& res) {
        char c = getchar();res = 0;int f = 1;
        while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
        while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
        res = f ? res : -res;
    }
    inline ll qpow(ll a, ll b) {
        ll ans = 1, base = a;
        while (b) {
            if (b & 1) ans = (ans * base % mod +mod )%mod;
            base = (base * base % mod + mod)%mod;
            b >>= 1;
        }
        return ans;
    }
}
using namespace fastIO;
const int N = 1e5+5;
int Case,n,l;
int a[N],minn[N],f[N],maxx[N],vis[N*10],pre[N*10];
int main(){
	Case=1;
	//input(Case);
	while(Case--){
		input(n);
		ll ans = 0;
		for(int i=1;i<=n;i++){
			input(l);
			bool flag = false;
			int mn = 0x3f3f3f3f,mx = 0;
			for(int j=1;j<=l;j++){
				input(a[j]);
				if(j>=2&&a[j]>a[j-1]) flag = true;
				mn = min(mn,a[j]);
				mx = max(mx,a[j]);
			}
			if(flag) f[i]=1;
			else vis[mx]++;
			maxx[i]=mx;
			minn[i]=mn;
			
		}
		pre[0]=vis[0];
		for(int i=1;i<=1e6;i++){
			pre[i] = pre[i-1] + vis[i];
		}
		for(int i = 1;i<=n;i++){
			//cout<<f[i]<<" "<<ans<<" "<<pre[minn[i]]<<" "<<minn[i]<<endl;
			ans += n;
			if(!f[i]) ans -= pre[minn[i]];
			
		}
		printf("%lld\n",ans);
	}
	return 0;
}
/*
5
1 1
1 1
1 2
1 4
1 3
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值