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. css3的高级而有用且很少人知道的属性和样式

    1.-webkit-mask 概属性可以给一个元素添加蒙层,蒙层可以是一个渐变或者半透明的png图片,这张png图片的 alpha 为 0 的位置会不显示元素这部分,alpha 为 1 的位置会显示元 ...

  2. installshield中的installscript编程

    上一篇说道了InstallShield高速建立一个打包程序,如今说说InstallShield里面的installscript脚本. 该文属于个人理解.參考一些相关文章. 相关资料下载地址:http: ...

  3. 七、备忘录模式Memento(行为型模式)

    其目的是,在不违反封装原则的前提下.採集和备份一个对象的内部状态以便这个对象能够在以后恢复到之前的某个状态. 在Memento模式中,有例如以下角色: 1.Memento (备忘录) * 存储Orig ...

  4. ITK Configuring and Building in VisualStudio及hello world程序编译

    1. ITK Configuring and Building in VisualStudio With Visual Studio 2010 on Windows 7 (32-bit): Launc ...

  5. Chrome格式化JavaScript

    在network或者source的tab中找到对应的JavaScript文件 重点在右下角的{}图标,点击一下,就会帮你自动格式化了 https://plus.google.com/+AddyOsma ...

  6. Kaggle "Microsoft Malware Classification Challenge"——就是沙箱恶意文件识别,有 Opcode n-gram特征 ASM文件图像纹理特征 还有基于图聚类方法

    使用图聚类方法:Malware Classification using Graph Clustering 见 https://github.com/rahulp0491/Malware-Classi ...

  7. cf2.25

    T1 题意:判断给出的数中有多少不同的大于的数. content:傻逼题,5min手速 T2 题意:给出p.y,输出y~p+1中最大一个不是2-p的倍数的数. content:答案很简单,但是很难想到 ...

  8. Oracle查询列重命名

    select count(*) 呼入量 from crm_cisco_call_detail

  9. bzoj4247: 挂饰(背包dp)

    4247: 挂饰 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1136  Solved: 454[Submit][Status][Discuss] ...

  10. HTML--使用单选框、复选框,让用户选择

    在使用表单设计调查表时,为了减少用户的操作,使用选择框是一个好主意,html中有两种选择框,即单选框和复选框,两者的区别是单选框中的选项用户只能选择一项,而复选框中用户可以任意选择多项,甚至全选.请看 ...