• ACM-ICPC 2018 沈阳赛区现场赛 E. The Kouga Ninja Scrolls (切比雪夫距离+线段树)


    题目链接:

    题意:在二维平面上有 n 个人,每个人有一个位置(xi, yi)和门派 ci,m 个操作:①改变第 k 个人的位置;②改变第 k 个人的门派;③询问区间[l,r]之间不同门派的两个人的最大曼哈顿距离。

    题解:首先需要将曼哈顿距离转化成切比雪夫距离(不懂戳https://www.cnblogs.com/zwfymqz/p/8253530.html),然后用线段树维护一个区间内两个不同门派 x 和 y 的最大最小和次大次小值即可。(维护次大/次小值是因为若区间内的最大值和最小值的人是同一个门派的话则不能进行比较)(代码写的很丑,建议理解做法后自己写一个...

      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 #define ll long long
      4 #define ull unsigned long long
      5 #define mst(a,b) memset((a),(b),sizeof(a))
      6 #define mp(a,b) make_pair(a,b)
      7 #define pi acos(-1)
      8 #define pii pair<int,int>
      9 #define pb push_back
     10 #define fi first
     11 #define se second
     12 const int INF = 0x3f3f3f3f;
     13 const double eps = 1e-6;
     14 const int MAXN = 1e5 + 10;
     15 const int MAXM = 1e7 + 10;
     16 const ll mod = 1e9 + 7;
     17 
     18 ll x[MAXN],y[MAXN];
     19 int c[MAXN];
     20 
     21 struct node {
     22     pair<ll,int> mxx[2], mnx[2];
     23     pair<ll,int> mxy[2], mny[2];
     24 }st[MAXN<<2];
     25 
     26 void pushup(int rt) {
     27     st[rt] = st[rt<<1];
     28     if(st[rt].mxx[0].se == -1) st[rt].mxx[0] = st[rt<<1|1].mxx[0], st[rt].mxx[1] = st[rt<<1|1].mxx[1];
     29     else if(st[rt<<1|1].mxx[0] >= st[rt].mxx[0]) {
     30         if(st[rt<<1|1].mxx[0].se != st[rt].mxx[0].se) {
     31             st[rt].mxx[1] = st[rt].mxx[0], st[rt].mxx[0] = st[rt<<1|1].mxx[0];
     32             if(st[rt<<1|1].mxx[1].se != -1 && st[rt<<1|1].mxx[1].se != st[rt].mxx[0].se && st[rt<<1|1].mxx[1].fi >= st[rt].mxx[1].fi)
     33                 st[rt].mxx[1] = st[rt<<1|1].mxx[1];
     34         } else {
     35             st[rt].mxx[0] = st[rt<<1|1].mxx[0];
     36             if(st[rt<<1|1].mxx[1].se != -1 && st[rt<<1|1].mxx[1].se != st[rt].mxx[0].se && st[rt<<1|1].mxx[1].fi >= st[rt].mxx[1].fi)
     37                 st[rt].mxx[1] = st[rt<<1|1].mxx[1];
     38         }
     39     } else {
     40         if(st[rt<<1|1].mxx[0].se != st[rt].mxx[0].se && st[rt<<1|1].mxx[0].fi >= st[rt].mxx[1].fi)
     41             st[rt].mxx[1] = st[rt<<1|1].mxx[0];
     42         else if(st[rt<<1|1].mxx[1].se != -1 && st[rt<<1|1].mxx[1].se != st[rt].mxx[0].se && st[rt<<1|1].mxx[1].fi >= st[rt].mxx[1].fi)
     43             st[rt].mxx[1] = st[rt<<1|1].mxx[1];
     44     }
     45     //mnx
     46     if(st[rt].mnx[0].se == -1) st[rt].mnx[0] = st[rt<<1|1].mnx[0], st[rt].mnx[1] = st[rt<<1|1].mnx[1];
     47     else if(st[rt<<1|1].mnx[0] <= st[rt].mnx[0]) {
     48         if(st[rt<<1|1].mnx[0].se != st[rt].mnx[0].se) {
     49             st[rt].mnx[1] = st[rt].mnx[0], st[rt].mnx[0] = st[rt<<1|1].mnx[0];
     50             if(st[rt<<1|1].mnx[1].se != -1 && st[rt<<1|1].mnx[1].se != st[rt].mnx[0].se && st[rt<<1|1].mnx[1].fi <= st[rt].mnx[1].fi)
     51                 st[rt].mnx[1] = st[rt<<1|1].mnx[1];
     52         } else {
     53             st[rt].mnx[0] = st[rt<<1|1].mnx[0];
     54             if(st[rt<<1|1].mnx[1].se != -1 && st[rt<<1|1].mnx[1].se != st[rt].mnx[0].se && st[rt<<1|1].mnx[1].fi <= st[rt].mnx[1].fi)
     55                 st[rt].mnx[1] = st[rt<<1|1].mnx[1];
     56         }
     57     } else {
     58         if(st[rt<<1|1].mnx[0].se != st[rt].mnx[0].se && st[rt<<1|1].mnx[0].fi <= st[rt].mnx[1].fi)
     59             st[rt].mnx[1] = st[rt<<1|1].mnx[0];
     60         else if(st[rt<<1|1].mnx[1].se != -1 && st[rt<<1|1].mnx[1].se != st[rt].mnx[0].se && st[rt<<1|1].mnx[1].fi <= st[rt].mnx[1].fi)
     61             st[rt].mnx[1] = st[rt<<1|1].mnx[1];
     62     }
     63     //mxy
     64     if(st[rt].mxy[0].se == -1) st[rt].mxy[0] = st[rt<<1|1].mxy[0], st[rt].mxy[1] = st[rt<<1|1].mxy[1];
     65     else if(st[rt<<1|1].mxy[0] >= st[rt].mxy[0]) {
     66         if(st[rt<<1|1].mxy[0].se != st[rt].mxy[0].se) {
     67             st[rt].mxy[1] = st[rt].mxy[0], st[rt].mxy[0] = st[rt<<1|1].mxy[0];
     68             if(st[rt<<1|1].mxy[1].se != -1 && st[rt<<1|1].mxy[1].se != st[rt].mxy[0].se && st[rt<<1|1].mxy[1].fi >= st[rt].mxy[1].fi)
     69                 st[rt].mxy[1] = st[rt<<1|1].mxy[1];
     70         } else {
     71             st[rt].mxy[0] = st[rt<<1|1].mxy[0];
     72             if(st[rt<<1|1].mxy[1].se != -1 && st[rt<<1|1].mxy[1].se != st[rt].mxy[0].se && st[rt<<1|1].mxy[1].fi >= st[rt].mxy[1].fi)
     73                 st[rt].mxy[1] = st[rt<<1|1].mxy[1];
     74         }
     75     } else {
     76         if(st[rt<<1|1].mxy[0].se != st[rt].mxy[0].se && st[rt<<1|1].mxy[0].fi >= st[rt].mxy[1].fi)
     77             st[rt].mxy[1] = st[rt<<1|1].mxy[0];
     78         else if(st[rt<<1|1].mxy[1].se != -1 && st[rt<<1|1].mxy[1].se != st[rt].mxy[0].se && st[rt<<1|1].mxy[1].fi >= st[rt].mxy[1].fi)
     79             st[rt].mxy[1] = st[rt<<1|1].mxy[1];
     80     }
     81     //mny
     82     if(st[rt].mny[0].se == -1) st[rt].mny[0] = st[rt<<1|1].mny[0], st[rt].mny[1] = st[rt<<1|1].mny[1];
     83     else if(st[rt<<1|1].mny[0] <= st[rt].mny[0]) {
     84         if(st[rt<<1|1].mny[0].se != st[rt].mny[0].se) {
     85             st[rt].mny[1] = st[rt].mny[0], st[rt].mny[0] = st[rt<<1|1].mny[0];
     86             if(st[rt<<1|1].mny[1].se != -1 && st[rt<<1|1].mny[1].se != st[rt].mny[0].se && st[rt<<1|1].mny[1].fi <= st[rt].mny[1].fi)
     87                 st[rt].mny[1] = st[rt<<1|1].mny[1];
     88         } else {
     89             st[rt].mny[0] = st[rt<<1|1].mny[0];
     90             if(st[rt<<1|1].mny[1].se != -1 && st[rt<<1|1].mny[1].se != st[rt].mny[0].se && st[rt<<1|1].mny[1].fi <= st[rt].mny[1].fi)
     91                 st[rt].mny[1] = st[rt<<1|1].mny[1];
     92         }
     93     } else {
     94         if(st[rt<<1|1].mny[0].se != st[rt].mny[0].se && st[rt<<1|1].mny[0].fi <= st[rt].mny[1].fi)
     95             st[rt].mny[1] = st[rt<<1|1].mny[0];
     96         else if(st[rt<<1|1].mny[1].se != -1 && st[rt<<1|1].mny[1].se != st[rt].mny[0].se && st[rt<<1|1].mny[1].fi <= st[rt].mny[1].fi)
     97             st[rt].mny[1] = st[rt<<1|1].mny[1];
     98     }
     99 }
    100 
    101 void build(int rt,int l,int r) {
    102     if(l == r) {
    103         st[rt].mxx[0].fi = st[rt].mnx[0].fi = x[l], st[rt].mxx[0].se = st[rt].mnx[0].se = c[l];
    104         st[rt].mxx[1].se = st[rt].mnx[1].se = -1;
    105         st[rt].mxy[0].fi = st[rt].mny[0].fi = y[l], st[rt].mxy[0].se = st[rt].mny[0].se = c[l];
    106         st[rt].mxy[1].se = st[rt].mny[1].se = -1;
    107         return ;
    108     }
    109     int mid = (l + r) >> 1;
    110     build(rt<<1,l,mid);
    111     build(rt<<1|1,mid + 1,r);
    112     pushup(rt);
    113 }
    114 
    115 void update(int rt,int l,int r,int pos) {
    116     if(l == r) {
    117         st[rt].mxx[0].fi = st[rt].mnx[0].fi = x[l], st[rt].mxx[0].se = st[rt].mnx[0].se = c[l];
    118         st[rt].mxx[1].se = st[rt].mnx[1].se = -1;
    119         st[rt].mxy[0].fi = st[rt].mny[0].fi = y[l], st[rt].mxy[0].se = st[rt].mny[0].se = c[l];
    120         st[rt].mxy[1].se = st[rt].mny[1].se = -1;
    121         return ;
    122     }
    123     int mid = (l + r) >> 1;
    124     if(pos <= mid) update(rt<<1,l,mid,pos);
    125     else update(rt<<1|1,mid + 1,r,pos);
    126     pushup(rt);
    127 }
    128 
    129 node ans;
    130 
    131 void query(int rt,int l,int r,int ql,int qr) {
    132     if(ql <= l && qr >= r) {
    133         //mxx
    134         if(ans.mxx[0].se == -1) ans.mxx[0] = st[rt].mxx[0], ans.mxx[1] = st[rt].mxx[1];
    135         else if(st[rt].mxx[0] >= ans.mxx[0]) {
    136             if(st[rt].mxx[0].se != ans.mxx[0].se) {
    137                 ans.mxx[1] = ans.mxx[0], ans.mxx[0] = st[rt].mxx[0];
    138                 if(st[rt].mxx[1].se != -1 && st[rt].mxx[1].se != ans.mxx[0].se && st[rt].mxx[1].fi >= ans.mxx[1].fi)
    139                     ans.mxx[1] = st[rt].mxx[1];
    140             } else {
    141                 ans.mxx[0] = st[rt].mxx[0];
    142                 if(st[rt].mxx[1].se != -1 && st[rt].mxx[1].se != ans.mxx[0].se && st[rt].mxx[1].fi >= ans.mxx[1].fi)
    143                     ans.mxx[1] = st[rt].mxx[1];
    144             }
    145         } else {
    146             if(st[rt].mxx[0].se != ans.mxx[0].se && st[rt].mxx[0].fi >= ans.mxx[1].fi)
    147                 ans.mxx[1] = st[rt].mxx[0];
    148             else if(st[rt].mxx[1].se != -1 && st[rt].mxx[1].se != ans.mxx[0].se && st[rt].mxx[1].fi >= ans.mxx[1].fi)
    149                 ans.mxx[1] = st[rt].mxx[1];
    150         }
    151         //mnx
    152         if(ans.mnx[0].se == -1) ans.mnx[0] = st[rt].mnx[0], ans.mnx[1] = st[rt].mnx[1];
    153         else if(st[rt].mnx[0] <= ans.mnx[0]) {
    154             if(st[rt].mnx[0].se != ans.mnx[0].se) {
    155                 ans.mnx[1] = ans.mnx[0], ans.mnx[0] = st[rt].mnx[0];
    156                 if(st[rt].mnx[1].se != -1 && st[rt].mnx[1].se != ans.mnx[0].se && st[rt].mnx[1].fi <= ans.mnx[1].fi)
    157                     ans.mnx[1] = st[rt].mnx[1];
    158             } else {
    159                 ans.mnx[0] = st[rt].mnx[0];
    160                 if(st[rt].mnx[1].se != -1 && st[rt].mnx[1].se != ans.mnx[0].se && st[rt].mnx[1].fi <= ans.mnx[1].fi)
    161                     ans.mnx[1] = st[rt].mnx[1];
    162             }
    163         } else {
    164             if(st[rt].mnx[0].se != ans.mnx[0].se && st[rt].mnx[0].fi <= ans.mnx[1].fi)
    165                 ans.mnx[1] = st[rt].mnx[0];
    166             else if(st[rt].mnx[1].se != -1 && st[rt].mnx[1].se != ans.mnx[0].se && st[rt].mnx[1].fi <= ans.mnx[1].fi)
    167                 ans.mnx[1] = st[rt].mnx[1];
    168         }
    169         //mxy
    170         if(ans.mxy[0].se == -1) ans.mxy[0] = st[rt].mxy[0], ans.mxy[1] = st[rt].mxy[1];
    171         else if(st[rt].mxy[0] >= ans.mxy[0]) {
    172             if(st[rt].mxy[0].se != ans.mxy[0].se) {
    173                 ans.mxy[1] = ans.mxy[0], ans.mxy[0] = st[rt].mxy[0];
    174                 if(st[rt].mxy[1].se != -1 && st[rt].mxy[1].se != ans.mxy[0].se && st[rt].mxy[1].fi >= ans.mxy[1].fi)
    175                     ans.mxy[1] = st[rt].mxy[1];
    176             } else {
    177                 ans.mxy[0] = st[rt].mxy[0];
    178                 if(st[rt].mxy[1].se != -1 && st[rt].mxy[1].se != ans.mxy[0].se && st[rt].mxy[1].fi >= ans.mxy[1].fi)
    179                     ans.mxy[1] = st[rt].mxy[1];
    180             }
    181         } else {
    182             if(st[rt].mxy[0].se != ans.mxy[0].se && st[rt].mxy[0].fi >= ans.mxy[1].fi)
    183                 ans.mxy[1] = st[rt].mxy[0];
    184             else if(st[rt].mxy[1].se != -1 && st[rt].mxy[1].se != ans.mxy[0].se && st[rt].mxy[1].fi >= ans.mxy[1].fi)
    185                 ans.mxy[1] = st[rt].mxy[1];
    186         }
    187         //mny
    188         if(ans.mny[0].se == -1) ans.mny[0] = st[rt].mny[0], ans.mny[1] = st[rt].mny[1];
    189         else if(st[rt].mny[0] <= ans.mny[0]) {
    190             if(st[rt].mny[0].se != ans.mny[0].se) {
    191                 ans.mny[1] = ans.mny[0], ans.mny[0] = st[rt].mny[0];
    192                 if(st[rt].mny[1].se != -1 && st[rt].mny[1].se != ans.mny[0].se && st[rt].mny[1].fi <= ans.mny[1].fi)
    193                     ans.mny[1] = st[rt].mny[1];
    194             } else {
    195                 ans.mny[0] = st[rt].mny[0];
    196                 if(st[rt].mny[1].se != -1 && st[rt].mny[1].se != ans.mny[0].se && st[rt].mny[1].fi <= ans.mny[1].fi)
    197                     ans.mny[1] = st[rt].mny[1];
    198             }
    199         } else {
    200             if(st[rt].mny[0].se != ans.mny[0].se && st[rt].mny[0].fi <= ans.mny[1].fi)
    201                 ans.mny[1] = st[rt].mny[0];
    202             else if(st[rt].mny[1].se != -1 && st[rt].mny[1].se != ans.mny[0].se && st[rt].mny[1].fi <= ans.mny[1].fi)
    203                 ans.mny[1] = st[rt].mny[1];
    204         }
    205         return ;
    206     }
    207     int mid = (l + r) >> 1;
    208     if(qr <= mid) query(rt<<1,l,mid,ql,qr);
    209     else if(ql > mid) query(rt<<1|1,mid + 1,r,ql,qr);
    210     else {
    211         query(rt<<1,l,mid,ql,mid);
    212         query(rt<<1|1,mid + 1,r,mid + 1,qr);
    213     }
    214 }
    215 
    216 int main() {
    217 #ifdef local
    218     freopen("data.txt", "r", stdin);
    219 //    freopen("data.txt", "w", stdout);
    220 #endif
    221     int cas = 1;
    222     int t;
    223     scanf("%d",&t);
    224     while(t--) {
    225         printf("Case #%d:
    ",cas++);
    226         int n,m;
    227         scanf("%d%d",&n,&m);
    228         for(int i = 1; i <= n; i++) {
    229             ll x0,y0;
    230             scanf("%lld%lld%d",&x0,&y0,&c[i]);
    231             x[i] = x0 + y0, y[i] = x0 - y0;
    232         }
    233         build(1,1,n);
    234         while(m--) {
    235             int op;
    236             scanf("%d",&op);
    237             if(op == 1) {
    238                 int k;
    239                 ll x0,y0;
    240                 scanf("%d%lld%lld",&k,&x0,&y0);
    241                 x[k] += x0 + y0, y[k] += x0 - y0;
    242                 update(1,1,n,k);
    243             } else if(op == 2) {
    244                 int k,col;
    245                 scanf("%d%d",&k,&col);
    246                 c[k] = col;
    247                 update(1,1,n,k);
    248             } else if(op == 3) {
    249                 int l,r;
    250                 scanf("%d%d",&l,&r);
    251 
    252                 for(int i = 0; i < 2; i++) {
    253                     ans.mxx[i].se = ans.mnx[i].se = -1;
    254                     ans.mxy[i].se = ans.mny[i].se = -1;
    255                 }
    256                 query(1,1,n,l,r);
    257                 ll out = 0;
    258                 //x
    259                 if(ans.mxx[0].se != -1) {
    260                     if(ans.mxx[1].se != -1 && ans.mxx[1].se != ans.mxx[0].se)
    261                         out = max(out,abs(ans.mxx[0].fi - ans.mxx[1].fi));
    262                     if(ans.mnx[0].se != -1 && ans.mnx[0].se != ans.mxx[0].se)
    263                         out = max(out,abs(ans.mxx[0].fi - ans.mnx[0].fi));
    264                     if(ans.mnx[1].se != -1 && ans.mnx[1].se != ans.mxx[0].se)
    265                         out = max(out,abs(ans.mxx[0].fi - ans.mnx[1].fi));
    266                 }
    267                 if(ans.mxx[1].se != -1) {
    268                     if(ans.mnx[0].se != -1 && ans.mnx[0].se != ans.mxx[1].se)
    269                         out = max(out,abs(ans.mxx[1].fi - ans.mnx[0].fi));
    270                     if(ans.mnx[1].se != -1 && ans.mnx[1].se != ans.mxx[1].se)
    271                         out = max(out,abs(ans.mxx[1].fi - ans.mnx[1].fi));
    272                 }
    273                 if(ans.mnx[0].se != -1 && ans.mnx[1].se != -1)
    274                     out = max(out,abs(ans.mnx[0].fi - ans.mnx[1].fi));
    275                 //y
    276                 if(ans.mxy[0].se != -1) {
    277                     if(ans.mxy[1].se != -1 && ans.mxy[1].se != ans.mxy[0].se)
    278                         out = max(out,abs(ans.mxy[0].fi - ans.mxy[1].fi));
    279                     if(ans.mny[0].se != -1 && ans.mny[0].se != ans.mxy[0].se)
    280                         out = max(out,abs(ans.mxy[0].fi - ans.mny[0].fi));
    281                     if(ans.mny[1].se != -1 && ans.mny[1].se != ans.mxy[0].se)
    282                         out = max(out,abs(ans.mxy[0].fi - ans.mny[1].fi));
    283                 }
    284                 if(ans.mxy[1].se != -1) {
    285                     if(ans.mny[0].se != -1 && ans.mny[0].se != ans.mxy[1].se)
    286                         out = max(out,abs(ans.mxy[1].fi - ans.mny[0].fi));
    287                     if(ans.mny[1].se != -1 && ans.mny[1].se != ans.mxy[1].se)
    288                         out = max(out,abs(ans.mxy[1].fi - ans.mny[1].fi));
    289                 }
    290                 if(ans.mny[0].se != -1 && ans.mny[1].se != -1)
    291                     out = max(out,abs(ans.mny[0].fi - ans.mny[1].fi));
    292                 printf("%lld
    ",out);
    293             }
    294         }
    295     }
    296     return 0;
    297 }
  • 相关阅读:
    Oracle 行列转换
    Oracle中特殊的INSERT语句
    在.net中实现压缩多个文件为.zip文件 【转】
    JavaScript 原型链【转】
    Oracle安装中的DHCP问题
    每日一题力扣49
    每日一题力扣423
    每日一题力扣451
    每日一题力扣237
    每日一题力扣383
  • 原文地址:https://www.cnblogs.com/scaulok/p/9911162.html
Copyright © 2020-2023  润新知