hdu 2018 多校 第七场
1001 小甜甜 可爱的小甜甜又回来辣 然而活该被卡spfa(
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cmath> 5 #include <vector> 6 #include <queue> 7 #include <set> 8 #include <map> 9 #include <string> 10 #include <string.h> 11 #include <stdlib.h> 12 #include <time.h> 13 #include <climits> 14 15 using namespace std; 16 17 const int maxN=1e5+7; 18 const int maxM=2e5+7; 19 const int INF=1e9; 20 21 /////////// 22 23 struct edge{ 24 int to,nex,c; 25 }E[maxM*2]; 26 int head[maxN],cou; 27 28 void init(){ 29 memset(head,-1,sizeof(head)); 30 cou=0; 31 } 32 33 void addedge(int u,int v,int c){ 34 E[cou]=(edge){v,head[u],c}; 35 head[u]=cou++; 36 } 37 38 ///////// 39 40 char buf[30*1024*1024],*p1=buf,*p2=buf; 41 inline char nc(){ 42 return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++; 43 } 44 inline bool rea(int & x){ 45 char c=nc();x=0; 46 if(c==EOF) return false; 47 for(;!isdigit(c);c=nc()); 48 for(;isdigit(c);x=x*10+c-'0',c=nc()); 49 return true; 50 } 51 52 ///////// 53 54 typedef pair<int,int> pii; 55 // first:cost second:id 56 57 priority_queue <pii,vector<pii>,greater<pii> > que; 58 set <int> rem[maxN]; 59 bool vis[maxN]; 60 int cost[maxN]; 61 62 int n,m; 63 64 int bfs(){ 65 while (!que.empty()) que.pop(); 66 for (int i=1;i<=n;i++) vis[i]=0,cost[i]=INF; 67 68 que.push(pii(0,1)); 69 cost[1]=0; 70 71 int u,v,c; 72 pii tmp; 73 while (!que.empty()) { 74 tmp=que.top(); que.pop(); 75 u=tmp.second; 76 if (vis[u]) continue; 77 if (u==n) return cost[u]; 78 vis[u]=1; 79 for (int i=head[u];i!=-1;i=E[i].nex) { 80 v=E[i].to; 81 if (vis[v]) continue; 82 c=1; 83 if (rem[u].count(E[i].c)) c--; 84 if (cost[u]+c==cost[v]) rem[v].insert(E[i].c); 85 if (cost[u]+c<cost[v]) { 86 rem[v].clear(); 87 rem[v].insert(E[i].c); 88 cost[v]=cost[u]+c; 89 que.push(pii(cost[v],v)); 90 } 91 } 92 } 93 94 return -1; 95 } 96 97 queue <int> q; 98 99 int spfa(){ 100 while (!q.empty()) q.pop(); 101 for (int i=1;i<=n;i++) vis[i]=0,cost[i]=INF; 102 103 q.push(1); 104 vis[1]=1; 105 cost[1]=0; 106 107 while (!q.empty()) { 108 int u=q.front(); q.pop(); 109 vis[u]=0; 110 for (int i=head[u];i!=-1;i=E[i].nex) { 111 int v=E[i].to; 112 int c=1; 113 if (rem[u].count(E[i].c)) c--; 114 if (cost[v]==cost[u]+c && !rem[v].count(E[i].c)){ 115 rem[v].insert(E[i].c); 116 if (!vis[v]){ 117 q.push(v); 118 vis[v]=1; 119 } 120 } 121 if (cost[v]>cost[u]+c) { 122 cost[v]=cost[u]+c; 123 rem[v].clear(); 124 rem[v].insert(E[i].c); 125 if (!vis[v]) { 126 q.push(v); 127 vis[v]=1; 128 } 129 } 130 } 131 } 132 133 if (cost[n]>=INF) return -1; 134 return cost[n]; 135 } 136 137 void work(){ 138 init(); 139 rea(m); 140 for (int i=1;i<=m;i++) { 141 int a,b,c; 142 //scanf("%d%d%d",&a,&b,&c); 143 rea(a); rea(b); rea(c); 144 addedge(a,b,c); 145 addedge(b,a,c); 146 } 147 148 //printf("%d\n",bfs()); 149 printf("%d\n",spfa()); 150 } 151 152 int main(){ 153 //while (scanf("%d%d",&n,&m)!=EOF) work(); 154 while (rea(n)) work(); 155 return 0; 156 }
1008 BPM136 别问我为什么之前写错了那么多东西,最后还蜜汁到了9KB的代码长度
1 /* *********************************************** 2 Author :BPM136 3 Created Time :2018/8/13 14:04:59 4 File Name :1008.cpp 5 ************************************************ */ 6 7 #include<iostream> 8 #include<cstdio> 9 #include<algorithm> 10 #include<cstdlib> 11 #include<cmath> 12 #include<cstring> 13 #include<iomanip> 14 #include<bitset> 15 #include<queue> 16 #include<ctime> 17 #include<set> 18 #include<map> 19 #include<vector> 20 #include<cassert> 21 #include<functional> 22 #define pb push_back 23 #define popb pop_back 24 #define MID ((l+r)>>1) 25 #define LSON (k<<1) 26 #define RSON (k<<1|1) 27 #define get(a,i) ((a)&(1<<(i-1))) 28 #define PAU putchar(32) 29 #define ENT putchar(10) 30 #define clr(a,b) memset(a,b,sizeof(a)) 31 #define rep(_i,_a,_b) for(int _i=(_a);_i<=(_b);_i++) 32 #define down(_i,_a,_b) for(int _i=(_a);_i>=(_b);_i--) 33 #define FOR(_i,_b) for(int _i=1;_i<=(_b);_i++) 34 #define efo(_i,_a) for(int _i=last[(_a)];_i!=0;_i=e[_i].next) 35 #define Remax(a,b) if(b>a) a=b; 36 #define Remin(a,b) if(b<a) a=b; 37 #define SZ(x) ((int)(x).size()) 38 #define filein(x) freopen(#x".in","r",stdin) 39 #define fileout(x) freopen(#x".out","w",stdout) 40 #define file(x) freopen(#x".in","r",stdin),freopen(#x".out","w",stdout); 41 #define mkd(x) freopen(#x".in","w",stdout); 42 #define setlargestack(x) int _SIZE=x<<20;char *_PPP=(char*)malloc(_SIZE)+_SIZE;__asm__("movl %0, %%esp\n" :: "r"(_PPP)); 43 #define END system("pause") 44 #define read2(a,b) read(a),read(b) 45 #define read3(a,b,c) read(a),read(b),read(c) 46 #define readg(_x1,_y1,_x2,_y2) read(_x1),read(_y1),read(_x2),read(_y2) 47 using namespace std; 48 49 typedef long long ll; 50 typedef double DB; 51 typedef long double LD; 52 typedef unsigned int UI; 53 typedef unsigned long long ULL; 54 typedef vector<int> VI; 55 typedef vector<ll> Vll; 56 typedef set<int> SI; 57 typedef set<int , greater<int> > SIG; 58 typedef map<int, int > MII; 59 typedef map<int, int, greater<int> > MIIG; 60 61 namespace fastIO{ 62 #define BUF_SIZE 100000 63 #define OUT_SIZE 100000 64 //fread->read 65 bool IOerror=0; 66 inline char nc(){ 67 static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE; 68 if (p1==pend){ 69 p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin); 70 if (pend==p1){IOerror=1;return -1;} 71 //{printf("IO error!\n");system("pause");for (;;);exit(0);} 72 } 73 return *p1++; 74 } 75 inline bool blank(char ch){return ch==32||ch==10||ch==13||ch==9;} 76 inline bool enter(char ch){return ch==10||ch==13;} 77 inline void read(int &x){ 78 bool sign=0; char ch=nc(); x=0; 79 for (;blank(ch);ch=nc()); 80 if (IOerror)return; 81 if (ch==45)sign=1,ch=nc(); 82 for (;ch>=48&&ch<=57;ch=nc())x=x*10+ch-48; 83 if (sign)x=-x; 84 } 85 inline void read(ll &x){ 86 bool sign=0; char ch=nc(); x=0; 87 for (;blank(ch);ch=nc()); 88 if (IOerror)return; 89 if (ch==45)sign=1,ch=nc(); 90 for (;ch>=48&&ch<=57;ch=nc())x=x*10+ch-48; 91 if (sign)x=-x; 92 } 93 inline void read(double &x){ 94 bool sign=0; char ch=nc(); x=0; 95 for (;blank(ch);ch=nc()); 96 if (IOerror)return; 97 if (ch==45)sign=1,ch=nc(); 98 for (;ch>=48&&ch<=57;ch=nc())x=x*10+ch-48; 99 if (ch==46){ 100 double tmp=1; ch=nc(); 101 for (;ch>=48&&ch<=57;ch=nc())tmp/=10.0,x+=tmp*(ch-48); 102 } 103 if (sign)x=-x; 104 } 105 inline void read(char *s){ 106 char ch=nc(); 107 for (;blank(ch);ch=nc()); 108 if (IOerror)return; 109 for (;!blank(ch)&&!IOerror;ch=nc())*s++=ch; 110 *s=0; 111 } 112 inline void read(char *s,bool f) { 113 char ch=nc(); 114 for (;blank(ch);ch=nc()); 115 if(IOerror)return; 116 for(;!enter(ch)&&!IOerror;ch=nc())*s++=ch; 117 *s=0; 118 } 119 inline void read(char &c){ 120 for (c=nc();blank(c);c=nc()); 121 if (IOerror){c=-1;return;} 122 } 123 #undef OUT_SIZE 124 #undef BUF_SIZE 125 }; using namespace fastIO; 126 127 const int N = 100005; 128 129 struct edge { 130 int w,y,next; 131 int ty,pos; 132 }e[N<<1]; 133 int last[N],ne; 134 void add(int x,int y,int w) { 135 e[++ne].w=w; e[ne].y=y; e[ne].next=last[x]; last[x]=ne; 136 e[ne].ty=e[ne].pos=0; 137 } 138 void add2(int x,int y,int w) { 139 add(x,y,w); 140 add(y,x,w); 141 } 142 143 struct lowbit_tree { 144 Vll T; 145 int n; 146 void init(int _n) { 147 n=_n; 148 T.resize(n+1); 149 rep(i,0,n) T[i]=0; 150 } 151 void add(int x,int val) { 152 for(int i=x;i<=n;i+=i&-i) T[i]+=val; 153 } 154 ll sum(int x) { 155 ll ret=0; 156 if(x>n) x=n; 157 for(int i=x;i>0;i-=i&-i) ret+=T[i]; 158 return ret; 159 } 160 }at1,at2; 161 162 struct segment_tree { 163 #define LSON (k<<1) 164 #define RSON (k<<1|1) 165 #define MID ((l+r)>>1) 166 ll mx[N<<2]; 167 ll lz[N<<2]; 168 169 void init() { 170 clr(mx,0); 171 clr(lz,0); 172 } 173 void pushdown(int k) { 174 mx[LSON]+=lz[k]; 175 mx[RSON]+=lz[k]; 176 lz[LSON]+=lz[k]; 177 lz[RSON]+=lz[k]; 178 lz[k]=0; 179 } 180 void insert(int k,int l,int r,int pos,ll val) { 181 if(l==r) { 182 mx[k]=val; 183 return ; 184 } 185 int mid=MID; 186 if(pos<=mid) insert(LSON,l,mid,pos,val); 187 else insert(RSON,mid+1,r,pos,val); 188 } 189 void add(int k,int l,int r,int ll,int rr,int val) { 190 if(l==ll && r==rr) { 191 mx[k]+=val; 192 lz[k]+=val; 193 return ; 194 } 195 if(lz[k]!=0) pushdown(k); 196 int mid=MID; 197 if(rr<=mid) add(LSON,l,mid,ll,rr,val); 198 else if(ll>mid) add(RSON,mid+1,r,ll,rr,val); 199 else { 200 add(LSON,l,mid,ll,mid,val); 201 add(RSON,mid+1,r,mid+1,rr,val); 202 } 203 } 204 ll query(int k,int l,int r,int pos) { 205 if(l==r) return mx[k]; 206 if(lz[k]!=0) pushdown(k); 207 int mid=MID; 208 if(pos<=mid) return query(LSON,l,mid,pos); 209 else return query(RSON,mid+1,r,pos); 210 } 211 #undef LSON 212 #undef RSON 213 #undef MID 214 }t; 215 216 int dfn_id[N]; 217 int cnt_id; 218 int dfn_1[N],dfn_2[N]; 219 int dfn_e[N]; 220 int cnt; 221 int root[N]; 222 int deep[N]; 223 int fa[N][21]; 224 int be[N],en[N]; 225 ll w[N]; 226 int n,m; 227 228 void init() { 229 memset(last,0,sizeof(last)); 230 clr(w,0); 231 clr(root,0); 232 clr(deep,0); 233 ne=0; 234 at1.init(n+1); 235 at2.init(n+1); 236 t.init(); 237 } 238 239 int get_lca(int x,int y) { 240 if(deep[x]<deep[y])swap(x,y); 241 for(int i=20;i>=0;i--) 242 if(deep[fa[x][i]]>=deep[y]) 243 x=fa[x][i]; 244 if(x==y)return x; 245 for(int i=20;i>=0;i--) 246 if(fa[x][i]!=fa[y][i]) 247 x=fa[x][i],y=fa[y][i]; 248 return fa[x][0]; 249 } 250 251 int st[N]; 252 int top; 253 bitset<N> ins; 254 bitset<N> vis; 255 bool end_loop; 256 ll sum; 257 void find_loop(int x,int pre) { 258 st[++top]=x; 259 vis[x]=1; 260 ins[x]=1; 261 for(int i=last[x];i!=0;i=e[i].next) { 262 int y=e[i].y; 263 if(y==pre) continue; 264 if(ins[y]) { 265 cnt=0; 266 sum=0; 267 for(int j=top;st[j]!=y;j--) { 268 dfn_1[st[j]]=++cnt; 269 root[st[j]]=st[j]; 270 fa[st[j]][0]=0; 271 deep[st[j]]=1; 272 for(int k=last[st[j]];k!=0;k=e[k].next) if(e[k].y==st[j-1]) { 273 e[k].ty=1; e[k].pos=cnt+1; 274 at1.add(cnt+1,e[k].w); 275 sum+=e[k].w; 276 break; 277 } 278 } 279 dfn_1[y]=++cnt; 280 root[y]=y; 281 deep[y]=1; 282 fa[y][0]=0; 283 for(int k=last[y];k!=0;k=e[k].next) if(e[k].y==x) { 284 sum+=e[k].w; 285 break; 286 } 287 288 end_loop=1; 289 return ; 290 } 291 if(vis[y]==0) find_loop(y,x); 292 if(end_loop) return ; 293 } 294 ins[x]=0; 295 top--; 296 } 297 298 void dfs(int x) { 299 vis[x]=1; 300 be[x]=++cnt_id; 301 t.insert(1,1,n,cnt_id,w[x]); 302 rep(i,1,20) fa[x][i]=fa[fa[x][i-1]][i-1]; 303 for(int i=last[x];i!=0;i=e[i].next) { 304 int y=e[i].y; 305 if(root[y]==y) continue; 306 if(vis[y]) continue; 307 w[y]=w[x]+e[i].w; 308 fa[y][0]=x; 309 deep[y]=deep[x]+1; 310 root[y]=root[x]; 311 dfs(y); 312 } 313 en[x]=cnt_id; 314 } 315 316 void change_e(int id,int val) { 317 if(e[id].ty==1) { 318 at1.add(e[id].pos,val-e[id].w); 319 sum+=val-e[id].w; 320 } 321 } 322 323 void change_w(int k,int val) { 324 int id=dfn_e[k]; 325 int x=e[id+1].y; 326 int y=e[id].y; 327 if(root[x]==x && root[y]==y) { 328 change_e(id,val); 329 change_e(id+1,val); 330 e[id].w=e[id+1].w=val; 331 return ; 332 } 333 334 if(deep[x]<deep[y]) swap(x,y); 335 t.add(1,1,n,be[x],en[x],val-e[id].w); 336 e[id].w=e[id+1].w=val; 337 } 338 339 int main() { 340 int T; 341 read(T); 342 while(T--) { 343 read2(n,m); 344 init(); 345 346 int x,y,_w,op; 347 rep(i,1,n) { 348 read3(x,y,_w); 349 add2(x,y,_w); 350 dfn_e[i]=ne-1; 351 } 352 353 vis.reset(); 354 ins.reset(); 355 clr(st,0); 356 top=0; 357 end_loop=0; 358 find_loop(1,-1); 359 360 cnt_id=0; 361 vis.reset(); 362 rep(i,1,n) if(root[i]==i) dfs(i); 363 364 rep(o,1,m) { 365 read3(op,x,y); 366 if(op==0) change_w(x,y); 367 else { 368 if(root[x]==root[y]) { 369 int lca=get_lca(x,y); 370 ll ans_x=t.query(1,1,n,be[x]); 371 ll ans_y=t.query(1,1,n,be[y]); 372 ll ans_ =t.query(1,1,n,be[lca]); 373 ll ans=ans_x+ans_y-2*ans_; 374 printf("%lld\n",ans); 375 } else { 376 ll ans_x=t.query(1,1,n,be[x]); 377 ll ans_y=t.query(1,1,n,be[y]); 378 x=root[x]; 379 y=root[y]; 380 if(dfn_1[x]>dfn_1[y]) swap(x,y); 381 ll ans_1=abs(at1.sum(dfn_1[y])-at1.sum(dfn_1[x])); 382 ll ans_2=sum-ans_1; 383 ll ans=ans_x+ans_y+min(ans_1,ans_2); 384 printf("%lld\n",ans); 385 } 386 } 387 } 388 } 389 return 0; 390 } 391 392 /* 393 15 100 394 1 2 1 395 2 3 2 396 3 4 3 397 4 5 4 398 1 5 5 399 5 6 6 400 6 7 7 401 6 8 8 402 8 9 9 403 8 10 10 404 4 11 11 405 11 12 12 406 11 13 13 407 2 14 14 408 14 15 15 409 */
1009 小洛洛
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <cmath> 5 #include <cstdlib> 6 #include <algorithm> 7 using namespace std; 8 9 template<int BI, int BO> 10 struct IO_ { 11 char bufi[BI + 1], *qi, bufo[BO], *qo; 12 IO_(): qi(bufi + BI), qo(bufo) {} 13 ~IO_() { o_(EOF); } 14 int i_() { 15 if(qi == bufi + BI) bufi[fread(bufi, 1, BI, stdin)] = '\0', qi = bufi; 16 return *qi ? *qi++ : EOF; 17 } 18 void o_(int c) { 19 if(c != EOF) *qo++ = c; 20 if(qo == bufo + BO || (c == EOF && qo != bufo)) 21 fwrite(bufo, qo - bufo, 1, stdout), qo = bufo; 22 } 23 IO_ &operator>>(int &ret) { 24 ret = 0; int c; 25 while(isspace(c = i_())); 26 bool bm = c == '-' ? (c = i_(), true) : false; 27 while(ret = ret * 10 - (c - '0'), isdigit(c = i_())); 28 if(!bm) ret = -ret; 29 return *this; 30 } 31 IO_ &operator>>(char *p) { 32 int c; while(isspace(c = i_())); 33 do *p++ = c; while(!isspace(c = i_())); 34 *p = '\0'; 35 return *this; 36 } 37 IO_ &operator<<(char c) { return o_(c), *this; } 38 IO_ &operator<<(char *p) { while(*p) o_(*p++); return *this; } 39 IO_ &operator<<(int x) { 40 char s[12] = {}, *p = s + 11; 41 bool bm = x < 0 ? true : (x = -x, false); 42 while(*--p = '0' - x % 10, x /= 10); 43 if(bm) *--p = '-'; 44 while(*p) o_(*p++); 45 return *this; 46 } 47 }; 48 49 const int N = 100009; 50 int f[N], c[N][2], s[N]; 51 bool nroot(int x) { 52 return c[f[x]][0] == x || c[f[x]][1] == x; 53 } 54 void pushup(int x) { 55 s[x] = s[c[x][0]] + s[c[x][1]] + 1; 56 } 57 void rotate(int x) { 58 int y = f[x], z = f[y], k = c[y][1] == x, w = c[x][!k]; 59 if(nroot(y)) 60 c[z][c[z][1] == y] = x; 61 c[x][!k] = y; 62 c[y][k] = w; 63 if(w) 64 f[w] = y; 65 f[y] = x; 66 f[x] = z; 67 pushup(y); 68 } 69 void splay(int x) { 70 int y, z; 71 while(nroot(x)) { 72 y = f[x]; 73 z = f[y]; 74 if(nroot(y)) 75 rotate((c[y][0] == x) ^ (c[z][0] == y) ? y : x); 76 rotate(x); 77 } 78 pushup(x); 79 } 80 void access(int x) { 81 for(int y = 0; x; x = f[y = x]) 82 splay(x), c[x][1] = y, pushup(x); 83 } 84 85 int query_(int c) { 86 ++c; 87 access(c); 88 splay(c); 89 return s[c]; 90 } 91 92 void set_(int c, int f) { 93 ++c, ++f; 94 access(c); 95 splay(c); 96 ::c[c][0] = ::f[::c[c][0]] = 0; // cut 97 if(f) 98 ::f[c] = f; 99 pushup(c); 100 } 101 102 const int LOGN = 18; 103 int fa[LOGN][N], rawk[N]; 104 105 int fa_(int c, int k) { 106 for(int i = LOGN - 1; c != -1 && i >= 0; --i) 107 if(k & (1 << i)) 108 c = fa[i][c]; 109 return c; 110 } 111 112 void init_(int n) { 113 for(int i = 1; i < LOGN; ++i) 114 for(int j = 0; j < n; ++j) 115 fa[i][j] = (fa[i - 1][j] == -1 ? -1 : fa[i - 1][fa[i - 1][j]]); 116 117 memset(f, 0, sizeof f); 118 memset(c, 0, sizeof c); 119 memset(s, 0, sizeof s); 120 121 for(int i = 0; i < n; ++i) 122 set_(i, fa_(i, rawk[i])); 123 } 124 125 126 int main() { 127 auto &io = *new IO_<0x10000, 0x10000>; 128 129 int t; 130 io >> t; 131 while(t--) { 132 int n; 133 io >> n; 134 fa[0][0] = -1; 135 for(int i = 1; i < n; ++i) { 136 int a; 137 io >> a; 138 fa[0][i] = --a; 139 } 140 for(int i = 0; i < n; ++i) 141 io >> rawk[i]; 142 init_(n); 143 144 int m; 145 io >> m; 146 while(m--) { 147 int op, a; 148 io >> op >> a; 149 --a; 150 if(op == 1) 151 io << query_(a) << '\n'; 152 else { 153 int x; 154 io >> x; 155 set_(a, fa_(a, x)); 156 } 157 } 158 } 159 160 delete &io; 161 return 0; 162 }
1010 小洛洛
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <cmath> 5 #include <cstdlib> 6 #include <algorithm> 7 using namespace std; 8 9 template<int BI, int BO> 10 struct IO_ { 11 char bufi[BI + 1], *qi, bufo[BO], *qo; 12 IO_(): qi(bufi + BI), qo(bufo) {} 13 ~IO_() { o_(EOF); } 14 int i_() { 15 if(qi == bufi + BI) bufi[fread(bufi, 1, BI, stdin)] = '\0', qi = bufi; 16 return *qi ? *qi++ : EOF; 17 } 18 void o_(int c) { 19 if(c != EOF) *qo++ = c; 20 if(qo == bufo + BO || (c == EOF && qo != bufo)) 21 fwrite(bufo, qo - bufo, 1, stdout), qo = bufo; 22 } 23 IO_ &operator>>(int &ret) { 24 ret = 0; int c; 25 while(isspace(c = i_())); 26 bool bm = c == '-' ? (c = i_(), true) : false; 27 while(ret = ret * 10 - (c - '0'), isdigit(c = i_())); 28 if(!bm) ret = -ret; 29 return *this; 30 } 31 IO_ &operator>>(char *p) { 32 int c; while(isspace(c = i_())); 33 do *p++ = c; while(!isspace(c = i_())); 34 *p = '\0'; 35 return *this; 36 } 37 IO_ &operator<<(char c) { return o_(c), *this; } 38 IO_ &operator<<(char *p) { while(*p) o_(*p++); return *this; } 39 IO_ &operator<<(int x) { 40 char s[12] = {}, *p = s + 11; 41 bool bm = x < 0 ? true : (x = -x, false); 42 while(*--p = '0' - x % 10, x /= 10); 43 if(bm) *--p = '-'; 44 while(*p) o_(*p++); 45 return *this; 46 } 47 }; 48 49 typedef long long ll; 50 const int MOD = 1000000007; 51 int c, d, p; 52 53 struct M_ { 54 int m[3][3]; 55 56 static M_ id_() { 57 return {{ 1, 0, 0, 0, 1, 0, 0, 0, 1 }}; 58 } 59 60 M_ operator*(const M_ &r) const { 61 M_ ret = { {} }; 62 for(int i = 0; i < 3; ++i) 63 for(int j = 0; j < 3; ++j) 64 for(int k = 0; k < 3; ++k) 65 ret.m[i][j] = (ret.m[i][j] + (ll)m[i][k] * r.m[k][j]) % MOD; 66 return ret; 67 } 68 69 M_ pow_(int q) const { 70 M_ ret = id_(), p = *this; 71 for(; q; p = p * p, q >>= 1) 72 if(q & 1) 73 ret = ret * p; 74 return ret; 75 } 76 }; 77 78 int calc_(int a, int b, int n) { 79 if(n == 1) 80 return a; 81 if(n == 2) 82 return b; 83 M_ m = M_::id_(); 84 for(int i = 3; i <= n; ++i) { 85 int t = i <= p ? min(p / (p / i), n) : n; 86 M_ trans = {{ 0, 1, 0, c, d, p / i, 0, 0, 1 }}; 87 m = trans.pow_(t - i + 1) * m; 88 i = t; 89 } 90 return ((ll)m.m[1][0] * a + (ll)m.m[1][1] * b + m.m[1][2]) % MOD; 91 } 92 93 int main() { 94 auto &io = *new IO_<0x10000, 0x10000>; 95 96 int t; 97 io >> t; 98 while(t--) { 99 int a, b, n; 100 io >> a >> b >> c >> d >> p >> n; 101 io << calc_(a, b, n) << '\n'; 102 } 103 104 delete &io; 105 return 0; 106 }
1011 小洛洛
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <cmath> 5 #include <cstdlib> 6 #include <algorithm> 7 using namespace std; 8 9 template<int BI, int BO> 10 struct IO_ { 11 char bufi[BI + 1], *qi, bufo[BO], *qo; 12 IO_(): qi(bufi + BI), qo(bufo) {} 13 ~IO_() { o_(EOF); } 14 int i_() { 15 if(qi == bufi + BI) bufi[fread(bufi, 1, BI, stdin)] = '\0', qi = bufi; 16 return *qi ? *qi++ : EOF; 17 } 18 void o_(int c) { 19 if(c != EOF) *qo++ = c; 20 if(qo == bufo + BO || (c == EOF && qo != bufo)) 21 fwrite(bufo, qo - bufo, 1, stdout), qo = bufo; 22 } 23 IO_ &operator>>(int &ret) { 24 ret = 0; int c; 25 while(isspace(c = i_())); 26 bool bm = c == '-' ? (c = i_(), true) : false; 27 while(ret = ret * 10 - (c - '0'), isdigit(c = i_())); 28 if(!bm) ret = -ret; 29 return *this; 30 } 31 IO_ &operator>>(char *p) { 32 int c; while(isspace(c = i_())); 33 do *p++ = c; while(!isspace(c = i_())); 34 *p = '\0'; 35 return *this; 36 } 37 IO_ &operator<<(char c) { return o_(c), *this; } 38 IO_ &operator<<(char *p) { while(*p) o_(*p++); return *this; } 39 IO_ &operator<<(int x) { 40 char s[12] = {}, *p = s + 11; 41 bool bm = x < 0 ? true : (x = -x, false); 42 while(*--p = '0' - x % 10, x /= 10); 43 if(bm) *--p = '-'; 44 while(*p) o_(*p++); 45 return *this; 46 } 47 }; 48 49 const int N = 100002; 50 const int K = 6; 51 52 struct M_ { 53 int df[K], v[K]; 54 bool eaten; 55 } arr[N], *q[K][N], **r[K]; 56 57 bool le_(int k, const int (&a)[K], const int (&b)[K]) { 58 for(int i = 0; i < k; ++i) 59 if(!(a[i] <= b[i])) 60 return false; 61 return true; 62 } 63 64 int main() { 65 auto &io = *new IO_<0x10000, 0x10000>; 66 67 int t; 68 io >> t; 69 while(t--) { 70 int n, k; 71 io >> n >> k; 72 int cur[K]; 73 for(int i = 0; i < k; ++i) 74 io >> cur[i]; 75 for(int i = 0; i < n; ++i) { 76 for(int j = 0; j < k; ++j) 77 io >> arr[i].df[j]; 78 for(int j = 0; j < k; ++j) 79 io >> arr[i].v[j]; 80 arr[i].eaten = false; 81 for(int j = 0; j < k; ++j) 82 q[j][i] = arr + i; 83 } 84 for(int i = 0; i < k; ++i) { 85 sort(q[i], q[i] + n, [=](const M_ *a, const M_ *b) { 86 return a->df[i] < b->df[i]; 87 }); 88 r[i] = q[i]; 89 } 90 int ans = 0; 91 for(;;) { 92 bool grow = false; 93 int curdt[K] = {}; 94 for(int i = 0; i < k; ++i) 95 while(r[i] != q[i] + n && (*r[i])->df[i] <= cur[i]) { 96 if(le_(k, (*r[i])->df, cur) && !(*r[i])->eaten) { 97 for(int j = 0; j < k; ++j) 98 curdt[j] += (*r[i])->v[j]; 99 ++ans; 100 (*r[i])->eaten = true; 101 grow = true; 102 } 103 ++r[i]; 104 } 105 if(!grow) 106 break; 107 else 108 for(int j = 0; j < k; ++j) 109 cur[j] += curdt[j]; 110 } 111 io << ans << '\n'; 112 for(int i = 0; i < k; ++i) { 113 if(i) 114 io << ' '; 115 io << cur[i]; 116 } 117 io << '\n'; 118 } 119 120 delete &io; 121 return 0; 122 }