Powered By HC TECH : AutoACer Engine

5100-5199
This commit is contained in:
KiritoTRw 2016-09-10 11:37:56 +08:00 committed by GitHub
parent 596be6ae11
commit 808996d897
86 changed files with 5871 additions and 0 deletions

23
HDOJ/5100_autoAC.cpp Normal file
View File

@ -0,0 +1,23 @@
#include<iostream>
#include<cstdio>
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,k;
scanf("%d%d",&n,&k);
if(n<k)
printf("0\n");
else
{
int l=n%k;
if(l<=k/2)
printf("%d\n",n*n-l*l);
else
printf("%d\n",n*n-(k-l)*(k-l));
}
}
return 0;
}

38
HDOJ/5101_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
int n, k, v[1010][110], m[1100], arr[1010*110];
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int cnt=0;
scanf("%d%d", &n, &k);
for(int i=0; i<n; i++)
{
scanf("%d", &m[i]);
for(int j=0; j<m[i]; j++)
{
scanf("%d", &v[i][j]);
arr[cnt++]=v[i][j];
}
sort(v[i], v[i]+m[i]);
}
sort(arr, arr+cnt);
LL ans=0;
for(int i=0; i<cnt; i++)
ans+=arr+cnt-upper_bound(arr+i+1, arr+cnt, k-arr[i]);
for(int i=0; i<n; i++)
for(int j=0; j<m[i]; j++)
ans-=v[i]+m[i]-upper_bound(v[i]+j+1, v[i]+m[i], k-v[i][j]);
printf("%I64d\n", ans);
}
return 0;
}

76
HDOJ/5102_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include <map>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std ;
typedef long long LL ;
#define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define clr( a , x ) memset ( a , x , sizeof a )
const int MAXN = 100005 ;
const int MAXE = 200005 ;
struct Node {
int v , p , d ;
Node () {}
Node ( int v , int p , int d ) : v ( v ) , p ( p ) , d ( d ) {}
} ;
struct Edge {
int v , n ;
Edge () {}
Edge ( int v , int n ) : v ( v ) , n ( n ) {}
} ;
Node Q[3000005] ;
Edge E[MAXE] ;
int H[MAXN] , cntE ;
int head , tail ;
LL ans ;
int n , k ;
void clear () {
ans = 0 ;
cntE = 0 ;
clr ( H , -1 ) ;
}
void addedge ( int u , int v ) {
E[cntE] = Edge ( v , H[u] ) ;
H[u] = cntE ++ ;
}
void bfs () {
int cnt = 0 ;
head = tail = 0 ;
For ( i , 1 , n ) Q[tail ++] = Node ( i , 0 , 0 ) ;
while ( head != tail ) {
Node x = Q[head ++] ;
int u = x.v , p = x.p ;
for ( int i = H[u] ; ~i ; i = E[i].n ) {
int v = E[i].v ;
if ( v != p ) {
Q[tail ++] = Node ( v , u , x.d + 1 ) ;
ans += x.d + 1 ;
++ cnt ;
if ( cnt == k ) return ;
}
}
}
}
void solve () {
int u , v ;
clear () ;
scanf ( "%d%d" , &n , &k ) ;
k *= 2 ;
rep ( i , 1 , n ) {
scanf ( "%d%d" , &u , &v ) ;
addedge ( u , v ) ;
addedge ( v , u ) ;
}
bfs () ;
printf ( "%I64d\n" , ans / 2 ) ;
}
int main () {
int T ;
scanf ( "%d" , &T ) ;
while ( T -- ) solve () ;
return 0 ;
}

54
HDOJ/5103_autoAC.cpp Normal file
View File

@ -0,0 +1,54 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
int dp[15][1<<15];
int sum[1<<15];
int sub[1<<15];
int L[15],R[15],n;
int cal(int S){
int num=0;
for(int i=0; i<n; ++i)
if(S&(1<<i)) num++;
return num;
}
int main()
{
int cas;
scanf("%d",&cas);
for(int cc= 1; cc<=cas; ++cc){
scanf("%d",&n);
for(int i=0; i<n; ++i)
scanf("%d%d",&L[i],&R[i]);
for(int i=0; i<n; ++i){
dp[i][0]=1;
}
sum[0]=1; sub[0]=1;
for(int S=1; S<(1<<n); ++S){
sum[S]=0; sub[S]=0;
int ge = cal(S);
for(int i =0; i<n; ++i){
dp[i][S]=0;
if( ( S&(1<<i) )!=0 && L[i]<=ge&&R[i]>=ge ){
dp[i][S]= sub[S^(1<<i)];
sum[S]= (dp[i][S]+sum[S])%mod;
}
}
int j=0;
for( j=0; j<n; ++j ) if(S&(1<<j)) break;
for(int H=S; H>0; H=S&(H-1)){
if((H&(1<<j))==0) continue;
ll a = sum[H];
ll b = sub[S^H];
sub[S]= ( sub[S] + a*b%mod )%mod;
}
}
int ans=0;
for(int i=0; i<n; ++i) ans=(ans+ dp[i][(1<<n)-1])%mod;
printf("%d\n",ans);
}
return 0;
}

52
HDOJ/5104_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<stdlib.h>
#include<ctype.h>
#include<algorithm>
#include<vector>
#include<string.h>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<sstream>
#include<time.h>
#include<utility>
#include<malloc.h>
#include<stdexcept>
using namespace std;
int is_prime(int n )
{
for(int i=2 ;i*i<=n;i++ )
{
if ( n % i ==0)
return 0;
}
return 1;
}
int n,m;
int main ()
{
int prime[10005];
memset(prime,0,sizeof(prime));
for(int i=2 ;i<=10002;i++)
{
if ( is_prime(i) )
prime[i] = 1;
}
while (cin>>n)
{
int num =0;
for(int i=2 ;i<=n;i++)
for(int j=i ;j<=(n-i)/2;j++)
{
if (prime[i] && prime[j]&& prime[n-i-j])
{
num++;
}
}
cout<<num<<endl;
}
return 0;
}

56
HDOJ/5105_autoAC.cpp Normal file
View File

@ -0,0 +1,56 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
double a,b,c,d,L,R;
double f(double x)
{
return fabs(a*x*x*x+b*x*x+c*x+d);
}
const double eps=1e-9;
int dcmp(double x)
{
if(fabs(x)<=eps)return 0;
else return x<0?-1:1;
}
bool judge(double x)
{
return x<=R&&x>=L;
}
int main()
{
while(scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&L,&R)!=EOF)
{
double delta=4*b*b-12*a*c;
double ans;
if(dcmp(a)==0)
{
double mid=-c/2/b;
if(judge(mid))
ans=max(f(mid),f(L));
else ans=f(L);
ans=max(ans,f(R));
}
else if(delta<=0)
{
ans=max(f(L),f(R));
}
else
{
delta=sqrt(delta);
double x1=-2*b+delta;
x1=x1/6/a;
double x2=-2*b-delta;
x2=x2/6/a;
ans=f(L);
ans=max(ans,f(R));
if(judge(x1))
ans=max(ans,f(x1));
if(judge(x2))
ans=max(ans,f(x2));
}
printf("%.2lf\n",ans);
}
return 0;
}

57
HDOJ/5106_autoAC.cpp Normal file
View File

@ -0,0 +1,57 @@
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define clr0(x) memset(x,0,sizeof(x))
#define clr1(x) memset(x,-1,sizeof(x))
using namespace std;
typedef long long LL;
const int maxn = 1005;
const double eps = 1e-3;
const int inf = 0x7fffffff;
char p[maxn];
const LL mod = 1000000007;
LL c[maxn][maxn];
LL q[maxn];
void init()
{
for(int i = 0 ; i < maxn ; ++i){
c[i][0] = 1;
}
for(int i = 1 ; i < maxn ; i++){
for(int j = 1 ; j <= i ; ++j){
c[i][j] = (c[i-1][j-1] + c[i-1][j])%mod;
}
}
q[0] = 1;
for(int i = 1;i < maxn;++i){
q[i] = (q[i - 1]*2)%mod;
}
}
int main()
{
init();
int n,k;
while(~scanf("%d%s",&n,p)){
int m = strlen(p);
int k = 0;
LL ans = 0,tmp = 0;
for(int st = 0;st < m;++st){
if(p[st] == '1'){
ans += ((m-st-2>=0&&n-k-1>=0?c[m - st - 2][n - k - 1]:0) * (q[m - st - 1] - 1))%mod;
ans += (tmp * q[m - st]%mod * (m-st-1>=0&&n-k>=0?c[m - st - 1][n - k]:0))%mod;
ans %= mod;
}
tmp = (tmp*2 + p[st] - '0')%mod;
k += p[st] - '0';
}
if(ans < 0) ans += mod;
printf("%I64d\n",ans);
}
return 0;
}

125
HDOJ/5107_autoAC.cpp Normal file
View File

@ -0,0 +1,125 @@
#include "stdio.h"
#include "string.h"
#include "algorithm"
#include "map"
using namespace std;
map<int,int>mp;
struct Mark
{
int x,y,h,op,id;
}mark[60010];
struct Ans
{
int w;
int h[11];
}ans;
struct node
{
int l,r;
Ans x;
}data[300010];
int y[60010],pri[30010];
bool cmp_mark(Mark a,Mark b)
{
if (a.x!=b.x) return a.x<b.x;
else
if (a.y!=b.y) return a.y<b.y;
else
return a.op<b.op;
}
Ans Merge(Ans a,Ans b)
{
int i,j,k;
Ans c;
i=j=k=1;
while ((i<=a.w || j<=b.w) && k<=10)
{
if (j > b.w || (i <= a.w && a.h[i] < b.h[j]) )
c.h[k++] = a.h[i++];
else
c.h[k++] = b.h[j++];
}
c.w=k-1;
return c;
}
void build(int l,int r,int k)
{
int mid;
data[k].l=l;
data[k].r=r;
data[k].x.w=0;
if(l==r) return ;
mid=(l+r)/2;
build(l,mid,k*2);
build(mid+1,r,k*2+1);
}
void updata(int n,int op,int k)
{
int mid;
if (data[k].l==n && data[k].r==n)
{
data[k].x.w++;
data[k].x.h[data[k].x.w]=op;
sort(data[k].x.h+1,data[k].x.h+1+data[k].x.w);
return ;
}
mid=(data[k].l+data[k].r)/2;
if (n<=mid) updata(n,op,k*2);
else if (n>mid) updata(n,op,k*2+1);
data[k].x=Merge(data[k*2].x,data[k*2+1].x);
}
Ans query(int l,int r,int k)
{
int mid;
if (data[k].l==l && data[k].r==r)
return data[k].x;
mid=(data[k].l+data[k].r)/2;
if (r<=mid) return query(l,r,k*2);
else
if (l>mid) return query(l,r,k*2+1);
else
return Merge(query(l,mid,k*2),query(mid+1,r,k*2+1));
}
int main()
{
int n,m,i,cnt,temp;
while (scanf("%d%d",&n,&m)!=EOF)
{
for (i=0;i<n;i++)
{
scanf("%d%d%d",&mark[i].x,&mark[i].y,&mark[i].h);
y[i]=mark[i].y;
mark[i].op=1;
}
for (i=n;i<n+m;i++)
{
scanf("%d%d%d",&mark[i].x,&mark[i].y,&mark[i].h);
mark[i].id=i-n;
y[i]=mark[i].y;
mark[i].op=2;
}
cnt=n+m;
sort(y,y+cnt);
sort(mark,mark+cnt,cmp_mark);
temp=unique(y,y+cnt)-y;
for (i=0;i<temp;i++)
mp[y[i]]=i;
build(0,temp-1,1);
for (i=0;i<cnt;i++)
{
if (mark[i].op==1)
updata(mp[mark[i].y],mark[i].h,1);
else
{
ans=query(0,mp[mark[i].y],1);
if (mark[i].h<=ans.w)
pri[mark[i].id]=ans.h[mark[i].h];
else
pri[mark[i].id]=-1;
}
}
for (i=0;i<m;i++)
printf("%d\n",pri[i]);
}
return 0;
}

62
HDOJ/5108_autoAC.cpp Normal file
View File

@ -0,0 +1,62 @@
#include<bitset>
#include<map>
#include<vector>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<stack>
#include<queue>
#include<set>
#define inf 0x3f3f3f3f
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
inline int in()
{
int res=0;
char c;
while((c=getchar())<'0' || c>'9');
while(c>='0' && c<='9')res=res*10+c-'0',c=getchar();
return res;
}
const int N=32000;
bool vis[N];
int prime[10000];
int main()
{
int p=1;
prime[0]=2;
for(int i=3;i<N;i+=2)
{
if(!vis[i])
{
prime[p++]=i;
for(int j=i+i;j<N;j+=i)
{
vis[j]=1;
}
}
}
int n;
while(~scanf("%d",&n))
{
int tmp=n;
if(n==1)
{
puts("0");
continue;
}
int t=n;
for(int i=0;prime[i]*prime[i]<=n;i++)
{
while(n%prime[i]==0) n/=prime[i],t=prime[i];
}
t=max(t,n);
printf("%d\n",tmp/t);
}
return 0;
}

44
HDOJ/5109_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<cstdlib>
#include<ctime>
#include<cmath>
using namespace std;
typedef long long LL;
int a;
LL s, b, t;
char str[10];
int main() {
while (~scanf("%d%s", &a, str)) {
int len = strlen(str);
if (len == 1 && str[0] == '0') {
puts("0");
continue;
}
b = -1;
LL base = 1;
for (int i = 1; i <= len; i++)
base *= 10;
sscanf(str, "%I64d", &s);
for (int i = 1; i <= 10000; i *= 10) {
for (int j = (str[0] == '0'); j < a; j++) {
t = ((LL) (j) * base + s) * i;
int mod = (a - t % a) % a;
if (mod < i) {
t += mod;
if (b < 0 || t < b)
b = t;
}
}
}
printf("%I64d\n", b / a);
}
return 0;
}

112
HDOJ/5110_autoAC.cpp Normal file
View File

@ -0,0 +1,112 @@
#include <set>
#include <cmath>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long LL ;
#define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define clr( a , x ) memset ( a , x , sizeof a )
const int MAXN = 1005 ;
const int L = 33 ;
int G[MAXN][MAXN] ;
int down[MAXN][MAXN] ;
int prefix[MAXN][MAXN] ;
int prefix_dn[MAXN][MAXN] ;
int prefix_lt[MAXN][MAXN] ;
int prefix_rt[MAXN][MAXN] ;
int ans[L][MAXN][MAXN] ;
int n , m , q , sqrtD ;
char s[MAXN] ;
void scanf ( int &x , char c = 0 ) {
while ( ( c = getchar () ) < '0' || c > '9' ) ;
x = c - '0' ;
while ( ( c = getchar () ) >= '0' && c <= '9' ) x = x * 10 + c - '0' ;
}
void read () {
For ( i , 1 , n ) {
scanf ( "%s" , s + 1 ) ;
For ( j , 1 , m ) {
G[i][j] = ( s[j] == 'X' ) ;
prefix[i][j] = prefix[i][j - 1] + G[i][j] ;
}
}
}
void solve1 () {
int x , y , D ;
sqrtD = ( int ) sqrt ( ( double ) n ) ;
For ( k , 1 , sqrtD ) {
For ( i , 1 , n ) {
For ( j , 1 , m ) {
down[i][j] = G[i][j] ;
if ( i - k > 0 ) down[i][j] += down[i - k][j] ;
prefix_dn[i][j] = prefix_dn[i][j - 1] + down[i][j] ;
prefix_lt[i][j] = down[i][j] ;
if ( i - k > 0 ) {
if ( j - k <= 0 ) prefix_lt[i][j] += prefix_dn[i - k][j - 1] - prefix_dn[i - k][0] ;
else {
prefix_lt[i][j] += prefix_dn[i - k][j - 1] - prefix_dn[i - k][j - k] ;
prefix_lt[i][j] += prefix_lt[i - k][j - k] ;
}
}
}
rev ( j , m , 1 ) {
prefix_rt[i][j] = down[i][j] ;
if ( i - k > 0 ) {
if ( j + k > m ) prefix_rt[i][j] += prefix_dn[i - k][m] - prefix_dn[i - k][j] ;
else {
prefix_rt[i][j] += prefix_rt[i - k][j + k] ;
prefix_rt[i][j] += prefix_dn[i - k][j + k - 1] - prefix_dn[i - k][j] ;
}
}
}
For ( j , 1 , m ) ans[k][i][j] = prefix_lt[i][j] + prefix_rt[i][j] - down[i][j] ;
}
}
while ( q -- ) {
scanf ( x ) ;
scanf ( y ) ;
scanf ( D ) ;
if ( D <= sqrtD ) {
//printf ( "%d %d\n" , prefix_lt[x][y][D] , prefix_rt[x][y][D] ) ;
printf ( "%d\n" , ans[D][x][y] ) ;
}
else {
int L = y , R = y , sum = 0 ;
while ( x > 0 ) {
sum += prefix[x][R] - prefix[x][L - 1] ;
x -= D ;
L = max ( 1 , L - D ) ;
R = min ( m , R + D ) ;
}
printf ( "%d\n" , sum ) ;
}
}
}
void solve2 () {
int x , y , D ;
while ( q -- ) {
scanf ( x ) ;
scanf ( y ) ;
scanf ( D ) ;
int L = y , R = y , sum = 0 ;
while ( x > 0 ) {
sum += prefix[x][R] - prefix[x][L - 1] ;
x -= D ;
L = max ( 1 , L - D ) ;
R = min ( m , R + D ) ;
}
printf ( "%d\n" , sum ) ;
}
}
int main () {
while ( ~scanf ( "%d%d%d" , &n , &m , &q ) ) {
read () ;
if ( q > 1000 ) solve1 () ;
else solve2 () ;
}
return 0 ;
}

212
HDOJ/5111_autoAC.cpp Normal file
View File

