777C Alyona and Spreadsheet

本文介绍了一个关于表格排序的问题,即给定一个整数表格,在指定的行范围内判断是否存在至少一列是非递减排列的。通过动态规划的方法进行优化,使得查询效率大大提高。

During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables.

Now she has a table filled with integers. The table consists of n rows and m columns. By ai, j we will denote the integer located at the i-th row and the j-th column. We say that the table is sorted in non-decreasing order in the column j if ai, j ≤ ai + 1, j for all i from 1 to n - 1.

Teacher gave Alyona k tasks. For each of the tasks two integers l and r are given and Alyona has to answer the following question: if one keeps the rows from l to r inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such j that ai, j ≤ ai + 1, j for all i from l to r - 1 inclusive.

Alyona is too small to deal with this task and asks you to help!

Input

The first line of the input contains two positive integers n and m (1 ≤ n·m ≤ 100 000) — the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table.

Each of the following n lines contains m integers. The j-th integers in the i of these lines stands for ai, j (1 ≤ ai, j ≤ 109).

The next line of the input contains an integer k (1 ≤ k ≤ 100 000) — the number of task that teacher gave to Alyona.

The i-th of the next k lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n).

Output

Print "Yes" to the i-th line of the output if the table consisting of rows from li to ri inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No".

输入一个矩阵A,取矩阵的第l行至第r行,问其中是否有一列是非递减排列的。

动态规划。首先想到设dp[i][j]为第j列中,满足从第k行至第i行为非递减的最小的k。dp[i][j] = A[i][j] >= A[i - 1][j] ? A[i - 1][j] : i;当且仅当k<=l时这一列满足条件。所以搜索每一列,只要找到这样满足条件的一列就输出Yes。k个查询,复杂度为O(mn + nk)。

上面的做法会超时。每次给出一组l和r的时候,矩阵中所有的列都会被取出,所以不用每次都遍历所有列,先在dp[i][j]中找出每一行最小的,只要最小的比l小那么这些列中一定有一列满足条件,复杂度为O(mn)。查询的时候只要O(k)。

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main() {
	int m, n; cin >> m >> n;
	vector<vector<int>> sheet(m, vector<int>(n));
	vector<vector<int>> dp(m, vector<int>(n));
	for (int i = 0; i < m; i++) {
		for (int j = 0; j < n; j++) {
			cin >> sheet[i][j];
		}
	}
	for (int i = 0; i < n; i++) {
		dp[0][i] = 0;
	}
	for (int i = 1; i < m; i++) {
		for (int j = 0; j < n; j++) {
			if (sheet[i][j] >= sheet[i - 1][j]) {
				dp[i][j] = dp[i - 1][j];
			}
			else {
				dp[i][j] = i;
			}
		}
	}
	vector<int> dp2(m, 0);
	for (int i = 0; i < m; i++) {
		dp2[i] = dp[i][0];
		for (int j = 1; j < n; j++) {
			if (dp[i][j] < dp2[i]) {
				dp2[i] = dp[i][j];
			}
		}
	}
	int k; cin >> k;
	for (int i = 0; i < k; i++) {
		int l, r; cin >> l >> r;
		bool f = dp2[r - 1] <= l - 1;
		if (f) printf("Yes\n");
		else printf("No\n");
	}
}





#include<iostream> #include<vector> #define int long long using namespace std; int n,a[200005],fath[200005][31],dis[200005][31]; int cnt[200005],ans[200005]; vector <int> tree[200005]; void dfs(int now) { ans[now] = cnt[now]; for(auto i : tree[now]) { dfs(i); ans[now] += ans[i]; } return; } signed main() { cin >> n; for(int i = 1;i <= n;i++) cin >> a[i]; for(int i = 2;i <= n;i++) { cin >> fath[i][0] >> dis[i][0]; tree[fath[i][0]].push_back(i); } for(int j = 1;j <= 30;j++) { for(int i = 1;i <= n;i++) { if(fath[i][j - 1] != 1) { fath[i][j] = fath[fath[i][j - 1]][j - 1]; dis[i][j] = dis[fath[i][j - 1]][j - 1] + dis[i][j - 1]; } else { dis[i][j] = -1; } } } for(int i = 2;i <= n;i++) { int now = i,away = 0; for(int j = 30;j >= 0;j--) { if(dis[now][j] != -1 && away + dis[i][j] <= a[i]) away += dis[i][j],now = fath[now][j]; } cnt[fath[i][0]]++; cnt[fath[now][0]]--; } dfs(1); for(int i = 1;i <= n;i++) cout << ans[i] << ' '; } # CF739B Alyona and a tree ## 题目描述 Alyona有一棵有 $n$ 个节点的树。这棵树的根节点是 $1$。在每个节点里,Alyona写了一个正整数,在节点 $i$ 她写了正整数 $a_i$ 。另外,她在这棵树上的每条边上写了一个正整数(不同边上可能有不同的数)。 让我们定义 $dist(v,u)$ 作为从 $v$ 到 $u$ 的简单路径上的边权和。 当且仅当 $u$ 在 $v$ 的子树中并且 $dist(v,u)\leq a_u$,顶点 $v$ 控制顶点 $u(v\neq u)$ 。 Alyona想在某些顶点定居。为了做到这件事,她想知道在每个节点 $v$ 能控制几个节点。 ## 输入格式 第一行包含一个整数 $n (1\leq n\leq 2\times 10^5)$ 第二行有 $n$ 个整数 $a_1,a_2,\ldots,a_n(1\leq a_i\leq 10^9)$ ,作为节点 $i$ 的数。 下面的 $n-1$ 行,每行有两个整数。第 $i$ 行包含整数 $p_i,w_i(1\leq p_i\leq n,1\leq w_i\leq 10^9)$ ,分别为节点 $i+1$ 的在树上的父节点和 $p_i$ 和 $(i+1)$ 的边上的数字。 数据保证是个树。 ## 输出格式 输出 $n$ 个整数,第 $i$ 个数为节点 $i$ 能控制的点数。 ## 输入输出样例 #1 ### 输入 #1 ``` 5 2 5 1 4 6 1 7 1 1 3 5 3 6 ``` ### 输出 #1 ``` 1 0 1 0 0 ``` ## 输入输出样例 #2 ### 输入 #2 ``` 5 9 7 8 6 5 1 1 2 1 3 1 4 1 ``` ### 输出 #2 ``` 4 3 2 1 0 ``` ## 说明/提示 在样例中,节点 $1$ 控制了节点 $3$ ,节点 $3$ 控制节点 $5$ (注意,这并不代表节点 $1$ 控制了节点 $5$ ) Translated by @lolte
最新发布
10-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值