题目传送门

题目大意:

  给出m个1-n的全排列,问这m个全排列中有几个公共子串。

思路:

  首先单个的数字先计算到答案中,有n个。

  然后考虑多个数字,如果有两个数字相邻,那么在m个串中必定都能找到这两个数字并且位置相邻。那么我们枚举1-n所有的数字,比如先枚举p1是1,那p2就是在全排列1中p1后面的数字,然后往下面找,看看下面的全排列,p2是不是也在p1的后面,如果是的话,再看p3是不是在p2的后面。

  要注意的是,如果我们发现p2在p1的后面,那么我们就把p1p2连起来,合并到dp[1][p1]中(实际上最好用p1的位置当成第二维参数),并且标记一下,下次如果找到了一个被标记过的数字,我们就可以直接把

dp[1][p1]*dp[1][p2]累加到答案中,p2后面的片段就不需要再次扫描了。

  时间复杂度的话,每个元素最多和后面连一次,和前面连一次,所以最多被扫两次(实际上应该是1次,因为这个两次重复计算了,要除以2)。也就是说时间复杂度是O(nm)级别的。

  

//#pragma comment(linker,"/STACK:102400000,102400000")
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<stdlib.h>
//#include<unordered_map>
#define lson l,mid,rt<<1
#define rson mid+1,r,(rt<<1)|1
#define CLR(a,b) memset(a,b,sizeof(a))
#define mkp(a,b) make_pair(a,b)
typedef long long ll;
using namespace std;
inline ll read() {
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'') {
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='') {
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
const int maxn=1e5+;
int a[][maxn],pos[][maxn],n,m;
ll dp[][maxn];
bool vis[maxn];
ll ans;
int main() {
n=read(),m=read();
for(int i=; i<=m; i++) {
for(int j=; j<=n; j++) {
a[i][j]=read();
pos[i][a[i][j]]=j;
dp[i][j]=;
}
}
ans=n;
int flag;
for(int i=; i<=n; i++) {
int temp=pos[][i];//第一个指针
if(vis[temp])continue;
vis[temp]=;
int p1=i; //前元素
if(temp==n) {
continue;
}
int temp2=temp+;
int p2=a[][temp2];//后元素
flag=; while(flag) {
for(int k=; k<=m; k++) {
int xx=pos[k][p1];
if(a[k][xx+]==p2)continue;
flag=;
break;
}
if(flag) {
if(vis[temp2]==) { //已经访问过
ans+=(ll)dp[][temp]*dp[][temp2];
dp[][temp]+=dp[][temp2];
flag=;
} else {
ans+=(ll)dp[][temp]*dp[][temp2];
dp[][temp]+=dp[][temp2];
p1=p2;
vis[temp2]=;
temp2++;
if(temp2>n)break;
p2=a[][temp2];
} }
// printf("temp1:%d temp2:%d\n",temp,temp2);
} }
printf("%lld\n",ans);
}
D. Mysterious Crime
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Acingel is a small town. There was only one doctor here — Miss Ada. She was very friendly and nobody has ever said something bad about her, so who could've expected that Ada will be found dead in her house? Mr Gawry, world-famous detective, is appointed to find the criminal. He asked mm neighbours of Ada about clients who have visited her in that unlucky day. Let's number the clients from 11 to nn. Each neighbour's testimony is a permutation of these numbers, which describes the order in which clients have been seen by the asked neighbour.

However, some facts are very suspicious – how it is that, according to some of given permutations, some client has been seen in the morning, while in others he has been seen in the evening? "In the morning some of neighbours must have been sleeping!" — thinks Gawry — "and in the evening there's been too dark to see somebody's face...". Now he wants to delete some prefix and some suffix (both prefix and suffix can be empty) in each permutation, so that they'll be non-empty and equal to each other after that — some of the potential criminals may disappear, but the testimony won't stand in contradiction to each other.

In how many ways he can do it? Two ways are called different if the remaining common part is different.

Input

The first line contains two integers nn and mm (1≤n≤1000001≤n≤100000, 1≤m≤101≤m≤10) — the number of suspects and the number of asked neighbors.

Each of the next mm lines contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n). It is guaranteed that these integers form a correct permutation (that is, each number from 11 to nn appears exactly once).

Output

Output a single integer denoting the number of ways to delete some prefix and some suffix of each permutation (possibly empty), such that the remaining parts will be equal and non-empty.

Examples
input

Copy
3 2
1 2 3
2 3 1
output

Copy
4
input

Copy
5 6
1 2 3 4 5
2 3 1 4 5
3 4 5 1 2
3 5 4 2 1
2 3 5 4 1
1 2 3 4 5
output

Copy
5
input

Copy
2 2
1 2
2 1
output

Copy
2
Note

In the first example, all possible common parts are [1][1], [2][2], [3][3] and [2,3][2,3].

In the second and third examples, you can only leave common parts of length 11.

CodeForces 1043D Mysterious Crime 区间合并的更多相关文章

  1. Codeforces Round #276 (Div. 1) E. Sign on Fence (二分答案 主席树 区间合并)

    链接:http://codeforces.com/contest/484/problem/E 题意: 给你n个数的,每个数代表高度: 再给出m个询问,每次询问[l,r]区间内连续w个数的最大的最小值: ...

  2. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  3. CodeForces - 357C Knight Tournament 伪并查集(区间合并)

    Knight Tournament Hooray! Berl II, the king of Berland is making a knight tournament. The king has a ...

  4. CodeForces - 566D Restructuring Company 并查集的区间合并

    Restructuring Company Even the most successful company can go through a crisis period when you have ...

  5. CodeForces - 587E[线段树+线性基+差分] ->(线段树维护区间合并线性基)

    题意:给你一个数组,有两种操作,一种区间xor一个值,一个是查询区间xor的结果的种类数 做法一:对于一个给定的区间,我们可以通过求解线性基的方式求出结果的种类数,而现在只不过将其放在线树上维护区间线 ...

  6. [题解]Codeforces Round #519 - D. Mysterious Crime

    [题目] D. Mysterious Crime [描述] 有m个n排列,求一共有多少个公共子段. 数据范围:1<=n<=100000,1<=m<=10 [思路] 对于第一个排 ...

  7. POJ 3667 Hotel(线段树 区间合并)

    Hotel 转载自:http://www.cnblogs.com/scau20110726/archive/2013/05/07/3065418.html [题目链接]Hotel [题目类型]线段树 ...

  8. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

  9. HDU 1540 Tunnel Warfare 平衡树 / 线段树:单点更新,区间合并

    Tunnel Warfare                                  Time Limit: 4000/2000 MS (Java/Others)    Memory Lim ...

随机推荐

  1. Docker安装和使用Tomcat

    1.搜索Tomcat镜像                          docker search tomcat 2.下载Tomcat官方镜像                   docker p ...

  2. 关于box-sizing属性

    写在前面 文中错误或不足之处欢迎指正批评,共同交流! 在项目中写css组件时遇到一个问题: 要求两个按钮均分其父元素宽度,且父元素宽度不固定,像这样: 第一反应很自然的想到使用flex布局,但是由于需 ...

  3. $.post()参数及返回值

    JQuery中的$.post()函数 先看一个例子:$.post("adduser.ashx", { "name": name, "sex" ...

  4. MYSQL优化——索引覆盖

    索引覆盖:如果查询的列恰好是索引的一部分,那么查询只需要在索引文件上进行,不需要进行到磁盘中找数据,若果查询得列不是索引的一部分则要到磁盘中找数据. 建表: create table test_ind ...

  5. 远程访问Function时报错Remote table-valued function calls are not allowed.

    开始是这样调用的:select * from [LinkedServer].[db name].dbo.[function name](param1, param2) 原因: Only table-v ...

  6. BBS后台发送邮件&修改文章

    一:Django发送邮件 在setting中配置 # EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST ...

  7. javascript总结10:JavaScript的Switch语句

    1 switch语句 的作用: switch 语句用于基于不同的条件来执行不同的动作. 每当满足一个变量条件,就会执行当前的case内容. break 关键字用于跳出switch代码块.会终止swit ...

  8. eWebEditor9.x整合教程-Xproer.WordPaster

    版权所有 2009-2017 荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webplug/wordpa ...

  9. ios7适配--隐藏status bar

    //viewDidload if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) { // iOS 7 ...

  10. 【leetcode】Move Zeroes

    Move Zeroes 题目: Given an array nums, write a function to move all 0‘s to the end of it while maintai ...