HDU5845 Best Division
递归写法,好久不写很容易就gg了...
dp[i]=max(dp[j])+1,并且s[i]XORs[j]<=x
01字典树优化一下转移。
1 #include <bits/stdc++.h> 2 3 #define ll long long 4 #define ull unsigned long long 5 #define st first 6 #define nd second 7 #define pii pair<int, int> 8 #define pil pair<int, ll> 9 #define pli pair<ll, int> 10 #define pll pair<ll, ll> 11 #define tiii tuple<int, int, int> 12 #define pw(x) ((1LL)<<(x)) 13 #define lson l, m, rt<<1 14 #define rson m+1, r, rt<<1|1 15 #define FIN freopen("A.in","r",stdin); 16 #define FOUT freopen("A.out","w",stdout); 17 using namespace std; 18 /***********/ 19 template <class T> 20 bool scan (T &ret) { 21 char c; 22 int sgn; 23 if (c = getchar(), c == EOF) return 0; //EOF 24 while (c != '-' && (c < '0' || c > '9') ) c = getchar(); 25 sgn = (c == '-') ? -1 : 1; 26 ret = (c == '-') ? 0 : (c - '0'); 27 while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0'); 28 ret *= sgn; 29 return 1; 30 } 31 template<typename N,typename PN>inline N flo(N a,PN b){return a>=0?a/b:-((-a-1)/b)-1;} 32 template<typename N,typename PN>inline N cei(N a,PN b){return a>0?(a-1)/b+1:-(-a/b);} 33 template<typename T>inline int sgn(T a) {return a>0?1:(a<0?-1:0);} 34 template <class T1, class T2> 35 bool gmax(T1 &a, const T2 &b) { return a < b? a = b, 1:0;} 36 template <class T1, class T2> 37 bool gmin(T1 &a, const T2 &b) { return a > b? a = b, 1:0;} 38 template <class T> inline T lowbit(T x) {return x&(-x);} 39 40 template<class T1, class T2> 41 ostream& operator <<(ostream &out, pair<T1, T2> p) { 42 return out << "(" << p.st << ", " << p.nd << ")"; 43 } 44 template<class A, class B, class C> 45 ostream& operator <<(ostream &out, tuple<A, B, C> t) { 46 return out << "(" << get<0>(t) << ", " << get<1>(t) << ", " << get<2>(t) << ")"; 47 } 48 template<class T> 49 ostream& operator <<(ostream &out, vector<T> vec) { 50 out << "("; for(auto &x: vec) out << x << ", "; return out << ")"; 51 } 52 void testTle(int &a){ 53 while(1) a = a*(long long)a%1000000007; 54 } 55 const int inf = 0x3f3f3f3f; 56 const long long INF = 1e17; 57 const long long mod = 1000000007; 58 const double eps = 1e-6; 59 const int N = 1e5+5; 60 /***********/ 61 62 int a[N], X; 63 struct Tire{ 64 int tot, node[N][2], val[N], dp[N]; 65 int newnode(){ 66 ++tot; 67 node[tot][0] = node[tot][1] = val[tot] = 0; 68 dp[tot] = -1e9; 69 return tot; 70 } 71 void init(){ 72 tot = 0; 73 newnode(); 74 } 75 void pushup(int rt){ 76 val[rt] = 0; 77 dp[rt] = -1e9; 78 if(node[rt][0]&&val[ node[rt][0] ]) { 79 gmax(dp[rt], dp[ node[rt][0] ]); 80 val[rt] += val[ node[rt][0] ]; 81 } 82 if(node[rt][1]&&val[ node[rt][1] ]) { 83 gmax(dp[rt], dp[ node[rt][1] ]); 84 val[rt] += val[ node[rt][1] ]; 85 } 86 } 87 void update(int x, int d, int v, int now = 1, int dep = 30){ 88 if(dep == -1){ 89 val[now] += d; 90 if(val[now]) gmax(dp[now], v); 91 else dp[now] = -1e9; 92 return ; 93 } 94 int t = 1&(x>>dep); 95 if(!node[now][t]) node[now][t] = newnode(); 96 update(x, d, v, node[now][t], dep-1); 97 pushup(now); 98 } 99 int query(int x, int now = 1, int dep = 30){ 100 if(dep == -1) return dp[now]; 101 int t1 = 1&(X>>dep), t2 = 1&(x>>dep); 102 int ret = -1e9; 103 if(t1){ 104 if(node[now][t2]) gmax(ret, dp[ node[now][t2] ]); 105 if(node[now][t2^1]) gmax(ret, query(x, node[now][t2^1], dep-1)); 106 } 107 else if(node[now][t2]) gmax(ret, query(x, node[now][t2], dep-1)); 108 return ret; 109 } 110 }T; 111 int f[N]; 112 int main(){ 113 int t; scanf("%d", &t); 114 while(t--){ 115 int n, L, p, q, i; 116 scanf("%d%d%d", &n, &X, &L); 117 scanf("%d%d%d", a+1, &p, &q); 118 for(i = 2; i <= n; i++) 119 a[i] = (a[i-1]*(ll)p+q)%268435456LL; 120 for(int i = 2; i <= n; i++) 121 a[i] = a[i]^a[i-1]; 122 T.init(); 123 T.update(a[0], 1, f[0]); 124 for(i = 1; i <= n&&i <= L; i++){ 125 f[i] = T.query(a[i])+1; 126 T.update(a[i], 1, f[i]); 127 } 128 129 for( ; i <= n; i++){ 130 T.update(a[i-L-1], -1, f[i-L-1]); 131 f[i] = T.query(a[i])+1; 132 T.update(a[i], 1, f[i]); 133 } 134 printf("%d\n", max(f[n], 0)); 135 } 136 return 0; 137 }
诸神对凡人心生艳羡,厌倦天堂。