D. 80-th Level Archeology
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Archeologists have found a secret pass in the dungeon of one of the pyramids of Cycleland. To enter the treasury they have to open an unusual lock on the door. The lock consists of n words, each consisting of some hieroglyphs. The wall near the lock has a round switch. Each rotation of this switch changes the hieroglyphs according to some rules. The instruction nearby says that the door will open only if words written on the lock would be sorted inlexicographical order (the definition of lexicographical comparison in given in notes section).

The rule that changes hieroglyphs is the following. One clockwise rotation of the round switch replaces each hieroglyph with the next hieroglyph in alphabet, i.e. hieroglyph x (1 ≤ x ≤ c - 1) is replaced with hieroglyph (x + 1), and hieroglyph c is replaced with hieroglyph 1.

Help archeologist determine, how many clockwise rotations they should perform in order to open the door, or determine that this is impossible, i.e. no cyclic shift of the alphabet will make the sequence of words sorted lexicographically.

Input

The first line of the input contains two integers n and c (2 ≤ n ≤ 500 000, 1 ≤ c ≤ 106) — the number of words, written on the lock, and the number of different hieroglyphs.

Each of the following n lines contains the description of one word. The i-th of these lines starts with integer li(1 ≤ li ≤ 500 000), that denotes the length of the i-th word, followed by li integers wi, 1, wi, 2, ..., wi, li(1 ≤ wi, j ≤ c) — the indices of hieroglyphs that make up the i-th word. Hieroglyph with index 1 is the smallest in the alphabet and with index c — the biggest.

It's guaranteed, that the total length of all words doesn't exceed 106.

Output

If it is possible to open the door by rotating the round switch, print integer x (0 ≤ x ≤ c - 1) that defines the required number of clockwise rotations. If there are several valid x, print any of them.

If it is impossible to open the door by this method, print  - 1.

Examples
input
4 3
2 3 2
1 1
3 2 3 1
4 2 3 1 2
output
1
input
2 5
2 4 2
2 4 2
output
0
input
4 4
1 2
1 3
1 4
1 2
output
-1

题意:n个单词,c个字母,每一次操作都会使所有单词的所有字母变为它字典序的后一个字母。当所有单词按字典序从小到大排列时,
完成任务,问需要多少此操作。 思路:每两个单词成为字典序,都有一个操作次数的区间,一次枚举相邻的两个单词,求得n-1个区间,在这n-1个区间的交集中的数便满足要求。 前缀和:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define N 500005
#define C 1000005
vector<int> v[N];
int seg[C];
int main()
{
int n,c;
scanf("%d%d",&n,&c);
for(int i=; i<n; i++)
{
int nn;
scanf("%d",&nn);
for(int j=; j<nn; j++)
{
int num;
scanf("%d",&num);
v[i].push_back(num);
}
}
int ok=;
for(int i=; i<n-; i++)
{
int p=;
while()
{
if(p==v[i].size())
{
seg[]+=;
seg[c+]-=;
break;
}
else if(p==v[i+].size())
{
ok=;
break;
}
else if(v[i+][p]!=v[i][p])
{
if(v[i+][p]<v[i][p])
{
seg[c-v[i+][p]+]-=;
seg[c-v[i][p]+]+=;
}
else if(v[i+][p]>v[i][p])
{
seg[]+=;
seg[c-v[i+][p]+]-=;
seg[c-v[i][p]+]+=;
seg[c]-=;
}
break;
}
p++;
}
}
if(!ok)
printf("-1\n");
else
{
int pre=,res=-;
for(int i=; i<=c; i++)
{
pre+=seg[i];
//cout<<pre<<endl;
if(pre==n-)
{
res=i;
break;
}
}
printf("%d\n",res);
}
return ;
}

树状数组:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define N 500005
#define C 1000005 vector<int> v[N];
int n,c,s[C]; int lowbit(int x)
{
return x&(-x);
} void add(int k,int x)
{
for(int i=k; i<=c; i+=lowbit(i))
s[i]+=x;
} int Sum(int k)
{
int res=;
for(int i=k; i>; i-=lowbit(i))
res+=s[i];
return res;
} int main()
{
while(scanf("%d%d",&n,&c)!=EOF)
{
memset(s,,sizeof(s));
for(int i=; i<=n; i++)
v[i].clear();
for(int i=; i<n; i++)
{
int nn;
scanf("%d",&nn);
for(int j=; j<nn; j++)
{
int num;
scanf("%d",&num);
v[i].push_back(num);
}
}
int ok=;
for(int i=; i<n-; i++)
{
int p=;
while()
{
if(p==v[i].size())
{
add(,);
break;
}
else if(p==v[i+].size())
{
ok=-;
break;
}
else if(p<v[i+].size()&&p>=v[i].size())
{
add(,);
break;
}
else if(v[i][p]!=v[i+][p])
{
if(v[i][p]>v[i+][p])
{
add(c-v[i][p]+,);
add(c-v[i+][p]+,-);
}
else if(v[i][p]<v[i+][p])
{
add(,);
add(c-v[i+][p]+,-);
add(c-v[i][p]+,);
add(c+,-);
}
break;
}
p++;
}
}
if(!ok)
printf("-1\n");
else
{
int res=-;
for(int i=; i<=c+; i++)
if(Sum(i)==n-)
{
res=i-;
break;
}
printf("%d\n",res);
}
}
return ;
}
 