@ -0,0 +1,212 @@
#include <map>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long LL ;
#pragma comment(linker, "/STACK:16777216")
#define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define clr( a , x ) memset ( a , x , sizeof a )
#define mid ( ( l + r ) >> 1 )
const int MAXN = 100005 ;
const int MAXE = 100005 ;
struct Node {
Node* c[2] ;
int sum ;
} ;
struct Edge {
int v , n ;
Edge () {}
Edge ( int v , int n ) : v ( v ) , n ( n ) {}
} ;
struct Seg {
int L , R ;
Seg () {}
Seg ( int L , int R ) : L ( L ) , R ( R ) {}
} ;
struct HeavyLightDecompose {
Edge E[MAXE] ;
int H[MAXN] , cntE ;
int pre[MAXN] ;
int pos[MAXN] ;
int dep[MAXN] ;
int siz[MAXN] ;
int son[MAXN] ;
int top[MAXN] ;
int val[MAXN] ;
int tree_ ;
void clear () {
tree_idx = 0 ;
dep[1] = 0 ;
pre[1] = 0 ;
siz[0] = 0 ;
cntE = 0 ;
clr ( H , -1 ) ;
}
void addedge ( int u , int v ) {
E[cntE] = Edge ( v , H[u] ) ;
H[u] = cntE ++ ;
}
void dfs ( int u ) {
siz[u] = 1 ;
son[u] = 0 ;
for ( int i = H[u] ; ~i ; i = E[i].n ) {
int v = E[i].v ;
if ( v == pre[u] ) continue ;
pre[v] = u ;
dep[v] = dep[u] + 1 ;
dfs ( v ) ;
siz[u] += siz[v] ;
if ( siz[v] > siz[son[u]] ) son[u] = v ;
}
}
void rebuild ( int u , int top_ ) {
top[u] = top_ ;
pos[u] = ++ tree_ ;
if ( son[u] ) rebuild ( son[u] , top_ ) ;
for ( int i = H[u] ; ~i ; i = E[i].n ) {
int v = E[i].v ;
if ( v != pre[u] && v != son[u] ) {
rebuild ( v , v ) ;
}
}
}
} ;
Node pool[MAXN * 50] ;
Node* root[MAXN] ;
Node* cur ;
HeavyLightDecompose T1 , T2 ;
Seg seg[MAXN] ;
int top ;
int cnt ;
int n1 , n2 , q ;
map < int , int > mp ;
void clear () {
T1.clear () ;
T2.clear () ;
mp.clear () ;
cur = pool ;
}
void build ( Node* &now , int l , int r ) {
now = cur ++ ;
now->sum = 0 ;
if ( l == r ) return ;
int m = mid ;
build ( now->c[0] , l , m ) ;
build ( now->c[1] , m + 1 , r ) ;
}
void insert ( Node* &now , Node* old , int x , int v , int l , int r ) {
now = cur ++ ;
if ( l == r ) {
now->sum = old->sum + v ;
return ;
}
int m = mid ;
if ( x <= m ) {
now->c[1] = old->c[1] ;
insert ( now->c[0] , old->c[0] , x , v , l , m ) ;
} else {
now->c[0] = old->c[0] ;
insert ( now->c[1] , old->c[1] , x , v , m + 1 , r ) ;
}
now->sum = now->c[0]->sum + now->c[1]->sum ;
}
int query ( Node* now , Node* old , int x , int l , int r ) {
if ( x == 0 ) return 0 ;
int ans = 0 ;
while ( l < r ) {
int m = mid ;
if ( x <= m ) {
now = now->c[0] ;
old = old->c[0] ;
r = m ;
} else {
ans += now->c[0]->sum - old->c[0]->sum ;
now = now->c[1] ;
old = old->c[1] ;
l = m + 1 ;
}
}
ans += now->sum - old->sum ;
return ans ;
}
void dfs ( int u ) {
if ( mp.count ( T2.val[u] ) ) insert ( root[T2.pos[u]] , root[T2.pos[u] - 1] , mp[T2.val[u]] , 1 , 1 , cnt ) ;
else root[T2.pos[u]] = root[T2.pos[u] - 1] ;
if ( T2.son[u] ) dfs ( T2.son[u] ) ;
for ( int i = T2.H[u] ; ~i ; i = T2.E[i].n ) {
int v = T2.E[i].v ;
if ( v != T2.pre[u] && v != T2.son[u] ) {
dfs ( v ) ;
}
}
}
void get_seg ( int x , int y ) {
top = 0 ;
while ( T1.pos[T1.top[x]] != T1.pos[T1.top[y]] ) {
if ( T1.dep[T1.top[x]] < T1.dep[T1.top[y]] ) swap ( x , y ) ;
seg[top ++] = Seg ( T1.pos[T1.top[x]] , T1.pos[x] ) ;
x = T1.pre[T1.top[x]] ;
}
if ( T1.dep[x] > T1.dep[y] ) swap ( x , y ) ;
seg[top ++] = Seg ( T1.pos[x] , T1.pos[y] ) ;
}
int get_sum ( int x , int y ) {
int ans = 0 ;
while ( T2.pos[T2.top[x]] != T2.pos[T2.top[y]] ) {
if ( T2.dep[T2.top[x]] < T2.dep[T2.top[y]] ) swap ( x , y ) ;
rep ( i , 0 , top ) {
int L = seg[i].L ;
int R = seg[i].R ;
ans -= query ( root[T2.pos[x]] , root[T2.pos[T2.top[x]] - 1] , L - 1 , 1 , cnt ) ;
ans += query ( root[T2.pos[x]] , root[T2.pos[T2.top[x]] - 1] , R , 1 , cnt ) ;
}
x = T2.pre[T2.top[x]] ;
}
if ( T2.dep[x] > T2.dep[y] ) swap ( x , y ) ;
rep ( i , 0 , top ) {
int L = seg[i].L ;
int R = seg[i].R ;
ans -= query ( root[T2.pos[y]] , root[T2.pos[x] - 1] , L - 1 , 1 , cnt ) ;
ans += query ( root[T2.pos[y]] , root[T2.pos[x] - 1] , R , 1 , cnt ) ;
}
return ans ;
}
void solve () {
int u , v ;
clear () ;
cnt = n1 ;
For ( i , 2 , n1 ) {
scanf ( "%d" , &u ) ;
T1.addedge ( u , i ) ;
}
For ( i , 1 , n1 ) scanf ( "%d" , &T1.val[i] ) ;
scanf ( "%d" , &n2 ) ;
For ( i , 2 , n2 ) {
scanf ( "%d" , &u ) ;
T2.addedge ( u , i ) ;
}
For ( i , 1 , n2 ) scanf ( "%d" , &T2.val[i] ) ;
T1.dfs ( 1 ) ;
T1.rebuild ( 1 , 1 ) ;
For ( i , 1 , n1 ) mp[T1.val[i]] = T1.pos[i] ;
build ( root[0] , 1 , cnt ) ;
T2.dfs ( 1 ) ;
T2.rebuild ( 1 , 1 ) ;
dfs ( 1 ) ;
scanf ( "%d" , &q ) ;
while ( q -- ) {
scanf ( "%d%d" , &u , &v ) ;
get_seg ( u , v ) ;
scanf ( "%d%d" , &u , &v ) ;
int ans = get_sum ( u , v ) ;
printf ( "%d\n" , ans ) ;
}
}
int main () {
while ( ~scanf ( "%d" , &n1 ) ) solve () ;
return 0 ;
}

28
HDOJ/5112_autoAC.cpp Normal file
View File

@ -0,0 +1,28 @@
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define maxn 10010
struct Node {
int t, x;
} E[maxn];
bool cmp(Node a, Node b) {
return a.t < b.t;
}
int main() {
int T, N, i, j;
double ans;
scanf("%d", &T);
for(int cas = 1; cas <= T; ++cas) {
scanf("%d", &N);
for(i = 0; i < N; ++i)
scanf("%d%d", &E[i].t, &E[i].x);
sort(E, E + N, cmp);
for(ans = 0.0, i = 1; i < N; ++i) {
ans = max(ans, abs(E[i].x - E[i-1].x) * 1.0 / (E[i].t - E[i-1].t));
}
printf("Case #%d: %.2lf\n", cas, ans);
}
return 0;
}

61
HDOJ/5113_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef vector<int> vec;
typedef vector<vec> mat;
#define AA first
#define BB second
int T,N,M,K;
int m[10][10];
int c[100];
bool hasAns;
bool C(int x,int y,int c){
int row = true, column = true;
if( x-1>=1&&m[x-1][y]==c ) column = false;
if( y-1>=1 && m[x][y-1]==c ) row = false;
return row&&column;
}
void dfs(int x,int y,int cur){
for(int i=1;i<=K;i++){
if( c[i]>(cur+1)/2 ) return;
}
if(hasAns) return;
if( x==N+1 ){
hasAns = true;
puts("YES");
for(int i=1;i<=N;i++) {
for(int j=1;j<=M;j++ ){
printf(j==M?"%d\n":"%d ",m[i][j]);
}
}
return;
}
for(int i=1;i<=K;i++)if( c[i] ){
if( C(x,y,i) ){
c[i]--;
m[x][y] = i;
if( y==M ) dfs(x+1,1,cur-1);
else dfs(x,y+1,cur-1);
c[i]++;
m[x][y] = 0;
}
}
}
int main(){
scanf("%d",&T);
for(int cases = 1; cases <= T; cases++){
scanf("%d%d%d",&N,&M,&K);
for(int i=1;i<=K;i++){
scanf("%d",&c[i]);
}
hasAns = false;
printf("Case #%d:\n",cases);
dfs(1,1,N*M);
if(!hasAns) puts("NO");
}
return 0;
}

77
HDOJ/5114_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
int n,m;
int MIN(int x,int y)
{
if (x<y) return x;
return y;
}
int main()
{
int x1,y1,x2,y2,t,ii,i,j,k,fx11,fx12,fx21,fx22,move,x1o,x2o,y1o,y2o;
double ansx,ansy;
scanf("%d",&t);
for (ii=1;ii<=t;ii++)
{
scanf("%d%d%d%d%d%d",&n,&m,&x1,&y1,&x2,&y2);
fx11=1;fx12=1;fx21=1;fx22=1;
if (x1==n&&fx11>0||x1==0&&fx11<0) fx11=-fx11;
if (y1==m&&fx12>0||y1==0&&fx12<0) fx12=-fx12;
if (x2==n&&fx21>0||x2==0&&fx21<0) fx21=-fx21;
if (y2==m&&fx22>0||y2==0&&fx22<0) fx22=-fx22;
for (i=1;i<=410000;i++)
{
if (abs(x1-x2)==abs(y1-y2)&&((x2-x1)/fx11>0)&&((y2-y1)/fx12>0)&&((x1-x2)/fx21 >0)&&((y1-y2)/fx22>0))//杩㈢告
{
ansx=(x1+x2)/2.0;
ansy=(y1+y2)/2.0;
break;
}
if (fx11>0) move=n-x1;
else move=x1;
if (fx12>0) move=MIN(move,m-y1);
else move=MIN(move,y1);
if (fx21>0) move=MIN(move,n-x2);
else move=MIN(move,x2);
if (fx22>0) move=MIN(move,m-y2);
else move=MIN(move,y2);
//琛告
if (x1==x2&&((y2-y1)/fx12>0)&&((y1-y2)/fx22>0)&&fx11==fx21&&(x1+fx11*abs(y2-y1)/2.0)>=0&&(x1+fx11*abs(y2-y1)/2.0)<=n)
{
ansx=x1+fx11*abs(y2-y1)/2.0;
ansy=(y1+y2)/2.0;
break;
}
//告
if (y1==y2&&((x2-x1)/fx11>0)&&((x1-x2)/fx21>0)&&fx12==fx22&&(y1+fx12*abs(x2-x1)/2.0)>=0&&(y1+fx12*abs(x2-x1)/2.0)<=m)
{
ansx=(x1+x2)/2.0;
ansy=(y1+fx12*abs(x2-x1)/2.0);
break;
}
x1=move*fx11+x1;
y1=move*fx12+y1;
x2=move*fx21+x2;
y2=move*fx22+y2;
if (x1==n&&fx11>0||x1==0&&fx11<0) fx11=-fx11;
if (y1==m&&fx12>0||y1==0&&fx12<0) fx12=-fx12;
if (x2==n&&fx21>0||x2==0&&fx21<0) fx21=-fx21;
if (y2==m&&fx22>0||y2==0&&fx22<0) fx22=-fx22;
}
printf("Case #%d:\n",ii);
if (i==410001)
{
printf("Collision will not happen.\n");
continue;
}
else
{
printf("%.1lf %.1lf\n",ansx,ansy);
}
}
return 0;
}

34
HDOJ/5115_autoAC.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[210],b[210];
int f[210][210];
int main()
{
int t,ii,n,i,j,k,l,Min,m;
scanf("%d",&t);
for (ii=1;ii<=t;ii++)
{
memset(f,0,sizeof(0));
scanf("%d",&n);
for (i=1;i<=n;i++)
scanf("%d",&a[i]);
for (i=1;i<=n;i++)
scanf("%d",&b[i]);
for (i=1;i<=n;i++)
{
for (j=i;j<=n;j++)
f[i][j]=99999999;
}
for (l=0;l<=n;l++)
for (i=1;i<n+1-l;i++)
{
j=i+l;
for (k=i;k<=j;k++)
if (f[i][k-1]+f[k+1][j]+a[k]+b[i-1]+b[j+1]<f[i][j])
f[i][j]=f[i][k-1]+f[k+1][j]+a[k]+b[i-1]+b[j+1];
}
printf("Case #%d: %lld\n",ii,f[1][n]);
}
}

93
HDOJ/5116_autoAC.cpp Normal file
View File

@ -0,0 +1,93 @@
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define prt(k) cout<<#k" = "<<k<<endl;
typedef long long ll;
const int N = 40020;
const int M = 202;
#include <vector>
bool mp[M+5][M+5];
int dp[M+5][M+5];
int cnt[M+5][M+5];
ll S;
int gcd(int a, int b) { return b==0?a:gcd(b, a%b); }
int U[M][M], R[M][M];
ll T[M][M];
int f(int i, int j)
{
int ret = 0;
for (int k=1;k<=j;k++) if (gcd(i,k)==1) ret++;
return ret;
}
void init()
{
memset(cnt, 0, sizeof cnt);
for (int j=1;j<=200;j++)
for (int i=1;i<=200;i++)
{
dp[i][j] = f(i, j);
cnt[i][j] = cnt[i-1][j] + dp[i][j];
}
}
int n;
int main()
{
init();
int re; scanf("%d", &re); int ca = 1;
while (re--)
{
scanf("%d", &n);
memset(mp, false, sizeof mp);
memset(U, 0, sizeof U);
memset(R, 0, sizeof R);
memset(T, 0, sizeof T);
S = 0;
for (int i=0;i<n;i++)
{
int x, y; scanf("%d%d", &x, &y);
mp[x][y] = true;
}
n = 200;
for (int i=200;i>=1;i--)
for (int j=200;j>=1;j--)
if (mp[i][j]) {
if (mp[i+1][j]) U[i][j]=U[i+1][j]+1;
if (mp[i][j+1]) R[i][j]=R[i][j+1]+1;
}
for (int i=1;i<=200;i++)
for (int j=1;j<=200;j++)
if (mp[i][j]) {
ll tmp[M];
memset(tmp, 0, sizeof tmp);
for (int k=1; k<=U[i][j]; k++)
tmp[k] = dp[k][R[i][j]];
for (int k = U[i][j]; k; k--)
tmp[k-1] += tmp[k];
S += tmp[0];
for (int k=0; k<=U[i][j]; k++)
T[i+k][j] += tmp[k];
}
ll ans = 0;
for (int i=1;i<200;i++)
{
for (int j=1;j<200;j++)
if (U[i][j]&&R[i][j]) {
ll f = T[i][j];
ll pp, p;
pp = cnt[U[i][j]][R[i][j]];
f -= pp;
for (int k=1;k<=R[i][j];k++)
{
f += T[i][j+k];
p = dp[k][U[i][j]];
ans += f * p * 2;
}
ans += pp * pp;
}
}
ll ret = S * S - ans;
printf("Case #%d: %I64d\n", ca++ , ret);
}
return 0;
}

54
HDOJ/5117_autoAC.cpp Normal file
View File

@ -0,0 +1,54 @@
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int mod = 1000000007;
typedef long long LL;
LL dp[2][8];
int f[51][51];
int main()
{
int T,N,M,K,l,now;
LL tmp;
scanf("%d",&T);
for(int cas=1;cas<=T;cas++){
LL ans=0;
scanf("%d%d",&N,&M);
memset(f,0,sizeof f);
for(int i=0;i<M;i++){
scanf("%d",&K);
for(int j=0;j<K;j++){
scanf("%d",&l);
f[i][l-1]++;
}
}
for(int i=0;i<N;i++){
for(int j=i;j<N;j++){
for(int k=j;k<N;k++){
memset(dp,0,sizeof dp);
dp[0][0]=1;
now=1;
for(int s=0;s<M;s++,now^=1){
tmp=0;
if(f[s][i]) tmp|=1;
if(f[s][j]) tmp|=2;
if(f[s][k]) tmp|=4;
for(int t=0;t<8;t++){
dp[now][t]=dp[now^1][t];
dp[now][t]+=dp[now^1][t^tmp];
dp[now][t]%=mod;
}
}
#define u dp[now^1][7]
tmp = u;
if(i!=j&&j!=k) u*=6;
else if(i==j&&j==k) ;
else u*=3;
ans=(ans+u)%mod;
}
}
}
printf("Case #%d: %I64d\n",cas,ans);
}
return 0;
}

26
HDOJ/5119_autoAC.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
__int64 f[2][1048600];
int a[45];
int main()
{
int t,ii,n,i,j,Min,m;
__int64 ans;
scanf("%d",&t);
for (ii=1;ii<=t;ii++)
{
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++)
scanf("%d",&a[i]);
memset(f,0,sizeof(f));
f[0][0]=1;ans=0;
for (i=1;i<=n;i++)
for (j=0;j<1048576;j++)
f[i%2][j]=f[(i-1)%2][j]+f[(i-1)%2][j^a[i]];
for (i=m;i<1048576;i++)
ans+=f[n%2][i];
printf("Case #%d: %I64d\n",ii,ans);
}
}

