4 条题解

  • 1
    @ 2022-3-8 20:43:44

    zkw线段树a+b

    #include <bits/stdc++.h>
    // #include<bits/extc++.h>
    #define int long long  //__int128
    #define mmst0(x) memset(x, 0, sizeof(x))
    #define mmst3f(x) memset(x, 0x3f, sizeof(x))
    #define si(x) scanf("%d", &x)  // scanf("%lld",&x) // When define int ll
    #define pb(x) emplace_back(x)
    #define mkp(x, y) make_pair(x, y)
    #define fi first
    #define se second
    #define YESS printf("Yes\n")
    #define NOO printf("No\n")
    #define fori(a, b, c, d) for (int a = (b); a <= (c); a += (d))
    #define ford(a, b, c, d) for (int a = (b); a >= (c); a -= (d))
    using namespace std;
    // using namespace __gnu_pbds; // If using pbds don't using std!
    using ll = long long;
    // typedef long double rld; // use double pls!
    using ull = unsigned long long;
    
    const double eps = 1e-6;
    const int INF = 0x3f3f3f3f;  // 0x3f3f3f3f3f3f3f3f; // LLINF
    const int MAXN = (int)3e5 + 3;
    
    inline char nc() { return getchar(); }
    inline int read() {
      int s = 0, w = 1;
      char ch = nc();
      while (!isdigit(ch)) {
        if (ch == '-') w = -1;
        ch = nc();
      }
      while (isdigit(ch)) {
        s = (s << 3) + (s << 1) + (ch ^ 48);
        ch = nc();
      }
      return s * w;
    }
    // inline int read() {int x;si(x);return x;} // FAKE QUICK READ,JUST FOR DEBUG
    inline void read(int &x) {
      char ch = nc();
      x = 0;
      while (!(ch >= '0' && ch <= '9')) ch = nc();
      while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - 48, ch = nc();
    }  //
    int n, m, ans;
    int cnt;
    int r, p;
    int a[MAXN];
    int N;
    int tag[MAXN << 1];
    vector<int> tr(MAXN << 1);
    void build(int n) {
      N = 1 << (__lg(n + 5)) + 1;
      for (int i = 1; i <= n; ++i) tr[i + N] = a[i];
      for (int i = N; i; --i) tr[i] = tr[i << 1] + tr[i << 1 | 1];
    }
    void add(int u, int d) {
      for (int i = N + u; i; i >>= 1) tr[i] = d;
    }
    void add(int l, int r, int d) {
      int len = 1, cntl = 0, cntr = 0;
      for (l = N + l - 1, r = N + r + 1; l ^ r ^ 1; l >>= 1, r >>= 1, len <<= 1) {
        tr[l] += d * cntl, tr[r] += d * cntr;
        if (~l & 1) tag[l ^ 1] += d, tr[l ^ 1] += d * len, cntl += len;
        if (r & 1) tag[r ^ 1] += d, tr[r ^ 1] += d * len, cntr += len;
      }
      for (; l; l >>= 1, r >>= 1) {
        tr[l] += d * cntl, tr[r] += d * cntr;
      }
    }
    int query(int l, int r) {
      int cntl = 0, cntr = 0, len = 1, ans = 0;
      for (l = N + l - 1, r = N + 1 + r; l ^ r ^ 1; l >>= 1, r >>= 1, len <<= 1) {
        if (tag[l]) ans += cntl * tag[l];
        if (tag[r]) ans += cntr * tag[r];
        if (~l & 1) ans += tr[l ^ 1], cntl += len;
        if (r & 1) ans += tr[r ^ 1], cntr += len;
      }
      for (; l; l >>= 1, r >>= 1) {
        ans += cntl * tag[l] + cntr * tag[r];
      }
      return ans;
    }
    
    inline void work(signed CASE = 1, bool FINAL_CASE = false) {
      n = 2;
      for (int i = 1; i <= n; i++) a[i] = read();
      build(n);
      cout << query(1, 2);
    }
    signed main() {
      // ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
      // //freopen(".in", "r", stdin);//freopen(".out", "w", stdout);
      signed T = 1;  //(signed)read();//scanf("%d",&T);//cin>>T;
      for (signed CASE = 1; CASE <= T; CASE++) {  //
        // printf("Case #%d: ",CASE); //printf("Case %d: ",CASE); //printf("Case
        // #%d: \n",CASE); //printf("Case %d: \n",CASE); while(cin>>n) work(n);
        work(CASE, CASE == T);
      }
      return 0;
    }
    

    Fenwick Tree

    #include <bits/stdc++.h>
    // #include<bits/extc++.h>
    // #define int long long//__int128
    #define mmst0(x) memset(x, 0, sizeof(x))
    #define mmst3f(x) memset(x, 0x3f, sizeof(x))
    #define si(x) scanf("%d", &x)  // scanf("%lld",&x) // When define int ll
    #define pb(x) emplace_back(x)
    #define mkp(x, y) make_pair(x, y)
    #define fi first
    #define se second
    #define YESS printf("Yes\n")
    #define NOO printf("No\n")
    #define fori(i, a, b, c) for (int(i) = (a); (i) <= (b); (i) += c)
    #define ford(i, a, b, c) for (int(i) = (a); (i) >= (b); (i) -= c)
    using namespace std;
    // using namespace __gnu_pbds; // If using pbds don't using std!
    using ll = long long;
    // typedef long double rld; // use double pls!
    using ull = unsigned long long;
    
    const double eps = 1e-6;
    const int INF = 0x3f3f3f3f;  // 0x3f3f3f3f3f3f3f3f; // LLINF
    const int MAXN = (int)1e6 + 3;
    
    inline char nc() {
      static char buf[100000], *p1 = buf, *p2 = buf;
      return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
                 ? EOF
                 : *p1++;
    }
    inline int read() {
      int s = 0, w = 1;
      char ch = nc();
      while (!isdigit(ch)) {
        if (ch == '-') w = -1;
        ch = nc();
      }
      while (isdigit(ch)) {
        s = (s << 3) + (s << 1) + (ch ^ 48);
        ch = nc();
      }
      return s * w;
    }
    // inline int read() {int x;si(x);return x;} // FAKE QUICK READ,JUST FOR DEBUG
    //  inline void read(int &x){char ch=nc();x=0;while (!(ch>='0'&&ch<='9'))
    //  ch=nc();while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-48,ch=nc();} //
    //  根据参数个数自动选择 void prt(int
    //  x){if(x<0){putchar('-');x=-x;}if(x>9)prt(x/10);putchar((char)(x%10+'0'));}
    inline int lowbit(int x) { return x & (-x); }
    int tree[MAXN];
    inline void update(int i, int x) {
      for (int pos = i; pos < MAXN; pos += lowbit(pos)) tree[pos] += x;
    }
    inline int query(int n) {
      int ans = 0;
      for (int pos = n; pos; pos -= lowbit(pos)) ans += tree[pos];
      return ans;
    }
    inline int query(int l, int r) { return query(r) - query(l - 1); }
    int n, m, op, a, b;
    
    inline void work(signed CASE = 1, bool FINAL_CASE = false) {
      n = 2;
      for (int i = 1; i <= n; i++) update(i, read());
      cout << query(1, 2);
    }
    signed main() {
      // ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
      // //freopen(".in", "r", stdin);//freopen(".out", "w", stdout);
      signed T = 1;  //(signed)read();//scanf("%d",&T);// cin >> T;
      for (signed CASE = 1; CASE <= T; CASE++) {  //
        // printf("Case #%d: ",CASE); //printf("Case %d: ",CASE); //printf("Case
        // #%d: \n",CASE); //printf("Case %d: \n",CASE); while(cin>>n) work(n);
        work(CASE, CASE == T);
      }
      return 0;
    }
    

    信息

    ID
    729
    时间
    1000ms
    内存
    256MiB
    难度
    2
    标签
    递交数
    398
    已通过
    239
    上传者