HDU1556

题目链接

传送门

题目大意

题意是n次染色,每次染[a,b]区间的。最后询问每块区间被染的次数。

解题思路

树状数组向下查询、向上统计的典型应用。对于每次染色,我们把b之前包含的所有区间+1,a之前包含的所有区间-1,这样就修改了[a,b]区间的染色次数。

参考代码

#include <functional>  
#include <algorithm>  
#include <iostream>  
#include <fstream>  
#include <sstream>  
#include <iomanip>  
#include <numeric>  
#include <cstring>  
#include <climits>  
#include <cassert>  
#include <complex>  
#include <cstdio>  
#include <string>  
#include <vector>  
#include <bitset>  
#include <queue>  
#include <stack>  
#include <cmath>  
#include <ctime>  
#include <list>  
#include <set>  
#include <map>  

//#include <tr1/unordered_set>  
//#include <tr1/unordered_map>  
//#include <array>  

using namespace std;  

#define REP(i, n) for (int i=0;i<n;++i)  
#define FOR(i, a, b) for (int i=a;i<b;++i)  
#define DWN(i, b, a) for (int i=b-1;i>=a;--i)  
#define REP_1(i, n) for (LL i=1;i<=n;++i)  
#define FOR_1(i, a, b) for (int i=a;i<=b;++i)  
#define DWN_1(i, b, a) for (LL i=b;i>=a;--i)  
#define REP_C(i, n) for (int n____=n,i=0;i<n____;++i)  
#define FOR_C(i, a, b) for (int b____=b,i=a;i<b____;++i)  
#define DWN_C(i, b, a) for (int a____=a,i=b-1;i>=a____;--i)  
#define REP_N(i, n) for (i=0;i<n;++i)  
#define FOR_N(i, a, b) for (i=a;i<b;++i)  
#define DWN_N(i, b, a) for (i=b-1;i>=a;--i)  
#define REP_1_C(i, n) for (int n____=n,i=1;i<=n____;++i)  
#define FOR_1_C(i, a, b) for (int b____=b,i=a;i<=b____;++i)  
#define DWN_1_C(i, b, a) for (int a____=a,i=b;i>=a____;--i)  
#define REP_1_N(i, n) for (i=1;i<=n;++i)  
#define FOR_1_N(i, a, b) for (i=a;i<=b;++i)  
#define DWN_1_N(i, b, a) for (i=b;i>=a;--i)  
#define REP_C_N(i, n) for (int n____=(i=0,n);i<n____;++i)  
#define FOR_C_N(i, a, b) for (int b____=(i=0,b);i<b____;++i)  
#define DWN_C_N(i, b, a) for (int a____=(i=b-1,a);i>=a____;--i)  
#define REP_1_C_N(i, n) for (int n____=(i=1,n);i<=n____;++i)  
#define FOR_1_C_N(i, a, b) for (int b____=(i=a,b);i<=b____;++i)  
#define DWN_1_C_N(i, b, a) for (int a____=(i=b,a);i>=a____;--i)  

#define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)  
#define REP_S(i, str) for (char*i=str;*i;++i)  
#define REP_L(i, hd, suc) for (int i=hd;i;i=suc[i])  
#define REP_G(i, u) REP_L(i,hd[u],suc)  
#define REP_SS(x, s) for (int x=s;x;x=(x-1)&s)  
#define DO(n) for ( int ____n = n; ____n-->0; )  
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)  
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)  
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)  
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)  
#define REP_4(i, j, k, ii, n, m, l, nn) REP(i, n) REP(j, m) REP(k, l) REP(ii, nn)  
#define REP_4_1(i, j, k, ii, n, m, l, nn) REP_1(i, n) REP_1(j, m) REP_1(k, l) REP_1(ii, nn)  

#define ALL(A) A.begin(), A.end()  
#define LLA(A) A.rbegin(), A.rend()  
#define CPY(A, B) memcpy(A, B, sizeof(A))  
#define INS(A, P, B) A.insert(A.begin() + P, B)  
#define ERS(A, P) A.erase(A.begin() + P)  
#define LBD(A, x) (lower_bound(ALL(A), x) - A.begin())  
#define UBD(A, x) (lower_bound(ALL(A), x) - A.begin())  
#define CTN(T, x) (T.find(x) != T.end())  
#define SZ(A) int((A).size())  
#define PB push_back  
#define MP(A, B) make_pair(A, B)  
#define PTT pair<T, T>  
#define Ts *this  
#define rTs return Ts  
#define fi first  
#define se second  
#define re real()  
#define im imag()  

#define Rush for(int ____T=RD(); ____T--;)  
#define Display(A, n, m) {                      \  
  REP(i, n){                                    \  
        REP(j, m-1) cout << A[i][j] << " ";     \  
        cout << A[i][m-1] << endl;                \  
    }                                            \  
}  
#define Display_1(A, n, m) {                    \  
    REP_1(i, n){                                \  
        REP_1(j, m-1) cout << A[i][j] << " ";   \  
        cout << A[i][m] << endl;                \  
    }                                            \  
}  

typedef long long LL;  
//typedef long double DB;  
typedef double DB;  
typedef unsigned uint;  
typedef unsigned long long uLL;  

typedef vector<int> VI;  
typedef vector<char> VC;  
typedef vector<string> VS;  
typedef vector<LL> VL;  
typedef vector<DB> VF;  
typedef set<int> SI;  
typedef set<string> SS;  
typedef map<int, int> MII;  
typedef map<string, int> MSI;  
typedef pair<int, int> PII;  
typedef pair<LL, LL> PLL;  
typedef vector<PII> VII;  
typedef vector<VI> VVI;  
typedef vector<VII> VVII;  

template<class T> inline T& RD(T &);  
template<class T> inline void OT(const T &);  
//inline int RD(){int x; return RD(x);}  
inline LL RD(){LL x; return RD(x);}  
inline DB& RF(DB &);  
inline DB RF(){DB x; return RF(x);}  
inline char* RS(char *s);  
inline char& RC(char &c);  
inline char RC();  
inline char& RC(char &c){scanf(" %c", &c); return c;}  
inline char RC(){char c; return RC(c);}  
//inline char& RC(char &c){c = getchar(); return c;}  
//inline char RC(){return getchar();}  

template<class T> inline T& RDD(T &);  
inline LL RDD(){LL x; return RDD(x);}  

template<class T0, class T1> inline T0& RD(T0 &x0, T1 &x1){RD(x0), RD(x1); return x0;}  
template<class T0, class T1, class T2> inline T0& RD(T0 &x0, T1 &x1, T2 &x2){RD(x0), RD(x1), RD(x2); return x0;}  
template<class T0, class T1, class T2, class T3> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3){RD(x0), RD(x1), RD(x2), RD(x3); return x0;}  
template<class T0, class T1, class T2, class T3, class T4> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4); return x0;}  
template<class T0, class T1, class T2, class T3, class T4, class T5> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); return x0;}  
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); return x0;}  
template<class T0, class T1> inline void OT(const T0 &x0, const T1 &x1){OT(x0), OT(x1);}  
template<class T0, class T1, class T2> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2){OT(x0), OT(x1), OT(x2);}  
template<class T0, class T1, class T2, class T3> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3){OT(x0), OT(x1), OT(x2), OT(x3);}  
template<class T0, class T1, class T2, class T3, class T4> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);}  
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);}  
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5, const T6 &x6){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);}  
inline char& RC(char &a, char &b){RC(a), RC(b); return a;}  
inline char& RC(char &a, char &b, char &c){RC(a), RC(b), RC(c); return a;}  
inline char& RC(char &a, char &b, char &c, char &d){RC(a), RC(b), RC(c), RC(d); return a;}  
inline char& RC(char &a, char &b, char &c, char &d, char &e){RC(a), RC(b), RC(c), RC(d), RC(e); return a;}  
inline char& RC(char &a, char &b, char &c, char &d, char &e, char &f){RC(a), RC(b), RC(c), RC(d), RC(e), RC(f); return a;}  
inline char& RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g){RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g); return a;}  
inline DB& RF(DB &a, DB &b){RF(a), RF(b); return a;}  
inline DB& RF(DB &a, DB &b, DB &c){RF(a), RF(b), RF(c); return a;}  
inline DB& RF(DB &a, DB &b, DB &c, DB &d){RF(a), RF(b), RF(c), RF(d); return a;}  
inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e){RF(a), RF(b), RF(c), RF(d), RF(e); return a;}  
inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f){RF(a), RF(b), RF(c), RF(d), RF(e), RF(f); return a;}  
inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f, DB &g){RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g); return a;}  
inline void RS(char *s1, char *s2){RS(s1), RS(s2);}  
inline void RS(char *s1, char *s2, char *s3){RS(s1), RS(s2), RS(s3);}  
template<class T0,class T1>inline void RDD(T0&a, T1&b){RDD(a),RDD(b);}  
template<class T0,class T1,class T2>inline void RDD(T0&a, T1&b, T2&c){RDD(a),RDD(b),RDD(c);}  