53
HDOJ/5120_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
#define exp 1e-10
#define PI 3.141592654
using namespace std;
typedef long long ll;
struct Point
{
double x,y;
Point (double x=0,double y=0):x(x),y(y){}
};
double dist(Point a,Point b)
{
double x=(a.x-b.x)*(a.x-b.x);
double y=(a.y-b.y)*(a.y-b.y);
return sqrt(x+y);
}
double Area_of_overlap(Point c1,double r1,Point c2,double r2)
{
double d=dist(c1,c2);
if (r1+r2<d+exp) return 0;
if (d<fabs(r1-r2)+exp)
{
double r=min(r1,r2);
return PI*r*r;
}
double x=(d*d+r1*r1-r2*r2)/(2*d);
double t1=acos(x/r1);
double t2=acos((d-x)/r2);
return r1*r1*t1+r2*r2*t2-d*r1*sin(t1);
}
int main()
{
int t,ncase=1;
double r,R;
Point a,b;
scanf("%d",&t);
while (t--)
{
scanf("%lf%lf",&r,&R);
scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y);
double bb_area=Area_of_overlap(a,R,b,R);
double bs_area=Area_of_overlap(a,R,b,r);
double ss_area=Area_of_overlap(a,r,b,r);
printf("Case #%d: %.6lf\n",ncase++,bb_area-2.0*bs_area+ss_area);
}
return 0;
}

24
HDOJ/5122_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[1000010];
int main()
{
int t,ii,n,i,Min,ans;
scanf("%d",&t);
for (ii=1;ii<=t;ii++)
{
scanf("%d",&n);
for (i=1;i<=n;i++)
scanf("%d",&a[i]);
Min=a[n];
ans=0;
for (i=n-1;i>=1;i--)
{
if (a[i]>Min) ans++;
else Min=a[i];
}
printf("Case #%d: %d\n",ii,ans);
}
}

33
HDOJ/5123_autoAC.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int a[110];
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(a,0,sizeof(a));
int max=0;
for(int i=0;i<n;i++)
{
int x;
scanf("%d",&x);
a[x]++;
if(a[x]>max)
max=a[x];
}
int ans;
for(int i=0;i<=n;i++)
if(a[i]==max)
{
ans=i;
break;
}
printf("%d\n",ans);
}
return 0;
}

112
HDOJ/5124_autoAC.cpp Normal file
View File

@ -0,0 +1,112 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
struct line{
int l,r;
};
bool operator < (const line &a,const line &b)
{
if(a.r<b.r)return 1;
if(a.r==b.r){
if(a.l<b.l)return 1;
}
return 0;
}
line L[100005];
int pos[200005];
int d[400005];
int maxv[400005];
int yl,yr;
void maintain(int o,int l,int r)
{
int lc=o*2;
int rc=o*2+1;
if(r>l){
maxv[o]=max(maxv[lc],maxv[rc]);
}else maxv[o]=0;
maxv[o]+=d[o];
}
void update(int o,int l,int r)
{
int lc=o*2;
int rc=o*2+1;
if(yl<=l&&yr>=r){
d[o]++;
} else {
int mid=l+((r-l)>>1);
if(yl<=mid)update(lc,l,mid);
if(yr>mid)update(rc,mid+1,r);
}
maintain(o,l,r);
}
int query(int o,int l,int r,int add)
{
if(yl<=l&&yr>=r){
return maxv[o]+add;
} else {
int m=l+((r-l)>>1);
int ret=-1;
if(yl<=m)ret=max(ret,query(o*2 , l , m , add + d[o] ) );
if(yr>m)ret=max(ret , query(o*2+1 , m+1 , r , add+d[o]));
return ret;
}
}
int bisearch(int x,int tot)
{
int le=0;
int ri=tot-1;
while(le<ri){
int mid=(le+ri)>>1;
if(pos[mid]==x)return mid;
if(pos[mid]>x)ri=mid-1;
else {
le=mid+1;
}
}
return le;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
int tot=0;
set<int> s;
s.clear();
for(int i=0;i<n;++i){
scanf("%d%d",&L[i].l,&L[i].r);
if(s.find(L[i].l)==s.end()){
s.insert(L[i].l);
pos[tot++]=L[i].l;
}
if(s.find(L[i].r)==s.end()){
s.insert(L[i].r);
pos[tot++]=L[i].r;
}
}
sort(pos,pos+tot);
for(int i=0;i<=tot*4;++i){
d[i]=0;
maxv[i]=0;
}
int ans=0;
for(int i=0;i<n;++i){
int le=bisearch(L[i].l,tot);
int ri=bisearch(L[i].r,tot);
le++;
ri++;
yl=le;
yr=ri;
update(1,1,tot);
}
yl=1;yr=tot;
ans=query(1,1,tot,0);
printf("%d\n",ans);
}
return 0;
}

70
HDOJ/5125_autoAC.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define INF 1000000000
//typedef __int64 LL;
#define N 1005
int t, n, m, a[N], b[N], c[N][2*N], nn, num[2*N];
int lowbit(int x) {
return x & (-x);
}
int get_max(int x, int k) {
int ret = 0;
while(x > 0) {
ret = max(ret, c[k][x]);
x -= lowbit(x);
}
return ret;
}
void add(int x, int ad, int k) {
while(x <= nn) {
c[k][x] = max(ad, c[k][x]);
x += lowbit(x);
}
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
scanf("%d", &t);
while(t--) {
scanf("%d%d", &n, &m);
nn = 0;
for(int i = 1;i <= n;i ++) {
scanf("%d%d", &a[i], &b[i]);
nn ++; num[nn] = a[i];
nn ++; num[nn] = b[i];
}
sort(num+1, num+1+nn);
nn = unique(num+1, num+1+nn) - num - 1;
for(int i = 1; i<= n; i++) {
a[i] = lower_bound(num+1, num+1+nn, a[i]) - num;
b[i] = lower_bound(num+1, num+1+nn, b[i]) - num;
}
memset(c, 0, sizeof(c));
int ans = 0;
for(int i = 1; i <= n; i++) {
for(int j = 0; j <= m; j++) {
int temp;
temp = get_max(a[i] - 1, j) + 1;
add(a[i], temp, j);
ans = max(ans, temp);
if(j!=m) {
temp = get_max(b[i] - 1, j+1) + 1;
add(b[i], temp, j);
ans = max(ans, temp);
}
}
}
printf("%d\n", ans);
}
return 0;
}

139
HDOJ/5126_autoAC.cpp Normal file
View File

@ -0,0 +1,139 @@
#include<cstdio>
#include<algorithm>
using namespace std;
struct Node{
int x,y,z,p,t;
Node(){}
Node(int x,int y,int z,int p,int t):x(x),y(y),z(z),p(p),t(t){}
};
#define maxn 450007
Node star[maxn];
Node star2[maxn];
Node star3[maxn];
int tree[maxn];
int lowbit(int i){
return i&(-i);
}
void add(int i,int x){
while(i<maxn){
tree[i]+=x;
i+=lowbit(i);
}
}
int get(int i){
int ans = 0;
while(i>0){
ans += tree[i];
i -= lowbit(i);
}
return ans;
}
int res[maxn];
void CDQ2(int l,int r){
if(l == r) return ;
int mid = (l+r)/2;
CDQ2(l,mid);
CDQ2(mid+1,r);
int l1 =l, r1 = mid+1;
while(r1 <= r){
while(l1 <= mid && star2[l1].y <= star2[r1].y){
if(star2[l1].t == 0) add(star2[l1].z,1);
l1++;
}
if(star2[r1].t != 0){
res[star2[r1].p] += get(star2[r1].z)*star2[r1].t;
}
r1++;
}
while(l1 > l){
--l1;
if(star2[l1].t == 0) add(star2[l1].z,-1);
}
l1 = l, r1 = mid+1;
for(int i = l;i <= r; i++){
if((l1 <= mid && star2[l1].y <= star2[r1].y) || r1 > r )
star3[i] = star2[l1++];
else star3[i] = star2[r1++];
}
for(int i = l; i <= r; i++)
star2[i] = star3[i];
}
void CDQ1(int l,int r){
if(l == r) return ;
int mid = (l+r)/2;
CDQ1(l,mid);
CDQ1(mid+1,r);
int l1 = l, r1 = mid+1,n = 0;
while(r1 <= r){
while(star[l1].t != 0 && l1 <= mid) l1++;
while(star[r1].t == 0 && r1 <= r) r1++;
if(r1 > r) break;
if((star[l1].x <= star[r1].x && l1 <= mid)|| r1 > r)
star2[n++] = star[l1++];
else star2[n++] = star[r1++];
}
if(n > 0) CDQ2(0,n-1);
l1 = l, r1 = mid+1;
for(int i = l;i <= r; i++){
if((star[l1].x <= star[r1].x && l1 <= mid)|| r1 > r)
star3[i] = star[l1++];
else star3[i] = star[r1++];
}
for(int i = l;i <= r; i++)
star[i] = star3[i];
}
int compn(Node a,Node b){
return a.p < b.p;
}
int compz(Node a,Node b){
return a.z < b.z;
}
int main(){
int t,q,a,x1,y1,z1,x2,y2,z2;
scanf("%d",&t);
while(t--){
int n = 0;
scanf("%d",&q);
while(q--){
scanf("%d",&a);
if(a == 1){
scanf("%d%d%d",&x1,&y1,&z1);
star[n++] = Node(x1,y1,z1,n,0);
}
else {
scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2);
star[n++] = Node(x2,y2,z2,n,1);
star[n++] = Node(x2,y1-1,z2,n,-1);
star[n++] = Node(x1-1,y2,z2,n,-1);
star[n++] = Node(x2,y2,z1-1,n,-1);
star[n++] = Node(x1-1,y1-1,z2,n,1);
star[n++] = Node(x1-1,y2,z1-1,n,1);
star[n++] = Node(x2,y1-1,z1-1,n,1);
star[n++] = Node(x1-1,y1-1,z1-1,n,-1);
}
}
for(int i = 0;i < n; i++)
res[i] = 0;
sort(star,star+n,compz);
a = 1;
star[n].z = -1;
for(int i = 0;i < n; i++){
if(star[i].z != star[i+1].z)
star[i].z = a++;
else star[i].z = a;
}
sort(star,star+n,compn);
CDQ1(0,n-1);
sort(star,star+n,compn);
for(int i = 0;i < n; i++){
if(star[i].t != 0){
int ans = 0;
for(int j = 0;j < 8; j++)
ans += res[i+j];
printf("%d\n",ans);
i += 7;
}
}
}
return 0;
}

32
HDOJ/5127_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
#include<list>
typedef pair<long long ,long long > candy;
list<candy> s;
int main()
{
int n;
while( scanf("%d",&n) && n ){
s.clear();
while(n--){
long long x,y,z;
scanf("%I64d%I64d%I64d",&z,&x,&y);
candy aa ; aa.first=x; aa.second=y;
if(z==1 ) { s.push_front(aa); }
else if(z==-1) {
for(list<candy>::iterator i=s.begin();i!=s.end();i++)
if((*i).first==x&&(*i).second==y) { s.erase(i); break; }
}
else if(z==0) {
long long sum=-0x7f7f7f7f;
for(list<candy>::iterator i=s.begin();i!=s.end();i++) {
sum = max(sum, (*i).first*x+(*i).second*y);
}
cout<<sum<<endl;
}
}
}
return 0;
}

107
HDOJ/5128_autoAC.cpp Normal file
View File

@ -0,0 +1,107 @@
#include <vector>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 10010
#define LL long long
using namespace std;
bool vis[MAXN][MAXN];
struct DOT {
int x, y;
DOT() {}
DOT(int _x, int _y) {
x = _x; y = _y;
}
}dot[MAXN];
struct Matrix {
DOT up, down;
Matrix() { }
Matrix(DOT a, DOT b) {
up = a;
down = b;
}
}maxrix[MAXN];
int area(Matrix m) {
return (m.down.x-m.up.x)*(m.up.y-m.down.y);
}
vector<Matrix> vec;
bool cmp(DOT a, DOT b) {
if(a.x < b.x)
return true;
if(a.x > b.x)
return false;
return a.y <= b.y;
}
int son_judge(DOT t, Matrix m) {
int x1 = m.up.x;
int y1 = m.up.y;
int x2 = m.down.x;
int y2 = m.down.y;
if(t.x>=x1 && t.x<=x2 && t.y<=y1 && t.y>=y2) {
if(t.x>x1 && t.x<x2 && t.y<y1 && t.y>y2)
return 1;
return 0;
}
return -1;
}
int judge(int i, int j) {
DOT t1 = DOT(vec[i].up.x, vec[i].down.y);
DOT t2 = DOT(vec[i].down.x, vec[i].up.y);
int ok1 = son_judge(vec[i].up, vec[j]);
int ok2 = son_judge(vec[i].down, vec[j]);
int ok3 = son_judge(t1, vec[j]);
int ok4 = son_judge(t2, vec[j]);
if(ok1==1&&ok2==1&&ok3==1&&ok4==1)
return 1;
if(ok1==-1&&ok2==-1&&ok3==-1&&ok4==-1)
return -1;
return 0;
}
int main() {
int n;
while(scanf("%d", &n) && n) {
if(n < 8) {
puts("imp");
continue;
}
vec.clear();
memset(vis, 0, sizeof(vis));
for(int i=0; i<n; ++i) {
scanf("%d%d", &dot[i].x, &dot[i].y);
vis[dot[i].x][dot[i].y] = true;
}
sort(dot, dot+n, cmp);
int x1, x2, y1, y2;
for(int i=0; i<n; ++i) {
x1 = dot[i].x;
y1 = dot[i].y;
for(int j=i+1; j<n; ++j) {
x2 = dot[j].x;
y2 = dot[j].y;
if(x2<=x1 || y2>=y1) continue;
if(vis[x2][y1] && vis[x1][y2]) {
vec.push_back(Matrix(dot[i], dot[j]));
}
}
}
int ans = 0;
for(int i=0; i<vec.size(); ++i) {
for(int j=i+1; j<vec.size(); ++j) {
int tmp1 = judge(i, j);
int tmp2 = judge(j, i);
if(tmp1 == 1 || tmp2 == 1) {
ans = max(ans, max(area(vec[i]), area(vec[j])));
} else if(tmp1 == -1 && tmp2 == -1) {
ans = max(ans, area(vec[i])+area(vec[j]));
}
}
}
if(ans == 0)
puts("imp");
else printf("%d\n", ans);
}
return 0;
}

123
HDOJ/5130_autoAC.cpp Normal file
View File

@ -0,0 +1,123 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define eps 1e-8
using namespace std;
#define N 100017
struct Point{
double x,y;
Point(double x=0, double y=0):x(x),y(y) {}
void input() { scanf("%lf%lf",&x,&y); }
};
typedef Point Vector;
struct Circle{
Point c;
double r;
Circle(){}
Circle(Point c,double r):c(c),r(r) {}
Point point(double a) { return Point(c.x + cos(a)*r, c.y + sin(a)*r); }
void input() { scanf("%lf%lf%lf",&c.x,&c.y,&r); }
};
int dcmp(double x) {
if(x < -eps) return -1;
if(x > eps) return 1;
return 0;
}
template <class T> T sqr(T x) { return x * x;}
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; }
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
Vector VectorUnit(Vector x){ return x / Length(x);}
Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);}
double angle(Vector v) { return atan2(v.y, v.x); }
bool OnSegment(Point P, Point A, Point B) {
return dcmp(Cross(A-P,B-P)) == 0 && dcmp(Dot(A-P,B-P)) < 0;
}
double DistanceToSeg(Point P, Point A, Point B)
{
if(A == B) return Length(P-A);
Vector v1 = B-A, v2 = P-A, v3 = P-B;
if(dcmp(Dot(v1, v2)) < 0) return Length(v2);
if(dcmp(Dot(v1, v3)) > 0) return Length(v3);
return fabs(Cross(v1, v2)) / Length(v1);
}
double DistanceToLine(Point P, Point A, Point B){
Vector v1 = B-A, v2 = P-A;
return fabs(Cross(v1,v2)) / Length(v1);
}
Point DisP(Point A, Point B){
return Length(B-A);
}
bool SegmentIntersection(Point A,Point B,Point C,Point D) {
return max(A.x,B.x) >= min(C.x,D.x) &&
max(C.x,D.x) >= min(A.x,B.x) &&
max(A.y,B.y) >= min(C.y,D.y) &&
max(C.y,D.y) >= min(A.y,B.y) &&
dcmp(Cross(C-A,B-A)*Cross(D-A,B-A)) <= 0 &&
dcmp(Cross(A-C,D-C)*Cross(B-C,D-C)) <= 0;
}
Point Zero = Point(0,0);
double TriAngleCircleInsection(Circle C, Point A, Point B)
{
Vector OA = A-C.c, OB = B-C.c;
Vector BA = A-B, BC = C.c-B;
Vector AB = B-A, AC = C.c-A;
double DOA = Length(OA), DOB = Length(OB),DAB = Length(AB), r = C.r;
if(dcmp(Cross(OA,OB)) == 0) return 0;
if(dcmp(DOA-C.r) < 0 && dcmp(DOB-C.r) < 0) return Cross(OA,OB)*0.5;
else if(DOB < r && DOA >= r) {
double x = (Dot(BA,BC) + sqrt(r*r*DAB*DAB-Cross(BA,BC)*Cross(BA,BC)))/DAB;
double TS = Cross(OA,OB)*0.5;
return asin(TS*(1-x/DAB)*2/r/DOA)*r*r*0.5+TS*x/DAB;
}
else if(DOB >= r && DOA < r) {
double y = (Dot(AB,AC)+sqrt(r*r*DAB*DAB-Cross(AB,AC)*Cross(AB,AC)))/DAB;
double TS = Cross(OA,OB)*0.5;
return asin(TS*(1-y/DAB)*2/r/DOB)*r*r*0.5+TS*y/DAB;
}
else if(fabs(Cross(OA,OB)) >= r*DAB || Dot(AB,AC) <= 0 || Dot(BA,BC) <= 0) {
if(Dot(OA,OB) < 0) {
if(Cross(OA,OB) < 0) return (-acos(-1.0)-asin(Cross(OA,OB)/DOA/DOB))*r*r*0.5;
else return ( acos(-1.0)-asin(Cross(OA,OB)/DOA/DOB))*r*r*0.5;
}
else return asin(Cross(OA,OB)/DOA/DOB)*r*r*0.5;
}
else {
double x = (Dot(BA,BC)+sqrt(r*r*DAB*DAB-Cross(BA,BC)*Cross(BA,BC)))/DAB;
double y = (Dot(AB,AC)+sqrt(r*r*DAB*DAB-Cross(AB,AC)*Cross(AB,AC)))/DAB;
double TS = Cross(OA,OB)*0.5;
return (asin(TS*(1-x/DAB)*2/r/DOA)+asin(TS*(1-y/DAB)*2/r/DOB))*r*r*0.5 + TS*((x+y)/DAB-1);
}
}
Point p[507],A,B;
int main()
{
int n,i,j,cs = 1;
double k;
while(scanf("%d%lf",&n,&k)!=EOF)
{
for(i=1;i<=n;i++) p[i].input();
A.input(), B.input(), p[n+1] = p[1];
double D = (2.0*k*k*A.x-2.0*B.x)/(1.0-k*k);
double E = (2.0*k*k*A.y-2.0*B.y)/(1.0-k*k);
double F = (B.x*B.x+B.y*B.y-k*k*(A.x*A.x+A.y*A.y))/(1.0-k*k);
Circle C = Circle(Point(-D*0.5,-E*0.5),sqrt(D*D+E*E-4.0*F)*0.5);
double ans = 0.0;
for(i=1;i<=n;i++)
ans += TriAngleCircleInsection(C, p[i], p[i+1]);
printf("Case %d: %.10f\n",cs++,fabs(ans));
}
return 0;
}

