ZOJ 4009 And Another Data Structure Problem(ZOJ Monthly, March 2018 Problem F,发现循环节 + 线段树)

题目链接  ZOJ Monthly, March 2018 Problem F

题意很明确

这个模数很奇妙,在$[0, mod)$的所有数满足任意一个数立方$48$次对$mod$取模之后会回到本身。

所以开$48$棵线段树,和一个永久标记。当对某个区间操作时对这个区间加一层永久标记。

即当前我要查找的第$x$层,实际找的是第$up[i] + x$层。

时间复杂度$O(48nlogn)$

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b)	for (int i(a); i >= (b); --i)
#define	ls		i << 1
#define	rs		i << 1 | 1
#define	mid		((l + r) >> 1)
#define lson		ls, l, mid
#define	rson		rs, mid + 1, r

typedef long long LL;

const int N   = 1e5 + 10;
const int mod = 99971;

int T;
int a[N], nxt[N], cal[N][49];
int t[N << 2][49], up[N << 2];
int n, m;

void pushup(int i){
	rep(j, 0, 47){
		t[i][j] = t[ls][(j + up[ls] + 48) % 48] + t[rs][(j + up[rs] + 48) % 48];
		t[i][j] %= mod;
	}
}

void build(int i, int l, int r){
	up[i] = 0;
	if (l == r){
		rep(j, 0, 47) t[i][j] = cal[a[l]][j];
		return;
	}

	build(lson);
	build(rson);
	pushup(i);
}

void update(int i, int l, int r, int L, int R){
	if (L <= l && r <= R){
		++up[i];
		up[i] %= 48;
		return;
	}

	if (L <= mid) update(lson, L, R);
	if (R  > mid) update(rson, L, R);
	pushup(i);
}

int query(int i, int l, int r, int L, int R, int cnt){
	if (L <= l && r <= R){
		return t[i][(up[i] + cnt) % 48];
	}

	int ret = 0;
	int now = cnt + up[i];
	if (L <= mid) ret += query(lson, L, R, now);
	if (R  > mid) ret += query(rson, L, R, now);
	ret %= mod;
	return ret;
}	

int main(){

	scanf("%d", &T);
	rep(i, 1, mod - 1) nxt[i] = 1ll * i * i * i % mod;
	rep(i, 1, mod - 1){
		cal[i][0] = i;
		rep(j, 1, 47){
			cal[i][j] = nxt[cal[i][j - 1]];
		}
	}

	while (T--){
		scanf("%d%d", &n, &m);
		rep(i, 1, n){
			scanf("%d", a + i);
			a[i] %= mod;
		}

		build(1, 1, n);

		while (m--){
			int op, l, r;
			scanf("%d%d%d", &op, &l, &r);
			if (op == 1) update(1, 1, n, l, r);
			else printf("%d\n", query(1, 1, n, l, r, 0));
		}
	}

	return 0;
}

  

原文地址:https://www.cnblogs.com/cxhscst2/p/8549061.html

时间: 2024-11-10 14:31:55

ZOJ 4009 And Another Data Structure Problem(ZOJ Monthly, March 2018 Problem F,发现循环节 + 线段树)的相关文章

ZOJ Monthly, March 2018 Solution

A - Easy Number Game 水. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define ll long long 5 #define N 100010 6 ll arr[N]; 7 int n, m; 8 9 int main() 10 { 11 int t; scanf("%d", &t); 12 while (t--) 13 { 14 scanf("%d%d"

ZOJ Monthly, March 2018

Easy Number Game Time Limit: 2 Seconds      Memory Limit: 65536 KB The bored BaoBao is playing a number game. In the beginning, there are  numbers. For each turn, BaoBao will take out two numbers from the remaining numbers, and calculate the product

[UVA] 11995 - I Can Guess the Data Structure! [STL应用]

11995 - I Can Guess the Data Structure! Time limit: 1.000 seconds Problem I I Can Guess the Data Structure! There is a bag-like data structure, supporting two operations: 1 x Throw an element x into the bag. 2 Take out an element from the bag. Given

CDOJ 483 Data Structure Problem DFS

32‘20 Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/483 Description Data structure is a fundamental course of Computer Science, so that each contestant is highly likely to solve this data s

ZOJ 3686 A Simple Tree Problem(线段树)

A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each node has a boolean (0 or 1) labeled on it. Initially, all the labels are 0. We define this kind of operation: given a subtree, negate all its labels. An

OpenJudge cdqz/Data Structure Challenge 2 (Problem 5822) - 可持久化线段树

描述 给一个空数列,有M次操作,每次操作是以下三种之一: (1)在数列后加一个数 (2)求数列中某位置的值 (3)撤销掉最后进行的若干次操作(1和3) 输入 第一行一个正整数M. 接下来M行,每行开头是一个字符,若该字符为'A',则表示一个加数操作,接下来一个整数x,表示在数列后加一个整数x:若该字符为'Q',则表示一个询问操作,接下来一个整数x,表示求x位置的值:若该字符为'U',则表示一个撤销操作,接下来一个整数x,表示撤销掉最后进行的若干次操作. 输出 对每一个询问操作单独输出一行,表示答

uva 11995 - I Can Guess the Data Structure!

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=3146&mosmsg=Submission+received+with+ID+14262472 I Can Guess the Data Structure! There is a bag-like data structure, supporti

hdu-5929 Basic Data Structure(双端队列+模拟)

题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 207    Accepted Submission(s): 41 Problem Description Mr. Frog learned a basic data structure recently, which is called

Leetcode: Add and Search Word - Data structure design

Design a data structure that supports the following two operations: void addWord(word) bool search(word) search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter