1. 程式人生 > 實用技巧 >Codeforces Round #169 (Div. 2)補題記錄

Codeforces Round #169 (Div. 2)補題記錄

前三題沒什麼可說的

CF - 276C.Little Girl and Maximum Sum

記錄每個索引詢問的次數,將陣列內的數從大到小排序,詢問次數從大到小排序,相乘求和即可。

CF - 276D.Little Girl and Maximum XOR

\([l,r]\)區間內兩個數異或的最大值。

我們考慮\(l\)\(r\)異或所得的數\(x\),我們的答案就是從這個數\(x\)的最高位\(1\)開始,後面所有位全部置為\(1\)

注意\(long~long\)

#if __cplusplus >= 201103L
#pragma comment(linker, "/STACK:102400000,102400000")
#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
#else
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <string.h>
#include <vector>
#endif

using namespace std;
#define inf __INT_MAX__
#define enf INT_MIN
#define INF LLONG_MAX
#define ENF LLONG_MIN

const int MAXN = 2e5 + 10;
const double pi = acos(-1.0);
const double eps = 1e-6;
typedef long long ll;
typedef unsigned long long ull;
#define zhengfu(x) ((x > eps) - (x < -eps))

int main() {
    ll l, r;
    cin >> l >> r;
    if (l == r)
        cout << 0 << endl;
    else {
        ll tmp = l ^ r;
        for (ll i = 63; i >= 0; i--)
            if (tmp & (1LL << i)) {
                cout << (1LL << (i + 1)) - 1LL << endl;
                break;
            }
    }
}

CF - 276E.Little Girl and Problem on Trees

給你一棵樹,其中,除了根節點\(1\)以外,其他的節點度數不大於2。

也就是說,如果把\(1\)從中刪除,那麼就會變成一條條鏈。

兩個操作,一個操作是對於節點\(v\),令與其距離不超過\(d\)的節點加\(x\),包括自己。

另一個是詢問節點\(v\)上的加和是多少。

考慮題面:

​ 區間更新,單點查詢\(\longrightarrow\)顯然線段樹\(or\)樹狀陣列

​ 考慮區間更新:

  • 當距離\(d\)只侷限於節點\(v\)所在的鏈上,即\(depth[v]-d\)不包括根節點\(1\)

    只需要對於該鏈的節點進行簡單的區間更新即可。

  • 當距離\(d\)不侷限於\(v\)所在鏈時,顯然,更新包括了根節點\(1\)

    • 那麼, 我們可以根據樹的深度建一個樹狀陣列,我們只需要更新\([1,d-depth[v]]\)之間的節點即可

    • 所以,我們的更新過程就成了這樣:

      • 首先在鏈上區域性更新新\(x\),範圍是\([depth[v]-d,depth[v]+d]\)
      • 然後在深度樹狀陣列更新\(x\),範圍是\([1,d-depth[v]]\)
      • 然後在鏈上區域性更新\(-x\),範圍是\([1,d-depth[v]]\)

​ 再考慮單點查詢,答案顯然就變成了,深度樹狀陣列\(+\)鏈上樹狀陣列的和。

#if __cplusplus >= 201103L
#pragma comment(linker, "/STACK:102400000,102400000")
#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
#else
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <string.h>
#include <vector>
#endif

using namespace std;
#define inf __INT_MAX__
#define enf INT_MIN
#define INF LLONG_MAX
#define ENF LLONG_MIN

const int MAXN = 2e5 + 10;
const double pi = acos(-1.0);
const double eps = 1e-6;
typedef long long ll;
typedef unsigned long long ull;
#define zhengfu(x) ((x > eps) - (x < -eps))