80
HDOJ/5131_autoAC.cpp Normal file
View File

@ -0,0 +1,80 @@
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <cmath>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cctype>
using namespace std;
#define ll long long
const int maxn=202;
struct P
{
string name;
int k;
}p[maxn];
bool cmp(P a,P b)
{
if(a.k>b.k)
return true;
else if(a.k==b.k)
{
if(a.name<b.name)
return true;
return false;
}
return false;
}
int n;
map<string,int>mp;
void find(string str)
{
int major=1;
int minor=1;
for(int i=1;i<=n;i++)
{
if(p[i].name==str)
{
break;
}
if(p[i].k>mp[str])
major++;
if(p[i].k==mp[str])
minor++;
}
cout<<major;
if(minor!=1)
cout<<" "<<minor<<endl;
else
cout<<endl;
}
int main()
{
while(cin>>n&&n)
{
mp.clear();
for(int i=1;i<=n;i++)
cin>>p[i].name>>p[i].k;
sort(p+1,p+1+n,cmp);
for(int i=1;i<=n;i++)
{
cout<<p[i].name<<" "<<p[i].k<<endl;
mp[p[i].name]=p[i].k;
}
int q;
string str;
cin>>q;
while(q--)
{
cin>>str;
find(str);
}
}
return 0;
}

114
HDOJ/5134_autoAC.cpp Normal file
View File

@ -0,0 +1,114 @@
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<cmath>
#include<stack>
#include<set>
#include<map>
#define FIR first
#define SEC second
#define MP make_pair
#define inf 0x3f3f3f3f
#define LL long long
#define CLR(a, b) memset(a, b, sizeof(a))
using namespace std;
struct Point
{
double x, y;
Point(double x = 0, double y = 0)
:x(x), y(y) {}
};
double EP = 0;
double x_mult(Point sp, Point ep, Point op){
return (sp.x-op.x)*(ep.y-op.y)-(sp.y-op.y)*(ep.x-op.x);
}
double cross(Point a,Point b,Point c){
return (a.x-c.x)*(b.x-c.x)+(a.y-c.y)*(b.y-c.y);
}
double dist(Point a,Point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double cal_area(Point a,Point b,Point c,double r){
double A,B,C,x,y,tS;
A=dist(b,c);
B=dist(a,c);
C=dist(b,a);
if(A<r&&B<r)
return x_mult(a,b,c)/2;
else if(A<r&&B>=r){
x=(cross(a,c,b)+sqrt(r*r*C*C-x_mult(a,c,b)*x_mult(a,c,b)))/C;
tS=x_mult(a,b,c)/2;
return asin(tS*(1-x/C)*2/r/B*(1-EP))*r*r/2+tS*x/C;
}
else if(A>=r&&B<r){
y=(cross(b,c,a)+sqrt(r*r*C*C-x_mult(b,c,a)*x_mult(b,c,a)))/C;
tS=x_mult(a,b,c)/2;
return asin(tS*(1-y/C)*2/r/A*(1-EP))*r*r/2+tS*y/C;
}
else if(fabs(x_mult(a,b,c))>=r*C||cross(b,c,a)<=0||cross(a,c,b)<=0){
if(cross(a,b,c)<0)
if(x_mult(a,b,c)<0)
return (-acos(-1.0)-asin(x_mult(a,b,c)/A/B*(1-EP)))*r*r/2;
else return (acos(-1.0)-asin(x_mult(a,b,c)/A/B*(1-EP)))*r*r/2;
else return asin(x_mult(a,b,c)/A/B*(1-EP))*r*r/2;
}
else{
x=(cross(a,c,b)+sqrt(r*r*C*C-x_mult(a,c,b)*x_mult(a,c,b)))/C;
y=(cross(b,c,a)+sqrt(r*r*C*C-x_mult(b,c,a)*x_mult(b,c,a)))/C;
tS=x_mult(a,b,c)/2;
return (asin(tS*(1-x/C)*2/r/B*(1-EP))+asin(tS*(1-y/C)*2/r/A*(1-EP)))*r*r/2+tS*((y+x)/C-1);
}
}
double solve(Point p[], int n, Point cir, double r){
double area=0;
for(int i=0;i<n;i++){
area+=cal_area(p[i], p[(i+1)%n], cir, r);
}
return area;
}
double pi = acos(-1.0);
int main()
{
double v0, v1, D, T;int cas = 1;
while(cin >> v0 >> v1 >> D >> T)
{
printf("Case %d: ", cas ++);
Point cir = Point(-D, 0);
double r = v0 * T;
if(v0 * T <= D)
{
printf("%.7f\n", pi * r * r);
continue;
}
double lft = D / v0, rgt = T;
for(int i = 0; i < 100; i ++)
{
double mid = (lft + rgt) / 2;
double midmid = (mid + rgt) / 2;
double _mid = sqrt((v0 * mid) * (v0 * mid) - D * D) + v1 * (T - mid);
double _midmid = sqrt((v0 * midmid) * (v0 * midmid) - D * D) + v1 * (T - midmid);
if(_mid > _midmid) rgt = midmid;
else lft = mid;
}
double t = (lft + rgt) / 2;
double y = sqrt((v0 * t) * (v0 * t) - D * D) + v1 * (T - t);
double x1 = T / t * D, y1 = sqrt(v0 * T * v0 * T - x1 * x1);
x1 -= D;
Point p[8];
p[0] = Point(0, y);
p[1] = Point(x1, y1);
p[2] = Point(x1, -y1);
p[3] = Point(0, -y);
p[4] = Point(-x1, -y1);
p[5] = Point(-x1, y1);
p[6] = p[0];
double ans = solve(p, 6, cir, r);
ans = pi * r * r + x1 * y1 * 4 + x1 * (y - y1) * 2 - fabs(ans);
printf("%.7f\n", ans);
}
}

39
HDOJ/5135_autoAC.cpp Normal file
View File

@ -0,0 +1,39 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
int n, a[12];
double dp[1<<12];
double cal(int a, int b, int c){
if(a+b<=c) return 0.0;
double p = (a+b+c)*0.5;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
vector<int> V;
int main(){
while(~scanf("%d", &n) && n){
memset(dp, 0, sizeof(dp));
for(int i=0; i<n; i++) scanf("%d", a+i);
sort(a, a+n);
V.clear();
for(int i=0; i<n; i++){
for(int j=i+1; j<n; j++){
for(int k=j+1; k<n; k++){
int st = (1<<i)|(1<<j)|(1<<k);
dp[st] = cal(a[i], a[j], a[k]);
if(a[i]+a[j]>a[k]) V.push_back(st);
}
}
}
for(int i=0; i<(1<<n); i++){
for(int j=0; j<V.size(); j++){
if(i&V[j]) continue;
dp[i|V[j]] = max(dp[i|V[j]], dp[i]+dp[V[j]]);
}
}
printf("%.2f\n", dp[(1<<n)-1]);
}
return 0;
}

81
HDOJ/5136_autoAC.cpp Normal file
View File

@ -0,0 +1,81 @@
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll __int64
#define ull unsigned long long
#define eps 1e-8
#define NMAX 1000000005
#define MOD 1000000007
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
template<class T>
inline void scan_d(T &ret)
{
char c;
int flag = 0;
ret=0;
while(((c=getchar())<'0'||c>'9')&&c!='-');
if(c == '-')
{
flag = 1;
c = getchar();
}
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
if(flag) ret = -ret;
}
ll dp[100005],sum[100005];
void cal()
{
ll a = 3,b = MOD-2,ret = 1;
while(b)
{
if(b&1) ret = ret*a%MOD;
a = a*a%MOD;
b >>= 1;
}
cout<<ret<<endl;
}
int main()
{
dp[1] = dp[2] = 1;
sum[0] = 1;
sum[1] = 2;
ll tmp = 1;
for(int i = 3; i <= 100000; i++)
{
if(i%2)
{
ll a = ((tmp*(tmp-1LL+MOD)%MOD*(tmp-2LL+MOD)%MOD)*166666668LL%MOD + tmp*(tmp-1LL+MOD)%MOD + tmp)%MOD;
dp[i] = (sum[i/2-1]*((tmp*(tmp-1LL+MOD)%MOD)*500000004LL%MOD+tmp)%MOD+a)%MOD;
}
else
{
ll a = tmp*sum[i/2-2]%MOD;
tmp = (tmp*(tmp-1LL+MOD)%MOD*500000004LL%MOD+tmp+a)%MOD;
dp[i] = (tmp*(tmp-1LL+MOD)%MOD*500000004LL%MOD+tmp)%MOD;
sum[i/2] = (sum[i/2-1]+tmp)%MOD;
}
}
int n;
while(~scanf("%d",&n) && n)
{
printf("%I64d\n",dp[n]);
}
return 0;
}

41
HDOJ/5137_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 40;
const int inf = 1000000000;
int n, m, f[N][N], dp[N][N];
int solve(int x){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if(i==x || j==x) dp[i][j] = inf;
else dp[i][j] = f[i][j];
}
}
for(int k=1; k<=n; k++){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++) dp[i][j] = min(dp[i][j], dp[i][k]+dp[k][j]);
}
}
return dp[1][n];
}
int main(){
while(~scanf("%d %d", &n, &m) && (n||m)){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++) f[i][j] = i==j?0:inf;
}
int a, b, c;
while(m--){
scanf("%d %d %d", &a, &b, &c);
f[a][b] = min(f[a][b], c);
f[b][a] = f[a][b];
}
int ans = 0;
for(int i=2; i<n; i++){
ans = max(ans, solve(i));
}
if(ans==inf) puts("Inf");
else printf("%d\n", ans);
}
return 0;
}

32
HDOJ/5138_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n<3)
{
printf("%d\n",n-1);
}
else if(n<5)
{
printf("%d %d\n",n-2,n-1);
}
else if(n<8)
{
printf("%d %d %d\n",n-4,n-2,n-1);
}
else if(n<16)
{
printf("%d %d %d %d\n",n-7,n-4,n-2,n-1);
}
else
{
printf("%d %d %d %d %d\n",n-15,n-7,n-4,n-2,n-1);
}
}
return 0;
}

40
HDOJ/5139_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxn=100002;
const int mod=1000000007;
int ans[maxn];
vector<pair<int,int> >vc;
int n;
int cnt=0;
int main()
{
while(scanf("%d",&n)!=EOF)
{
vc.push_back(make_pair(n,cnt++));
}
sort(vc.begin(),vc.end());
int MAX=vc[cnt-1].first;
long long tp=1,result=1;
int t=0;
for(int i=1;i<=MAX;i++)
{
tp=tp*i;
if(tp>=mod)
tp%=mod;
result=result*tp;
if(result>=mod)
result%=mod;
while(t<cnt&&vc[t].first==i)
{
ans[vc[t].second]=result;
t++;
}
}
for(int i=0;i<cnt;i++)
cout<<ans[i]<<endl;
return 0;
}

148
HDOJ/5140_autoAC.cpp Normal file
View File

@ -0,0 +1,148 @@
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e5+10;
int tot,tree[maxn],c[maxn*20],lson[maxn*20],rson[maxn*20];
ll sum[maxn*20];
int build (int l, int r)
{
int root = tot++;
c[root] = sum[root] = 0;
if (l != r)
{
int mid = (l + r) >> 1;
lson[root] = build(l,mid);
rson[root] = build(mid+1,r);
}
return root;
}
int MAX;
int update (int root,int pos,int val,ll sa)
{
int newroot = tot++;
int tmp = newroot;
int l = 1, r = MAX;
c[newroot] = c[root] + val;
sum[newroot] = sum[root] + sa;
while (l < r)
{
int mid = (l + r) >> 1;
if (pos <= mid)
{
r = mid;
rson[newroot] = rson[root];
root = lson[root];
lson[newroot] = tot++;
newroot = lson[newroot];
}
else
{
l = mid + 1;
lson[newroot] = lson[root];
root = rson[root];
rson[newroot] = tot++;
newroot = rson[newroot];
}
c[newroot] = c[root] + val;
sum[newroot] = sum[root] + sa;
}
return tmp;
}
ll query(int root1,int root2,int l,int r,int ua,int ub)
{
if (ua > ub)
return 0;
if (ua <= l && ub >= r)
{
return sum[root2] - sum[root1];
}
int mid = (l + r) >> 1;
ll t1 = 0, t2 = 0;
if (ua <= mid)
t1 = query(lson[root1],lson[root2],l,mid,ua,ub);
if (ub > mid)
t2 = query(rson[root1],rson[root2],mid+1,r,ua,ub);
return t1 + t2;
}
int lev[maxn],idx1,idx2,age[maxn];
struct worker
{
int s,l,a;
bool operator < (const worker &rhs)const
{
return a < rhs.a;
}
} per[maxn];
int main(void)
{
int n;
while (~scanf ("%d",&n))
{
tot = 0;
memset(sum,0,sizeof(sum));
MAX = n;
int max_age = 0,min_age = inf;
for (int i = 1; i <= n; i++)
{
scanf ("%d%d%d",&per[i].s, &per[i].l, &per[i].a);
age[i-1] = per[i].a;
lev[i-1] = per[i].l;
max_age = max(max_age,age[i-1]);
min_age = min(min_age,age[i-1]);
}
sort(per+1, per+1+n);
sort(age, age+n);
sort(lev, lev+n);
idx1 = n;
idx2 = unique(lev,lev+n) - lev;
tree[0] = build(1,n);
for (int i = 1; i <= n; i++)
{
int tmp = lower_bound(lev,lev+idx2,per[i].l) - lev + 1;
tree[i] = update(tree[i-1], tmp, 1, (ll)per[i].s);
}
int Q;
ll k = 0;
scanf ("%d",&Q);
for (int i = 0; i < Q; i++)
{
ll LL,HL,LA,HA;
scanf ("%I64d%I64d%I64d%I64d",&LL,&HL,&LA,&HA);
LL += k, HL -= k;
LA += k, HA -= k;
if (LL > HL)
swap(LL, HL);
if (LA > HA)
swap(LA, HA);
LA = max(LA, (ll)0);
HA = min(HA, (ll)1000000000);
int idx_la = lower_bound(age,age+idx1,LA) - age + 1;
int idx_ha = lower_bound(age,age+idx1,HA) - age + 1;
if (age[idx_ha-1] == HA)
{
while (age[idx_ha-1] == HA && idx_ha <= n)
idx_ha ++;
idx_ha--;
}
else
idx_ha--;
LL = max(LL,(ll)0);
HL = min(HL,(ll)1000000000);
int i1 = lower_bound(lev,lev+idx2,LL) - lev + 1;
int i2 = lower_bound(lev,lev+idx2,HL) - lev + 1;
if (lev[i2-1] > HL)
i2--;
k = query(tree[idx_la-1],tree[idx_ha],1,MAX,i1,i2);
printf("%I64d\n",k);
}
}
return 0;
}

143
HDOJ/5141_autoAC.cpp Normal file
View File

