NOI模拟(5.21) TJOID1T1 数学计算 (bzoj5334)

数学计算

题目背景:

5.21 模拟 TJOI2018D1T1

分析:线段树

 

从来没想过自己竟然会死在傻逼线段树······每一个数值出现的位置是一段区间,直接线段树维护区间乘就可以了,然后我竟然傻逼到写了标记下放,然后没有清空······差一点就AK了,然后我T1线段树标记没有清空······还是去死一死吧。(教练啊,新高一什么时候学线段树啊,我要去听线段树基础······)

 

Source:

 

/*
	created by scarlyw
*/
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <cctype>
#include <vector>
#include <set>
#include <queue>
#include <ctime>
#include <deque>
#include <iterator>
#include <map>

inline char read() {
	static const int IN_LEN = 1024 * 1024;
	static char buf[IN_LEN], *s, *t;
	if (s == t) {
		t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
		if (s == t) return -1;
	}
	return *s++;
}

// /*
template<class T>
inline void R(T &x) {
	static char c;
	static bool iosig;
	for (c = read(), iosig = false; !isdigit(c); c = read()) {
		if (c == -1) return ;
		if (c == '-') iosig = true;	
	}
	for (x = 0; isdigit(c); c = read()) 
		x = ((x << 2) + x << 1) + (c ^ '0');
	if (iosig) x = -x;
}
//*/

const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
	if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
	*oh++ = c;
}

template<class T>
inline void W(T x) {
	static int buf[30], cnt;
	if (x == 0) write_char('0');
	else {
		if (x < 0) write_char('-'), x = -x;
		for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
		while (cnt) write_char(buf[cnt--]);
	}
}

inline void flush() {
	fwrite(obuf, 1, oh - obuf, stdout);
}

const int MAXN = 100000 + 10;

int n, x, t, mod, type;
int v[MAXN], tree[MAXN << 2 | 1];

inline void push_down(int k) {
    if (tree[k] != 1) {
        tree[k << 1] = (long long)tree[k << 1] * tree[k] % mod;
        tree[k << 1 | 1] = (long long)tree[k << 1 | 1] * tree[k] % mod;
        tree[k] = 1;
    }
}

inline void build(int k, int l, int r) {
    tree[k] = 1;
    if (l == r) return ;
    int mid = l + r >> 1;
    build(k << 1, l, mid), build(k << 1 | 1, mid + 1, r);
}

inline void modify(int k, int l, int r, int ql, int qr, int x) {
    if (ql <= l && r <= qr) 
        return (void)(tree[k] = (long long)tree[k] * x % mod);
    int mid = l + r >> 1;
    push_down(k);
    if (ql <= mid) modify(k << 1, l, mid, ql, qr, x);
    if (qr > mid) modify(k << 1 | 1, mid + 1, r, ql, qr, x);
}

inline void get_ans(int k, int l, int r) {
    if (l == r) return W(tree[k]), write_char('\n');
    int mid = l + r >> 1;
    push_down(k);
    get_ans(k << 1, l, mid), get_ans(k << 1 | 1, mid + 1, r);

}

inline void solve() {
    R(n), R(mod), build(1, 1, n);
    for (int i = 1; i <= n; ++i) {
        R(type), v[i] = 1;
        if (type == 1) R(v[i]);
        else R(x), modify(1, 1, n, x, i - 1, v[x]), v[x] = 1;
    } 
    for (int i = 1; i <= n; ++i) 
        if (v[i] != 1) modify(1, 1, n, i, n, v[i]), v[i] = 1;
    get_ans(1, 1, n);
}

int main() {
    freopen("cal.in", "r", stdin);
    freopen("cal.out", "w", stdout);
    R(t);
    while (t--) solve();
    flush();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值