codeforces_731D_(前缀和)(树状数组)的更多相关文章

  1. BZOJ-2743: [HEOI2012]采花 前缀和 树状数组

    BZOJ-2743 LUOGU:https://www.luogu.org/problemnew/show/P4113 题意: 给一个n长度的序列,m次询问区间,问区间中出现两次及以上的数字的个数.n ...

  2. Gym - 101630G The Great Wall (前缀和+树状数组+二分)

    题意:有一个序列,一开始所有的元素都是ai,你可以选择两个长度相等的区间,如果某个元素被一个区间覆盖,那么变为bi,如果被两个区间都覆盖,那么变为ci.问所有区间的选择方法中产生的第k小的元素总和. ...

  3. [CSP-S模拟测试]:斯诺(snow)(数学+前缀和+树状数组)

    题目传送门(内部题37) 输入格式 第一行一个整数$n$,表示区间的长度. 第二行一个长度为$n$的只包含$0,1,2$的字符串,表示给出的序列. 输出格式 一行一个整数,表示革命的区间的数量. 样例 ...

  4. HDU 3303 Harmony Forever 前缀和+树状数组||线段树

    Problem Description We believe that every inhabitant of this universe eventually will find a way to ...

  5. AtCoder4351 Median of Medians 二分, 树状数组

    题目大意 定义一个从小到大的数列的中位数为第 $ \frac{n}{2}+1 $ 项.求一个序列的所有连续子序列的中位数的中位数. $ (n \leqslant 100000)$ 问题分析 由于\(n ...

  6. poj3468 A Simple Problem with Integers (树状数组做法)

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  7. HDU 4325 离散化+树状数组 或者 不使用树状数组

    题意:给出一些花的开放时间段,然后询问某个时间点有几朵花正在开放. 由于ti<1e9,我们需要先将时间离散化,然后将时间点抽象为一个数组中的点,显然,我们需要进行区间更新和单点查询,可以考虑线段 ...

  8. gym102220H 差分+树状数组(区间修改和输出)

    这题目很有意思,让我学会了树状数组的差分,更加深刻理解了树状数组 树状数组的差分写法 void add(int x,int k) { for (int i = x;i <= n;i += low ...

  9. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)

    题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...

随机推荐

  1. 在docker上安装运行mysql实例

    ps:实验环境是:CentOS Linux release 7.3  64位1.获取mysql镜像从docker hub的仓库中拉取mysql镜像docker pull mysql查看镜像docker ...

  2. 苹果iPhone连接wifi就进去www.apple.com主页 下边显示 &lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;Success&lt;/TITLE&gt;&lt;/HEAD&gt;&lt;BODY&gt;Success&lt;/BODY&gt;

    苹果iPhone连接wifi就进去www.apple.com主页  下边显示 <HTML><HEAD><TITLE>Success</TITLE>< ...

  3. LeetCode 725. Split Linked List in Parts (分裂链表)

    Given a (singly) linked list with head node root, write a function to split the linked list into k c ...

  4. research plan1111

    Hello prof.Choi 感谢您的来电,与您的这次通话我已经期盼了很久.我来做个自我介绍,我叫陈飞,今年27岁了,是河北地质大学计算机科学专业的本科毕业生.我非常想提高自己的学历,现在经过刘老师 ...

  5. C - Gr-idian MST

    Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement On an xy plane, in an ...

  6. Expressions-->Member lookup

    7.4 Member lookup 在word文档的第140页 A member lookup is the process whereby凭借:通过…:借以:与…一致 the meaning of ...

  7. http协议的MP4文件播放问题的分析

    现在手上有两个链接 (1) http://202.108.16.173/cctv/video/8C/35/EB/E8/8C35EBE84E7B483C8741CF9A60154993/gphone/4 ...

  8. DStream 转换操作------有状态转换操作

    import org.apache.spark.SparkConf import org.apache.spark.streaming.{Seconds, StreamingContext} obje ...

  9. MySQL 字符编码问题详细解释

    http://www.codesoil.net/tag/charset Character Set Problem in PHP + MySQL4.1+ 和许多人一样,我也是在转移blog时才发现这个 ...

  10. 使用psutil模块获取电脑运行信息

    psutil是python的一个用于获取cpu信息的模块,非常好使,以下附上官方的一些example: CPU-> Examples >>> import psutil > ...