template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}  
template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}  
template<class T> inline void CLR(T &A){A.clear();}  

template<class T0, class T1> inline void RST(T0 &A0, T1 &A1){RST(A0), RST(A1);}  
template<class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2){RST(A0), RST(A1), RST(A2);}  
template<class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3){RST(A0), RST(A1), RST(A2), RST(A3);}  
template<class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);}  
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);}  
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);}  
template<class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x){FLC(A0, x), FLC(A1, x);}  
template<class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x);}  
template<class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x);}  
template<class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x);}  
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x);}  
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x), FLC(A6, x);}  
template<class T> inline void CLR(priority_queue<T, vector<T>, less<T> > &Q){while (!Q.empty()) Q.pop();}  
template<class T> inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q){while (!Q.empty()) Q.pop();}  
template<class T> inline void CLR(stack<T> &S){while (!S.empty()) S.pop();}  
template<class T> inline void CLR(queue<T> &Q){while (!Q.empty()) Q.pop();}  

template<class T0, class T1> inline void CLR(T0 &A0, T1 &A1){CLR(A0), CLR(A1);}  
template<class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2){CLR(A0), CLR(A1), CLR(A2);}  
template<class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3){CLR(A0), CLR(A1), CLR(A2), CLR(A3);}  
template<class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);}  
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);}  
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);}  
template<class T> inline void CLR(T &A, int n){REP(i, n) CLR(A[i]);}  

template<class T> inline bool EPT(T &a){return a.empty();}  
template<class T> inline T& SRT(T &A){sort(ALL(A)); return A;}  
template<class T, class C> inline T& SRT(T &A, C B){sort(ALL(A), B); return A;}  
template<class T> inline T& RVS(T &A){reverse(ALL(A)); return A;}  
template<class T> inline T& UNQQ(T &A){A.resize(unique(ALL(A))-A.begin());return A;}  
template<class T> inline T& UNQ(T &A){SRT(A);return UNQQ(A);}  


//}  

/** Constant List .. **/ //{  

const int MOD = int(1e9)+7;  
const int INF = 0x3f3f3f3f;  
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;  
const DB EPS = 1e-9;  
const DB OO = 1e20;  
const DB PI = acos(-1.0); //M_PI;  


//}  

/** Add On .. **/ //{  
// <<= '0. Nichi Joo ., //{  

template<class T> inline T& checkMin(T &a,const T b){if (b<a) a=b;return a;}  
template<class T> inline T& checkMax(T &a,const T b){if (a<b) a=b;return a;}  
template<class T> inline T& checkMin(T &a, T &b, const T x){checkMin(a, x), checkMin(b, x);return a;}  
template<class T> inline T& checkMax(T &a, T &b, const T x){checkMax(a, x), checkMax(b, x);return a;}  
template <class T, class C> inline T& checkMin(T& a, const T b, C c){if (c(b,a)) a = b;return a;}  
template <class T, class C> inline T& checkMax(T& a, const T b, C c){if (c(a,b)) a = b;return a;}  
template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}  
template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}  
template<class T> inline T min(T a, T b, T c, T d){return min(min(a, b), min(c, d));}  
template<class T> inline T max(T a, T b, T c, T d){return max(max(a, b), max(c, d));}  
template<class T> inline T min(T a, T b, T c, T d, T e){return min(min(min(a,b),min(c,d)),e);}  
template<class T> inline T max(T a, T b, T c, T d, T e){return max(max(max(a,b),max(c,d)),e);}  
template<class T> inline T sqr(T a){return a*a;}  
template<class T> inline T cub(T a){return a*a*a;}  
template<class T> inline T ceil(T x, T y){return (x - 1) / y + 1;}  
template<class T> T abs(T x){return x>0?x:-x;}  
inline int sgn(DB x){return x < -EPS ? -1 : x > EPS;}  
inline int sgn(DB x, DB y){return sgn(x - y);}  

inline DB cos(DB a, DB b, DB c){return (sqr(a)+sqr(b)-sqr(c))/(2*a*b);}  
inline DB cot(DB x){return 1./tan(x);};  
inline DB sec(DB x){return 1./cos(x);};  
inline DB csc(DB x){return 1./sin(x);};  

//}  


//基础包。。  
// <<= '2. Number Theory .,//{  
namespace NT{  
#define gcd __gcd  
inline LL lcm(LL a, LL b){return a*b/gcd(a,b);}  

inline void INC(int &a, int b){a += b; if (a >= MOD) a -= MOD;}  
inline int sum(int a, int b){a += b; if (a >= MOD) a -= MOD; return a;}  
/* 模数两倍刚好超 int 时。 
inline int sum(uint a, int b){a += b; a %= MOD;if (a < 0) a += MOD; return a;} 
inline void INC(int &a, int b){a = sum(a, b);} 
*/  

inline void DEC(int &a, int b){a -= b; if (a < 0) a += MOD;}  
inline int dff(int a, int b){a -= b; if (a < 0) a  += MOD; return a;}  
inline void MUL(int &a, int b){a = (LL)a * b % MOD;}  
inline int pdt(int a, int b){return (LL)a * b % MOD;}  

inline int gcd(int m, int n, int &x, int &y){  

    x = 1, y = 0; int xx = 0, yy = 1, q;  

    while (1){  
        q = m / n, m %= n;  
        if (!m){x = xx, y = yy; return n;}  
        DEC(x, pdt(q, xx)), DEC(y, pdt(q, yy));  
        q = n / m, n %= m;  
        if (!n) return m;  
        DEC(xx, pdt(q, x)), DEC(yy, pdt(q, y));  
    }  
}  

inline int sum(int a, int b, int c){return sum(a, sum(b, c));}  
inline int sum(int a, int b, int c, int d){return sum(sum(a, b), sum(c, d));}  
inline int pdt(int a, int b, int c){return pdt(a, pdt(b, c));}  
inline int pdt(int a, int b, int c, int d){return pdt(pdt(a, b), pdt(c, d));}  

inline int pow(int a, LL b){  
    int c(1); while (b){  
        if (b&1) MUL(c, a);  
        MUL(a, a), b >>= 1;  
    }  
    return c;  
}  

template<class T> inline T pow(T a, LL b){  
    T c(1); while (b){  
        if (b&1) c *= a;  
        a *= a, b >>= 1;  
    }  
    return c;  
}  

template<class T> inline T pow(T a, int b){  
    return pow(a, (LL)b);  
}  

inline int _I(int b){  
    int a = MOD, x1 = 0, x2 = 1, q; while (1){  
        q = a / b, a %= b;  
        if (!a) return x2;  
        DEC(x1, pdt(q, x2));  

        q = b / a, b %= a;  
        if (!b) return x1;  
        DEC(x2, pdt(q, x1));  
    }  
}  

inline void DIV(int &a, int b){MUL(a, _I(b));}  
inline int qtt(int a, int b){return pdt(a, _I(b));}  

} using namespace NT;//}  

//。。自带取模的环类。。  
struct Int{  
    int val;  

    operator int() const{return val;}  

    Int(int val = 0):val(val){  
        val %= MOD; if (val < 0) val += MOD;  
    }  
    Int(LL _val){  
        _val %= MOD; if (_val < 0) _val += MOD;  
        val = _val;  
    }  
    Int& operator +=(const int& rhs){INC(val, rhs);rTs;}  
    Int operator +(const int& rhs) const{return sum(val, rhs);}  
    Int& operator -=(const int& rhs){DEC(val, rhs);rTs;}  
    Int operator -(const int& rhs) const{return dff(val, rhs);}  
    Int& operator *=(const int& rhs){MUL(val, rhs);rTs;}  
    Int operator *(const int& rhs) const{return pdt(val, rhs);}  
    Int& operator /=(const int& rhs){DIV(val, rhs);rTs;}  
    Int operator /(const int& rhs) const{return qtt(val, rhs);}  
    Int operator-()const{return MOD-*this;}  
};  

//}  


