锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
鍥犱負鎶婁竴媯佃竟鏉冧負1鐨勬爲鐨勬嫭鍙峰簭鍒楁嬁鍑烘潵錛屾爲涓婃煇涓ょ偣鐨勮窛紱誨氨鏄湪鎷彿搴忓垪涓袱鐐歸棿娌″尮閰嶆嫭鍙風殑涓暟錛堟湁宸︽嫭鍙峰彸鎷彿閫夋嫨鐨勫尯鍒紝鍏蜂綋鍒嗘瀽澶勭悊錛夈傚綋鐒訛紝鏃㈢劧鏄涓緹ゆ爲鎿嶄綔錛岄偅灝辯洿鎺ョ敤鍔ㄦ佹爲灝辮浜嗐?br>
浜庢槸灝卞幓瀛︿簡鍔ㄦ佹爲銆傚彂鐜板叾瀹炰笉綆楀緢闅撅紙1.鎸囨椂闂村鏉傚害鍧囨憡logn鐨勭畻娉曪紝榪樻湁鍩轟簬杞婚噸杈瑰墫鍒嗙殑涓ユ牸logn鐨勭畻娉?2.濡傛灉浣犲splay鐔熺殑璇濓級錛屽啓璧鋒潵涔熷氨鍩烘湰涓婂氨鏄竴媯祍play錛屼篃綆楁瘮杈冨ソ鍐欑殑銆傘傦紙浠ュ悗灝卞憡鍒礬寰勫墫鍒嗕簡銆傘傚お楹葷儲浜嗐傘傚鏉傚害涔熸病鍔ㄦ佹爲濂姐傘傦級
浠ヤ笅鎵璇寸殑鍔ㄦ佹爲閮芥槸鍩轟簬splay鐨勬椂闂村鏉傚害鍧囨憡logn鐨勫姩鎬佹爲銆?br>
鍔ㄦ佹爲鐨勪富瑕佹濇兂灝辨槸錛氱被浼艱交閲嶈竟鍓栧垎涓鏍鳳紝鎶婃暣媯墊爲鍒掑垎鎴愯嫢騫插疄杈?solid edge)鍜岃櫄杈?dashed edge)錛屼絾榪欎釜閮芥槸鏍規(guī)嵁浣犵殑闇瑕佹潵璁懼畾鐨勶紝涓嶅儚杞婚噸杈逛竴鏍鋒瘡涓偣寰涓嬮兘蹇呴』鏈変竴鏉¢噸杈癸紙鍗曠嫭鐨勫彾瀛愯妭鐐圭畻闀垮害涓?鐨勯噸杈癸級錛岃屾槸姣忔鎶婁綘鎵闇瑕佹搷浣滅殑鐐瑰埌鏍圭殑杈歸兘鏀逛負瀹炶竟(expose鎿嶄綔)錛屼笖姣忎釜鐐瑰線涓嬬殑瀹炶竟鏁頒笉瓚呰繃1銆備慨鏀規(guī)部閫斿鏋滄湁涓涓偣宸茬粡鏈変簡瀹炶竟杈歸偅涔堝氨鎶婂畠鍘熸潵鐨勫疄杈規(guī)敼鎴愯櫄杈廣傝繖鏍鋒瘡嬈″涓涓偣鎿嶄綔閮芥槸鍦ㄤ竴鏉″疄璺緞涓?solid path)銆傚浜庢瘡涓鏉″疄璺緞錛岄兘鐢ㄤ竴媯祍play鏉ョ淮鎶ゅ氨琛屼簡銆傦紙splay鍙互涔辮漿涔辨嫈涔辨帴澶埥浜嗐傘? -!褰撶劧鏄湪涓瀹氳鍒欎笅鐨勪貢銆傘傦級
* $File: bounce.cpp
* $Date: Fri Jul 09 20:59:27 2010 +0800
* $Author: Tim
* $Solution: Dynamic Tree with Splay Tree implementation
* $Time complexity: O(mlogn) , per operation amorized O(logn);
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#define MAXN 200005
using namespace std;
class SplayNode
{
public:
int fa, lt, rt, size;
};
SplayNode node[MAXN + 1];
// functions below are belong to splay tree
// we can see that, this splay tree is quite
// simple, and just 'splay' function
// and size maintaining supported.
// but that what all we need to
// solve this problem
void Renew(int x)
{
if (!x)
return;
node[x].size = node[node[x].lt].size + node[node[x].rt].size + 1;
}
void RightRotate(int x)
{
int lc = node[x].lt, fa = node[x].fa;
node[x].lt = node[lc].rt; node[node[x].lt].fa = x;
node[lc].rt = x; node[x].fa = lc;
node[lc].fa = fa;
if (x == node[fa].lt)
node[fa].lt = lc;
else
node[fa].rt = lc;
Renew(x);
Renew(lc);
}
void LeftRotate(int x)
{
int rc = node[x].rt, fa = node[x].fa;
node[x].rt = node[rc].lt; node[node[x].rt].fa = x;
node[rc].lt = x; node[x].fa = rc;
node[rc].fa = fa;
if (x == node[fa].lt)
node[fa].lt = rc;
else
node[fa].rt = rc;
Renew(x);
Renew(rc);
}
void splay(int x, int FA = 0)
{
int fa, Fa;
while ((fa = node[x].fa) != FA)
{
if ((Fa = node[fa].fa) == FA)
{
if (x == node[fa].lt)
RightRotate(fa);
else
LeftRotate(fa);
}
else
{
if (x == node[fa].lt)
{
if (fa == node[Fa].lt)
{
RightRotate(Fa);
RightRotate(fa);
}
else
{
RightRotate(fa);
LeftRotate(Fa);
}
}
else
{
if (fa == node[Fa].rt)
{
LeftRotate(Fa);
LeftRotate(fa);
}
else
{
LeftRotate(fa);
RightRotate(Fa);
}
}
}
}
}
// end splay
int root;
int query_rank(int id)
{
splay(id);
return node[node[id].lt].size + 1;
}
int father[MAXN + 1];
int n;
void Init()
{
scanf("%d", &n);
for (int i = 1, k; i <= n; i ++)
{
scanf("%d", &k);
k += i;
if (k > n + 1)
k = n + 1;
father[i] = k;
node[i].size = 1;
}
node[n + 1].size = 1;
}
int split(int id)
// isolate id and the node right after it on the solid path
// and return that node
{
splay(id);
if (node[id].rt)
{
int rc = node[id].rt;
node[id].rt = node[rc].fa = 0;
node[id].size -= node[rc].size;
return rc;
}
else
return 0;
}
void Link(int id, int fa)
// let fa be the father of id,
// we assume that before this,
// id is the head of a solid path,
// and fa is the tail of a solid path,
// this was done by function 'cut' and 'split'
{
splay(id);
assert(!node[id].lt);
splay(fa);
assert(!node[fa].rt);
node[fa].rt = id;
node[fa].size += node[id].size;
node[id].fa = fa;
}
int get_head(int x)
// get the head of the solid path which x is in.
{
while (node[x].fa)
x = node[x].fa;
while (node[x].lt)
x = node[x].lt;
splay(x);
return x;
}
void expose(int id)
// turn the edges between id and the root of the tree id is in
// all into solid edges. with this operation, we can query what
// we want conveniently in a splay tree.
{
while (true)
{
id = get_head(id);
if (!father[id])
break;
split(father[id]);
Link(id, father[id]);
}
}
int query_depth(int id)
{
expose(id);
return query_rank(id) - 1;
}
void cut(int id)
// this function isolated the subtree rooted id
{
expose(id);
split(father[id]);
}
void modify_father(int id, int fa)
{
cut(id);
split(fa);
father[id] = fa;
Link(id, fa);
}
void Solve()
{
int m, cmd, id, k;
scanf("%d", &m);
while (m --)
{
scanf("%d%d", &cmd, &id);
id ++;
if (cmd == 1)
printf("%d\n", query_depth(id));
else
{
scanf("%d", &k);
k += id;
if (k > n + 1)
k = n + 1;
modify_father(id, k);
}
}
}
int main()
{
freopen("bounce.in", "r", stdin);
freopen("bounce.out", "w", stdout);
Init();
Solve();
return 0;
}
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define MAXN 8
#define BLANK 0
#define LEFT 1
#define RIGHT 2
#define MAXSTATE 174420
#define MAXSTATEAMOUNT 835
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define ll long long
using namespace std;
int n,m;
char map[MAXN+1][MAXN+1];
int nState = 0;
int State[MAXSTATEAMOUNT+1];
int id[MAXSTATE+1];
int Tx, Ty, FinalState;
ll f[MAXN+1][MAXN+1][MAXSTATEAMOUNT+1];
ll ans;
void Reset(){
nState = 0, Tx = -1;
memset(f, 0, sizeof(f));
ans = 0;
}
bool Init(){
scanf("%d%d",&n,&m);
if (!n) return false;
Reset();
for (int i = n-1; i>=0; i--){
scanf("%s", map[i]);
if (Tx == -1)
for (int j = m-1; j>=0; j--)
if (map[i][j] == '.'){
Tx = i, Ty = j;
break;
}
for (int j = 0; j<m; j++)
if (map[i][j] == '.') map[i][j] = 0;
else map[i][j] = 1;
}
return true;
}
void dfs(int pos, int r_bracket, int state){
if (pos < 0){
State[nState] = state;
id[state] = nState;
/*
printf("%d: ", nState);
for (int i = 0; i<=m; i++)
printf("%d ", buffer[i]);
printf("\n");
*/
nState ++;
return;
}
if (pos >= r_bracket) // blank
dfs(pos - 1, r_bracket, (state << 2));
if (pos > r_bracket) // right bracket
dfs(pos - 1, r_bracket + 1, (state << 2) | RIGHT);
if (r_bracket) // left bracket
dfs(pos - 1, r_bracket - 1, (state << 2) | LEFT);
}
#define MASK 3
#define Get(state, p) (((state) >> (p<<1)) & MASK)
inline bool OK(int i, int j, int state){
if (Get(state, j) == 1 && Get(state, j+1) == 2 && !(i == Tx && j == Ty)) return false;
for (int k = 0; k<j; k++)
if ((map[i][k] || map[i+1][k]) && Get(state, k)) return false;
if (((j && map[i][j-1]) || (map[i][j])) && Get(state, j)) return false;
for (int k = j+1; k<=m; k++)
if (((i && map[i-1][k-1]) || (map[i][k-1])) && Get(state, k)) return false;
return true;
}
inline int Modify(int state, int p, int v){
return state - (((state >> (p << 1)) & MASK) << (p << 1)) + (v << (p << 1));
}
inline int FindRight(int p, int state){
int cnt = 0, t;
for (int i = p; i<=m; i++){
t = Get(state,i);
if (t == 1) cnt++;
if (t == 2) cnt--;
if (cnt == 0) return i;
}
return -1;
}
inline int FindLeft(int p, int state){
int cnt = 0, t;
for (int i = p; i>=0; i--){
t = Get(state, i);
if (t == 2) cnt++;
if (t == 1) cnt--;
if (cnt == 0) return i;
}
return -1;
}
void Solve(){
dfs(m, 0, 0);
f[0][0][id[(1 << 2) + (2 << (2 * m))]] = 1;
FinalState = (1 << (2 * Ty)) + (2 << (2 * (Ty+1)));
int p, q, tmp, tmp2, state, v, i, j, k;
ll *a;
for (i = 0; i < n; i++){
for (j = 0; j < m; j++){
a = f[i][j+1];
for (k = 0; k < nState; k++)
if ((v = f[i][j][k])){
state = State[k];
p = Get(state, j), q = Get(state, j+1);
if (p == 0 && q == 0){
if (!map[i][j]){
tmp = Modify(Modify(state, j, 1), j+1, 2);
if (OK(i, j+1, tmp))
a[id[tmp]] += v;
}else
a[k] += v;
}else if(p == 0){ // conditions below ensured map[i][j] is empty, because there exists at least one bracket on one side of the grid (i,j)
if (OK(i, j+1, state))
a[k] += v;
tmp = Modify(Modify(state, j, q), j+1, 0);
if (OK(i, j+1, tmp))
a[id[tmp]] += v;
}else if (q == 0){
if (OK(i, j+1, state))
a[k] += v;
tmp = Modify(Modify(state, j, 0), j+1, p);
if (OK(i, j+1, tmp))
a[id[tmp]] += v;
}else{
tmp = Modify(Modify(state, j, 0), j+1, 0);
if (p == 1 && q == 1){
tmp2 = Modify(tmp, FindRight(j+1, state), 1);
if (OK(i, j+1, tmp2))
a[id[tmp2]] += v;
}else if (p == 2 && q == 2){
tmp2 = Modify(tmp, FindLeft(j, state), 2);
if (OK(i, j+1, tmp2))
a[id[tmp2]] += v;
}else if (p == 1 && q == 2){
if (i == Tx && j == Ty && state == FinalState){
printf("%I64d\n", v);
return;
}
}else if (p == 2 && q == 1){
if (OK(i, j+1, tmp))
a[id[tmp]] += v;
}
}
}
}
for (int k = 0; k < nState; k++)
if (Get(State[k], m) == 0 && OK(i+1, 0, tmp = (State[k] << 2)))
f[i+1][0][id[tmp]] += f[i][m][k];
}
printf("%I64d\n", ans);
}
int main(){
while (Init())
Solve();
return 0;
}
銆傘備笂杈硅繖涓摼鎺ュ鏋滄湁浜?span onclick="tagshow(event)" class="t_tag">涓嬭澆灝變細淇濈暀7澶┿傘傘?澶╂病浜轟笅灝辨病浜嗐傘?br>嬈茶喘浠庨熴傘傘?br>orz涓鍒囩鐗涖傘?br>
銆愰鐩弿榪般?/span>
lxhgww鏈榪戞帴鍒頒簡涓涓敓鎴愬瓧絎︿覆鐨勪換鍔★紝浠誨姟闇瑕佷粬鎶?/span>n涓?/span>1鍜?/span>m涓?/span>0緇勬垚瀛楃涓詫紝浣嗘槸浠誨姟榪樿姹傚湪緇勬垚鐨勫瓧絎︿覆涓紝鍦ㄤ換鎰忕殑鍓?/span>k涓瓧絎︿腑錛?/span>1鐨勪釜鏁頒笉鑳藉皯浜?/span>0鐨勪釜鏁般傜幇鍦?/span>lxhgww鎯寵鐭ラ亾婊¤凍瑕佹眰鐨勫瓧絎︿覆鍏辨湁澶氬皯涓紝鑱槑鐨勭▼搴忓憳浠紝浣犱滑鑳藉府鍔╀粬鍚楋紵
銆愯緭鍏ャ?/span>
杈撳叆鏁版嵁鏄竴琛岋紝鍖呮嫭2涓暟瀛?/span>n鍜?/span>m
銆愯緭鍑恒?/span>
杈撳嚭鏁版嵁鏄竴琛岋紝鍖呮嫭1涓暟瀛楋紝琛ㄧず婊¤凍瑕佹眰鐨勫瓧絎︿覆鏁扮洰錛岃繖涓暟鍙兘浼氬緢澶э紝鍙渶杈撳嚭榪欎釜鏁伴櫎浠?/span>20100403鐨勪綑鏁?/span>
銆愭牱渚嬭緭鍏ャ?/span>
2 2
銆愭牱渚嬭緭鍑恒?/span>
2
銆愭暟鎹寖鍥淬?/span>
瀵逛簬30%鐨勬暟鎹紝淇濊瘉1<=m<=n<=1000
瀵逛簬100%鐨勬暟鎹紝淇濊瘉1<=m<=n<=1000000