@ -0,0 +1,143 @@
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define ls(p) p<<1
#define rs(p) p<<1|1
using namespace std;
typedef long long LL;
const int N=100010;
int a[N],b[N];
int dp[N];
int sta[N],en[N];
int len,st;
struct node{
int l,r;
int len,sta;
}tree[N<<2];
void pushup(int p)
{
if(tree[ls(p)].len>tree[rs(p)].len)
{
tree[p].len=tree[ls(p)].len;
tree[p].sta=tree[ls(p)].sta;
}
else if(tree[rs(p)].len>tree[ls(p)].len)
{
tree[p].len=tree[rs(p)].len;
tree[p].sta=tree[rs(p)].sta;
}
else
tree[p].sta=max(tree[ls(p)].sta,tree[rs(p)].sta);
}
void build(int p,int l,int r)
{
tree[p].l=l;tree[p].r=r;
tree[p].len=-1;
tree[p].sta=-1;
if(l==r)
return;
int m=(l+r)>>1;
build(ls(p),l,m);
build(rs(p),m+1,r);
}
void update(int p,int pos,int len,int st)
{
if(tree[p].l==tree[p].r)
{
if(tree[p].len==len&&tree[p].sta<st)
tree[p].sta=st;
else if(tree[p].len<len)
{
tree[p].len=len;
tree[p].sta=st;
}
return;
}
int m=(tree[p].l+tree[p].r)>>1;
if(pos<=m) update(ls(p),pos,len,st);
else update(rs(p),pos,len,st);
pushup(p);
}
void query(int p,int l,int r)
{
if(l<=tree[p].l&&tree[p].r<=r)
{
if(tree[p].len>len)
{
len=tree[p].len;
st=tree[p].sta;
}
else if(tree[p].len==len&&st<tree[p].sta)
st=tree[p].sta;
return;
}
int m=(tree[p].l+tree[p].r)>>1;
if(l<=m) query(ls(p),l,r);
if(r>m) query(rs(p),l,r);
}
int main()
{
int n;
while(scanf("%d",&n)==1)
{
LL ret=0;
int cnt;
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
b[i]=a[i];
}
if(n==1)
{
printf("1\n");
continue;
}
sort(b+1,b+1+n);
cnt=unique(b+1,b+1+n)-(b+1);
dp[1]=sta[1]=1;
build(1,1,cnt);
int mpp;
int lest=-1;
for(int i=1;i<=n;i++)
{
len=st=-1;
mpp=lower_bound(b+1,b+1+cnt,a[i])-b;
if(mpp==1)
{
dp[i]=1;
sta[i]=i;
}
else
query(1,1,mpp-1);
if(st==-1)
{
dp[i]=1;
sta[i]=i;
}
else
{
dp[i]=len+1;
sta[i]=st;
}
update(1,mpp,dp[i],sta[i]);
lest=max(lest,dp[i]);
}
int last=n+1;
for(int i=n;i>=1;i--)
{
if(dp[i]==lest)
{
en[i]=last-1;
last=i;
}
}
for (int i=1;i<=n;++i)
{
if (dp[i]==lest)
ret+=(LL)sta[i]*(LL)(en[i]-i+1);
}
printf("%I64d\n", ret);
}
return 0;
}

17
HDOJ/5142_autoAC.cpp Normal file
View File

@ -0,0 +1,17 @@
#include<cstring>
#include<cstdio>
#include<cmath>
int buf[40];
void go(int n) {
int i, j = 0, res = 0;
memset(buf, 0, sizeof(buf));
do buf[j++] = n % 2; while (n /= 2);
for (i = j - 1; ~i; i--) res += buf[i] * (int)pow(2, j - i - 1);
printf("%d\n", res);
}
int main() {
int t, n;
scanf("%d", &t);
while (t--) scanf("%d", &n), go(n);
return 0;
}

27
HDOJ/5143_autoAC.cpp Normal file
View File

@ -0,0 +1,27 @@
#include<iostream>
using namespace std;
int a1,a2,a3,a4;
bool good(int i,int j,int k)
{
if(((a1-i-k==0)||(a1-i-k>=3))&&((a2-i-j-k==0)||(a2-i-j-k>=3))&&((a3-i-j-k==0)||(a3-i-j-k>=3))&&((a4-j-k==0)||(a4-j-k>=3)))return 1;
else return 0;
}
int main()
{
int n;
int i,j,k;
int flag;
cin>>n;
while(n--)
{
cin>>a1>>a2>>a3>>a4;
flag=0;
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
for(k=0;k<=2;k++)
if(good(i,j,k)){flag=1;break;}
if(flag)cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}

40
HDOJ/5144_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const double pi=acos(-1.0);
const double g=9.8;
double v,h;
double ans(double a)
{
double a1=v*v*sin(a)*sin(a);
double a2=2*g*h;
a1=a1+a2;
a1=sqrt(a1);
a1=a1/g;
a1=a1+v*sin(a)/g;
a1=a1*v*cos(a);
return a1;
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
scanf("%lf%lf",&h,&v);
double l=0;
double r=pi/2;
while (r-l>=0.0000001)
{
double m=(l+r)/2;
double mm=(m+r)/2;
if (ans(m)<ans(mm))
l=m;
else
r=mm;
}
printf("%.2f\n",ans(l));
}
}

91
HDOJ/5145_autoAC.cpp Normal file
View File

@ -0,0 +1,91 @@
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn = 30300;
const int MOD = 1000000007;
int MID;
int a[maxn], p[maxn];
int cnt[maxn];
struct query
{
int l, r, id;
bool operator < (const query& rhs) const
{
return l / MID < rhs.l / MID || (l / MID == rhs.l / MID && r < rhs.r);
}
}q[maxn];
long long Inv(long long x, long long mod)
{
long long r, y;
for(r = 1, y = mod - 2; y; x = x * x % mod, y >>= 1)
(y & 1) && (r = r * x % mod);
return r;
}
int inv[maxn];
void init()
{
inv[0] = 1;
for(int i = 1; i < maxn; i ++)
{
inv[i] = Inv(i, MOD);
}
}
int main()
{
int T, n, m;
init();
scanf("%d", &T);
while(T --)
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++)
scanf("%d", &a[i]);
a[0] = 0;
for(int i = 0; i < m; i ++)
{
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
}
MID = sqrt(n + 0.0);
sort(q, q + m);
int L = 1, R = 1;
int ans = 1, tot = 1;
memset(cnt, 0, sizeof(cnt));
cnt[a[1]] ++;
for(int i = 0; i < m; i ++)
{
while(R < q[i].r)
{
R ++; tot ++; cnt[a[R]] ++;
ans = 1ll * ans * inv[cnt[a[R]]] % MOD;
ans = 1ll * ans * tot % MOD;
}
while(R > q[i].r)
{
ans = 1ll * ans * cnt[a[R]] % MOD;
ans = 1ll * ans * inv[tot] % MOD;
R --; tot --; cnt[a[R + 1]] --;
}
while(L < q[i].l)
{
ans = 1ll * ans * cnt[a[L]] % MOD;
ans = 1ll * ans * inv[tot] % MOD;
L ++; tot --; cnt[a[L - 1]] --;
}
while(L > q[i].l)
{
L --; tot ++; cnt[a[L]] ++;
ans = 1ll * ans * inv[cnt[a[L]]] % MOD;
ans = 1ll * ans * tot % MOD;
}
p[q[i].id] = ans;
}
for(int i = 0; i < m; i ++)
{
printf("%d\n", p[i]);
}
}
}

34
HDOJ/5146_autoAC.cpp Normal file
View File

@ -0,0 +1,34 @@
#include<iostream>
#include<cstring>
#define maxn 1000+5
using namespace std;
int n;
int mapp[maxn];
int main()
{
int t;
cin>>t;
while(t--)
{
memset(mapp,0,sizeof(mapp));
cin>>n;
int j=0,o=0;
for(int i=1;i<=n;i++)
{
cin>>mapp[i];
if(i%2) j+=mapp[i];
else o+=mapp[i];
}
int flag=0;
for(int i=1;i<=n/2;i++)
{
if(mapp[i]!=mapp[n-i+1])
{
flag=1;break;
}
}
if(!flag||j!=o) cout<<"No"<<endl;
else cout<<"Yes"<<endl;
}
return 0;
}

65
HDOJ/5147_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int maxn=50010;
int a[maxn];
int pre[maxn],suf[maxn];
int n;
int c[maxn];
int lowbit(int x)
{
return x&(-x);
}
void update(int i,int x)
{
while(i<=n)
{
c[i]+=x;
i+=lowbit(i);
}
}
int sum(int i)
{
int s=0;
while(i>0)
{
s+=c[i];
i-=lowbit(i);
}
return s;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
{
pre[i]=sum(a[i]);
update(a[i],1);
}
memset(c,0,sizeof(c));
for(int i=n;i>=1;i--)
{
suf[i]=(n-i)-sum(a[i]);
update(a[i],1);
}
long long ans=0,dp=0;
for(int i=1;i<=n-1;i++)
{
ans+=dp*suf[i];
dp+=pre[i];
}
printf("%lld\n",ans);
}
return 0;
}

64
HDOJ/5148_autoAC.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 2300
#define ll long long
const long long inf = 200000 * 10000000LL;
int n,k;
ll dp[maxn][120];
ll tmp[120];
struct edge
{
int u,v,w,next;
}e[maxn*10];
int head[maxn],cnt;
void add(int u,int v,int w)
{
e[cnt].u=u;
e[cnt].v=v;
e[cnt].w=w;
e[cnt].next=head[u];
head[u]=cnt++;
}
void dfs(int u,int fa)
{
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].v;
if(v==fa) continue;
dfs(v,u);
for(int j=0;j<=k;j++) tmp[j]=dp[u][j];
for(int j=0;j<=k;j++)
for(int t=0;t<=j;t++)
tmp[j]=min(tmp[j],dp[u][j-t]+dp[v][t]+(k-t)*t*e[i].w*2);
for(int j=0;j<=k;j++) dp[u][j]=tmp[j];
}
}
int main()
{
int cas;
scanf("%d",&cas);
while(cas--)
{
memset(head,-1,sizeof(head));
cnt=0;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
for(int j=0;j<=k;j++)
{
if(j<=1) dp[i][j]=0;
else dp[i][j]=inf;
}
for(int i=1;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
dfs(1,-1);
printf("%I64d\n",dp[1][k]);
}
return 0;
}

33
HDOJ/5150_autoAC.cpp Normal file
View File

@ -0,0 +1,33 @@
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define MAX 1007
using namespace std;
int n;
int num;
int sum;
int gcd ( int a , int b )
{
return b==0?a:gcd ( b , a%b );
}
int main ( )
{
while ( ~scanf ( "%d" , &n ) )
{
sum = 0;
for ( int i = 0 ; i < n ; i ++ )
{
bool flag = true;
scanf ( "%d" , &num );
for ( int i = 2 ; i < num ; i ++ )
if ( gcd ( num , i ) > 1 )
{
flag = false;
break;
}
if ( flag ) sum+=num;
}
printf ( "%d\n" , sum );
}
}

44
HDOJ/5151_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
using namespace std;
#define INF 1000000000
#define N 111
typedef __int64 LL;
const LL mod = 1000000007;
LL dp[N][N], C[N][N];
int n, num[N];
void init() {
for(int i = 0; i < N; i++) {
for(int j = 0; j <= i; j++) {
if(j == 0 || j == i) C[i][j] = 1;
else C[i][j] = (C[i-1][j-1] + C[i-1][j]) % mod;
}
}
}
LL dfs(int L, int R) {
if(dp[L][R] != -1) return dp[L][R];
if(L == R) return dp[L][R] = 1;
dp[L][R] = (dfs(L+1, R) + dfs(L, R - 1)) % mod;
for(int i = L+1; i <= R-1; i ++) {
if(num[i-1] != num[i+1]) continue;
dp[L][R] = (dp[L][R] + (dfs(L, i - 1) * dfs(i+1, R) % mod) * C[R- L][i - L]%mod) % mod;
}
return dp[L][R];
}
int main() {
init();
while(scanf("%d", &n) != EOF) {
memset(dp, -1, sizeof(dp));
for(int i = 1;i <= n; i++) scanf("%d", &num[i]);
printf("%I64d\n", dfs(1, n));
}
return 0;
}

134
HDOJ/5152_autoAC.cpp Normal file
View File

@ -0,0 +1,134 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long LL ;
#define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define clr( a , x ) memset ( a , x , sizeof a )
#define cpy( a , x ) memcpy ( a , x , sizeof a )
#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define lson ls , l , m
#define rson rs , m + 1 , r
#define rt o , l , r
#define root 1 , 1 , n
#define mid ( ( l + r ) >> 1 )
const int MAXN = 100005 ;
const int MAXE = 100005 ;
const int mod = 2333333 ;
struct Edge {
LL v ;
int n ;
Edge () {}
Edge ( LL v , int n ) : v ( v ) , n ( n ) {}
} ;
int mo[19] = { 2333333 , 2196720 , 580608 , 165888 , 55296 , 18432 , 6144 , 2048 , 1024 , 512 , 256 , 128 , 64 , 32 , 16 , 8 , 4 , 2 , 1 } ;
Edge E[MAXE] ;
int H[MAXN] , cntE ;
int sum[MAXN << 2] ;
int len[MAXN << 2] ;
LL add[MAXN << 2] ;
int n , m ;
void clear () {
cntE = 0 ;
clr ( H , -1 ) ;
}
void addedge ( int u , LL v ) {
E[cntE] = Edge ( v , H[u] ) ;
H[u] = cntE ++ ;
}
int pow ( int a , int b , int mod ) {
int res = 1 ;
while ( b ) {
if ( b & 1 ) res = ( LL ) res * a % mod ;
a = ( LL ) a * a % mod ;
b >>= 1 ;
}
return res % mod ;
}
void pushdown ( int o ) {
if ( add[o] ) {
add[ls] += add[o] ;
add[rs] += add[o] ;
sum[ls] = ( sum[ls] + add[o] % mod * len[ls] ) % mod ;
sum[rs] = ( sum[rs] + add[o] % mod * len[rs] ) % mod ;
add[o] = 0 ;
}
}
void build ( int o , int l , int r ) {
add[o] = 0 ;
len[o] = r - l + 1 ;
if ( l == r ) {
scanf ( "%d" , &sum[o] ) ;
addedge ( l , sum[o] ) ;
sum[o] %= mod ;
return ;
}
int m = mid ;
build ( lson ) ;
build ( rson ) ;
sum[o] = ( sum[ls] + sum[rs] ) % mod ;
}
void update ( int L , int R , int v , int o , int l , int r ) {
if ( L <= l && r <= R ) {
add[o] += v ;
sum[o] = ( sum[o] + ( LL ) len[o] * v ) % mod ;
return ;
}
int m = mid ;
pushdown ( o ) ;
if ( L <= m ) update ( L , R , v , lson ) ;
if ( m < R ) update ( L , R , v , rson ) ;
sum[o] = ( sum[ls] + sum[rs] ) % mod ;
}
int calc ( int i , int x ) {
if ( x == 19 ) return 0 ;
LL tmp = E[i].n == -1 ? E[i].v : pow ( 2 , calc ( E[i].n , x + 1 ) , mo[x] ) + E[i].v ;
return tmp < mo[x] ? tmp : tmp % mo[x] + mo[x] ;
}
void modify ( int x , int o , int l , int r ) {
if ( l == r ) {
E[H[l]].v += add[o] ;
add[o] = 0 ;
addedge ( l , 0 ) ;
sum[o] = calc ( H[l] , 0 ) % mod ;
return ;
}
int m = mid ;
pushdown ( o ) ;
if ( x <= m ) modify ( x , lson ) ;
else modify ( x , rson ) ;
sum[o] = ( sum[ls] + sum[rs] ) % mod ;
}
int query ( int L , int R , int o , int l , int r ) {
if ( L <= l && r <= R ) return sum[o] ;
int m = mid ;
pushdown ( o ) ;
if ( R <= m ) return query ( L , R , lson ) ;
if ( m < L ) return query ( L , R , rson ) ;
return ( query ( L , R , lson ) + query ( L , R , rson ) ) % mod ;
}
void solve () {
int op , l , r , x ;
clear () ;
build ( root ) ;
while ( m -- ) {
scanf ( "%d" , &op ) ;
if ( op == 1 ) {
scanf ( "%d%d" , &l , &r ) ;
printf ( "%d\n" , query ( l , r , root ) ) ;
} else if ( op == 2 ) {
scanf ( "%d" , &x ) ;
modify ( x , root ) ;
} else {
scanf ( "%d%d%d" , &l , &r , &x ) ;
update ( l , r , x , root ) ;
}
}
}
int main () {
while ( ~scanf ( "%d%d" , &n , &m ) ) solve () ;
return 0 ;
}

27
HDOJ/5154_autoAC.cpp Normal file
View File

@ -0,0 +1,27 @@
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n, m, u, v, dp[110][110];
while(~scanf("%d%d", &n,&m))
{
memset(dp, 0, sizeof(dp));
for(int i=1; i<=m; i++)
{
scanf("%d%d", &v,&u);
dp[u][v] = 1;
}
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
dp[i][j] = max(dp[i][j], dp[i][k]&dp[k][j]);
int flag = 0;
for(int i=1; i<=n; i++)
if(dp[i][i] == 1) flag = 1;
printf(!flag ? "YES\n" : "NO\n");
}
return 0;
}

45
HDOJ/5155_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define Mod 1000000007
#define lll __int64
using namespace std;
lll c[55][55];
lll dp[55][55];
void calc()
{
for(int i=0;i<=51;i++)
{
c[i][0] = 1;
for(int j=1;j<=i;j++)
c[i][j] = (c[i-1][j-1]+c[i-1][j])%Mod;
}
}
int main()
{
int n,m,i,j,k,t;
calc();
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,0,sizeof(dp));
dp[0][0] = 1;
for(i=1;i<=n;i++)
{
for(k=1;k<=m;k++)
{
for(j=0;j<=k;j++)
{
for(t=max(1,k-j);t<=k;t++)
{
dp[i][k] = (dp[i][k]+dp[i-1][j]*c[m-j][k-j]%Mod*c[j][t-(k-j)]%Mod)%Mod;
}
}
}
}
printf("%I64d\n",dp[n][m]%Mod);
}
return 0;
}

109
HDOJ/5156_autoAC.cpp Normal file
View File