/** I/O Accelerator Interface .. **/ //{  
#define g (c=getchar())  
#define d isdigit(g)  
#define p x=x*10+c-'0'  
#define n x=x*10+'0'-c  
#define pp l/=10,p  
#define nn l/=10,n  
template<class T> inline T& RD(T &x){  
    char c;while(!d);x=c-'0';while(d)p;  
    return x;  
}  
template<class T> inline T& RDD(T &x){  
    char c;while(g,c!='-'&&!isdigit(c));  
    if (c=='-'){x='0'-g;while(d)n;}  
    else{x=c-'0';while(d)p;}  
    return x;  
}  
inline DB& RF(DB &x){  
    //scanf("%lf", &x);  
    char c;while(g,c!='-'&&c!='.'&&!isdigit(c));  
    if(c=='-')if(g=='.'){x=0;DB l=1;while(d)nn;x*=l;}  
        else{x='0'-c;while(d)n;if(c=='.'){DB l=1;while(d)nn;x*=l;}}  
    else if(c=='.'){x=0;DB l=1;while(d)pp;x*=l;}  
        else{x=c-'0';while(d)p;if(c=='.'){DB l=1;while(d)pp;x*=l;}}  
    return x;  
}  
#undef nn  
#undef pp  
#undef n  
#undef p  
#undef d  
#undef g  
inline char* RS(char *s){  
    //gets(s);  
    scanf("%s", s);  
    return s;  
}  

LL last_ans; int Case; template<class T> inline void OT(const T &x){  
    printf("Case #%d: ", ++Case);  
    //printf("%lld\n", x);  
    //printf("%.9f\n", x);  
    //printf("%d\n", x);  
    cout << x << endl;  
    //last_ans = x;  
}  
//}  
// <<= '1. Bitwise Operation ., //{  
namespace BO{  

inline bool _1(int x, int i){return bool(x&1<<i);}  
inline bool _1(LL x, int i){return bool(x&1LL<<i);}  
inline LL _1(int i){return 1LL<<i;}  
inline LL _U(int i){return _1(i) - 1;};  

inline int reverse_bits(int x){  
    x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa);  
    x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);  
    x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0);  
    x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);  
    x = ((x >>16) & 0x0000ffff) | ((x <<16) & 0xffff0000);  
    return x;  
}  

inline LL reverse_bits(LL x){  
    x = ((x >> 1) & 0x5555555555555555LL) | ((x << 1) & 0xaaaaaaaaaaaaaaaaLL);  
    x = ((x >> 2) & 0x3333333333333333LL) | ((x << 2) & 0xccccccccccccccccLL);  
    x = ((x >> 4) & 0x0f0f0f0f0f0f0f0fLL) | ((x << 4) & 0xf0f0f0f0f0f0f0f0LL);  
    x = ((x >> 8) & 0x00ff00ff00ff00ffLL) | ((x << 8) & 0xff00ff00ff00ff00LL);  
    x = ((x >>16) & 0x0000ffff0000ffffLL) | ((x <<16) & 0xffff0000ffff0000LL);  
    x = ((x >>32) & 0x00000000ffffffffLL) | ((x <<32) & 0xffffffff00000000LL);  
    return x;  
}  

template<class T> inline bool odd(T x){return x&1;}  
template<class T> inline bool even(T x){return !odd(x);}  
template<class T> inline T low_bit(T x) {return x & -x;}  
template<class T> inline T high_bit(T x) {T p = low_bit(x);while (p != x) x -= p, p = low_bit(x);return p;}  
template<class T> inline T cover_bit(T x){T p = 1; while (p < x) p <<= 1;return p;}  
template<class T> inline int cover_idx(T x){int p = 0; while (_1(p) < x ) ++p; return p;}  

inline int clz(int x){return __builtin_clz(x);}  
inline int clz(LL x){return __builtin_clzll(x);}  
inline int ctz(int x){return __builtin_ctz(x);}  
inline int ctz(LL x){return __builtin_ctzll(x);}  
inline int lg2(int x){return !x ? -1 : 31 - clz(x);}  
inline int lg2(LL x){return !x ? -1 : 63 - clz(x);}  
inline int low_idx(int x){return !x ? -1 : ctz(x);}  
inline int low_idx(LL x){return !x ? -1 : ctz(x);}  
inline int high_idx(int x){return lg2(x);}  
inline int high_idx(LL x){return lg2(x);}  
inline int parity(int x){return __builtin_parity(x);}  
inline int parity(LL x){return __builtin_parityll(x);}  
inline int count_bits(int x){return __builtin_popcount(x);}  
inline int count_bits(LL x){return __builtin_popcountll(x);}  

} using namespace BO;  
#define N 100010  
int n, c[N];  

int lowbit(int x)  
{  
    return x & (- x);  
}  

void update(int i , int num)  
{  
    while(i > 0)  
    {  
        c[i] += num;  
        i -= lowbit(i);  
    }  
}  

int getsum(int i)  
{  
    int sum = 0;  
    while(i <= n)  
    {  
        sum += c[i];  
        i += lowbit(i);  
    }  
    return sum;  
}  

int main()  
{  
    #ifdef ZH  
    freopen("in.txt","r",stdin);  
    #endif  
    while(~scanf("%d",&n) && n)  
    {  
        memset(c , 0, sizeof(c));  
        int a , b;  
        REP(i , n)  
        {  
            scanf("%d%d",&a,&b);  
            update(b , 1);  
            update(a - 1 , -1);  
        }  
        FOR(i , 1, n)  
            printf("%d ",getsum(i));  
        printf("%d\n",getsum(n));  
    }  
}  

HDU1166

题目链接

传送门

解题思路

树状数组模板题

参考代码

#include <functional>  
#include <algorithm>  
#include <iostream>  
#include <fstream>  
#include <sstream>  
#include <iomanip>  
#include <numeric>  
#include <cstring>  
#include <climits>  
#include <cassert>  
#include <complex>  
#include <cstdio>  
#include <string>  
#include <vector>  
#include <bitset>  
#include <queue>  
#include <stack>  
#include <cmath>  
#include <ctime>  
#include <list>  
#include <set>  
#include <map>  

//#include <tr1/unordered_set>  
//#include <tr1/unordered_map>  
//#include <array>  

using namespace std;  

#define REP(i, n) for (int i=0;i<n;++i)  
#define FOR(i, a, b) for (int i=a;i<b;++i)  
#define DWN(i, b, a) for (int i=b-1;i>=a;--i)  
#define REP_1(i, n) for (LL i=1;i<=n;++i)  
#define FOR_1(i, a, b) for (int i=a;i<=b;++i)  
#define DWN_1(i, b, a) for (LL i=b;i>=a;--i)  
#define REP_C(i, n) for (int n____=n,i=0;i<n____;++i)  
#define FOR_C(i, a, b) for (int b____=b,i=a;i<b____;++i)  
#define DWN_C(i, b, a) for (int a____=a,i=b-1;i>=a____;--i)  
#define REP_N(i, n) for (i=0;i<n;++i)  
#define FOR_N(i, a, b) for (i=a;i<b;++i)  
#define DWN_N(i, b, a) for (i=b-1;i>=a;--i)  
#define REP_1_C(i, n) for (int n____=n,i=1;i<=n____;++i)  
#define FOR_1_C(i, a, b) for (int b____=b,i=a;i<=b____;++i)  
#define DWN_1_C(i, b, a) for (int a____=a,i=b;i>=a____;--i)  
#define REP_1_N(i, n) for (i=1;i<=n;++i)  
#define FOR_1_N(i, a, b) for (i=a;i<=b;++i)  
#define DWN_1_N(i, b, a) for (i=b;i>=a;--i)  
#define REP_C_N(i, n) for (int n____=(i=0,n);i<n____;++i)  
#define FOR_C_N(i, a, b) for (int b____=(i=0,b);i<b____;++i)  
#define DWN_C_N(i, b, a) for (int a____=(i=b-1,a);i>=a____;--i)  
#define REP_1_C_N(i, n) for (int n____=(i=1,n);i<=n____;++i)  
#define FOR_1_C_N(i, a, b) for (int b____=(i=a,b);i<=b____;++i)  
#define DWN_1_C_N(i, b, a) for (int a____=(i=b,a);i>=a____;--i)  

#define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)  
#define REP_S(i, str) for (char*i=str;*i;++i)  
#define REP_L(i, hd, suc) for (int i=hd;i;i=suc[i])  
#define REP_G(i, u) REP_L(i,hd[u],suc)  
#define REP_SS(x, s) for (int x=s;x;x=(x-1)&s)  
#define DO(n) for ( int ____n = n; ____n-->0; )  
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)  
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)  
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)  
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)  
#define REP_4(i, j, k, ii, n, m, l, nn) REP(i, n) REP(j, m) REP(k, l) REP(ii, nn)  
#define REP_4_1(i, j, k, ii, n, m, l, nn) REP_1(i, n) REP_1(j, m) REP_1(k, l) REP_1(ii, nn)  

