[Educational Codeforces Round 56 (Rated for Div. 2)] —G. Multidimensional Queries(二进制状压,线段树)
G. Multidimensional Queries
time limit per test
6 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
You are given an array aa of nn points in kk-dimensional space. Let the distance between two points axax and ayay be ∑i=1k|ax,i−ay,i|∑i=1k|ax,i−ay,i| (it is also known as Manhattan distance).
You have to process qq queries of the following two types:
- 11 ii b1b1 b2b2 ... bkbk — set ii-th element of aa to the point (b1,b2,…,bk)(b1,b2,…,bk);
- 22 ll rr — find the maximum distance between two points aiai and ajaj, where l≤i,j≤rl≤i,j≤r.
题意:
在一个维度坐标系中,点的距离为。
现在给定个点,和个询问,每一个询问有种:
- 代表将第个点的坐标改为
- ,代表让你找出两个距离最大的节点,输出他们的距离即可。
思路:
点的距离为。
仔细思考上面等式,右边表达式中会有很多无效的状态,但是所有状态中的最大值一定是等于左侧表达式的。
然后我们可以建立个线段树,第颗数维护的是中第位为时取正好,否则取符号的总和最大值。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #include <sstream> #include <bitset> #include <unordered_map> // #include <bits/stdc++.h> #define ALL(x) (x).begin(), (x).end() #define sz(a) int(a.size()) #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define chu(x) if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl #define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c)) #define du2(a,b) scanf("%d %d",&(a),&(b)) #define du1(a) scanf("%d",&(a)); using namespace std; typedef long long ll; ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;} ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;} void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}} inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;} inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') fh = -1; c = getchar();} while (c >= '0' && c <= '9') tmp = tmp * 10 + c - 48, c = getchar(); return tmp * fh;} void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}} void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}} const int maxn = 200010; const int inf = 0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ #define DEBUG_Switch 0 ll segment_tree[maxn << 2][35]; int n, k; int a[maxn][6]; void pushup(int rt) { repd(j, 0, (1<<k)- 1) { segment_tree[rt][j] = max(segment_tree[rt << 1][j], segment_tree[rt << 1 | 1][j]); } } void show(int rt) { repd(j, 0, k - 1) printf("%d %d %lld\n",rt,j,segment_tree[rt][j]); } void build(int rt, int l, int r) { if (l == r) { for (int i = 0; i < (1 << k); ++i) { ll sum = 0ll; repd(j, 0, k - 1) { if (i & (1 << j)) { sum += a[l][j + 1]; } else { sum -= a[l][j + 1]; } } // printf("%d %d %lld\n",l,i,sum); segment_tree[rt][i] = sum; } } else { int mid = (l + r) >> 1; build(rt << 1, l, mid); build(rt << 1 | 1, mid + 1, r); pushup(rt); } // show(rt); } void update(int rt, int l, int r, int id) { if (l == r && l == id) { for (int i = 0; i < (1 << k); ++i) { ll sum = 0ll; repd(j, 0, k - 1) { if (i & (1 << j)) { sum += a[id][j + 1]; } else { sum -= a[id][j + 1]; } } segment_tree[rt][i] = sum; } return ; } else { int mid = (l + r) >> 1; if (id > mid) update(rt << 1 | 1, mid + 1, r, id); else update(rt << 1, l, mid, id); pushup(rt); } } ll f[35]; void query(int rt, int l, int r, int ql, int qr) { if (l >= ql && qr >= r) { for (int i = 0; i < (1 << k); ++i) { f[i] = max(f[i], segment_tree[rt][i]); } } else { int mid = (l + r) >> 1; if (ql <= mid) { query(rt << 1, l, mid, ql, qr); } if (qr > mid) { query(rt << 1 | 1, mid + 1, r, ql, qr); } } } int main() { #if DEBUG_Switch freopen("C:\\code\\input.txt", "r", stdin); #endif //freopen("C:\\code\\output.txt","r",stdin); n = readint(); k = readint(); repd(i, 1, n) { repd(j, 1, k) { a[i][j] = readint(); } } build(1, 1, n); int q = readint(); while (q--) { int op = readint(); if (op == 1) { int id = readint(); repd(j, 1, k) { a[id][j] = readint(); } update(1, 1, n, id); } else { int l = readint(); int r = readint(); for (int i = 0; i < (1 << k); ++i) { f[i] = -1e18; } query(1, 1, n, l, r); ll ans = -1e18; for (int i = 0; i < (1 << k); ++i) { int opponent =((1 << k)-1)^i; ans = max(ans, f[i] + f[opponent]); } printf("%lld\n", ans ); } } return 0; }