@ -0,0 +1,109 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long LL ;
#define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define clr( a , x ) memset ( a , x , sizeof a )
#define cpy( a , x ) memcpy ( a , x , sizeof a )
const int MAXN = 50005 ;
const int MAXE = 2100005 ;
struct Edge {
int v , n ;
Edge () {}
Edge ( int v , int n ) : v ( v ) , n ( n ) {}
} ;
Edge E[MAXE] ;
int H[MAXN] , cntE ;
int A[MAXN] , B[MAXN << 1] , Q[MAXN] ;
int vis[MAXN] ;
int num[MAXN] ;
int p[MAXN] ;
int n , m ;
void clear () {
cntE = 1 ;
clr ( A , 0 ) ;
clr ( B , 0 ) ;
clr ( Q , 0 ) ;
clr ( H , 0 ) ;
clr ( vis , 0 ) ;
clr ( num , 0 ) ;
}
int find ( int x ) {
int tmp , ans , o = x ;
while ( p[x] != x ) x = p[x] ;
ans = x , x = o ;
while ( p[x] != x ) {
tmp = p[x] ;
p[x] = ans ;
x = tmp ;
}
return ans ;
}
void addedge ( int u , int v , int H[] ) {
E[cntE] = Edge ( v , H[u] ) ;
H[u] = cntE ++ ;
}
void dfs ( int u , int f = 0 ) {
for ( int i = A[u] ; i ; i = E[i].n ) addedge ( E[i].v , u , B ) ;
for ( int i = H[u] ; i ; i = E[i].n ) if ( E[i].v != f ) dfs ( E[i].v , u ) ;
}
void tarjan ( int u , int f = 0 ) {
p[u] = u ;
vis[u] = 1 ;
for ( int i = Q[u] ; i ; i = E[i].n ) if ( vis[E[i].v] ) -- num[find ( E[i].v )] ;
for ( int i = H[u] ; i ; i = E[i].n ) {
int v = E[i].v ;
if ( vis[v] ) continue ;
tarjan ( v , u ) ;
p[v] = u ;
}
for ( int i = H[u] ; i ; i = E[i].n ) {
int v = E[i].v ;
if ( v == f ) continue ;
num[u] += num[v] ;
}
}
void scanf ( int& x , char c = 0 ) {
while ( ( c = getchar () ) < '0' ) ;
x = c - '0' ;
while ( ( c = getchar () ) >= '0' ) x = x * 10 + c - '0' ;
}
void solve () {
int u , v ;
clear () ;
rep ( i , 1 , n ) {
scanf ( u ) ;
scanf ( v ) ;
addedge ( u , v , H ) ;
addedge ( v , u , H ) ;
}
For ( i , 1 , m ) {
scanf ( u ) ;
scanf ( v ) ;
addedge ( u , v , A ) ;
++ num[u] ;
}
dfs ( 1 ) ;
For ( i , 1 , 100000 ) if ( B[i] ) {
u = 0 ;
for ( int j = B[i] ; j ; j = E[j].n ) {
v = E[j].v ;
if ( u ) {
if ( u != v ) {
addedge ( u , v , Q ) ;
addedge ( v , u , Q ) ;
} else -- num[v] ;
}
u = v ;
}
}
tarjan ( 1 ) ;
For ( i , 1 , n ) printf ( "%d%c" , num[i] , i < n ? ' ' : '\n' ) ;
}
int main () {
while ( ~scanf ( "%d%d" , &n , &m ) ) solve () ;
return 0 ;
}

79
HDOJ/5157_autoAC.cpp Normal file
View File

@ -0,0 +1,79 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long LL ;
#define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define clr( a , x ) memset ( a , x , sizeof a )
#define cpy( a , x ) memcpy ( a , x , sizeof a )
#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define lson ls , l , m
#define rson rs , m + 1 , r
#define rt o , l , r
#define root 1 , 1 , n
#define mid ( ( l + r ) >> 1 )
const int MAXN = 100005 ;
const int N = 26 ;
struct Palindromic_Tree {
int next[MAXN][N] ;
int fail[MAXN] ;
int len[MAXN] ;
int num[MAXN] ;
int S[MAXN] , n ;
int last ;
int p ;
int newnode ( int l ) {
rep ( i , 0 , N ) next[p][i] = 0 ;
num[p] = 0 ;
len[p] = l ;
return p ++ ;
}
void init () {
p = 0 ;
newnode ( 0 ) ;
newnode ( -1 ) ;
last = 1 ;
n = 0 ;
S[0] = -1 ;
fail[0] = 1 ;
}
int get_fail ( int v ) {
while ( S[n - len[v] - 1] != S[n] ) v = fail[v] ;
return v ;
}
int add ( int c ) {
c -= 'a' ;
S[++ n] = c ;
int cur = get_fail ( last ) ;
if ( !next[cur][c] ) {
int now = newnode ( len[cur] + 2 ) ;
fail[now] = next[get_fail ( fail[cur] )][c] ;
next[cur][c] = now ;
num[now] = num[fail[now]] + 1 ;
}
last = next[cur][c] ;
return num[last] ;
}
} ;
Palindromic_ T ;
char s[MAXN] ;
LL sum[MAXN] ;
int n ;
LL ans ;
void solve () {
ans = 0 ;
T.init () ;
n = strlen ( s ) ;
sum[n] = 0 ;
rev ( i , n - 1 , 0 ) sum[i] = sum[i + 1] + T.add ( s[i] ) ;
T.init () ;
rep ( i , 0 , n ) ans += sum[i + 1] * T.add ( s[i] ) ;
printf ( "%I64d\n" , ans ) ;
}
int main () {
while ( ~scanf ( "%s" , s ) ) solve () ;
return 0 ;
}

15
HDOJ/5158_autoAC.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
int main()
{
int n,m,i,j;
while(scanf("%d %d",&n,&m)!=EOF)
{
for(i=0,j=-1;i<m;i++)
{
j+=1;
j%=n;
}
printf("%d\n",j);
}
return 1;
}

19
HDOJ/5159_autoAC.cpp Normal file
View File

@ -0,0 +1,19 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
int main()
{
int T,a,b;
scanf("%d",&T);
for (int i=1;i<=T;i++)
{
scanf("%d%d",&a,&b);
double a1=1-pow(1-1.0/a,b);
double x=a;
double ans=x*(x+1)/2*a1;
printf("Case #%d: %.3lf\n",i,ans);
}
}

92
HDOJ/5160_autoAC.cpp Normal file
View File

@ -0,0 +1,92 @@
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
#define LL __int64
const int N=100005;
const int MOD=1000000007;
LL Ext_gcd(LL a,LL b,LL &x,LL &y){
if(b==0) { x=1, y=0; return a; }
LL ret= Ext_gcd(b,a%b,y,x);
y-= a/b*x;
return ret;
}
LL Inv(LL a,int m){
LL d,x,y,t= (LL)m;
d= Ext_gcd(a,t,x,y);
if(d==1) return (x%t+t)%t;
return -1;
}
LL Cm(LL n, LL m, LL p){
LL a=1, b=1;
if(m>n) return 0;
while(m){
a=(a*n)%p;
b=(b*m)%p;
m--;
n--;
}
return (LL)a*Inv(b,p)%p;
}
struct Dt{
LL x;
int cnt;
Dt(){}
Dt(LL _x,int _c){
x=_x;
cnt=_c;
}
};
LL a[N],fro[N],bac[N];
int main(){
int T;
int n,cas=0;
scanf("%d",&T);
while(T--){
memset(fro,0,sizeof(fro));
memset(bac,0,sizeof(bac));
scanf("%d",&n);
for(int i=0;i<n;++i)
scanf("%I64d",&a[i]);
sort(a,a+n);
int len=n,cnt=1;
fro[0]=1;
for(int i=1;i<n;++i){
if(a[i]==a[i-1]){
++cnt;
fro[i]=fro[i-1];
}
else{
fro[i]=fro[i-1]*Cm(len,cnt,MOD)%MOD;
len-=cnt;
cnt=1;
}
}
LL pre=1,cnm=1;
len=cnt=1;
bac[n-1]=1;
for(int i=n-2;i>=0;--i){
++len;
if(a[i]==a[i+1]){
++cnt;
cnm=cnm*len%MOD*Inv(cnt,MOD)%MOD;
bac[i]=pre*cnm%MOD;
}
else{
cnt=1;
cnm=len;
pre=bac[i+1];
bac[i]=pre*cnm%MOD;
}
}
bac[n]=1;
LL ans=0;
for(int i=0;i<n;++i){
ans=(ans+(fro[i]*bac[i+1]%MOD*a[i]%MOD))%MOD;
}
printf("Case #%d: %I64d\n",++cas,ans);
}
return 0;
}

36
HDOJ/5162_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
int t,n,a,b,c;
int q[5],p[5];
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d %d %d",&a,&b,&c);
p[i]=q[i]=max(max(a,b),c);
}
sort(p,p+n,cmp);
for(int i=0;i<n;i++)
if(q[0]==p[i])
printf("%d",i+1);
for(int j=1;j<n;j++)
for(int i=0;i<n;i++)
if(q[j]==p[i])
{
printf(" %d",i+1);
continue;
}
printf("\n");
}
return 0;
}

36
HDOJ/5163_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include<iostream>
#include<cstdio>
using namespace std;
__int64 tt[100005];
__int64 sum[100005];
int main()
{
int t;
scanf("%d",&t);
int n,m;
int x,y;
while(t--)
{
scanf("%d %d",&n,&m);
tt[1]=tt[0]=0;
for(int i=2;i<=n;i++)
{
scanf("%I64d",&tt[i]);
tt[i]=tt[i-1]+tt[i];
}
for(int i=1;i<=m;i++)
{
scanf("%d %d",&x,&y);
int now=(i-1)%n+1;
if(now<=x && x<y)
sum[i]=tt[y]-tt[now];
if(now>x && x<y)
sum[i]=2*tt[n]-tt[now]+tt[y];
if(y<x)
sum[i]=2*tt[n]-tt[now]-tt[y];
}
for(int i=1;i<=m;i++)
printf("%I64d\n",sum[i]);
}
return 0;
}

165
HDOJ/5164_autoAC.cpp Normal file
View File

@ -0,0 +1,165 @@
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 1e9
#define inf (-((LL)1<<40))
#define lson k<<1, L, mid
#define rson k<<1|1, mid+1, R
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define FOPENIN(IN) freopen(IN, "r", stdin)
#define FOPENOUT(OUT) freopen(OUT, "w", stdout)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
template<class T> T CMP_MIN(T a, T b) { return a < b; }
template<class T> T CMP_MAX(T a, T b) { return a > b; }
template<class T> T MAX(T a, T b) { return a > b ? a : b; }
template<class T> T MIN(T a, T b) { return a < b ? a : b; }
template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b; }
typedef __int64 LL;
const int MAXN = 110000;
const int MAXM = 1000006;
const double eps = 1e-10;
const LL MOD = 1000000007;
int T, n, m, s, k, x, y;
int val[MAXM], f[MAXM], last[MAXM];
LL ans;
struct Node {
int up, down;
Node(){}
Node(int _up, int _down) {
int g = GCD(_up, _down);
up = _up / g;
down = _down / g;
}
bool operator == (const Node& A) const {
return up == A.up && down == A.down;
}
bool operator != (const Node& A) const {
return up != A.up || down != A.down;
}
bool operator < (const Node& A) const {
if(up != A.up) return up < A.up;
return down < A.down;
}
};
map<int, map<Node, int> >ch;
vector<Node>e[MAXM];
Node fa[MAXN], chi[MAXN];
void init()
{
s = 1; ans = 0;
ch.clear(); e[0].clear();
mem0(val); mem0(f); mem0(last);
}
void input(int n, Node* a)
{
scanf("%d", &x);
for(int i = 1; i < n; i ++) {
scanf("%d", &y);
a[i - 1] = Node(x, y);
x = y;
}
}
void addEdge()
{
int p = 0;
for(int i = 0; i < k - 1; i ++) {
Node u = chi[i];
if(ch[p][u] == 0) {
e[p].push_back(u);
val[s] = 0;
e[s].clear();
ch[p][u] = s++;
}
p = ch[p][u];
}
val[p] ++;
}
void getFail()
{
queue<int>q;
for(int i = 0; i < e[0].size(); i ++) {
int u = ch[0][e[0][i]];
f[u] = last[u] = 0;
q.push(u);
}
while(!q.empty()) {
int r = q.front(); q.pop();
for(int i = 0; i < e[r].size(); i ++) {
Node c = e[r][i];
int u = ch[r][c];
if(!u) {
ch[r][c] = ch[f[r]][c];
continue;
}
q.push(u);
int v = f[r];
while(v && !ch[v][c]) v = f[v];
f[u] = ch[v][c];
last[u] = val[f[u]] ? f[u] : last[f[u]];
val[u] += val[last[u]];
}
}
}
void getNext()
{
f[0] = f[1] = 0;
for(int i = 1; i < k - 1; i ++) {
int j = f[i];
while(j && chi[i] != chi[j]) j = f[j];
f[i + 1] = chi[i] == chi[j] ? j + 1 : 0;
}
}
LL KMP()
{
if(k == 1) return ans;
getNext();
int j = 0;
for(int i = 0; i < n - 1; i ++) {
while(j && chi[j]!=fa[i]) j = f[j];
if(chi[j] == fa[i]) j ++;
if(j == k - 1) ans ++;
}
return ans;
}
LL ACT()
{
getFail();
int j = 0;
for(int i = 0; i < n - 1; i ++) {
Node c = fa[i];
while(j && !ch[j][c]) j = f[j];
j = ch[j][c];
ans += val[j];
}
return ans;
}
int main()
{
while(~scanf("%d", &T)) while(T--) {
scanf("%d %d", &n, &m);
init();
input(n, fa);
for(int i = 1; i <= m; i ++) {
scanf("%d", &k);
input(k, chi);
if(k > 1) addEdge();
else ans += n;
}
cout<< (m == 1 ? KMP() : ACT()) << endl;
}
return 0;
}

99
HDOJ/5165_autoAC.cpp Normal file
View File

@ -0,0 +1,99 @@
#include<iostream>
#include<cstdio>
#include<vector>
#include<map>
#include<queue>
#include<cstring>
using namespace std;
#define MP make_pair
#define PB push_back
const int N=10005;
vector<int> mp[N],rv[N];
map< pair<int,int>,int > vis;
int f[N],p2i[N][N];
int dp[N*N][2];
int n,m;
struct Dt{
int v,turn;
Dt(){}
Dt(int _v,int _turn){ v=_v; turn=_turn; }
};
queue<Dt> que;
void init(){
int id=0;
for(int i=1;i<=n;++i)
for(int j=i;j<=n;++j)
p2i[j][i]=p2i[i][j]=id++;
for(int i=0;i<id;++i){
mp[i].clear();
rv[i].clear();
}
vis.clear();
for(int i=0;i<id;++i){
dp[i][0]=dp[i][1]=0;
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&m);
init();
for(int i=0;i<m;++i){
for(int j=1;j<=n;++j)
scanf("%d",&f[j]);
for(int j=1;j<=n;++j)
for(int k=j+1;k<=n;++k){
int fr=p2i[j][k];
int to=p2i[f[j]][f[k]];
if(vis[MP(fr,to)]==0){
mp[fr].PB(to);
rv[to].PB(fr);
vis[MP(fr,to)]=1;
}
}
}
for(int i=1;i<=n;++i){
que.push(Dt(p2i[i][i],0));
que.push(Dt(p2i[i][i],1));
dp[p2i[i][i]][0]=dp[p2i[i][i]][1]=1;
}
while(!que.empty()){
Dt now=que.front();
que.pop();
if(now.turn==0){
int to=now.v;
for(int i=0;i<rv[to].size();++i){
int fr=rv[to][i];
if(dp[fr][1]==0){
int st=1;
for(int i=0;i<mp[fr].size();++i)
st&=dp[mp[fr][i]][0];
dp[fr][1]=st;
if(dp[fr][1])
que.push(Dt(fr,1));
}
}
}
else{
int to=now.v;
for(int i=0;i<rv[to].size();++i){
int fr=rv[to][i];
if(dp[fr][0]==0){
dp[fr][0]=1;
que.push(Dt(fr,0));
}
}
}
}
int ans=1;
for(int i=1;i<=n;++i){
for(int j=i;j<=n;++j){
ans&=dp[p2i[i][j]][0];
}
}
if(ans) puts("YES");
else puts("NO");
}
return 0;
}

74
HDOJ/5166_autoAC.cpp Normal file
View File

@ -0,0 +1,74 @@
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 10000+5
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
bool cmp(int a,int b){
return a>b;
};
int a[1004];
int b[2];
int main()
{
int t,n,mark;
cin>>t;
while(t--)
{
mark=0;
cle(a);
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1);
cle(b);
int j=0;
for(int i=n-1;i>=1;i--)
{
if(a[i+1]-a[i]==3)
{
mark=1;cout<<a[i]+1<<" "<<a[i]+2<<endl;break;
}
if(a[i+1]-a[i]==2)
{
b[j++]=a[i]+1;
}
if(a[i+1]-a[i]==1)continue;
}
if(j==2)
{
cout<<b[1]<<" "<<b[0]<<endl;
}
else if(j==0&&!mark)
{
if(a[1]>2)cout<<a[1]-2<<" "<<a[1]-1<<endl;
else cout<<a[n]+1<<" "<<a[n]+2<<endl;
}
else if(j==1)
{
if(a[1]>1)cout<<a[1]-1<<" "<<b[0]<<endl;
else cout<<b[0]<<" "<<a[n]+1<<endl;
}
}
return 0;
}

48
HDOJ/5167_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include<stdio.h>
#include<math.h>
#include<string.h>
long long fib[50], ans[50];
int num, flag, sum, cnt;
int DFS(int n, int p) {
if (n == 1) {
flag = 1;
return 1;
}
for (int i = p; i < cnt; i++) {
if (n % ans[i] == 0 && DFS(n / ans[i], i)) {
flag = 1;
return 1;
}
}
return 0;
}
int main() {
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i < 46; i++) {
fib[i] = fib[i - 1] + fib[i - 2];
}
int T;
scanf("%d", &T);
while (T--) {
scanf("%d", &num);
flag = cnt = 0;
if (num == 0) {
printf("Yes\n");
continue;
}
for (int i = 3; fib[i] <= num; i++) {
if (num == fib[i]) {
flag = 1;
break;
}
if (num % fib[i] == 0) {
ans[cnt++] = fib[i];
}
}
if (!flag) DFS(num, 0);
if (flag) printf("Yes\n");
else printf("No\n");
}
return 0;
}

86
HDOJ/5168_autoAC.cpp Normal file
View File