#define ALL(A) A.begin(), A.end()  
#define LLA(A) A.rbegin(), A.rend()  
#define CPY(A, B) memcpy(A, B, sizeof(A))  
#define INS(A, P, B) A.insert(A.begin() + P, B)  
#define ERS(A, P) A.erase(A.begin() + P)  
#define LBD(A, x) (lower_bound(ALL(A), x) - A.begin())  
#define UBD(A, x) (lower_bound(ALL(A), x) - A.begin())  
#define CTN(T, x) (T.find(x) != T.end())  
#define SZ(A) int((A).size())  
#define PB push_back  
#define MP(A, B) make_pair(A, B)  
#define PTT pair<T, T>  
#define Ts *this  
#define rTs return Ts  
#define fi first  
#define se second  
#define re real()  
#define im imag()  

#define Rush for(int ____T=RD(); ____T--;)  
#define Display(A, n, m) {                      \  
  REP(i, n){                                    \  
        REP(j, m-1) cout << A[i][j] << " ";     \  
        cout << A[i][m-1] << endl;                \  
    }                                            \  
}  
#define Display_1(A, n, m) {                    \  
    REP_1(i, n){                                \  
        REP_1(j, m-1) cout << A[i][j] << " ";   \  
        cout << A[i][m] << endl;                \  
    }                                            \  
}  

typedef long long LL;  
//typedef long double DB;  
typedef double DB;  
typedef unsigned uint;  
typedef unsigned long long uLL;  

typedef vector<int> VI;  
typedef vector<char> VC;  
typedef vector<string> VS;  
typedef vector<LL> VL;  
typedef vector<DB> VF;  
typedef set<int> SI;  
typedef set<string> SS;  
typedef map<int, int> MII;  
typedef map<string, int> MSI;  
typedef pair<int, int> PII;  
typedef pair<LL, LL> PLL;  
typedef vector<PII> VII;  
typedef vector<VI> VVI;  
typedef vector<VII> VVII;  

template<class T> inline T& RD(T &);  
template<class T> inline void OT(const T &);  
//inline int RD(){int x; return RD(x);}  
inline LL RD(){LL x; return RD(x);}  
inline DB& RF(DB &);  
inline DB RF(){DB x; return RF(x);}  
inline char* RS(char *s);  
inline char& RC(char &c);  
inline char RC();  
inline char& RC(char &c){scanf(" %c", &c); return c;}  
inline char RC(){char c; return RC(c);}  
//inline char& RC(char &c){c = getchar(); return c;}  
//inline char RC(){return getchar();}  

template<class T> inline T& RDD(T &);  
inline LL RDD(){LL x; return RDD(x);}  

template<class T0, class T1> inline T0& RD(T0 &x0, T1 &x1){RD(x0), RD(x1); return x0;}  
template<class T0, class T1, class T2> inline T0& RD(T0 &x0, T1 &x1, T2 &x2){RD(x0), RD(x1), RD(x2); return x0;}  
template<class T0, class T1, class T2, class T3> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3){RD(x0), RD(x1), RD(x2), RD(x3); return x0;}  
template<class T0, class T1, class T2, class T3, class T4> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4); return x0;}  
template<class T0, class T1, class T2, class T3, class T4, class T5> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5); return x0;}  
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline T0& RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6); return x0;}  
template<class T0, class T1> inline void OT(const T0 &x0, const T1 &x1){OT(x0), OT(x1);}  
template<class T0, class T1, class T2> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2){OT(x0), OT(x1), OT(x2);}  
template<class T0, class T1, class T2, class T3> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3){OT(x0), OT(x1), OT(x2), OT(x3);}  
template<class T0, class T1, class T2, class T3, class T4> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);}  
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);}  
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(const T0 &x0, const T1 &x1, const T2 &x2, const T3 &x3, const T4 &x4, const T5 &x5, const T6 &x6){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);}  
inline char& RC(char &a, char &b){RC(a), RC(b); return a;}  
inline char& RC(char &a, char &b, char &c){RC(a), RC(b), RC(c); return a;}  
inline char& RC(char &a, char &b, char &c, char &d){RC(a), RC(b), RC(c), RC(d); return a;}  
inline char& RC(char &a, char &b, char &c, char &d, char &e){RC(a), RC(b), RC(c), RC(d), RC(e); return a;}  
inline char& RC(char &a, char &b, char &c, char &d, char &e, char &f){RC(a), RC(b), RC(c), RC(d), RC(e), RC(f); return a;}  
inline char& RC(char &a, char &b, char &c, char &d, char &e, char &f, char &g){RC(a), RC(b), RC(c), RC(d), RC(e), RC(f), RC(g); return a;}  
inline DB& RF(DB &a, DB &b){RF(a), RF(b); return a;}  
inline DB& RF(DB &a, DB &b, DB &c){RF(a), RF(b), RF(c); return a;}  
inline DB& RF(DB &a, DB &b, DB &c, DB &d){RF(a), RF(b), RF(c), RF(d); return a;}  
inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e){RF(a), RF(b), RF(c), RF(d), RF(e); return a;}  
inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f){RF(a), RF(b), RF(c), RF(d), RF(e), RF(f); return a;}  
inline DB& RF(DB &a, DB &b, DB &c, DB &d, DB &e, DB &f, DB &g){RF(a), RF(b), RF(c), RF(d), RF(e), RF(f), RF(g); return a;}  
inline void RS(char *s1, char *s2){RS(s1), RS(s2);}  
inline void RS(char *s1, char *s2, char *s3){RS(s1), RS(s2), RS(s3);}  
template<class T0,class T1>inline void RDD(T0&a, T1&b){RDD(a),RDD(b);}  
template<class T0,class T1,class T2>inline void RDD(T0&a, T1&b, T2&c){RDD(a),RDD(b),RDD(c);}  

template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}  
template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}  
template<class T> inline void CLR(T &A){A.clear();}  

template<class T0, class T1> inline void RST(T0 &A0, T1 &A1){RST(A0), RST(A1);}  
template<class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2){RST(A0), RST(A1), RST(A2);}  
template<class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3){RST(A0), RST(A1), RST(A2), RST(A3);}  
template<class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);}  
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);}  
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);}  
template<class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x){FLC(A0, x), FLC(A1, x);}  
template<class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x);}  
template<class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x);}  
template<class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x);}  
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x);}  
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x){FLC(A0, x), FLC(A1, x), FLC(A2, x), FLC(A3, x), FLC(A4, x), FLC(A5, x), FLC(A6, x);}  
template<class T> inline void CLR(priority_queue<T, vector<T>, less<T> > &Q){while (!Q.empty()) Q.pop();}  
template<class T> inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q){while (!Q.empty()) Q.pop();}  
template<class T> inline void CLR(stack<T> &S){while (!S.empty()) S.pop();}  
template<class T> inline void CLR(queue<T> &Q){while (!Q.empty()) Q.pop();}  

template<class T0, class T1> inline void CLR(T0 &A0, T1 &A1){CLR(A0), CLR(A1);}  
template<class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2){CLR(A0), CLR(A1), CLR(A2);}  
template<class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3){CLR(A0), CLR(A1), CLR(A2), CLR(A3);}  
template<class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);}  
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);}  
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);}  
template<class T> inline void CLR(T &A, int n){REP(i, n) CLR(A[i]);}  

template<class T> inline bool EPT(T &a){return a.empty();}  
template<class T> inline T& SRT(T &A){sort(ALL(A)); return A;}  
template<class T, class C> inline T& SRT(T &A, C B){sort(ALL(A), B); return A;}  
template<class T> inline T& RVS(T &A){reverse(ALL(A)); return A;}  
template<class T> inline T& UNQQ(T &A){A.resize(unique(ALL(A))-A.begin());return A;}  
template<class T> inline T& UNQ(T &A){SRT(A);return UNQQ(A);}  


//}  

/** Constant List .. **/ //{  

const int MOD = int(1e9)+7;  
const int INF = 0x3f3f3f3f;  
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;  
const DB EPS = 1e-9;  
const DB OO = 1e20;  
const DB PI = acos(-1.0); //M_PI;  


//}  

/** Add On .. **/ //{  
// <<= '0. Nichi Joo ., //{  

