线段树:
/*
#define maxn 1000005
int gnode[maxn];
long long gnum[maxn*4];
the segment gnum must full binary tree,
the left child is loc*2+1,
and the right child is loc*2+2.
*/
void GxdSegmentTreeAdd(long long* gnum,int glen,int gfl,int gfr,int gnow,int gkeyloc)
{
gnum[gnow]++;
if(gfl==gfr) return ;
int mid = (gfl+gfr)/2;
if(gkeyloc>mid) GxdSegmentTreeAdd(gnum,glen,mid+1,gfr,gnow*2+2,gkeyloc);
else GxdSegmentTreeAdd(gnum,glen,gfl,mid,gnow*2+1,gkeyloc);
}
long long GxdSegmentTreeSearch(long long* gnum,int glen,int gfl,int gfr,int gnow,int gkeyl,int gkeyr)
{
if(gfl==gkeyl&&gfr==gkeyr) return tree[gnow];
int mid = (gfl+gfr)/2;
if(gkeyl>mid) return GxdSegmentTreeSearch(gnum,glen,mid+1,gfr,gnow*2+2,gkeyl,gkeyr);
else if(gkeyr<=mid) return GxdSegmentTreeSearch(gnum,glen,gfl,mid,gnow*2+1,gkeyl,gkeyr);
else return GxdSegmentTreeSearch(gnum,glen,gfl,mid,gnow*2+1,gkeyl,mid)+GxdSegmentTreeSearch(gnum,glen,mid+1,gfr,gnow*2+2,mid+1,gkeyr);
}
结构体排序:
struct Node{
int c;
int v;
};
Node num[maxn];
int cmp(const void* a,const void* b)
{
Node *x = (Node*)a;
Node *y = (Node*)b;
return x->v - y->v; //ascend sort
}
qsort(num,n,sizeof(num[0]),cmp);
Vector容器迭代:
vector<string> cnt;
string ansstr;
for(auto str:cnt)
{
if(ans[str] > maxnum)
{
maxnum = ans[str];
ansstr = str;
}
}
运算符重载:
struct Pallet
{
public:
int l;
int w;
public:
void init(int a,int b){l=max(a,b);w=min(a,b);}
bool operator == (const Pallet& t){if(t.l==l&&t.w==w) return true;return false;}
};
重定向:
#ifdef LOCAL
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
fclose(stdin);
fclose(stdout);
#endif
整行读取:
//使用fgets(buf,maxn,stdin);来读取一整行