@ -0,0 +1,86 @@
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <vector>
#define LL __int64
using namespace std;
const LL INF=(LL)0x3f3f3f3f*0x3f3f3f3f;
const int N=100010;
const int M=200020;
struct TEdge{
int u,v,w;
bool operator<(const TEdge& a)const {
if(w<a.w) return true;
return false;
}
}Cedge[M];
int tot;
struct Point{
int lw;
LL alw;
Point(int c,LL e){lw=c; alw=e;}
};
vector<Point>point[N];
int n,m,k;
void addedge(int u,int v,int w){
Cedge[tot].u=u;
Cedge[tot].v=v;
Cedge[tot].w=w;
tot++;
}
void Push_into(int p,Point t){
point[p].push_back(t);
}
bool cmp(Point a,Point b){
if(a.lw==b.lw) return a.alw>b.alw;
return a.lw<b.lw;
}
int main(){
int T,u,v,w,sz;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&k);
tot=0;
for(int i=1;i<=n;i++){
point[i].clear();
}
for(int i=1;i<=m;i++){
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
}
sort(Cedge,Cedge+tot);
for(int i=0;i<tot;i++){
u=Cedge[i].u;
v=Cedge[i].v;
if(u==1){
if(point[v].empty()){
Push_into(v,Point(Cedge[i].w,Cedge[i].w));
}
else{
if(point[v][point[v].size()-1].alw>Cedge[i].w){
Push_into(v,Point(Cedge[i].w,Cedge[i].w));
}
}
continue;
}
if(point[u].empty()) continue;
else {
int p=upper_bound(point[u].begin(),point[u].end(),Point(Cedge[i].w-k,-INF),cmp)-point[u].begin();
if(p==0) continue;
else if(p>0 && p<point[u].size()) --p;
else if(point[u][point[u].size()-1].lw<=Cedge[i].w-k) p=point[u].size()-1;
else continue;
if(point[v].empty())
Push_into(v,Point(Cedge[i].w,point[u][p].alw+(LL)Cedge[i].w));
else if(point[v][point[v].size()-1].alw>point[u][p].alw+(LL)Cedge[i].w)
Push_into(v,Point(Cedge[i].w,point[u][p].alw+(LL)Cedge[i].w));
}
}
if(point[n].empty())
printf("-1\n");
else printf("%I64d\n",point[n][point[n].size()-1].alw);
}
return 0;
}

25
HDOJ/5170_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include <stdio.h>
#include <math.h>
#define eps 1e-12
int main()
{
double a,b,c,d;
while(scanf("%lf%lf%lf%lf",&a,&b,&c,&d)!=EOF)
{
double s,s1;
s=b*log(a);
s1=d*log(c);
if(a==1 &&c==1)
{
printf("=\n");
continue;
}
if(fabs(s-s1)<eps)
printf("=\n");
else if(s>s1)
printf(">\n");
else if(s<s1)
printf("<\n");
}
return 0;
}

77
HDOJ/5171_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define LL _int64
#define mod 10000007
int a[100010];
struct matrix
{
LL m[3][3];
};
matrix mul(matrix x, matrix y)
{
matrix temp;
memset(temp.m, 0, sizeof(temp.m));
int i, j, k;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (x.m[i][j] == 0)
{
continue;
}
for (k = 0; k < 3; k++)
{
if (y.m[j][k] == 0)
{
continue;
}
temp.m[i][k] = (temp.m[i][k] + (x.m[i][j] * y.m[j][k]) % mod) % mod;
}
}
}
return temp;
}
matrix quickpow(matrix a, int n)
{
matrix res;
int i;
memset(res.m, 0, sizeof(res.m));
for (i = 0; i < 3; i++)
{
res.m[i][i] = 1;
}
while (n)
{
if (n & 1)
{
res = mul(res,a);
}
n >>= 1;
a = mul(a, a);
}
return res;
}
int main()
{
int n, k;
while ((scanf("%d%d", &n, &k)) != EOF)
{
LL sum = 0;
for (int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
sum += a[i];
}
sort(a, a + n);
matrix ans;
ans.m[0][0] = 1; ans.m[0][1] = 1; ans.m[0][2] = 1;
ans.m[1][0] = 0; ans.m[1][1] = 1; ans.m[1][2] = 1;
ans.m[2][0] = 0; ans.m[2][1] = 1; ans.m[2][2] = 0;
ans = quickpow(ans, k);
printf("%d\n", (ans.m[0][0] * sum + ans.m[0][1] * a[n - 1] + ans.m[0][2] * a[n - 2]) % mod);
}
return 0;
}

72
HDOJ/5172_autoAC.cpp Normal file
View File

@ -0,0 +1,72 @@
#include <iostream>
#include <string.h>
#include <math.h>
#include <queue>
#include <algorithm>
#include <stdlib.h>
#include <map>
#include <set>
#include <stdio.h>
using namespace std;
#define LL __int64
#define pi acos(-1.0)
const int mod=1e9+7;
const int INF=0x3f3f3f3f;
const double eqs=1e-9;
#define root 1, n, 1
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1
int a[1000002], pre[1000002], pos[1000002];
int Max[4000000];
LL sum[1000002];
void PushUp(int rt)
{
Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);
}
void build(int l, int r, int rt)
{
if(l==r){
Max[rt]=pre[l];
return ;
}
int mid=l+r>>1;
build(lson);
build(rson);
PushUp(rt);
}
int Query(int ll, int rr, int l, int r, int rt)
{
if(ll<=l&&rr>=r){
return Max[rt];
}
int mid=l+r>>1, ans=-1;
if(ll<=mid) ans=max(ans,Query(ll,rr,lson));
if(rr>mid) ans=max(ans,Query(ll,rr,rson));
return ans;
}
int main()
{
int n, m, i, j, x, l, r, ans;
while(scanf("%d%d",&n,&m)!=EOF){
memset(pos,-1,sizeof(pos));
sum[0]=0;
for(i=1;i<=n;i++){
scanf("%d",&x);
pre[i]=pos[x];
pos[x]=i;
sum[i]=sum[i-1]+x;
}
build(root);
while(m--){
scanf("%d%d",&l,&r);
if(sum[r]-sum[l-1]!=(LL)(r-l+1)*(r-l+2)/2){
puts("NO");
continue ;
}
ans=Query(l,r,root);
if(ans<l) puts("YES");
else puts("NO");
}
}
return 0;
}

50
HDOJ/5174_autoAC.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define ll long long
int const MAX = 2147483647;
ll a[105], b[105];
int re[105];
int main()
{
int n, ca = 1;
while(scanf("%d", &n) != EOF)
{
memset(re ,0 , sizeof(re));
int ans = 0;
for(int i = 1; i <= n; i++)
scanf("%I64d", &a[i]);
sort(a + 1, a + n + 1);
int cnt = 0, pos = 1;
a[n + 1] = -1;
for(int i = 1; i <= n; i++)
{
if(a[i] == a[i + 1])
cnt++;
else
{
b[pos] = a[i];
re[pos++] = cnt;
if(i != n)
cnt = 0;
}
}
if(cnt == n - 1)
{
printf("Case #%d: -1\n", ca++);
continue;
}
b[0] = b[pos-1];
b[pos] = b[1];
re[0] = re[pos-1];
re[pos] = re[1];
for(int i = 1; i < pos; i++)
if((b[i] + b[i - 1]) % MAX == b[i + 1])
{
ans ++;
ans += re[i];
}
printf("Case #%d: %d\n", ca++, ans);
}
}

61
HDOJ/5175_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=101000;
typedef long long LL;
LL fac[maxn],ans[maxn];
LL N;
int cnt;
int cas;
void init()
{
LL tmp=sqrt(N);
cnt=0;
for(LL i=1;i<=tmp;i++)
if(N%i==0)
{
fac[cnt++]=i;
if(i*i!=N){fac[cnt++]=N/i;}
}
}
LL gcd(LL a,LL b)
{
if(b==0)return a;
return gcd(b,a%b);
}
void solve()
{
int num=0;
for(int i=0;i<cnt;i++)
if((gcd(N,N^fac[i])==fac[i])&&((N^fac[i])<=N)&&((N^fac[i])!=0)&&(fac[i]!=N))
ans[num++]=(N^fac[i]);
sort(ans,ans+num);
bool first=true;
printf("Case #%d:\n%d\n",cas++,num);
for(int i=0;i<num;i++)
{
if(first)first=false;
else printf(" ");
printf("%I64d",ans[i]);
}
printf("\n");
}
int main()
{
cas=1;
while(scanf("%I64d",&N)!=EOF)
{
init();
solve();
}
return 0;
}

88
HDOJ/5176_autoAC.cpp Normal file
View File

@ -0,0 +1,88 @@
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 200010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
#pragma comment(linker,"/STACK:1024000000,1024000000")
using namespace std;
int num[N],fa[N],n;
inline int read() {
int x=0; char ch=getchar();
while(ch<'0'||ch>'9'){ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x;
}
struct edge
{
int u,v,w;
}e[N];
int cmp1(edge a,edge b)
{
return a.w<b.w;
}
int cmp2(edge a,edge b)
{
return a.w>b.w;
}
void init()
{
for(int i=1;i<=n;i++)fa[i]=i,num[i]=1;
}
int find(int x)
{
return fa[x]==x?x:fa[x]=find(fa[x]);
}
int main()
{
int cas=1;
while(scanf("%d",&n)>0)
{
for(int i=1;i<n;i++)
{
e[i].u=read();
e[i].v=read();
e[i].w=read();
}
init();sort(e+1,e+n,cmp1);
unsigned long long maxsum=0,t=1;
for(int i=1;i<n;i++)
{
int pa=find(e[i].u);
int pb=find(e[i].v);
if(pa==pb)continue;
maxsum+=t*num[pa]*num[pb]*e[i].w;
fa[pa]=pb;
num[pb]+=num[pa];
}
unsigned long long minsum=0;
init();sort(e+1,e+n,cmp2);
for(int i=1;i<n;i++)
{
int pa=find(e[i].u);
int pb=find(e[i].v);
if(pa==pb)continue;
minsum+=t*num[pa]*num[pb]*e[i].w;
fa[pa]=pb;
num[pb]+=num[pa];
}
printf("Case #%d: ",cas++);
cout<<maxsum-minsum<<endl;
}
}

46
HDOJ/5178_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int kase;
scanf ("%d", &kase);
while (kase--)
{
int n, k;
long long ans = 0;
int num[100200], pos[100200];
scanf ("%d %d", &n, &k);
for (int i = 0; i < n; i++)
{
scanf ("%d", &num[i]);
}
sort (num, num + n);
for (int i = 0; i < n; i++)
{
if (i == 0)
{
for (int j = i + 1; j <= n; j++)
if ( (j < n && num[j] - num[i] > k) || (j == n))
{
pos[i] = j - 1;
ans += j - i - 1;
break;
}
}
else
{
for (int j = pos[i - 1]; j <= n; j++)
if ( (j < n && num[j] - num[i] > k) || j == n)
{
pos[i] = j - 1;
ans += j - i - 1;
break;
}
}
}
printf ("%I64d\n", ans);
}
return 0;
}

52
HDOJ/5179_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
using namespace std;
int dp[25][25][3];
int bit[25];
int dfs(int pos,int pre,int flag,int z)
{
if(pos==0)return 1;
if( !flag && dp[pos][pre][z]!=-1)return dp[pos][pre][z];
int end=flag?bit[pos]:9;
int i=0,ans=0;
if(z==0)i=1;
for(;i<=end;i++)
{
if(pre==0)
ans+=dfs(pos-1,i,flag&&(i==end),z&&(i==0));
else
{
if( pre>=i && pre%i==0)ans+=dfs(pos-1,i,flag&&(i==end),z&&(i==0));
}
}
if(!flag)dp[pos][pre][z]=ans;
return ans;
}
int cal(int x)
{
int len=0;
memset(bit,0,sizeof(bit));
memset(dp,-1,sizeof(dp));
while(x)
{
bit[++len]=x%10;
x/=10;
}
return dfs(len,0,1,1);
}
int main()
{
int l,r;
int T;
cin>>T;
while(T--)
{
scanf("%d%d",&l,&r);
int s1=cal(r);
int s2=cal(l-1);
printf("%d\n",s1-s2);
}
return 0;
}

36
HDOJ/5182_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include<iostream>
#include<algorithm>
#define maxn 100+5
using namespace std;
struct stu
{
int id,a,b,c;
};
stu mapp[maxn];
bool cmp(stu x,stu y)
{
if(x.c==y.c)
{
if(x.b==y.b) return x.id<y.id;
return x.b<y.b;
}
return x.c>y.c;
}
int main()
{
int n;
while(cin>>n)
{
for(int i=0;i<n;i++)
{
cin>>mapp[i].a>>mapp[i].b;
mapp[i].c=mapp[i].a-mapp[i].b;
mapp[i].id=i;
}
sort(mapp,mapp+n,cmp);
cout<<mapp[0].id;
for(int i=1;i<n;i++) cout<<" "<<mapp[i].id;
cout<<endl;
}
return 0;
}

95
HDOJ/5183_autoAC.cpp Normal file
View File