template<class T> inline T& checkMin(T &a,const T b){if (b<a) a=b;return a;}  
template<class T> inline T& checkMax(T &a,const T b){if (a<b) a=b;return a;}  
template<class T> inline T& checkMin(T &a, T &b, const T x){checkMin(a, x), checkMin(b, x);return a;}  
template<class T> inline T& checkMax(T &a, T &b, const T x){checkMax(a, x), checkMax(b, x);return a;}  
template <class T, class C> inline T& checkMin(T& a, const T b, C c){if (c(b,a)) a = b;return a;}  
template <class T, class C> inline T& checkMax(T& a, const T b, C c){if (c(a,b)) a = b;return a;}  
template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}  
template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}  
template<class T> inline T min(T a, T b, T c, T d){return min(min(a, b), min(c, d));}  
template<class T> inline T max(T a, T b, T c, T d){return max(max(a, b), max(c, d));}  
template<class T> inline T min(T a, T b, T c, T d, T e){return min(min(min(a,b),min(c,d)),e);}  
template<class T> inline T max(T a, T b, T c, T d, T e){return max(max(max(a,b),max(c,d)),e);}  
template<class T> inline T sqr(T a){return a*a;}  
template<class T> inline T cub(T a){return a*a*a;}  
template<class T> inline T ceil(T x, T y){return (x - 1) / y + 1;}  
template<class T> T abs(T x){return x>0?x:-x;}  
inline int sgn(DB x){return x < -EPS ? -1 : x > EPS;}  
inline int sgn(DB x, DB y){return sgn(x - y);}  

inline DB cos(DB a, DB b, DB c){return (sqr(a)+sqr(b)-sqr(c))/(2*a*b);}  
inline DB cot(DB x){return 1./tan(x);};  
inline DB sec(DB x){return 1./cos(x);};  
inline DB csc(DB x){return 1./sin(x);};  

//}  


//基础包。。  
// <<= '2. Number Theory .,//{  
namespace NT{  
#define gcd __gcd  
inline LL lcm(LL a, LL b){return a*b/gcd(a,b);}  

inline void INC(int &a, int b){a += b; if (a >= MOD) a -= MOD;}  
inline int sum(int a, int b){a += b; if (a >= MOD) a -= MOD; return a;}  
/* 模数两倍刚好超 int 时。 
inline int sum(uint a, int b){a += b; a %= MOD;if (a < 0) a += MOD; return a;} 
inline void INC(int &a, int b){a = sum(a, b);} 
*/  

inline void DEC(int &a, int b){a -= b; if (a < 0) a += MOD;}  
inline int dff(int a, int b){a -= b; if (a < 0) a  += MOD; return a;}  
inline void MUL(int &a, int b){a = (LL)a * b % MOD;}  
inline int pdt(int a, int b){return (LL)a * b % MOD;}  

inline int gcd(int m, int n, int &x, int &y){  

    x = 1, y = 0; int xx = 0, yy = 1, q;  

    while (1){  
        q = m / n, m %= n;  
        if (!m){x = xx, y = yy; return n;}  
        DEC(x, pdt(q, xx)), DEC(y, pdt(q, yy));  
        q = n / m, n %= m;  
        if (!n) return m;  
        DEC(xx, pdt(q, x)), DEC(yy, pdt(q, y));  
    }  
}  

inline int sum(int a, int b, int c){return sum(a, sum(b, c));}  
inline int sum(int a, int b, int c, int d){return sum(sum(a, b), sum(c, d));}  
inline int pdt(int a, int b, int c){return pdt(a, pdt(b, c));}  
inline int pdt(int a, int b, int c, int d){return pdt(pdt(a, b), pdt(c, d));}  

inline int pow(int a, LL b){  
    int c(1); while (b){  
        if (b&1) MUL(c, a);  
        MUL(a, a), b >>= 1;  
    }  
    return c;  
}  

template<class T> inline T pow(T a, LL b){  
    T c(1); while (b){  
        if (b&1) c *= a;  
        a *= a, b >>= 1;  
    }  
    return c;  
}  

template<class T> inline T pow(T a, int b){  
    return pow(a, (LL)b);  
}  

inline int _I(int b){  
    int a = MOD, x1 = 0, x2 = 1, q; while (1){  
        q = a / b, a %= b;  
        if (!a) return x2;  
        DEC(x1, pdt(q, x2));  

        q = b / a, b %= a;  
        if (!b) return x1;  
        DEC(x2, pdt(q, x1));  
    }  
}  

inline void DIV(int &a, int b){MUL(a, _I(b));}  
inline int qtt(int a, int b){return pdt(a, _I(b));}  

} using namespace NT;//}  

//。。自带取模的环类。。  
struct Int{  
    int val;  

    operator int() const{return val;}  

    Int(int val = 0):val(val){  
        val %= MOD; if (val < 0) val += MOD;  
    }  
    Int(LL _val){  
        _val %= MOD; if (_val < 0) _val += MOD;  
        val = _val;  
    }  
    Int& operator +=(const int& rhs){INC(val, rhs);rTs;}  
    Int operator +(const int& rhs) const{return sum(val, rhs);}  
    Int& operator -=(const int& rhs){DEC(val, rhs);rTs;}  
    Int operator -(const int& rhs) const{return dff(val, rhs);}  
    Int& operator *=(const int& rhs){MUL(val, rhs);rTs;}  
    Int operator *(const int& rhs) const{return pdt(val, rhs);}  
    Int& operator /=(const int& rhs){DIV(val, rhs);rTs;}  
    Int operator /(const int& rhs) const{return qtt(val, rhs);}  
    Int operator-()const{return MOD-*this;}  
};  

//}  


/** I/O Accelerator Interface .. **/ //{  
#define g (c=getchar())  
#define d isdigit(g)  
#define p x=x*10+c-'0'  
#define n x=x*10+'0'-c  
#define pp l/=10,p  
#define nn l/=10,n  
template<class T> inline T& RD(T &x){  
    char c;while(!d);x=c-'0';while(d)p;  
    return x;  
}  
template<class T> inline T& RDD(T &x){  
    char c;while(g,c!='-'&&!isdigit(c));  
    if (c=='-'){x='0'-g;while(d)n;}  
    else{x=c-'0';while(d)p;}  
    return x;  
}  
inline DB& RF(DB &x){  
    //scanf("%lf", &x);  
    char c;while(g,c!='-'&&c!='.'&&!isdigit(c));  
    if(c=='-')if(g=='.'){x=0;DB l=1;while(d)nn;x*=l;}  
        else{x='0'-c;while(d)n;if(c=='.'){DB l=1;while(d)nn;x*=l;}}  
    else if(c=='.'){x=0;DB l=1;while(d)pp;x*=l;}  
        else{x=c-'0';while(d)p;if(c=='.'){DB l=1;while(d)pp;x*=l;}}  
    return x;  
}  
#undef nn  
#undef pp  
#undef n  
#undef p  
#undef d  
#undef g  
inline char* RS(char *s){  
    //gets(s);  
    scanf("%s", s);  
    return s;  
}  

LL last_ans; int Case; template<class T> inline void OT(const T &x){  
    printf("Case #%d: ", ++Case);  
    //printf("%lld\n", x);  
    //printf("%.9f\n", x);  
    //printf("%d\n", x);  
    cout << x << endl;  
    //last_ans = x;  
}  
//}  
// <<= '1. Bitwise Operation ., //{  
namespace BO{  

inline bool _1(int x, int i){return bool(x&1<<i);}  
inline bool _1(LL x, int i){return bool(x&1LL<<i);}  
inline LL _1(int i){return 1LL<<i;}  
inline LL _U(int i){return _1(i) - 1;};  

inline int reverse_bits(int x){  
    x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa);  
    x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);  
    x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0);  
    x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);  
    x = ((x >>16) & 0x0000ffff) | ((x <<16) & 0xffff0000);  
    return x;  
}  

inline LL reverse_bits(LL x){  
    x = ((x >> 1) & 0x5555555555555555LL) | ((x << 1) & 0xaaaaaaaaaaaaaaaaLL);  
    x = ((x >> 2) & 0x3333333333333333LL) | ((x << 2) & 0xccccccccccccccccLL);  
    x = ((x >> 4) & 0x0f0f0f0f0f0f0f0fLL) | ((x << 4) & 0xf0f0f0f0f0f0f0f0LL);  
    x = ((x >> 8) & 0x00ff00ff00ff00ffLL) | ((x << 8) & 0xff00ff00ff00ff00LL);  
    x = ((x >>16) & 0x0000ffff0000ffffLL) | ((x <<16) & 0xffff0000ffff0000LL);  
    x = ((x >>32) & 0x00000000ffffffffLL) | ((x <<32) & 0xffffffff00000000LL);  
    return x;  
}  

template<class T> inline bool odd(T x){return x&1;}  
template<class T> inline bool even(T x){return !odd(x);}  
template<class T> inline T low_bit(T x) {return x & -x;}  
template<class T> inline T high_bit(T x) {T p = low_bit(x);while (p != x) x -= p, p = low_bit(x);return p;}  
template<class T> inline T cover_bit(T x){T p = 1; while (p < x) p <<= 1;return p;}  
template<class T> inline int cover_idx(T x){int p = 0; while (_1(p) < x ) ++p; return p;}  

