标签:opera show content it! 区间 algo mis comment 负数
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 942 Accepted Submission(s):
208
#pragma comment (linker,"/STACK:102400000,102400000") #include <math.h> #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #define ll long long #define mod 1000000007 using namespace std; const int N = 101015; int t, n, m, ans; int l[N], r[N], L[N], R[N], C[N], f[N]; ll w[N], s[N], sum[N], prd[N<<2]; inline void Maintain(int rt){ L[rt] = R[rt] = C[rt]; if(l[rt]){ L[rt] = min(L[rt], L[l[rt]]); R[rt] = max(R[rt], R[l[rt]]); } if(r[rt]){ L[rt] = min(L[rt], L[r[rt]]); R[rt] = max(R[rt], R[r[rt]]); } } inline ll dfs(int cur){ s[cur] = w[cur]; if(l[cur]){ s[cur] += dfs(l[cur])%mod; } L[cur] = R[cur] = C[cur] = ans++; if(r[cur]){ s[cur] += dfs(r[cur])%mod; } Maintain(cur); return sum[C[cur]] = s[cur]; } inline void Build(int rt, int l, int r){ if(l == r){ prd[rt] = sum[l]; return; } int m = (l+r)>>1; Build(rt<<1, l, m); Build(rt<<1|1, m+1, r); prd[rt] = prd[rt<<1]*prd[rt<<1|1]%mod; } inline void Modify(int rt, int l, int r, int pos, ll val){ if(l == r){ prd[rt] = val; return; } int m = (l+r)>>1; if(pos <= m) Modify(rt<<1, l, m, pos, val); else Modify(rt<<1|1, m+1, r, pos, val); prd[rt] = prd[rt<<1]*prd[rt<<1|1]%mod; } inline ll Query(int rt, int l, int r, int L, int R){ if(L <= l && r <= R) return prd[rt]; ll res = 1; int m = (l+r)>>1; if(L <= m) res = res*Query(rt<<1, l, m, L, R)%mod; if(R > m) res = res*Query(rt<<1|1, m+1, r, L, R)%mod; return res; } int main(){ scanf("%d", &t); for (int i = 1; i <= t; i ++) { printf("Case #%d:\n", i); f[0] = f[1] = 0; //wa点,必须置零 scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%lld%d%d", &w[i], &l[i], &r[i]); f[l[i]] = i; f[r[i]] = i; } int x, y, Q; ans = 1, dfs(1); Build(1, 1, n); for(int i = 1; i <= m; i++) { scanf("%d%d", &Q, &x); if(!Q){ y = l[x]; if(!y) continue; s[x] = (s[x]+s[r[y]]-s[y]+mod)%mod; //wa点,可能出现负数 s[y] = (s[y]+s[x]-s[r[y]]+mod)%mod; Modify(1, 1, n, C[x], s[x]); Modify(1, 1, n, C[y], s[y]); if(x == l[f[x]]){ l[f[x]] = y; }else{ r[f[x]] = y; } f[y] = f[x], l[x] = r[y], f[r[y]] = x; r[y] = x, f[x] = y; Maintain(x); Maintain(y); }else if(Q == 1){ y = r[x]; if(!y) continue; s[x] = (s[x]+s[l[y]]-s[y]+mod)%mod; //wa点,可能出现负数 s[y] = (s[y]+s[x]-s[l[y]]+mod)%mod; Modify(1, 1, n, C[x], s[x]); Modify(1, 1, n, C[y], s[y]); if (x == l[f[x]]){ l[f[x]] = y; }else{ r[f[x]] = y; } f[y] = f[x], r[x] = l[y], f[l[y]] = x; l[y] = x; f[x] = y; Maintain(x); Maintain(y); }else { printf("%lld\n", Query(1, 1, n, L[x], R[x])); } } } }
标签:opera show content it! 区间 algo mis comment 负数
原文地址:https://www.cnblogs.com/microcodes/p/12803539.html