@ -0,0 +1,95 @@
#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 100000000
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 1000010
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
inline LL read()
{
char ch=getchar();LL x=0,f=1;
while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int MAXN=1000010;
const int HASH=1000007;
struct HASHMAP
{
int head[HASH],next[MAXN],size;
LL state[MAXN];
void init()
{
size=0;
memset(head,-1,sizeof(head));
}
bool check(LL val)
{
int h=(val%HASH+HASH)%HASH;
for(int i=head[h];~i;i=next[i])
if(state[i]==val)return true;
return false;
}
int insert(LL val)
{
int h=(val%HASH+HASH)%HASH;
for(int i=head[h];~i;i=next[i])
{
if(val==state[i])
return 1;
}
state[size]=val;
next[size]=head[h];
head[h]=size++;
return 0;
}
}H1,H2;
LL a[N];
int main()
{
int T,n,k,cas=1;
T=read();
while(T--)
{
n=read();k=read();
for(int i=0;i<n;i++)a[i]=read();
LL sum=0;
H1.init();H2.init();
H1.insert(0);H2.insert(0);
int flag=0;
for(int i=n-1;i>=0&&!flag;i--)
{
if(i&1)sum-=a[i];
else sum+=a[i];
if(i%2==0)
{
if(H1.check(sum-k))flag=1;
}
else
{
if(H2.check(-sum-k))flag=1;
}
H1.insert(sum);
H2.insert(-sum);
}
printf("Case #%d: ",cas++);
if(flag)puts("Yes.");
else puts("No.");
}
}

63
HDOJ/5184_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cctype>
#include <algorithm>
#define LL __int64
using namespace std;
const LL N=1000005;
const LL MOD=1000000007;
LL mod[N];
LL ny[N];
char str[N];
int n;
LL C(int a,int b){
if(b<0 || a<0 || a<b) return 0;
return mod[a]*ny[b]%MOD*ny[a-b]%MOD;
}
int for_ny(int a,int m){
int p=1,q=0,b=m,c,d;
while(b>0){
c=a/b;
d=a; a=b; b=d%b;
d=p; p=q; q=d-c*q;
}
return p<0?p+m:p;
}
int main(){
mod[0]=1;
ny[0]=for_ny(mod[0],MOD);
for(int i=1;i<N;i++){
mod[i]=(mod[i-1]*i)%MOD;
ny[i]=(LL)for_ny((int)mod[i],(int)MOD);
}
while(scanf("%d",&n)!=EOF){
getchar();
scanf("%s",str);
if(n%2){
puts("0");
continue;
}
int len=strlen(str);
int p=0,q=0,i;
for(i=0;i<len;i++){
if(str[i]=='(') p++;
if(str[i]==')') q++;
if(q>p) break;
}
if(i<len){
puts("0");
continue;
}
n/=2;
q=n-q,p=n-p;
if(q<0||p<0){
puts("0");
continue;
}
int m=p+q;
LL ans=(C(m,q)-C(m,q+1)+MOD)%MOD;
printf("%I64d\n",ans);
}
return 0;
}

28
HDOJ/5185_autoAC.cpp Normal file
View File

@ -0,0 +1,28 @@
#include<stdio.h>
#include<string.h>
#include<math.h>
#define ll long long
#define max(a,b) a>b?a:b
int dp[320][50005];
int main(){
int T;
while(scanf("%d",&T)!=EOF){
for(int q=1;q<=T;q++){
int n,mod,ans=0;
scanf("%d%d",&n,&mod);
memset(dp,0,sizeof(dp));
int m=(sqrt(8.0*n+1)-1)/2;
int i,j;
dp[0][0]=1;
for(i=1;i<=m;i++){
for(j=0;j<=n;j++){
if(i>j)dp[i][j]=0;
else dp[i][j]=(dp[i-1][j-i]+dp[i][j-i])%mod;
if(j==n)ans=(ans+dp[i][j])%mod;
}
}
printf("Case #%d: %d\n",q,ans);
}
}
return 0;
}

53
HDOJ/5186_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
int ans[300];
char str[300];
int n,b;
map<char,int>toINT;
map<int,char>toCHAR;
int i,j;
void init()
{
for( i = 0;i< 10;++i )
{
toINT[ '0' + i ] = i;
toCHAR[ i ] = '0' +i;
}
for( i = 0; i< 26; ++i )
{
toINT[ 'a' + i ] = i+10;
toCHAR[ i + 10 ] = 'a' + i;
}
}
int main()
{
init();
while( ~scanf("%d %d",&n,&b) )
{
memset(ans,0,sizeof(ans));
int ml = 0;
str[0] = '\0';
while( n-- )
{
scanf("%s",str);
int len = strlen(str)-1;
ml = ml > len? ml : len;
for( i = len ,j=0; i>=0 ;--i,j++ )
ans[ j ] = ( toINT[str[i]] + ans[j] ) %b;
}
for( i = ml; i>=0 ; --i )
if( ans[i] !=0)
break;
if( i < 0)
puts("0");
else
{
for(;i>=0;--i)
putchar( toCHAR[ ans[i] ] );
puts("");
}
}
}

39
HDOJ/5187_autoAC.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
using namespace std;
LL n, p;
LL multi(LL a, LL b) {
LL ret = 0;
while(b) {
if(b & 1) ret = (ret + a) % p;
a = (a + a) % p;
b >>= 1;
}
return ret;
}
LL powmod(LL a, LL b) {
LL ret = 1;
while(b) {
if(b & 1) ret = multi(ret, a) % p;
a = multi(a, a) % p;
b >>= 1;
}
return ret;
}
int main() {
while(cin >> n >> p) {
if(p == 1) {
cout << 0 << endl;
} else if(n == 1) {
cout << 1 << endl;
} else {
LL ans = powmod(2, n) - 2;
if(ans < 0) ans += p;
cout << ans << endl;
}
}
return 0;
}

72
HDOJ/5188_autoAC.cpp Normal file
View File

@ -0,0 +1,72 @@
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<ctype.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define B(x) (1<<(x))
typedef long long ll;
void cmax(ll& a,ll b){ if(b>a)a=b; }
const int oo=0x3f3f3f3f;
const ll OO=1LL<<61;
const int MOD=2012;
const int maxt=3100000;
const int maxn=40;
map<int,ll>dp[2];
struct Node{
int v,t,l;
}a[maxn];
bool cmp(Node x,Node y){
return x.l-x.t<y.l-y.t;
}
int main(){
int n,w,now,pre,ans;
map<int,ll>::iterator it;
ll sum;
while(scanf("%d %d",&n,&w)!=EOF){
memset(dp,0,sizeof dp);
sum=0;
for(int i=1;i<=n;i++){
scanf("%d %d %d",&a[i].t,&a[i].v,&a[i].l);
sum+=a[i].v;
}
if(sum<w){
printf("zhx is naive!\n");
continue;
}
sort(a+1,a+1+n,cmp);
for(int i=0;i<2;i++)dp[i].clear();
now=0;
pre=1;
dp[now][0]=0;
for(int i=1;i<=n;i++){
now^=1;
pre^=1;
dp[now].clear();
for(it=dp[pre].begin();it!=dp[pre].end();++it){
int u=it->first;
ll dp_pre=it->second;
cmax(dp[now][u],dp_pre);
int uu=max(a[i].l,u+a[i].t);
cmax(dp[now][uu],dp_pre+a[i].v);
}
}
ans=-1;
for(it=dp[now].begin();it!=dp[now].end();++it){
if(it->second>=w){
ans=it->first;
break;
}
}
if(ans!=-1)
printf("%d\n",ans);
else
puts("zhx is naive!");
}
return 0;
}

26
HDOJ/5190_autoAC.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#define INF 0x3fffffff
using namespace std;
struct node {
int a, b;
}dian[105];
int main() {
int n, m;
while(scanf("%d %d", &n, &m) != EOF) {
for(int i = 0; i < m; i++) {
scanf("%d %d", &dian[i].a, &dian[i].b);
}
int ans = INF;
for(int i = 0; i<m; i++) {
int t = n / dian[i].a;
if(n % dian[i].a) t++;
if(t * dian[i].b < ans) ans = t * dian[i].b;
}
printf("%d\n", ans);
}
return 0;
}

63
HDOJ/5191_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<cmath>
using namespace std;
long long n,w,h;
long long sums[300600];
long long sumout[350560];
long long mins=50000*50000;
int main()
{
while(~scanf("%I64d%I64d%I64d",&n,&w,&h))
{
int aa;
long long sum=0;
long long sout=0;
for(int i=0;i<=n+w+w+1;i++)
{
sums[i]=sumout[i]=0;
}
mins=1234567891234;
for(int i=w;i<w+n;i++)
{
scanf("%d",&aa);
if(aa>h)
sout=sout+aa-h;
sumout[i]=sout;
sum=sum+aa;
sums[i]=sum;
}
for(int i=n+w;i<=n+w+w+1;i++) //娉ㄦ涓涓㈢sum涔瑕存
{
sums[i]=sums[i-1];
sumout[i]=sumout[i-1];
}
if(sum<w*h)
{
printf("-1\n");
continue;
}
for(int i=1;i<=n+w-1;i++)
{
int tt=i+w-1;
int ss=i-1;
long long allout=sumout[tt]-sumout[ss];
long long allsum=sums[tt]-sums[ss];
if(allsum>=w*h)
{
if(allout<mins)mins=allout;
}
else
{
if(allout+w*h-allsum<mins)mins=allout+w*h-allsum;
}
}
printf("%I64d\n",mins);
}
return 0;
}

142
HDOJ/5192_autoAC.cpp Normal file
View File

@ -0,0 +1,142 @@
#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll __int64
#define ull unsigned long long
#define eps 1e-8
#define NMAX 1000000000
#define MOD 1000000
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
template<class T>
inline void scan_d(T &ret)
{
char c;
int flag = 0;
ret=0;
while(((c=getchar())<'0'||c>'9')&&c!='-');
if(c == '-')
{
flag = 1;
c = getchar();
}
while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
if(flag) ret = -ret;
}
const int maxn = 50000+10;
ll a[maxn*3], t1[maxn], t2[maxn];
inline int lowbit(int x)
{
return x&(-x);
}
void add(int x, ll k, int flag)
{
while(x <= maxn)
{
if(flag) t1[x] += k;
else t2[x] += k;
x += lowbit(x);
}
}
ll sum(int x, int flag)
{
ll ret = 0;
while(x)
{
if(flag) ret += t1[x];
else ret += t2[x];
x -= lowbit(x);
}
return ret;
}
inline void update(ll *a, ll b, ll h)
{
if(a[0] < 0 || a[0] > b || (a[0] == b && a[1] < h))
{
a[0] = b;
a[1] = h;
}
}
int main()
{
int n,h;
ll w;
while(~scanf("%d%I64d%d",&n,&w,&h))
{
memset(t1,0,sizeof(t1));
memset(t2,0,sizeof(t2));
ll all = 0;
for(int i = 1; i <= w; i++)
a[i] = a[i+w+n] = 0;
for(int i = w+1; i <= w+n; i++)
{
scanf("%I64d",&a[i]);
all += a[i];
}
for(int i = 1; i < w; i++)
add(1,1,0);
ll suma = 0,ans[2];
ans[0] = -1;
for(int i = w; i <= n+w+w; i++)
{
int p = (a[i] == 0) ? 1 : a[i];
add(p,1,0);
if(i > w)
{
if(i <= n+w) add(p,p,1);
p = (a[i-w] == 0) ? 1 : a[i-w];
add(p,-1,0);
if(i-w > w) add(p,-p,1);
}
suma += a[i]-a[i-w];
ll th = suma/w;
ll tmp[2];
tmp[0] = -1;
if(th >= h)
{
if(th*w <= all)
{
ll ge = sum(th,0),he = sum(th,1);
update(tmp, max(ge*th-he,suma-he-(w-ge)*th),th);
}
}
if(th+1 >= h)
{
if((th+1)*w <= all)
{
ll ge = sum(th+1,0),he = sum(th+1,1);
update(tmp, max(ge*(th+1)-he,suma-he-(w-ge)*(th+1)),th+1);
}
}
if(tmp[0] == -1)
{
if(h*w <= all)
{
ll ge = sum(h,0),he = sum(h,1);
update(tmp, max(ge*h-he,suma-he-(w-ge)*h),h);
}
}
update(ans,tmp[0],tmp[1]);
}
if(ans[0] == -1) printf("-1\n");
else printf("%I64d %I64d\n",ans[1],ans[0]);
}
return 0;
}

164
HDOJ/5193_autoAC.cpp Normal file
View File

@ -0,0 +1,164 @@
#include<cstdio>
#include<cstring>
using namespace std;
const int m=150;
inline int lowbit(int x)
{
return x&-x;
}
void add(int c[],int pos,int x)
{
while(pos<=20000)
{
c[pos]+=x;
pos+=lowbit(pos);
}
}
int sum(int c[],int l,int r)
{
int sum1=0,sum2=0;
l--;
while(l>0)
{
sum1+=c[l];
l-=lowbit(l);
}
while(r>0)
{
sum2+=c[r];
r-=lowbit(r);
}
return sum2-sum1;
}
struct data
{
int s,a[2*m+5];
data *next;
int c[20005];
data()
{
memset(c,0,sizeof(c));
next=NULL;
}
};
data *root;
void insert(int x,int pos)
{
if(root==NULL)
{
root=new data;
root->s=1;
root->a[1]=x;
add(root->c,x,1);
return ;
}
data *k=root;
while(pos>k->s && k->next!=NULL)
{
pos-=k->s;
k=k->next;
}
memmove(k->a+pos+1,k->a+pos,sizeof(int)*(k->s-pos+1));
k->s++;
k->a[pos]=x;
add(k->c,x,1);
if(k->s==2*m)
{
data *t=new data;
t->next=k->next;
k->next=t;
memcpy(t->a+1,k->a+m+1,sizeof(int)*m);
for(int i=1;i<=m;i++)
{
add(k->c,t->a[i],-1);
add(t->c,t->a[i],1);
}
t->s=k->s=m;
}
}
void del(int pos)
{
data *k=root;
while(pos>k->s && k->next!=NULL)
{
pos-=k->s;
k=k->next;
}
add(k->c,k->a[pos],-1);
memmove(k->a+pos,k->a+pos+1,sizeof(int)*(k->s-pos));
k->s--;
}
int find(int pos)
{
data *k=root;
while(pos>k->s && k->next!=NULL)
{
pos-=k->s;
k=k->next;
}
return k->a[pos];
}
int work(int pos)
{
int res=0;
data *k=root;
int x=find(pos);
while(pos>k->s && k->next!=NULL)
{
pos-=k->s;
res+=sum(k->c,x+1,20000);
k=k->next;
}
for(int i=1;i<pos;i++) if(k->a[i]>x) res++;
for(int i=pos+1;i<=k->s;i++) if(k->a[i]<x) res++;
while(k->next!=NULL)
{
k=k->next;
res+=sum(k->c,1,x-1);
}
return res;
}
void destroy(data *k)
{
if(k->next!=NULL) destroy(k->next);
delete k;
}
int main()
{
int n,p;
while(~scanf("%d %d",&n,&p))
{
root=NULL;
int ans=0;
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
insert(x,i);
ans+=work(i);
}
while(p--)
{
int c;
scanf("%d",&c);
if(c==0)
{
int x,y;
scanf("%d %d",&x,&y);
x++;
insert(y,x);
ans+=work(x);
}
else
{
int x;
scanf("%d",&x);
ans-=work(x);
del(x);
}
printf("%d\n",ans);
}
destroy(root);
}
return 0;
}

62
HDOJ/5194_autoAC.cpp Normal file
View File

@ -0,0 +1,62 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)
#define eps 1e-8
#define fre(i,a,b) for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v) memset ((t) , v, sizeof(t))
#define ssf(n) scanf("%s", n)
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf printf
#define bug pf("Hi\n")
using namespace std;
typedef __int64 ll;
#define INF 0x3f3f3f3f
#define N 10005
ll C(ll a,ll b)
{
ll x=1,y=1;
ll i;
b=min(b,a-b);
if(b==0) return 1;
fre(i,1,b+1)
{
x=x*a;
y=y*i;
a--;
}
return x/y;
}
ll fdd(ll x,ll y)
{
if(y==0) return x;
return fdd(y,x%y);
}
int main()
{
ll i,j;
ll n,m;
while(~scanf("%I64d%I64d",&n,&m))
{
j=min(n,m);
ll ans=0;
fre(i,1,j+1)
ans=ans+i*C(n,i)*C(m,i);
ll y=C(n+m,n);
ll x=fdd(ans,y);
pf("%I64d/%I64d\n",ans/x,y/x);
}
return 0;
}

90
HDOJ/5195_autoAC.cpp Normal file
View File

@ -0,0 +1,90 @@
#include "stdio.h"
#include "string.h"
#include "vector"
using namespace std;
const int inf=0x3f3f3f3f;
vector<int>mark[100010];
int key,cnt;
int du[100010];
struct node
{
int x,l,r;
}data[400010];
int Min(int a,int b)
{
if (a<b) return a;
else return b;
}
void build(int l,int r,int k)
{
int mid;
data[k].l=l;
data[k].r=r;
if (l==r)
{
data[k].x=du[data[k].l];
return;
}
mid=(l+r)/2;
build(l,mid,k*2);
build(mid+1,r,k*2+1);
data[k].x=Min(data[k*2].x,data[k*2+1].x);
}
void updata(int x,int k)
{
int mid;
if (data[k].l==data[k].r)
{
data[k].x--;
return;
}
mid=(data[k].l+data[k].r)/2;
if (x<=mid) updata(x,k*2);
else updata(x,k*2+1);
data[k].x=Min(data[k*2].x,data[k*2+1].x);
}
void query(int k)
{
if (data[k].l==data[k].r)
{
cnt-=data[k].x;
key=data[k].l;
data[k].x=inf;
return ;
}
if (data[k*2+1].x<=cnt)
query(k*2+1);
else
query(k*2);
data[k].x=Min(data[k*2].x,data[k*2+1].x);
}
int main()
{
int n,m,i,a,b,x;
while (scanf("%d%d%d",&n,&m,&cnt)!=EOF)
{
memset(du,0,sizeof(du));
for (i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
mark[a].push_back(b);
du[b]++;
}
build(1,n,1);
for (a=1;a<=n;a++)
{
query(1);
if (a==1)
printf("%d",key);
else
printf(" %d",key);
for (i=0;i<mark[key].size();i++)
{
x=mark[key][i];
updata(x,1);
}
}
printf("\n");
}
return 0;
}

85
HDOJ/5196_autoAC.cpp Normal file
View File

@ -0,0 +1,85 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 100005;
typedef long long ll;
int n, m;
ll q;
struct Hash {
int v, id;
void read(int id) {
scanf("%d", &v);
this->id = id;
}
} h[N];
bool cmp(Hash a, Hash b) {
return a.v < b.v;
}
int a[N];
int bit[N], l[N];
inline int lowbit(int x) {return (x&(-x));}
void add(int x, int v) {
while (x <= n) {
bit[x] += v;
x += lowbit(x);
}
}
int get(int x) {
int ans = 0;
while (x) {
ans += bit[x];
x -= lowbit(x);
}
return ans;
}
ll sum[N], sum2[N];
ll ans[N];
int X[N], Y[N];
void gao(ll q, int bo) {
if (q < 0) return;
memset(bit, 0, sizeof(bit));
int r = 0;
ll tot = 0;
for (int i = 1; i <= n; i++) {
while (r <= n && tot <= q) {
r++;
if (r > n) break;
tot += (r - i) - get(a[r]);
add(a[r], 1);
}
l[i] = r - 1;
add(a[i], -1);
tot -= get(a[i] - 1);
}
sum[n + 1] = 0;
for (int i = n; i >= 1; i--)
sum[i] = sum[i + 1] + (l[i] - i + 1);
int x, y;
for (int i = 0; i < m; i++) {
int x = X[i], y = Y[i];
int tmp = lower_bound(l + x, l + y + 1, y) - l - 1;
ans[i] += bo * (sum[x] - sum[tmp + 1] + sum2[y - tmp]);
}
}
int main() {
for (int i = 1; i < N; i++)
sum2[i] = sum2[i - 1] + i;
while (~scanf("%d%d%I64d", &n, &m, &q)) {
for (int i = 1; i <= n; i++)
h[i].read(i);
sort(h + 1, h + n + 1, cmp);
int tmp = 1;
a[h[1].id] = 1;
for (int i = 1; i <= n; i++) {
if (h[i].v != h[i - 1].v) tmp++;
a[h[i].id] = tmp;
}
for (int i = 0; i < m; i++) scanf("%d%d", &X[i], &Y[i]);
memset(ans, 0, sizeof(ans));
gao(q, 1); gao(q - 1, -1);
for (int i = 0; i < m; i++)
printf("%I64d\n", ans[i]);
}
return 0;
}

113
HDOJ/5197_autoAC.cpp Normal file

File diff suppressed because one or more lines are too long

47
HDOJ/5198_autoAC.cpp Normal file
View File

@ -0,0 +1,47 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
int nn,num;
int main(){
char str[100];
int i;
while(~scanf("%s",str)){
nn = 0;
bool flag = true;
int len = strlen(str);
char tmp,a = 0,b = 0,c = 0;
tmp = str[0];
a = tmp;
for(i =0;i < len;i++){
if(tmp==str[i]){
nn++;
}
else {
tmp = str[i];
b = tmp;
break;
}
}
if(nn*3!=len) flag = false;
for(int k = i;i<k+nn;i++){
if(str[i]!=tmp){
flag = false;
break;
}
}
tmp = str[i];
if(tmp == a||tmp == b) flag = false;
for(int k = i;i<k+nn;i++){
if(str[i]!=tmp){
flag = false;
break;
}
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}

48
HDOJ/5199_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include<cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define MAXN 1000010
using namespace std;
struct node{
int id;
int h;
}q[MAXN];
int ans[MAXN];
priority_queue<int,vector<int>,greater<int> > pq;
bool cmp(node a,node b){
if(a.h==b.h) return a.id<b.id;
return a.h<b.h;
}
int n,m;
int main(){
while(scanf("%d%d",&n,&m)==2){
while(!pq.empty()) pq.pop();
int tm;
for(int i=0;i<n;++i)
{
scanf("%d",&tm);
pq.push(tm);
}
for(int i=0;i<m;++i){
scanf("%d",&q[i].h);
q[i].id=i;
}
sort(q,q+m,cmp);
memset(ans,0,sizeof(ans));
for(int i=0;i<m;++i){
int tans=0;
while(!pq.empty()&&pq.top()<q[i].h) pq.pop();
while(!pq.empty()&&i<m&&pq.top()==q[i].h){
tans++;
pq.pop();
}
ans[q[i].id]=tans;
if(pq.empty())
break;
}
for(int i=0;i<m;++i)
printf("%d\n",ans[i]);
}
return 0;
}