inline int clz(int x){return __builtin_clz(x);}  
inline int clz(LL x){return __builtin_clzll(x);}  
inline int ctz(int x){return __builtin_ctz(x);}  
inline int ctz(LL x){return __builtin_ctzll(x);}  
inline int lg2(int x){return !x ? -1 : 31 - clz(x);}  
inline int lg2(LL x){return !x ? -1 : 63 - clz(x);}  
inline int low_idx(int x){return !x ? -1 : ctz(x);}  
inline int low_idx(LL x){return !x ? -1 : ctz(x);}  
inline int high_idx(int x){return lg2(x);}  
inline int high_idx(LL x){return lg2(x);}  
inline int parity(int x){return __builtin_parity(x);}  
inline int parity(LL x){return __builtin_parityll(x);}  
inline int count_bits(int x){return __builtin_popcount(x);}  
inline int count_bits(LL x){return __builtin_popcountll(x);}  

} using namespace BO;  

#define N 1000010  

int a[N] , c[N];  
int n;  

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 sum = 0;  
    while(i > 0)  
    {  
        sum += c[i];  
        i -= lowbit(i);  
    }  
    return sum;  
}  

int main()  
{  
    #ifdef ZH  
    freopen("in.txt","r",stdin);  
    #endif  
    int T;  
    scanf("%d",&T);  
    int cas = 1;  
    while(T--)  
    {  
        scanf("%d",&n);  
        memset(a , 0 , sizeof(a));  
        memset(c , 0 , sizeof(c));  
        int m;  
        FOR_1(i , 1 , n)  
        {  
            scanf("%d",&m);  
            a[i] += m;  
            update(i , m);  
        }  
        printf("Case %d:\n", cas ++);  
        char str[11];  
        int x , y;  
        while(scanf("%s",str) && strcmp(str , "End"))  
        {  
            scanf("%d%d",&x,&y);  
            if(strcmp(str , "Add") == 0)  
            {  
                a[x] += y;  
                update(x , y);  
            }  
            else if(strcmp(str , "Sub") == 0)  
            {  
                a[x] -= y;  
                update(x , -y);  
            }  
            else if(strcmp(str , "Query") == 0)  
                printf("%d\n",sum(y) - sum(x - 1));  
        }  
    }  
}  

HDU1052

题目链接

传送门

题目大意

题意是田忌赛马的变形,胜一场得200,输一场失200,平局不得分。第一行n代表n匹马,然后一行输入田忌马的速度, 接下来输入国王马的速度,最后求田忌的金币数。

解题思路

首先对t和k的数组进行从小到大排序。首先比较最小的,如果t最小的马能赢k最小的马的话,那就win++,如果t最 小的马不能赢的话,就让它跟k最大的马比,把k最大的马刷掉,同时lose++。平局的时候要多加考虑,因为t输一场、赢一 场和平局最后的收益一样大,如果t最大的马比k最大的马小的话,那么此时用t最小的马去刷点k最大的马,即故意输一局, 这无疑会给t最大的马减轻不少压力,尽管此时输了一局,但在之后t的马中一定能有把k当前最小马刷掉的马,所以说输一局、 赢一局和平局收益相同。

参考代码

#include <functional>  
#include <algorithm>  
#include <iostream>  
#include <fstream>  
#include <sstream>  
#include <iomanip>  
#include <numeric>  
#include <cstring>  
#include <cassert>  
#include <cstdio>  
#include <string>  
#include <vector>  
#include <bitset>  
#include <queue>  
#include <stack>  
#include <cmath>  
#include <ctime>  
#include <list>  
#include <set>  
#include <map>  
#define REP0(i, n) for (int i=0;i<int(n);++i)  
#define REP1(i, n) for (int i=1;i<=int(n);++i)  
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)  
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)  
using namespace std;  
#define REP(i, n) for (int i=0;i<int(n);++i)  
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)  
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)  
#define REP_1(i, n) for (int i=1;i<=int(n);++i)  
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)  
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)  
#define REP_C(i, n) for (int n____=int(n),i=0;i<n____;++i)  
#define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)  
#define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i>=a____;--i)  
#define REP_N(i, n) for (i=0;i<int(n);++i)  
#define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)  
#define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)  
#define REP_1_C(i, n) for (int n____=int(n),i=1;i<=n____;++i)  
#define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i<=b____;++i)  
#define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i>=a____;--i)  
#define REP_1_N(i, n) for (i=1;i<=int(n);++i)  
#define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)  
#define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)  
#define REP_C_N(i, n) for (n____=int(n),i=0;i<n____;++i)  
#define FOR_C_N(i, a, b) for (b____=int(b),i=a;i<b____;++i)  
#define DWN_C_N(i, b, a) for (a____=int(a),i=b-1;i>=a____;--i)  
#define REP_1_C_N(i, n) for (n____=int(n),i=1;i<=n____;++i)  
#define FOR_1_C_N(i, a, b) for (b____=int(b),i=a;i<=b____;++i)  
#define DWN_1_C_N(i, b, a) for (a____=int(a),i=b;i>=a____;--i)  
#define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)  
#define REP_S(it, str) for (char*it=str;*it;++it) // 用于字符串的 .. .  
#define REP_G(it, u) for (int it=hd[u];it;it=suc[it]) // 用于图论的 .. .  
#define DO(n) for ( int ____n ## __line__ = n; ____n ## __line__ -- ; )  
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)  
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)  
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)  
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)  
#define PB push_back  
#define MP(A, B) make_pair(A, B)  
#define PTT pair<T, T>  
#define fi first  
#define se second  
#define Display(A, n, m) {                      \  
    REP(i, n){                                  \  
        REP(j, m) cout << A[i][j] << " ";       \  
        cout << endl;                         \  
    }                                           \  
}  
#define Display_1(A, n, m) {                    \  
    REP_1(i, n){                                \  
        REP_1(j, m) cout << A[i][j] << " ";     \  
        cout << endl;                         \  
    }                                           \  
}  
typedef long long LL;  
typedef double DB;  
typedef unsigned UINT;  
typedef unsigned long long ULL;  
const int dx[] = {-1, 0, 1, 0};  
const int dy[] = {0, 1, 0, -1};  
const int MOD = 1000000007;  
const int INF = 0x3f3f3f3f;  
const LL INFF = 1LL << 60;  
const DB EPS = 1e-9;  
const DB OO = 1e15;  
const DB PI = acos(-1.0); //M_PI;  

#define N 100010  

int a[N] , b[N] , vis[N];  

int main()  
{  
    #ifdef ZH  
    freopen("in.txt","r",stdin);  
    #endif  
    int n;  
    while(~scanf("%d",&n))  
    {  
        if(n == 0)  
            break;  
        REP(i , n)  
        {  
            scanf("%d",&a[i]);  
            vis[i] = 0;  
        }  
        REP(i , n)  
            scanf("%d",&b[i]);  
        sort(a , a + n);  
        sort(b , b + n);  
        int win = 0 , lose = 0;  
        int tl = 0 , tf = n - 1;  
        int kl = 0 , kf = n - 1;  
        while(tl <= tf)  
        {  
            if(a[tl] > b[kl])  
            {  
                win ++;  
                tl ++;  
                kl ++;  
            }  
            else if(a[tl] < b[kl])  
            {  
                lose ++;  
                tl ++;  
                kf --;  
            }  
            else  
            {  
                if(a[tf] > b[kf])  
                {  
                    win ++;  
                    tf --;  
                    kf --;  
                }  
                else  
                {  
                    if(a[tl] < b[kf])  
                    lose ++;  
                    tl ++;  
                    kf --;  
                }  
            }  

        }  
        printf("%d\n",200 * (win - lose));  
    }  
}  

HDU1789

题目链接

传送门

题目大意

题意是说先输入T,代表T组测试样例。每组样例输入n,接下来两行输入,第一行输入每份作业的截止日期, 第二行输入每份作业超过截止日期会扣的分数。求扣的最少分数。

解题思路

按照扣分从大到小排个序,优先考虑扣分多的。对于扣分多的作业,最好让它离截止日期越近越好(在截止 日期之前筛选)。vis数组起标记作用,最开始初始化为0,然后哪天被用了,就把哪天标记为1。把无论如何也不 能按时完成的那些扣得分数加起来(通过flag判断)。

参考代码

