CodeChef submission 62926 (C++ 4.0.0-8) plaintext list. Status: TLE, problem F1, contest AUG09. By gmark (Mark Greve), 2009-08-02 20:38:12.
// Golf Course // Source: CodeChef August 2009 Algorithm Challenge // CodeChef ID: F1 // Date: 02-08-2009 // Author: Mark Greve #include <algorithm> #include <bitset> #include <cassert> #include <cctype> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <functional> #include <iomanip> #include <iostream> #include <list> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <utility> #include <vector> using namespace std; // FAST IO char *start; const int MAXBUF=10000000; char buf[MAXBUF]; char outbuf[MAXBUF]; int outat; void skip() { while (*start != 0 && !('0' <= *start && *start <= '9')) start++; } void GETNUM(int& n){ skip(); n=0; while ('0' <= *start && *start <= '9') { n = n*10 + *start-'0', ++start; } } void take_input() { // Take input using fast I/O buf[sz]=0; start=buf; outat=0; } void outc(char c) { outbuf[outat++] = c; } void out(int x) { char s[100]; int at = 0; do { s[at++] = x%10, x/=10; } while(x>0); for (int i=at-1;i>=0;--i) outbuf[outat++] = s[i]+'0'; } void flushoutbuf() { //fwrite(outbuf, sizeof(char),outat,stdout); outbuf[outat++]='\0'; } // END FAST IO typedef long long LL; const int BITS=14; const int MAX=1<<BITS; int val(LL x) { return x&((1<<(BITS))-1); } int num(LL x) { return x>>BITS; } /* // A simple bit trie to speed up the computation LL gcnt[2*MAX]; int val(LL x) { return x&((1<<(BITS))-1); } int num(LL x) { return x>>BITS; } vector<pair<int,int> > ops; void insert(int f, int am) { //printf("INSERT %d AM: %d\n", f,am); ops.push_back(make_pair(f,am)); f += MAX-1; gcnt[f] += am; while (f>0) f = (f-1)/2, gcnt[f] += am; } LL getmax() { int cur = 0, rc; for (int i=0;i<BITS;++i) { if (gcnt[rc=(2*cur+2)]>0) cur = rc; else cur = 2*cur + 1; } //printf("MINV: %d (%d)\n", cur-(MAX-1), gcnt[cur]); LL ret = (gcnt[cur]<<BITS) + cur-(MAX-1); //assert(val(ret)==cur-(MAX-1)); //assert(num(ret)==gcnt[cur]); return ret; } void reset() { //printf("RESET........\n"); if (1 || ops.size()<=100) { int sz = ops.size(); for (int i=0;i<sz;++i) { insert(ops[i].first,-ops[i].second); } } else memset(gcnt,0,sizeof gcnt); ops.clear(); } */ /* template <class T> struct stree { struct node { node *p, *l, *r; T key; int ss; // augmented data, subtree size int cnt; // count of the number of nodes with this key node(node* p=0, node* l=0, node* r=0) : p(p), l(l), r(r), cnt(1), ss(1) {} ~node() { delete l, delete r; } }; node* root; // Construct an empty splay tree stree() : root(0) {} // Disassemble the tree ~stree() { delete root; } // Recompute information in a node (if needed) void update(node* x) { if (0==x) return; x->ss = x->cnt; if (0 != x->l) x->ss += x->l->ss; if (0 != x->r) x->ss += x->r->ss; } // Rotate x one level up void rotate(node* x) { node* y = x->p; if (0==y) return; x->p = y->p; if (0 != y->p) (y->p->l == y) ? (y->p->l = x) : (y->p->r = x); y->p = x; if (y->l == x) { if (0 != x->r) x->r->p = y; y->l = x->r, x->r = y; } else { if (0 != x->l) x->l->p = y; y->r = x->l, x->l = y; } update(y), update(x), update(x->p); } // Splay a node to the root void splay(node* x) { if (0==x) return; while (x->p != 0) { node* y = x->p; if (0 == y->p) rotate(x); else if ( (y->p->l == y && x == y->l) || (y->p->r == y && x == y->r) ) rotate(y), rotate(x); else rotate(x), rotate(x); } root = x; } node* search(const T& x) { for (node* cur = root; 0 != cur;) { if (x < cur->key) { if (0!=cur->l) cur = cur->l; else return cur; } else if (x > cur->key) { if (0!=cur->r) cur = cur->r; else return cur; } else return cur; } } // Returns the number of occurrences of x in the tree int count(const T& x) { return splay(search(x)), ((0!=root && x == root->key) ? root->cnt : 0); } // Insert num copies of key x in the tree void insert(const T& x, int num=1) { if (0==root){root=new node;root->cnt=num;root->key=x;splay(root);return;} node* cur = search(x); if (x < cur->key) cur->l = new node(cur), cur->l->cnt = num, cur->l->key = x, splay(cur->l); else if (x > cur->key) cur->r = new node(cur), cur->r->cnt = num, cur->r->key = x, splay(cur->r); else cur->cnt += num, splay(cur); } // Erase num elements with key x (if there are less than num all are erased) void erase(const T& x, int num=1) { if (0==root) return; splay(search(x)); if (x != root->key) return; root->cnt -= num; if (root->cnt > 0) return; while (root->l != 0 && root->l->r != 0) rotate(root->l->r); node* tmp = root; if (0 == root->l && 0==root->r) root = 0; else if (0 == root->l) root = root->r, root->p = 0; else if (0 == root->r) root = root->l, root->p = 0; else root->l->r = root->r, root->l->r->p = root->l, root = root->l, root->p = 0; if (0!=root) update(root->l), update(root->r); update(root); tmp->l = tmp->r = 0; delete tmp; } }; stree<int> *st; void insert(int f, int am) { //printf("INSERT %d %d\n", f,am); if (am<0) st->erase(f,-am); else st->insert(f,am); } LL getmax() { stree<int>::node* x = st->search(1<<25); //printf("MAX: %d %d\n", x->key, x->cnt); LL ret = ((LL(x->cnt))<<BITS) + LL(x->key); return ret; } void reset() { //printf("RESET\n"); delete st; st = new stree<int>(); } */ map<int,int> st; void insert(int f, int am) { //printf("INSERT %d %d\n", f,am); if (am<0) st[f] -= am; else st[f] += am; } LL getmax() { int key = (--st.end())->first; int cnt = (--st.end())->second; LL ret = ((LL(cnt))<<BITS) + LL(key); return ret; } void reset() { //printf("RESET\n"); st.clear(); } LL row[501][501]; LL col[501][501]; int A[501][501]; int main() { take_input(); int NCASES; GETNUM(NCASES); for (int z=1;z<=NCASES;++z) { int M,N,K; GETNUM(M); GETNUM(N); GETNUM(K); for (int i=0;i<M;++i) for (int j=0;j<N;++j) GETNUM(A[i][j]); // Precompute row values. for (int i=0;i<M;++i) { reset(); for (int j=0;j<K;++j) insert(A[i][j],1); for (int j=0;j<N-K+1;++j) { row[i][j] = getmax(); if (j+1!=N-K+1) insert(A[i][j],-1), insert(A[i][j+K],1); } } // Precompute column values for (int j=0;j<N;++j) { reset(); for (int i=0;i<K;++i) insert(A[i][j],1); for (int i=0;i<M-K+1;++i) { col[i][j] = getmax(); if (i+1!=M-K+1) insert(A[i][j],-1), insert(A[i+K][j],1); } } outc('C'); outc('a'); outc('s'); outc('e'); outc(' '); out(z); outc(':'); outc('\n'); for (int i=0;i<M-K+1;++i) { reset(); for (int k=0;k<K;++k) insert(val(col[i][k]), num(col[i][k])); for (int j=0;j<N-K+1;++j) { if (j!=0) outc(' '); LL maxv = getmax(); int v = val(maxv), cnt = num(maxv); out(v); if (cnt!=1) outc('('), out(cnt), outc(')'); if (j+1!=N-K+1) { insert(val(col[i][j+K]), num(col[i][j+K])); insert(val(col[i][j]), -num(col[i][j])); } } outc('\n'); } outc('\n'); } flushoutbuf(); }
Comments

