Loading

2021“MINIEYE杯”中国大学生算法设计超级联赛(3)1007. Photoshop Layers(前缀和)

Problem Description

Pixels in a digital picture can be represented with three integers (R,G,B) in the range 0 to 255 that indicate the intensity of the red, green, and blue colors. The color of a pixel can be expressed as a six-digit hexadecimal capital string. For example, (R=100,G=255,B=50) can be expressed as ''𝟼𝟺𝙵𝙵𝟹𝟸''.

There are n layers in Photoshop workstation, labeled by 1,2,…,n from bottom to top. The screen will display these layers from bottom to top. In this problem, you only need to handle the case that the color of all the pixels in a layer are the same. The color of the i-th layer is ci=(Ri,Gi,Bi), the blending mode of the i-th layer is mi (mi∈{1,2}):

  • If mi=1, the blending mode of this layer is ''Normal''. Assume the previous color displayed on the screen is (Rp,Gp,Bp), now the new color will be (Ri,Gi,Bi).
  • If mi=2, the blending mode of this layer is ''Linear Dodge''. Assume the previous color displayed on the screen is (Rp,Gp,Bp), now the new color will be (min(Rp+Ri,255), min(Gp+Gi,255), min(Bp+Bi,255)).

You will be given q queries. In the i-th query, you will be given two integers li and ri (1≤li≤ri≤n). Please write a program to compute the final color displayed on the screen if we only keep all the layers indexed within [li,ri] without changing their order. Note that the color of the background is (R=0,G=0,B=0).

Input

The first line contains a single integer T (1≤T≤10), the number of test cases. For each test case:

The first line of the input contains two integers n and q (1≤n,q≤100000), denoting the number of layers and the number of queries.

In the next n lines, the i-th line contains an integer mi and a six-digit hexadecimal capital string ci, describing the i-th layer.

In the next q lines, the i-th line contains two integers li and ri (1≤li≤ri≤n), describing the i-th query.

Output

For each query, print a single line containing a six-digit hexadecimal capital string, denoting the final displayed color.

Sample Input

1
5 5
1 64C832
2 000100
2 010001
1 323C21
2 32C8C8
1 2
1 3
2 3
2 4
2 5

Sample Output

64C932
65C933
010101
323C21
64FFE9

题意真的很难懂..每次询问的意思就是以l为bottom,r为top,没有别的意思~

读入的时候维护一个数组pos,pos[i]表示i这个位置左边最靠近i的m = 1的位置(因为只要出现m = 1的位置,bottom到这个位置之前实际上都不用考虑)。每次查询的时候实际上就是把pos[r]的颜色和pos[r] + 1到r总共的贡献相加,然后对255取min即可。可以用前缀和实现。注意输入输出必须scanf,否则会T。

#include <bits/stdc++.h>
using namespace std;
int n, q, m[100005], c[100005][3], sum[100005][3];
int pos[100005];
int getnum(char c) {
	if(c >= '0' && c <= '9') return c - '0';
	else return c - 'A' + 10;
}
int tran(char c1, char c2) {
	int ans = 0;
	ans += getnum(c2);
	ans += getnum(c1) * 16;
	return ans;
}
char putnum(int x) {
	if(x <= 9) return (char)('0' + x);
	else return (char)('A' + x - 10);
}
void print(int x) {
	char c1 = putnum(x % 16), c2 = putnum(x / 16);
	printf("%c%c", c2, c1);
	return;
}
int main() {
	int t;
	cin >> t;
	while(t--) {
		scanf("%d%d", &n, &q);
		int lst = -1;
		for(int i = 1; i <= n; i++) {
			scanf("%d", &m[i]);
			if(m[i] == 1) lst = i;
			pos[i] = lst;
			char s[10];
			scanf("%s", s + 1);
			c[i][0] = tran(s[1], s[2]);
			c[i][1] = tran(s[3], s[4]);
			c[i][2] = tran(s[5], s[6]);
			sum[i][0] = sum[i - 1][0] + c[i][0];
			sum[i][1] = sum[i - 1][1] + c[i][1];
			sum[i][2] = sum[i - 1][2] + c[i][2];
		} 
		for(int i = 1; i <= q; i++) {
			int l, r;
			scanf("%d%d", &l, &r);
			int R, G, B;
			if(pos[r] >= l && pos[r] <= r) {
				R = min(255, c[pos[r]][0] + sum[r][0] - sum[pos[r]][0]);
				G = min(255, c[pos[r]][1] + sum[r][1] - sum[pos[r]][1]);
				B = min(255, c[pos[r]][2] + sum[r][2] - sum[pos[r]][2]);
			} else {
				R = min(255, sum[r][0] - sum[l - 1][0]);
				G = min(255, sum[r][1] - sum[l - 1][1]);
				B = min(255, sum[r][2] - sum[l - 1][2]);
			}
			print(R);
			print(G);
			print(B);
			puts("");
		}
	}
}
posted @ 2021-07-27 23:36  脂环  阅读(125)  评论(0编辑  收藏  举报