#include <functional>  
#include <algorithm>  
#include <iostream>  
#include <fstream>  
#include <sstream>  
#include <iomanip>  
#include <numeric>  
#include <cstring>  
#include <cassert>  
#include <cstdio>  
#include <string>  
#include <vector>  
#include <bitset>  
#include <queue>  
#include <stack>  
#include <cmath>  
#include <ctime>  
#include <list>  
#include <set>  
#include <map>  
#define REP0(i, n) for (int i=0;i<int(n);++i)  
#define REP1(i, n) for (int i=1;i<=int(n);++i)  
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)  
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)  
using namespace std;  
#define REP(i, n) for (int i=0;i<int(n);++i)  
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)  
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)  
#define REP_1(i, n) for (int i=1;i<=int(n);++i)  
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)  
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)  
#define REP_C(i, n) for (int n____=int(n),i=0;i<n____;++i)  
#define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)  
#define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i>=a____;--i)  
#define REP_N(i, n) for (i=0;i<int(n);++i)  
#define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)  
#define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)  
#define REP_1_C(i, n) for (int n____=int(n),i=1;i<=n____;++i)  
#define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i<=b____;++i)  
#define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i>=a____;--i)  
#define REP_1_N(i, n) for (i=1;i<=int(n);++i)  
#define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)  
#define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)  
#define REP_C_N(i, n) for (n____=int(n),i=0;i<n____;++i)  
#define FOR_C_N(i, a, b) for (b____=int(b),i=a;i<b____;++i)  
#define DWN_C_N(i, b, a) for (a____=int(a),i=b-1;i>=a____;--i)  
#define REP_1_C_N(i, n) for (n____=int(n),i=1;i<=n____;++i)  
#define FOR_1_C_N(i, a, b) for (b____=int(b),i=a;i<=b____;++i)  
#define DWN_1_C_N(i, b, a) for (a____=int(a),i=b;i>=a____;--i)  
#define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)  
#define REP_S(it, str) for (char*it=str;*it;++it) // 用于字符串的 .. .  
#define REP_G(it, u) for (int it=hd[u];it;it=suc[it]) // 用于图论的 .. .  
#define DO(n) for ( int ____n ## __line__ = n; ____n ## __line__ -- ; )  
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)  
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)  
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)  
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)  
#define PB push_back  
#define MP(A, B) make_pair(A, B)  
#define PTT pair<T, T>  
#define fi first  
#define se second  
#define Display(A, n, m) {                      \  
    REP(i, n){                                  \  
        REP(j, m) cout << A[i][j] << " ";       \  
        cout << endl;                         \  
    }                                           \  
}  
#define Display_1(A, n, m) {                    \  
    REP_1(i, n){                                \  
        REP_1(j, m) cout << A[i][j] << " ";     \  
        cout << endl;                         \  
    }                                           \  
}  
typedef long long LL;  
typedef double DB;  
typedef unsigned UINT;  
typedef unsigned long long ULL;  
const int dx[] = {-1, 0, 1, 0};  
const int dy[] = {0, 1, 0, -1};  
const int MOD = 1000000007;  
const int INF = 0x3f3f3f3f;  
const LL INFF = 1LL << 60;  
const DB EPS = 1e-9;  
const DB OO = 1e15;  
const DB PI = acos(-1.0); //M_PI;  

struct node  
{  
    int date;  
    int score;  
}q[1000010];  
int vis[1000010];  

bool cmp(node a ,node b)  
{  
    return a.score > b.score;  
}  

int main()  
{  
    #ifdef ZH  
    freopen("in.txt","r",stdin);  
    #endif  

    int T;  
    scanf("%d",&T);  
    while(T--)  
    {  
        int n;  
        scanf("%d",&n);  
        FOR_1(i , 1 , n)  
        {  
            scanf("%d",&q[i].date);  
            vis[i] = 0;  
        }  
        FOR_1(i , 1 , n)  
            scanf("%d",&q[i].score);  
        sort(q + 1 , q + n + 1 , cmp);  
        int sum = 0;  
        FOR_1(i , 1 , n)  
        {  
            int flag = 0;  
            DWN_1(j , q[i].date , 1)  
            {  
                if(!vis[j])  
                {  
                    vis[j] = 1;  
                    flag = 1;  
                    break;  
                }  
            }  
            if(!flag)  
                sum += q[i].score;  
        }  
        printf("%d\n",sum);  
    }  
}  

HDU1072

题目链接

传送门

题目大意

给你一个图,0代表墙,1代表可以走,2代表起始点,3代表结束点,4代表时间重置点。

解题思路

逃生搜索的一种变形,主要在它的生存时间可以重置。对起始点进行BFS搜索,vis数组记录该点是否走过。在四个方向搜索时,如果满足坐标不越界,该点没走过并且该点不是墙,则进行if判断:
(1)此时坐标恰好等于终点,返回step;
(2)maps等于1,时间减1;
(3)maps等于4,时间重置,标记走过。如果最后队列为空时也不能走出,返回-1.

参考代码

#include <functional>  
#include <algorithm>  
#include <iostream>  
#include <fstream>  
#include <sstream>  
#include <iomanip>  
#include <numeric>  
#include <cstring>  
#include <cassert>  
#include <cstdio>  
#include <string>  
#include <vector>  
#include <bitset>  
#include <queue>  
#include <stack>  
#include <cmath>  
#include <ctime>  
#include <list>  
#include <set>  
#include <map>  
#define REP0(i, n) for (int i=0;i<int(n);++i)  
#define REP1(i, n) for (int i=1;i<=int(n);++i)  
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)  
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)  
using namespace std;  
#define REP(i, n) for (int i=0;i<int(n);++i)  
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)  
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)  
#define REP_1(i, n) for (int i=1;i<=int(n);++i)  
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)  
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)  
#define REP_C(i, n) for (int n____=int(n),i=0;i<n____;++i)  
#define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)  
#define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i>=a____;--i)  
#define REP_N(i, n) for (i=0;i<int(n);++i)  
#define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)  
#define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)  
#define REP_1_C(i, n) for (int n____=int(n),i=1;i<=n____;++i)  
#define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i<=b____;++i)  
#define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i>=a____;--i)  
#define REP_1_N(i, n) for (i=1;i<=int(n);++i)  
#define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)  
#define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)  
#define REP_C_N(i, n) for (n____=int(n),i=0;i<n____;++i)  
#define FOR_C_N(i, a, b) for (b____=int(b),i=a;i<b____;++i)  
#define DWN_C_N(i, b, a) for (a____=int(a),i=b-1;i>=a____;--i)  
#define REP_1_C_N(i, n) for (n____=int(n),i=1;i<=n____;++i)  
#define FOR_1_C_N(i, a, b) for (b____=int(b),i=a;i<=b____;++i)  
#define DWN_1_C_N(i, b, a) for (a____=int(a),i=b;i>=a____;--i)  

//#define ECH(it, A) for (typeof(A.begin()) it=A.begin(); it != A.end(); ++it)  
#define ECH(it, A) for (__typeof(A.begin()) it=A.begin(); it != A.end(); ++it)  
#define REP_S(it, str) for (char*it=str;*it;++it) // 用于字符串的 .. .  
#define REP_G(it, u) for (int it=hd[u];it;it=suc[it]) // 用于图论的 .. .  
#define DO(n) for ( int ____n ## __line__ = n; ____n ## __line__ -- ; )  
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)  
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)  
#define REP_3(i, j, k, n, m, l) REP(i, n) REP(j, m) REP(k, l)  
#define REP_3_1(i, j, k, n, m, l) REP_1(i, n) REP_1(j, m) REP_1(k, l)  
#define ALL(A) A.begin(), A.end()  
#define LLA(A) A.rbegin(), A.rend()  
#define CPY(A, B) memcpy(A, B, sizeof(A))  
#define INS(A, P, B) A.insert(A.begin() + P, B)  
#define ERS(A, P) A.erase(A.begin() + P)  
#define BSC(A, X) find(ALL(A), X) // != A.end()  
#define CTN(T, x) (T.find(x) != T.end())  
#define SZ(A) int(A.size())  
#define PB push_back  
#define MP(A, B) make_pair(A, B)  
#define PTT pair<T, T>  
#define fi first  
#define se second  
#define ALL(A) A.begin(), A.end()  
#define LLA(A) A.rbegin(), A.rend()  
#define CPY(A, B) memcpy(A, B, sizeof(A))  
#define INS(A, P, B) A.insert(A.begin() + P, B)  
#define ERS(A, P) A.erase(A.begin() + P)  
#define BSC(A, X) find(ALL(A), X) // != A.end()  
#define CTN(T, x) (T.find(x) != T.end())  
#define SZ(A) int(A.size())  
#define PB push_back  
#define MP(A, B) make_pair(A, B)  
#define PTT pair<T, T>  
#define fi first  
#define se second  

