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. HTML表单数据转JSON

    问题描述 后端使用如下方式接收前端传入参数: @PostMapping(value = "/test", produces = MediaType.APPLICATION_JSON ...

  2. Spring Boot在开发时实现热部署(开发时修改文件保存后自动重启应用)(spring-boot-devtools)

    热部署是什么 大家都知道在项目开发过程中,常常会改动页面数据或者修改数据结构,为了显示改动效果,往往需要重启应用查看改变效果,其实就是重新编译生成了新的Class文件,这个文件里记录着和代码等对应的各 ...

  3. Ubuntu中LightDM是什么(转)

    LightDM(Light Display Manager)是一个全新的轻量级Linux桌面显示管理器,而传统的Ubuntu是使用GNOME桌面标准的GDM. LightDM是一个跨桌面显示管理器,其 ...

  4. CentOS设置程序开机启动程序/服务的方法(转)

    注意:CentOS 6下基本没什么问题,CentOS 7估计不一定能行. 在CentOS系统下,主要有两种方法设置自己安装的程序开机启动. 1.把启动程序的命令添加到/etc/rc.d/rc.loca ...

  5. namenode启动成功,但是不能通过web访问50070问题

    我在CentOS遇到这个问题,50070不行但8088可以,尝试了各种方法无法解决,各个进程全都启动,格式化namenode,各种配置正常均无法解决.后来觉得是默认访问端口没有生效,所以尝试添加端口配 ...

  6. Cocos2d-HTML5搭载nodejs express3

    源代码 已经上传到github Cocos2d-HTML5 入门第一天搭载了express3 server.Cocos2d-html5配置改了不少路径,改得有点乱. 今天又重搭了一遍server,力求 ...

  7. php 把一个数组分成有n个元素的二维数组的算法

    一.第一种解法 <?php //把一个数组分成几个数组 //$arr 是数组 //$num 是数组的个数 function partition($arr,$num){ //数组的个数 $list ...

  8. CountDownTimer完整具体演示样例

    MainActivity例如以下: package cc.cv; import android.os.Bundle; import android.os.CountDownTimer; import ...

  9. FOBiz组合模糊查询

    List list= delegator.findList("Entity",condition , null, null, null, false);其中condition为:组 ...

  10. YTU 2720: 删出多余的空格

    2720: 删出多余的空格 时间限制: 1 Sec  内存限制: 128 MB 提交: 338  解决: 201 题目描述 小平在给弟弟检查英语作业时时,发现每个英语句子单词之间的空格个数不等,请你编 ...