求hack
查看原帖
求hack
49562
tocek_shiki楼主2018/12/15 12:05

线段树模板题啊,莫名其妙WA4, TLE1

#include <bits/stdc++.h>
#define newline printf ("\n")
#define space printf (" ")
#define cinfalse ios::sync_with_stdio(false)
#define rint register int
#define For(i, a, b) for (rint i = a; i <= b; i ++)
#define Low(i, a, b) for (rint i = a; i >= b; i --)
#define FFr(i, a, b, c) for (rint i = a; i <= b; i += c)
#define FLw(i, a, b, c) for (rint i = a; i >= b; i -= c)
#define min(a, b) (a)>(b)?(b):(a)
#define max(a, b) (a)>(b)?(a):(b)
#define MAXN 100005
using namespace std;

namespace Segment_Tree
{
	struct node
	{
		int sum;
		int maxn;
	} tr[MAXN*4];
	
	inline void push_up (int nwpos) {tr[nwpos].maxn = max(tr[nwpos<<1].maxn, tr[nwpos<<1|1].maxn), tr[nwpos].sum = tr[nwpos<<1].sum+tr[nwpos<<1|1].sum;}
	
	void build (int nwpos, int l, int r)
	{
		if (l == r)
		{
			scanf ("%d", &tr[nwpos].maxn), tr[nwpos].sum = tr[nwpos].maxn;
			return ; 
		}
		rint mid = (l+r)>>1;
		build (nwpos<<1, l, mid), build(nwpos<<1|1, mid+1, r);
		push_up(nwpos); 
	}
	
	void modify (int nwpos, int l, int r, int L, int R)
	{
		if (tr[nwpos].maxn == 1) return ;
		if (l == r && l == L) {tr[nwpos].maxn = tr[nwpos].sum = floor(sqrt(tr[nwpos].maxn)); return ;}
		rint mid = (l+r)>>1;
		if (L <= mid) modify(nwpos<<1, l, mid, L, min(R, mid));
		if (R > mid) modify(nwpos<<1|1, mid+1, r, max(L, mid+1), R);
		push_up(nwpos); 
	}
	
	int query (int nwpos, int l, int r, int L, int R)
	{
		if (l == L && r == R) return tr[nwpos].sum;
		rint mid = (l+r)>>1, ans = 0;
		if (L <= mid) ans += query(nwpos<<1, l, mid, L, min(R, mid));
		if (R > mid) ans += query(nwpos<<1|1, mid+1, r, max(L, mid+1), R);
		return ans;
	}
}

using namespace Segment_Tree;
int n, m;
int main()
{
	scanf ("%d", &n);
	build (1, 1, n);
	scanf ("%d", &m);
	while (m --)
	{
		rint a, b, c;
		scanf ("%d%d%d", &a, &b, &c);
		if (a == 1) printf ("%d\n", query(1, 1, n, b, c));
		else modify(1, 1, n, b, c);
	}
    return 0;
}

2018/12/15 12:05
加载中...