#define Rush int T____; RD(T____); DO(T____)  
#define Rush int T____; RD(T____); DO(T____)  

#define Display(A, n, m) {                      \  
    REP(i, n){                                  \  
        REP(j, m) cout << A[i][j] << " ";       \  
        cout << endl;                         \  
    }                                           \  
}  

#define Display_1(A, n, m) {                    \  
    REP_1(i, n){                                \  
        REP_1(j, m) cout << A[i][j] << " ";     \  
        cout << endl;                         \  
    }                                           \  
}  
typedef long long LL;  
//typedef long double DB;  
typedef double DB;  
typedef unsigned UINT;  
typedef unsigned long long ULL;  

typedef vector<int> VI;  
typedef vector<char> VC;  
typedef vector<string> VS;  
typedef vector<LL> VL;  
typedef vector<DB> VD;  
typedef map<int, int> MII;  
typedef map<string, int> MSI;  
typedef map<LL, int> MLI;  
typedef map<DB, int> MDI;  
typedef pair<int, int> PII;  
typedef pair<int, bool> PIB;  
typedef pair<LL, LL> PLL;  
typedef vector<PII> VII;  
typedef vector<VI> VVI;  
typedef vector<VII> VVII;  
const int dx[] = {-1, 0, 1, 0};  
const int dy[] = {0, 1, 0, -1};  
const int MOD = 1000000007;  
const int INF = 0x3f3f3f3f;  
const LL INFF = 1LL << 60;  
const DB EPS = 1e-9;  
const DB OO = 1e15;  
const DB PI = acos(-1.0); //M_PI;  
#define N 81  
int n , m , i , j , k , t , flag;  
int maps[N][N] , vis[N][N] , d[4][2] = {-1, 0, 1, 0, 0, -1, 0 ,1};  

struct node  
{  
    int x , y , time ,step;  
};  


int bfs(node st , node ed)  
{  
    memset(vis , 0 , sizeof(vis));  
    queue<node> q;  
    node now , next;  
    now.x = st.x;  
    now.y = st.y;  
    now.time = 6;  
    now.step = 0;  
    vis[st.x][st.y] = 1;  
    q.push(now);  
    while(!q.empty())  
    {  
        now = q.front();  
        q.pop();  
        REP(i , 4)  
        {  
            next.x = now.x + d[i][0];  
            next.y = now.y + d[i][1];  
            next.step = now.step + 1;  
            next.time = now.time - 1;  
            if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && !vis[next.x][next.y] && next.time >= 1 && maps[next.x][next.y] != 0)  
            {  
                if(next.x == ed.x && next.y == ed.y)  
                    return next.step;  
                else if(maps[next.x][next.y] == 4)  
                {  
                    next.time = 6;  
                    vis[next.x][next.y] = 1;  
                }  
                else if(maps[next.x][next.y] == 1)  
                    next.time = now.time - 1;  
                q.push(next);  
            }  
        }  
    }  
    return -1;  
}  


int main()  
{  
    #ifdef ZH  
    freopen("in.txt","r",stdin);  
    #endif  
    int T;  
    scanf("%d",&T);  
    node st ,ed;  
    while(T--)  
    {  
        scanf("%d%d",&n,&m);  
        memset(maps , 0 , sizeof(maps));  
        REP(i , n)  
        {  
            REP(j , m)  
            {  
                scanf("%d",&maps[i][j]);  
                if(maps[i][j] == 2)  
                {  
                    st.x = i;  
                    st.y = j;  
                }  
                else if(maps[i][j] == 3)  
                {  
                    ed.x = i;  
                    ed.y = j;  
                }  
            }  
        }  
        printf("%d\n",bfs(st , ed));  
    }  
}  

HDU1016

题目链接

传送门

题目大意

题目大意是说要求输出所有素数环,保证任意相邻两个数相加是素数,这里特别注意判断下第一个数和最后一个数相加是否是素数。

解题思路

数据不是太大,所以先把素数表写出来,开个vis数组来表示数字是否被标记过,v数组来存储最后的输出结果,
v[1]=1。从2开始DFS搜索,如果s等于n + 1,说明已经搜索完毕,此时首尾作特殊判断。否则的话从2到n开始遍历,如果vis[i]没有被标记,并且 i 和上一个保存的数字num[s-1]加起来也是个素数的话,把 i 入num数组,vis[i]标记为1,接下来搜索dfs( s + 1 ),最后记得回溯,把vis[i]置回0。

参考代码

#include <iostream>  
#include <cstring>  
#include <cstdio>  
#include <cstdlib>  
#include <cmath>  
#include <string>  
#include <vector>  
#include <list>  
#include <map>  
#include <queue>  
#include <stack>  
#include <bitset>  
#include <algorithm>  
#include <numeric>  
#include <functional>  
using namespace std;  
#define REP(i, n) for (int i=0;i<int(n);++i)  
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)  
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)  
#define REP_1(i, n) for (int i=1;i<=int(n);++i)  
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)  
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)  
#define REP_C(i, n) for (int n____=int(n),i=0;i<n____;++i)  
#define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)  
#define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i>=a____;--i)  
#define REP_N(i, n) for (i=0;i<int(n);++i)  
#define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)  
#define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)  
#define REP_1_C(i, n) for (int n____=int(n),i=1;i<=n____;++i)  
#define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i<=b____;++i)  
#define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i>=a____;--i)  
#define REP_1_N(i, n) for (i=1;i<=int(n);++i)  
#define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)  
#define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)  
#define REP_C_N(i, n) for (n____=int(n),i=0;i<n____;++i)  
#define FOR_C_N(i, a, b) for (b____=int(b),i=a;i<b____;++i)  
#define DWN_C_N(i, b, a) for (a____=int(a),i=b-1;i>=a____;--i)  
#define REP_1_C_N(i, n) for (n____=int(n),i=1;i<=n____;++i)  
#define FOR_1_C_N(i, a, b) for (b____=int(b),i=a;i<=b____;++i)  
#define DWN_1_C_N(i, b, a) for (a____=int(a),i=b;i>=a____;--i)  
#define Display(A, n, m) {                      \  
    REP(i, n){                                  \  
        REP(j, m) cout << A[i][j] << " ";       \  
        cout << endl;                         \  
    }                                           \  
}  

#define Display_1(A, n, m) {                    \  
    REP_1(i, n){                                \  
        REP_1(j, m) cout << A[i][j] << " ";     \  
        cout << endl;                         \  
    }                                           \  
}  
typedef long long LL;  
typedef double DB;  
typedef unsigned UINT;  
typedef unsigned long long ULL;  

typedef vector<int> VI;  
typedef vector<char> VC;  
typedef vector<string> VS;  
typedef vector<LL> VL;  
typedef vector<DB> VD;  
typedef map<int, int> MII;  
typedef map<string, int> MSI;  
typedef map<LL, int> MLI;  
typedef map<DB, int> MDI;  
typedef pair<int, int> PII;  
typedef pair<int, bool> PIB;  
typedef pair<LL, LL> PLL;  
typedef vector<PII> VII;  
typedef vector<VI> VVI;  
typedef vector<VII> VVII;  
const int dx[] = {-1, 0, 1, 0};  
const int dy[] = {0, 1, 0, -1};  

const int MOD = 1000000007;  
const int INF = 0x3f3f3f3f;  
const LL INFF = 1LL << 60;  
const DB EPS = 1e-9;  
const DB OO = 1e15;  
const DB PI = acos(-1.0);  
#define N 51  
int n , i , j ,k;  
int num[N] , vis[N] , v[N];  
int ispri[] = {0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0};  

void dfs(int s)  
{  
    if(s == n + 1)  
    {  
        if(ispri[v[s - 1] + 1])  
        {  
            FOR_1(i , 1 , n)  
                printf("%d%c",v[i],i == n ? '\n' : ' ');  
        }  
    }  
    else  
    {  
        FOR_1(i , 2 , n)  
        {  
            if(!vis[i] && ispri[i + v[s - 1]])  
            {  
                vis[i] = 1;  
                v[s] = i;  
                dfs(s + 1);  
                vis[i] = 0;  
            }  
        }  
    }  
}  

int main()  
{  
    #ifdef DoubleQ  
    freopen("in.txt","r",stdin);  
    #endif  
    int cas = 1;  
    while(~scanf("%d",&n))  
    {  
        memset(vis , 0 , sizeof(vis));  
        v[1] = 1;  
        printf("Case %d:\n",cas);  
        dfs(2);  
        cas++;  
        printf("\n");  
    }  
}