zoukankan      html  css  js  c++  java
  • [HDOJ3333]Turing Tree(离线,树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333

    题意:求区间[l,r]内不重复的数字的和。

    思路:存下所有的查询后排序,扫描原数组的时候记录当前数字的下标并且查看是否出现过,未出现过则更新a[i]到bit的[1,i]位置上;否则更新[上次位置,i]。一边更新一边查询,这样求和不会重复。

    相当于一边更新一边查,把唯一的结果都尽可能地往右堆,因为保证了查询左边界一定是在移动的。

      1 #include <algorithm>
      2 #include <iostream>
      3 #include <iomanip>
      4 #include <cstring>
      5 #include <climits>
      6 #include <complex>
      7 #include <fstream>
      8 #include <cassert>
      9 #include <cstdio>
     10 #include <bitset>
     11 #include <vector>
     12 #include <deque>
     13 #include <queue>
     14 #include <stack>
     15 #include <ctime>
     16 #include <set>
     17 #include <map>
     18 #include <cmath>
     19 using namespace std;
     20 #define fr first
     21 #define sc second
     22 #define cl clear
     23 #define BUG puts("here!!!")
     24 #define W(a) while(a--)
     25 #define pb(a) push_back(a)
     26 #define Rint(a) scanf("%d", &a)
     27 #define Rll(a) scanf("%I64d", &a)
     28 #define Rs(a) scanf("%s", a)
     29 #define Cin(a) cin >> a
     30 #define FRead() freopen("in", "r", stdin)
     31 #define FWrite() freopen("out", "w", stdout)
     32 #define Rep(i, len) for(int i = 0; i < (len); i++)
     33 #define For(i, a, len) for(int i = (a); i < (len); i++)
     34 #define Cls(a) memset((a), 0, sizeof(a))
     35 #define Clr(a, x) memset((a), (x), sizeof(a))
     36 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
     37 #define lrt rt << 1
     38 #define rrt rt << 1 | 1
     39 #define pi 3.14159265359
     40 #define RT return
     41 #define lowbit(x) x & (-x)
     42 #define onecnt(x) __builtin_popcount(x)
     43 typedef long long LL;
     44 typedef long double LD;
     45 typedef unsigned long long ULL;
     46 typedef pair<int, int> pii;
     47 typedef pair<string, int> psi;
     48 typedef pair<LL, LL> pll;
     49 typedef map<string, int> msi;
     50 typedef vector<int> vi;
     51 typedef vector<LL> vl;
     52 typedef vector<vl> vvl;
     53 typedef vector<bool> vb;
     54 
     55 typedef struct Query {
     56     int l, r;
     57     int id;
     58     Query() {}
     59     Query(int ll, int rr, int ii) : l(ll), r(rr), id(ii) {}
     60 }Query;
     61 const int maxn = 30100;
     62 const int maxq = 100100;
     63 int n, q, cur;
     64 int a[maxn];
     65 LL bit[maxn];
     66 map<int, int> id;
     67 
     68 Query query[maxq];
     69 LL ret[maxq];
     70 
     71 LL sum(int i) {
     72     LL s = 0;
     73     while(i > 0) {
     74         s += bit[i];
     75         i -= lowbit(i);
     76     }
     77     return s;
     78 }
     79 
     80 void add(int i, int x) {
     81     while(i <= n) {
     82         bit[i] += x;
     83         i += lowbit(i);
     84     }
     85 }
     86 
     87 bool cmp(Query a, Query b) {
     88     if(a.r == b.r) return a.l < b.l;
     89     return a.r < b.r;
     90 }
     91 
     92 int main() {
     93     // FRead();
     94     int T, l, r;
     95     Rint(T);
     96     W(T) {
     97         Cls(bit); id.clear(); cur = 0;
     98         Rint(n);
     99         For(i, 1, n+1) Rint(a[i]);
    100         Rint(q);
    101         Rep(i, q) {
    102             Rint(l); Rint(r);
    103             query[i] = Query(l, r, i);
    104         }
    105         sort(query, query+q, cmp);
    106         For(i, 1, n+1) {
    107             if(id.find(a[i]) == id.end()) {
    108                 add(i, a[i]);
    109                 id[a[i]] = i;
    110             }
    111             else {
    112                 add(i, a[i]);
    113                 add(id[a[i]], -a[i]);
    114                 id[a[i]] = i;
    115             }
    116             while(query[cur].r == i) {
    117                 ret[query[cur].id] = sum(query[cur].r) - sum(query[cur].l-1);
    118                 cur++;
    119             }
    120         }
    121         Rep(i, q) printf("%I64d
    ", ret[i]);
    122     }
    123     RT 0;
    124 }
  • 相关阅读:
    数据库事务之不可重复读
    数据库事务与脏读
    图结构代码实现
    哈希表与散列函数
    数据库表设计与视图
    B树和B+树
    java之字符串中查找字串的常见方法
    剑指 Offer 15. 二进制中1的个数——JS
    剑指 Offer 03. 数组中重复的数字——JS
    算法设计与分析——排序
  • 原文地址:https://www.cnblogs.com/kirai/p/5868996.html
Copyright © 2011-2022 走看看