Codeforces 1030F Putting Boxes Together 樹狀數組
阿新 • • 發佈:2019-05-15
lld space sca using code 線段 prefix sub i++
Putting Boxes Together
二分找到分界點, 左邊的往右移, 右邊的往左移。
其實把a[ i ] - i 之後就變成了移到一個點, 然後我維護移到一個線段都要維護自閉了。
#include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL pair<LL, LL> #definePLI pair<LL, int> #define PII pair<int, int> #define SZ(x) ((int)x.size()) #define ALL(x) (x).begin(), (x).end() #define fio ios::sync_with_stdio(false); cin.tie(0); using namespace std; const int N = 2e5 + 7; const int inf = 0x3f3f3f3f; const LL INF = 0x3f3f3f3f3f3f3f3f; constint mod = 1e9 + 7; const double eps = 1e-8; const double PI = acos(-1); template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;} template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;} template<class T, classS> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;} template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;} int n, q, a[N], w[N]; struct Bit { LL a[N]; void modify(int x, int v) { for(int i = x; i <= n; i += i & -i) a[i] += v; } LL sum(int x) { LL ans = 0; for(int i = x; i; i -= i & -i) ans += a[i]; return ans; } LL query(int L, int R) { if(L > R) return 0; return sum(R) - sum(L - 1); } } bit; struct Bit2 { int a[N]; void modify(int x, int v) { for(int i = x; i <= n; i += i & -i) add(a[i], v); } int sum(int x) { LL ans = 0; for(int i = x; i; i -= i & -i) add(ans, a[i]); return ans; } int query(int L, int R) { if(L > R) return 0; return (sum(R) - sum(L - 1) + mod) % mod; } } prefix, suffix; int main() { LL ret = 0; scanf("%d%d", &n, &q); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); for(int i = 1; i <= n; i++) scanf("%d", &w[i]); for(int i = 1; i <= n; i++) { bit.modify(i, w[i]); prefix.modify(i, 1LL * abs(a[i] - a[1] - i + 1) * w[i] % mod); } for(int i = n; i >= 1; i--) suffix.modify(i, 1LL * abs(a[n] - a[i] - n + i) * w[i] % mod); while(q--) { int x, y; scanf("%d%d", &x, &y); if(x < 0) { x = -x; bit.modify(x, -w[x]); prefix.modify(x, mod - (1LL * abs(a[x] - a[1] - x + 1) * w[x] % mod)); suffix.modify(x, mod - (1LL * abs(a[n] - a[x] - n + x) * w[x] % mod)); w[x] = y; bit.modify(x, w[x]); prefix.modify(x, 1LL * abs(a[x] - a[1] - x + 1) * w[x] % mod); suffix.modify(x, 1LL * abs(a[n] - a[x] - n + x) * w[x] % mod); } else { int len = y - x + 1; int low = a[x], high = a[y], p = low, low2, high2, p2, fen; while(low <= high) { int mid = low + high >> 1; low2 = x, high2 = y, p2 = y + 1; while(low2 <= high2) { int mid2 = low2 + high2 >> 1; if(a[mid2] > mid + mid2 - x) p2 = mid2, high2 = mid2 - 1; else low2 = mid2 + 1; } LL valL = bit.query(x, p2 - 1); LL valR = bit.query(p2, y); if(valL > valR) p = mid, high = mid - 1, fen = p2; else low = mid + 1; } LL ans = 0; if(fen <= y) { add(ans, prefix.query(fen, y)); sub(ans, bit.query(fen, y) % mod * (p - (a[1] + x - 1)) % mod); } if(fen - 1 >= x) { add(ans, suffix.query(x, fen - 1)); sub(ans, bit.query(x, fen - 1) % mod * (a[n] - (n - x) - p) % mod); } printf("%lld\n", ans); } } return 0; } /* */
Codeforces 1030F Putting Boxes Together 樹狀數組