/*
每條鏈一個樹狀陣列,記錄鏈上不過根的更新
一個總樹狀陣列記錄過根的深度的更新
*/
vector<int> e[MAXN];
struct node {
    vector<int> nd;
    int n;
    void init() {
        n = nd.size() - 1;
    }
    inline int lowbit(int x) //核心操作,取x這個數的二進位制最後一位1
    {
        return -x & x;
    }
    inline void update(int x, int k) //單點修改
    {
        for (; x <= n; x += lowbit(x))
            nd[x] += k;
    }
    void update(int l, int r, int k) {  //區間更新
        update(l, k);
        if (r < n)
            update(r + 1, -k);
    }
    inline int query(int x) //求x的字首和
    {
        int sum(0);
        for (; x > 0; x -= lowbit(x))
            sum += nd[x];
        return sum;
    }
} tree[MAXN];
int depmax = enf;
int dp[MAXN];
int nm[MAXN];
void dfs(int anc, int x, int dep, int num) {
    tree[num].nd.push_back(0);
    dp[x] = dep, nm[x] = num;
    if (e[x].size() == 1) {
        depmax = max(dep, depmax);
        tree[num].init();
        return;
    }
    for (auto it : e[x])
        if (it != anc)
            dfs(x, it, dep + 1, num);
}
int main() {
    int n, q;
    cin >> n >> q;
    for (int i = 1, x, y; i < n; i++) {
        cin >> x >> y;
        e[x].push_back(y);
        e[y].push_back(x);
    }
    for (int i = 1; i <= e[1].size(); i++) {
        tree[i + 1].nd.push_back(0);
        tree[i + 1].nd.push_back(0);
        dfs(1, e[1][i - 1], 2, i + 1);
    }

    dp[1] = nm[1] = 1;
    for (int i = 0; i <= depmax; i++)
        tree[1].nd.push_back(0);
    tree[1].init();

    for (int i = 1, j, v, x, d; i <= q; i++) {
        cin >> j >> v;
        int pos = dp[v], tmp = nm[v];
        if (!j) {
            scanf("%d%d", &x, &d);
            tree[tmp].update(max(2, pos - d), pos + d, x);
            if (pos - d < 2) {
                int len = d - pos + 2;
                tree[tmp].update(2, len, -x);
                tree[1].update(1, len, x);
            }
        } else {
            if (v == 1)
                cout << tree[1].query(pos) << endl;
            else
                cout << tree[tmp].query(pos) + tree[1].query(pos) << endl;
        }
    }
}

六花保佑程式碼不\(WA\)

