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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/scar_lyw/article/details/80412059

数学计算

题目背景:

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;
}
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页