Codeforces-927(div3)-C. LR-remainders

这题很经典啊,对于除怎么解决取模的问题.

这种题确实是非常经典,就是这个取模怎么解决有除的问题呢??

一开始,出于某种奇怪的心态(不就是日常装杯吗....),压根没考虑到.以此警醒自己,认真对待,仔细审题,仔细理解.

回归正文:

处理有除法的取模,可用线段树,就是凑区间吗,能想到这点就可以了,线段树还是挺好写的,就是凑区间嘛

复制代码
// Problem: C. LR-remainders
// Contest: Codeforces - Codeforces Round 927 (Div. 3)
// URL: https://codeforces.com/contest/1932/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// Date: 2024-03-11 19:38:23
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define endl '\n'
#define ls u << 1
#define rs u << 1 | 1
using namespace std;
const int N = 200005;
struct Tree {
	int l, r;
	int sum;
}tr[N * 4];
int n, m, a[200005];
void build(int u, int l, int r) {
	tr[u] = { l,r };
	if (l == r) {
		tr[u].sum = a[l] % m;
	}
	else {
		int mid = l + r >> 1;
		build(ls, l, mid);
		build(rs, mid + 1, r);
		tr[u].sum = tr[ls].sum * tr[rs].sum % m;
	}
}
int query(int u, int x, int y) {
	int l = tr[u].l, r = tr[u].r, mid = l + r >> 1;
	if (x <= l && r <= y)
		return tr[u].sum;
	int ans = 1;
	if (x <= mid)
		ans = ans * query(ls, x, y) % m;
	if (y >= mid + 1)
		ans = ans * query(rs, x, y) % m;
	return ans;
}
void solve() {
	cin >> n >> m;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
	}
	build(1, 1, n);
	string s; cin >> s;
	for (int i = 0, l = 1, r = n; i < s.size(); ++i) {
		cout << query(1, l, r) % m << " ";
		if (s[i] == 'L')++l;
		else --r;
	}
	cout << endl;
}
signed main() {
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int t = 1;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}
相关推荐
海的透彻4 小时前
dockerfile如何快速构建容器(java服务)
java·开发语言
code2cat4 小时前
Java工具篇之Gradle:用任务图组织构建与交付
java·开发语言·单元测试·gradle
北京中科新远科技4 小时前
高速交换机端口预算怎么做?下行、上联、拆分和冗余计算框架
开发语言·php
xiaoye-duck4 小时前
《C++ 项目:手写大模型接入 SDK》DeepSeek接口设计(下):流式响应实现和测试
开发语言·网络·c++·语言模型
AI源码5 小时前
基于 Python 的短视频用户行为分析系统
开发语言·python
旖旎夜光6 小时前
【C++ 基础入门】类和对象(上):封装、对象模型与 this 指针
开发语言·c++·笔记·学习
揽星河..6 小时前
【C语言】结构体
c语言·开发语言
2601_966949656 小时前
如何封装 qd.klines.batch(),让批量 K 线查询函数在多个策略中复用?
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
神仙别闹7 小时前
基于Python实现英词语自动对齐系统
开发语言·python
唐青枫7 小时前
C 语言 malloc() 详解与实战 Demo
c语言