首页 > 代码库 > 区间查询/二分/树状数组/伸展树

区间查询/二分/树状数组/伸展树

一.手写二分

#include <bits/stdc++.h>int a[5] = {5, 3, 3, 2, 1};template <class T>inline T bfind(T r, T key){    T l = 1, m, k, flag = 0;    while(l != r)    {        m = (r + l) / 2;        if(a[m] > key) l = m + 1;        if(a[m] < key) r = m - 1;        if(a[m] == key)        {            r = m - 1;            flag = 1;            k = m;            if(a[r] != key) break;        }    }    return flag ? k : -1;}int main(int argc, char *argv[]){    int key = 1;    printf("%d\n", bfind(5, key));        return 0;}

二.库函数

#include <bits/stdc++.h>std::vector <int> A(5);int main(int argc, char *argv[]){    A[0] = 1;A[1] = 2;A[2] = 3;A[3] = 3;A[4] = 5;    for(int i = 0; i < A.size(); i++)        std::cout << A.size() - (upper_bound(A.begin(), A.end(), A[i]) - A.begin()) << "\n";        return 0;}

 三.树状数组

#include <stdio.h>#include <stdlib.h>#include <math.h>#include <time.h>#include <string.h>#include <iostream>#include <vector>#include <list>#include <stack>#include <queue>#include <map>#include <set>#include <algorithm>typedef short int int16;///32767typedef int int32;///2147483647typedef long long int64;///9223372036854775807const double PI=acos(-1.0);///3.141593const long long MOD=(long long)1E9+7LL;///1000000007int Fibonacci[100]={0,1};///int Prime[100];///int Caltan[100];///template <class T> T Susake_pow(T a,T b)///pow{T res;if(b==0) return 1;else while((b&1)==0){b>>=1;a*=a;}res=a;b>>=1;while(b!=0){a*=a;if((b&1)!=0)res*=a;b>>=1;}return res;}template<class T> inline T gcd(T a,T b)///gcd{if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}template<class T> inline T lcm(T a,T b)///lcm{if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}template<class T> inline char *Susake_nsystem(T n)///itoa(26){T t=0,i;char *s,*p;s=(char *)malloc(sizeof(char)*1000);p=(char *)malloc(sizeof(char)*1000);while(n){s[t]=n%26+64;if(s[t]==64){s[t]+=26;n-=26;}t++;n/=26;}s[t]=\0;for(i = 0; i < t; i++)p[i]=s[t-1-i];p[i]=\0;free(s);return p;}int Susake_system(char *s)///atoi(26){int len=strlen(s),i,sum=0;char p[1000];for(i=0;i<len;i++)p[i]=s[len-1-i]-64;for(i=0;i<len;i++)sum+=p[i]*Susake_pow(26,i);return sum;}int isdigit(int c);int islower(int c);int isupper(int c);int tolower(int c);int toupper(int c);///union_findint fa[1];template <class T> inline T union_find(T x){return fa[x] == x ? x : fa[x] = union_find(fa[x]);}///tree arrayint t_a[200001], t_c[200001];int t_n;template <class T> inline T lowbit(T x){return x&(-x);}void update(int p,int x){while(p<=t_n){t_c[p]+=x;p+=lowbit(p);}}template <class T> inline T sum(T p){T s=0;while(p>0){s+=t_c[p];p-=lowbit(p);}return s;}int main(int argc, char *argv[]){    int t;    scanf("%d", &t);    char s[100];    for(int z = 1; z <= t; z++)    {        memset(t_a, 0, sizeof(t_a));        memset(t_c, 0, sizeof(t_c));        scanf("%d", &t_n);        for(int i = 1; i <= t_n; i++)        {            t_a[i] += 0;            update(i, 0);        }        for(int i = 1; i <= t_n; i++)        {            int x;            scanf("%d%*c", &x);            t_a[i] += x;            update(i, x);        }        printf("Case %d:\n", z);        while(scanf("%s", s))        {            if(strcmp(s, "End") == 0) break;            if(strcmp(s, "Add") == 0)            {                int j, k;                scanf("%d%d", &j, &k);                t_a[j] += k;                update(j, k);            }            if(strcmp(s, "Sub") == 0)            {                int j, k;                scanf("%d%d", &j, &k);                t_a[j] -= k;                update(j, -k);            }            if(strcmp(s, "Query") == 0)            {                int j, k;                scanf("%d%d", &j, &k);                printf("%d\n", sum(k) - sum(j - 1));            }        }        getchar();    }    return 0;}

四.伸展树

#include <bits/stdc++.h>#include <ext/pb_ds/assoc_container.hpp>#include <ext/pb_ds/priority_queue.hpp>#include <ext/pb_ds/tree_policy.hpp>#include <ext/pb_ds/trie_policy.hpp>std::priority_queue <int> pr_q;__gnu_pbds::trie <std::string, int> trie_t;__gnu_pbds::tree <int, int, std::less<int>, __gnu_pbds::splay_tree_tag, __gnu_pbds::tree_order_statistics_node_update> sp_t;__gnu_pbds::priority_queue<std::pair <int, int>,std::greater<std::pair <int, int> >, __gnu_pbds::pairing_heap_tag > pa_h;int main(int argc, char *argv[]){    sp_t[1] = 1;///0下标,为1的有一个    sp_t[2] = 1;///1下标,为2的有一个    sp_t[3] = 2;///2下标,为3的有两个    sp_t[5] = 1;///3下标,为5的有一个    int sum = 0;    int key = 5;///查询比key大的个数    for(int i = sp_t.order_of_key(key) + 1; i < sp_t.size(); i++)///从查找的元素的后一个序列开始        sum += sp_t[sp_t.find_by_order(i) -> first];    printf("%d\n", sum);        return 0;}

 

区间查询/二分/树状数组/伸展树