codeforces_731D_(前缀和)(树状数组)
2 seconds
256 megabytes
standard input
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.
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.
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.
4 3
2 3 2
1 1
3 2 3 1
4 2 3 1 2
1
2 5
2 4 2
2 4 2
0
4 4
1 2
1 3
1 4
1 2
-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_(前缀和)(树状数组)的更多相关文章
- BZOJ-2743: [HEOI2012]采花 前缀和 树状数组
BZOJ-2743 LUOGU:https://www.luogu.org/problemnew/show/P4113 题意: 给一个n长度的序列,m次询问区间,问区间中出现两次及以上的数字的个数.n ...
- Gym - 101630G The Great Wall (前缀和+树状数组+二分)
题意:有一个序列,一开始所有的元素都是ai,你可以选择两个长度相等的区间,如果某个元素被一个区间覆盖,那么变为bi,如果被两个区间都覆盖,那么变为ci.问所有区间的选择方法中产生的第k小的元素总和. ...
- [CSP-S模拟测试]:斯诺(snow)(数学+前缀和+树状数组)
题目传送门(内部题37) 输入格式 第一行一个整数$n$,表示区间的长度. 第二行一个长度为$n$的只包含$0,1,2$的字符串,表示给出的序列. 输出格式 一行一个整数,表示革命的区间的数量. 样例 ...
- HDU 3303 Harmony Forever 前缀和+树状数组||线段树
Problem Description We believe that every inhabitant of this universe eventually will find a way to ...
- AtCoder4351 Median of Medians 二分, 树状数组
题目大意 定义一个从小到大的数列的中位数为第 $ \frac{n}{2}+1 $ 项.求一个序列的所有连续子序列的中位数的中位数. $ (n \leqslant 100000)$ 问题分析 由于\(n ...
- poj3468 A Simple Problem with Integers (树状数组做法)
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- HDU 4325 离散化+树状数组 或者 不使用树状数组
题意:给出一些花的开放时间段,然后询问某个时间点有几朵花正在开放. 由于ti<1e9,我们需要先将时间离散化,然后将时间点抽象为一个数组中的点,显然,我们需要进行区间更新和单点查询,可以考虑线段 ...
- gym102220H 差分+树状数组(区间修改和输出)
这题目很有意思,让我学会了树状数组的差分,更加深刻理解了树状数组 树状数组的差分写法 void add(int x,int k) { for (int i = x;i <= n;i += low ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...
随机推荐
- VS2017 +NetCore2.2.0+WebApi项目整合SwaggerUI 以及遇到的坑
1.新建一个WebApi项目,这里不说了. 2.打开项目nuget管理控制台,在 https://www.nuget.org/ 搜索swagger的包:Swashbuckle.AspNetCore , ...
- win7下安装SQLSERVER2000
来自为知笔记(Wiz)
- A new Graph Game
点击打开链接 题意:给你一张N个节点的无向图.然后给出M条边,给出第 I 条边到第J条边的距离.然后问你是否存在子环,假设存在,则输出最成环的最短距离和 解析:构图:选定源点及汇点,然后将源点至个点流 ...
- HDU - 3631 Shortest Path(Floyd最短路)
Shortest Path Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u SubmitStat ...
- 500万url的es 批删除
bash 循环 算术计算 读写文件 [root@hadoop2 ~]# sh looh.sh1234LIZ1 2 3 4 0 1 2 3 4 5 6 7 8 9 10 0games:x:12:100 ...
- 大神是如何装逼的 之 vim插件使用taglist和nerdtree
本文转载自:http://blog.csdn.net/yaoxingshuai/article/details/51385332 本文主要讲述如何在vim下配置taglist,nerdtree(看代码 ...
- [luogu4735]最大异或和
https://zybuluo.com/ysner/note/1238161 题面 给定一个初始长度为\(N\)的非负整数序列\(\{a\}\). 有\(m\)个操作,操作分为两种: 在序列末尾加一个 ...
- AAC的AudioSpecificConfig细节
AAC格式里有个复杂的AudioSpecificConfig, 在FLV格式里称为AAC sequence header.在正式播放ADTS AAC数据包之前,需要用AudioSpecificConf ...
- 杂项:E-Learning
ylbtech-杂项:E-Learning 1.返回顶部 1. E-Learning:英文全称为(Electronic Learning),中文译作“数字(化)学习”.“电子(化)学习”.“网络(化) ...
- ref 和out的区别
在C#语言中,参数的传递有两种,一种是值传递,一种是引用传递.ref与out这两种方式都属于引用传递,只是他们的用法稍有不同.下面看几个例子 使用ref的例子 class test { static ...