/**
       爆ぜろリアル!弾けろシナプス!パニッシュメント・ディス・ワールド!     
                                                                          
                         ';!!!:`                                        
                     .;%%%%$$$$%%%;.                                    
                   .;$%;.         :%;.                                  
                  `||`              :!.                                 
                 .!:                 ::                                 
                 :;                  .`.                                
                 '`                   ..                                
                 ``                   .                                 
                  ..                 ..                                 
                                     ..                                 
                         `;||!:';|%$$$%%%$$%|;'.                        
                     '|%%%%%%%%%%%%%%%%%%%%%%%%%%%%|:.                  
                   ;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%$|:               
                .:!%%%%%%%%%%|!|%%%%$$&$$$%%%%%%%%%%%%%%%%|'   `:'.     
               '!|%%%%%||%%%%%%%%%%%$&&&$%%%%%$$$%%%%%%%%%%%|'':':'.    
             `|%%%%%%%%%%%%%$%%%%%%%$&&&&$$%%%$$$%%%%%%%%%%%%|;:::'::':`
            :%%%%%%$$%%%$$%%$%%%%%%%$&&&&&$$%%|%$%%%|!|%%%%%$!'::::;|!:'.
          .;$%%%%%$$%%%$$%%$%%%%%%%%$&&&&&$$$%%$$$%%%!!%$%%%%!::!%%%%|;'.
         .;%$%%%$$$%%%$$%%||%$$%%%%$$&&&&&&$$%%$$%%%%%%%$&&&$%$$$$%%%%|:.
         ;%%%%%$$$%%$&$%%!:!$$$%%%%$&$||&&&&$$%$$$%%%%%%$$$%%%%$$$$%%%|'
        :%%$%%$$$$%$&&$%!`'|$&$%%%%$&|`.!&&&&$%$$$%%%%%%%$%%%%%%$$$$%%%;
       `!%|%%%$&$%%$&$$|'.'|&&$%%%%$%|!'.:$&&&$$$$%%%%%%%%%%%%%%$$$$%%%|`
       ;%':%%$&&$%$&|!|'.`:|&&$%%%$$|'''``'%&&&$$$%%%%%%%%%%%%%%$&&$%%%%:
      `;' ;%%$&$%%$%;;:   .:!%$%%%$|`  ..  .!&&&$$$%%$%%%$%$$%$$%$&$%%%$;
      '' .!%$&&$$%$;`:'      `!%%%%:.... ....:$&$$$%$$%%$$$$$%$$%$&$%%%%;
      .. .;%$&&&$%|:.        ...;%|'..........'|$%%%$$%%$$&&$%$$%%%$%%%$;
          ;$$&&&$%!`        .`....'`.... ......`!%%%$$%%$&&&$$&&$!!$%%%$:
          '%$&&&&$!`.       .`........``.......`!%%$$$%$&&&&$$&&$;;$%%%%'
          '|$&&&&!``..```.  ........`|@@|'....`:|%%$$%%$&&&&$$&&$':$%%%|`
          `|$&&&&$'  .``````'''::`..........``.:%%$&$%|'`!&&$$&&%':%%%%;.
           ;$$&&|;!:`...........  .':`....`````!%$&&$%|;';&&&%|%%';%%%%:
           '%$&$:  ...................`''`...`:%%$&$$!`..!&&&$;`:;|%%%!`
            ;$&&%:.....................  .':``!%$&&$;...:$&%'`;' ;%%%%: 
            .;$&&&$:..........`'............`!%$&&&;..'%&&!     `!%%%:  
             .':|&&&|''`..............   ...'|$&&$||$&&&|`      :|'``   
                  .!%: `|$%|!:'`.........`:!%$&&&&&$;''.       `.       
                  .:!%$%||||||%%$$$$$$$$$%$%%$$$%%:.                    
             `!%$%%%%%%%|||||||||||%|||||||%%%%%$%%%%|:                 
          '%%%%%%%%%%%%||||||||!!|||||||||||%$$%%%%%%%%$%:              
         :%$$$%%%$%$$%||||||||!;%%||||||||%%%%%%%%%%%%%%%%|'            
         .;$%%%%$$$$$%%|||||%$$$$$%||||||||%$$%%%%%%%%%%%%%%'           
          .!%%%%%%$$%%$%%|||||;;%||||||||%$$&&|!%$$%%%$$$$$%;.          
           .!%%%%%%%%%%%%%%$%||%%%%%%%%%%%$&$$;`;%%%%%%%%$$!.           
             :%%%%%%%%%%%%%%||%%%%%%%%%%%$$$%%%%%%%%%%%%%|'             
                :%%%%%%%%%%$$%%%%%%%%%%%%%%%;`;%%%%%%%|:                
               .;%%%%%%%%%%%%%%%%%%%%%%%%%%%:..:|%%%!.                  
               '%%%%%%%%%%%%%%%%%%%%%%%%%%%%%|;:`                       
               ;$%%%%%%%%%%|%%%%%%%%%%%%%%%%%%%%'                       
              `|%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%$;                       
              .:!%$%%%%%%%%%%%%%%%%%%%%%%%%%%%%$!.                      
              ;!';%%%%%%%%%%%%%%%%%%%%%!:`.  `|$%'                      
             .;%%%%%%%%%%%||%%%%%%%%%|:';|%$%%%%%;.                     
             .!%%%%%%%%%%%%$$%%%%%%%%%%%%%%%%%%%%%'                     
             .!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%$;.                    
             .;%%%%%%%%%%%%$%%%%%%%%%%%%%%%%%%%%%%%'                    
              ;%$$%%%%%%%%$$$$%%%%%%%%%%%%%%%%%%%%%'                    
             `|$&$%%%%%%$%$$$%%%%%%%%%%%%$%%%%%$&%%!`                   
             :%$&$%%||%%%%$$%%%$%%%%%$$!:|%||%%$&$%%!`                  
**/