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. RMAN RECOVERY

    Data Recovery Advisor The health monitor and the ADR The capabilities and limitations of DRA using t ...

  2. linux sed 命令的用法

    原文  http://blog.chinaunix.net/uid-24426415-id-77244.html ------------------------------------------- ...

  3. LeetCode 705. Design HashSet (设计哈希集合)

    题目标签:HashMap 题目让我们设计一个 hashset,有add,contains,remove 功能. 建立一个boolean array,index 是数字的值,具体看code. Java ...

  4. kafka01

    消息队列松耦合 消息队列

  5. Windows中的时区信息

    时区 Windows API 没有提供直接得到特定时区信息的功能,Windows 系统中的时区信息存储在注册表中下面的位置: HKEY_LOCAL_MACHINE     SOFTWARE      ...

  6. 【总结】设备树语法及常用API函数【转】

    本文转载自:http://blog.csdn.net/fengyuwuzu0519/article/details/74352188 一.DTS编写语法   二.常用函数 设备树函数思路是:uboot ...

  7. Silverlight 2学习笔记二:三个基本布局控件(Canvas、StackPanel、Grid )

    这篇文章主要是翻译了ScottGu博客的文章:Silverlight Tutorial Part 2: Using Layout Management.虽然是翻译,但通过笔记记录,我发现对这三个布局控 ...

  8. 【SCOI 2011】 糖果

    [题目链接] 点击打开链接 [算法] 当x = 1时,连边(a,b,0)和(b,a,0) 当x = 2时,连边(a,b,1) 当x = 3时,连边(b,a,0) 当x = 4时,连边(b,a,1) 当 ...

  9. 第二周 Leetcode 493. Reverse Pairs(HARD)

    leetcode 493跟经典的逆序对问题没有什么区别, 首先考虑对数组前半部和后半部求逆序对数,若能保证两段数组都有序,则显然可以在线性时间内求出对数. 所以我们采用归并排序的方法,一方面让数组有序 ...

  10. E20170618-hm

    sentinel   n. 岗哨,哨兵; node   n. 节点; (计算机网络的) 节点; [医] 结节; 植物的节; traverse  n. 穿过; 横贯,横切; 横木; [建] 横梁; vt ...