各种自己用的模版(持续更_ios各种自己用的模版(持续更_i++_02
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<queue>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<ctime>
  7 #include<cstring>
  8 #define inf 2147483647
  9 #define For(i,a,b) for(register int i=a;i<=b;i++)
 10 #define p(a) putchar(a)
 11 #define g() getchar()
 12 
 13 using namespace std;
 14 int n,m,s,t;
 15 int x,y,v;
 16 int d[10010];
 17 struct node
 18 {
 19     int n;
 20     int v;
 21     node *next;
 22 }*e[500010];
 23 queue<int>q;
 24 bool vis[10010];
 25 
 26 void in(int &x)
 27 {
 28     int y=1;
 29     char c=g();x=0;
 30     while(c<'0'||c>'9')
 31     {
 32     if(c=='-')
 33     y=-1;
 34     c=g();
 35     }
 36     while(c<='9'&&c>='0')x=x*10+c-'0',c=g();
 37     x*=y;
 38 }
 39 void o(int x)
 40 {
 41     if(x<0)
 42     {
 43         p('-');
 44         x=-x;
 45     }
 46     if(x>9)o(x/10);
 47     p(x%10+'0');
 48 }
 49 
 50 void push(int x,int y,int v)
 51 {
 52     node *p;
 53     p=new node();
 54     p->n=y;
 55     p->v=v;
 56     if(e[x]==NULL) 
 57     e[x]=p;
 58     else
 59     {
 60         p->next=e[x]->next;
 61         e[x]->next=p;
 62     }
 63 }
 64 
 65 void spfa(int x)
 66 {
 67     node *p;
 68     For(i,1,n)
 69     d[i]=inf;
 70     d[x]=0;
 71     q.push(x);
 72     while(q.size()>0)
 73     {
 74         t=q.front();
 75         vis[t]=true;
 76         for(node *i=e[t];i!=NULL;i=i->next)
 77         {
 78             if(d[t]+i->v<d[i->n])
 79             {
 80                 d[i->n]=d[t]+i->v;
 81                 if(!vis[i->n])
 82                 q.push(i->n);
 83             }
 84         }
 85         vis[t]=false;
 86         q.pop();
 87     }
 88 }
 89 
 90 int main()
 91 {
 92     in(n);in(m);in(s);
 93     For(i,1,m)
 94     {
 95         in(x);in(y);in(v);
 96         push(x,y,v);
 97     }
 98     spfa(s);
 99     For(i,1,n)
100     o(d[i]),p(' ');
101      return 0;
102 }
spfa求最短路
各种自己用的模版(持续更_ios各种自己用的模版(持续更_i++_02
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<queue>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<ctime>
  7 #include<set>
  8 #include<map>
  9 #include<stack>
 10 #include<cstring>
 11 #define inf 2147483647
 12 #define ls rt<<1
 13 #define rs rt<<1|1
 14 #define lson ls,nl,mid,l,r
 15 #define rson rs,mid+1,nr,l,r
 16 #define N 200010
 17 #define For(i,a,b) for(register long long i=a;i<=b;i++)
 18 #define p(a) putchar(a)
 19 #define g() getchar()
 20 
 21 using namespace std;
 22 long long n,m,s;
 23 long long x,y,v;
 24 bool vis[N];
 25 
 26 struct node{
 27     long long n;
 28     long long v;
 29     node *next;
 30 }*e[N];
 31 
 32 struct Node
 33 {
 34     long long i;
 35     long long d;
 36     bool operator < (const Node&temp)const{
 37         return d>temp.d;
 38     }
 39 }a[N];
 40 
 41 priority_queue<Node>q;
 42 
 43 void in(long long &x){
 44     long long y=1;
 45     char c=g();x=0;
 46     while(c<'0'||c>'9'){
 47         if(c=='-')y=-1;
 48         c=g();
 49     }
 50     while(c<='9'&&c>='0'){
 51         x=(x<<1)+(x<<3)+c-'0';c=g();
 52     }
 53     x*=y;
 54 }
 55 void o(long long x){
 56     if(x<0){
 57         p('-');
 58         x=-x;
 59     }
 60     if(x>9)o(x/10);
 61     p(x%10+'0');
 62 }
 63 
 64 void push(long long x,long long y,long long v){
 65     node *p;
 66     p=new node();
 67     p->n=y;
 68     p->v=v;
 69     if(e[x]==NULL)
 70         e[x]=p;
 71     else{
 72         p->next=e[x]->next;
 73         e[x]->next=p;
 74     }
 75 }
 76 
 77 void hd(){
 78     a[s].d=0;
 79     q.push(a[s]);
 80     while(!q.empty()){
 81         long long t=q.top().i;
 82         q.pop();
 83         if(vis[t])continue;
 84         vis[t]=true;
 85         for(register node *i=e[t];i;i=i->next){
 86             if(a[t].d+i->v<a[i->n].d){
 87                 a[i->n].d=a[t].d+i->v;
 88                 q.push(a[i->n]);
 89             }
 90         }
 91     }
 92 }
 93 
 94 int main(){
 95     in(n);in(m);in(s);
 96 
 97     For(i,1,m){
 98         in(x);in(y);in(v);
 99         push(x,y,v);
100     }
101 
102     For(i,1,n){
103         a[i].i=i;
104         a[i].d=inf;
105     }
106     hd();
107     For(i,1,n){
108         o(a[i].d);
109         p(' ');
110     }
111     return 0;
112 }
dijkstra最短路
各种自己用的模版(持续更_ios各种自己用的模版(持续更_i++_02
  1 // luogu-judger-enable-o2
  2 #include<iostream>
  3 #include<cstdio>
  4 #include<queue>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<ctime>
  8 #include<set>
  9 #include<map>
 10 #include<stack>
 11 #include<cstring>
 12 #define inf 2147483647
 13 #define For(i,a,b) for(register long long i=a;i<=b;i++)
 14 #define p(a) putchar(a)
 15 #define g() getchar()
 16 
 17 using namespace std;
 18 long long T;
 19 long long x,y,v;
 20 long long n,m;
 21 bool flag;
 22 bool vis[2010];
 23 long long d[2010];
 24 int num[2010];
 25 
 26 struct node
 27 {
 28     long long n;
 29     long long v;
 30     node *next;
 31 }*e[400010];
 32 queue<int>q;
 33 
 34 void in(long long &x)
 35 {
 36     long long y=1;
 37     char c=g();x=0;
 38     while(c<'0'||c>'9')
 39     {
 40     if(c=='-')
 41     y=-1;
 42     c=g();
 43     }
 44     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
 45     x*=y;
 46 }
 47 void o(long long x)
 48 {
 49     if(x<0)
 50     {
 51         p('-');
 52         x=-x;
 53     }
 54     if(x>9)o(x/10);
 55     p(x%10+'0');
 56 }
 57 
 58 inline void push(long long x,long long y,long long v)
 59 {
 60     node *p;
 61     p=new node();
 62     p->n=y;
 63     p->v=v;
 64     if(e[x]==NULL)
 65     e[x]=p;
 66     else
 67     {
 68         p->next=e[x]->next;
 69         e[x]->next=p;
 70     }
 71 }
 72 
 73 inline void spfa(long long x){
 74    q.push(x);
 75    vis[x]=true;
 76    num[1]++;
 77    while(!q.empty()){
 78     int t=q.front();q.pop();
 79     vis[t]=true;
 80     for(node *i=e[t];i;i=i->next){
 81 
 82         if(d[i->n]>d[t]+i->v){
 83             d[i->n]=d[t]+i->v;
 84             num[i->n]++;
 85                 if(num[i->n]>=n){
 86                     flag=true;
 87                     return;
 88                 }
 89             if(!vis[i->n]){
 90                 q.push(i->n);
 91                 vis[i->n]=true;
 92             }
 93         }
 94 
 95     }
 96     vis[t]=false;
 97    }
 98 }
 99 
100 int main()
101 {
102     in(T);
103     while(T--)
104     {
105         in(n),in(m);
106 
107         For(i,1,n)
108         e[i]=NULL,vis[i]=false,d[i]=inf,num[i]=0;
109         while(!q.empty())
110             q.pop();  
111         flag=false;
112 
113         For(i,1,m){
114             in(x),in(y),in(v);
115             push(x,y,v);
116             if(v>=0)
117             push(y,x,v);
118         }
119 
120         d[1]=0;
121         spfa(1);
122 
123         if(flag)
124         puts("YE5");
125         else
126         puts("N0");
127     }
128      return 0;
129 }
负环
各种自己用的模版(持续更_ios各种自己用的模版(持续更_i++_02
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<set>
 8 #include<map>
 9 #include<stack>
10 #include<cstring>
11 #define inf 2147483647
12 #define For(i,a,b) for(register int i=a;i<=b;i++)
13 #define p(a) putchar(a)
14 #define g() getchar()
15 //by war
16 //2017.11.9
17 using namespace std;
18 int f[100010][20];
19 int n,m;
20 int x,y;
21 void in(int &x)
22 {
23     int y=1;
24     char c=g();x=0;
25     while(c<'0'||c>'9')
26     {
27     if(c=='-')
28     y=-1;
29     c=g();
30     }
31     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
32     x*=y;
33 }
34 void o(int x)
35 {
36     if(x<0)
37     {
38         p('-');
39         x=-x;
40     }
41     if(x>9)o(x/10);
42     p(x%10+'0');
43 }
44 
45 void st()
46 {
47     int ind=log2(n);
48     For(j,1,ind)
49       For(i,1,n-(1<<j)+1)
50         f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
51 }
52 
53 int query(int l,int r)
54 {
55     int ind=log2(r-l+1);
56     return max(f[l][ind],f[r-(1<<ind)+1][ind]);
57 }
58 
59 int main()
60 {
61     in(n),in(m);
62     For(i,1,n)
63     {
64         in(x);
65         f[i][0]=x;
66     }
67     st();
68     For(i,1,m)
69     {
70         in(x),in(y);
71         o(query(x,y)),p('\n');
72     }
73      return 0;
74 }
ST表
各种自己用的模版(持续更_ios各种自己用的模版(持续更_i++_02
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<queue>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<ctime>
  7 #include<set>
  8 #include<map>
  9 #include<stack>
 10 #include<cstring>
 11 #define inf 2147483647
 12 #define ls rt<<1
 13 #define rs rt<<1|1
 14 #define lson ls,nl,mid,l,r
 15 #define rson rs,mid+1,nr,l,r
 16 #define N 100010
 17 #define For(i,a,b) for(int i=a;i<=b;i++)
 18 #define p(a) putchar(a)
 19 #define g() getchar()
 20 
 21 using namespace std;
 22 int n,m,x,y,cnt;
 23 int dfn[20010],low[20010];
 24 int del[20010];
 25 
 26 struct node{
 27     int n;
 28     node *next;
 29 }*e[200010];
 30 
 31 void in(int &x){
 32     int y=1;
 33     char c=g();x=0;
 34     while(c<'0'||c>'9'){
 35         if(c=='-')y=-1;
 36         c=g();
 37     }
 38     while(c<='9'&&c>='0'){
 39         x=(x<<1)+(x<<3)+c-'0';c=g();
 40     }
 41     x*=y;
 42 }
 43 void o(int x){
 44     if(x<0){
 45         p('-');
 46         x=-x;
 47     }
 48     if(x>9)o(x/10);
 49     p(x%10+'0');
 50 }
 51 
 52 void push(int x,int y){
 53     node *p;
 54     p=new node();
 55     p->n=y;
 56     if(e[x]==0)
 57         e[x]=p;
 58     else{
 59         p->next=e[x]->next;
 60         e[x]->next=p;
 61     }
 62 }
 63 
 64 void tarjan(int x,int fa){
 65     int child=0;
 66     dfn[x]=low[x]=++cnt;
 67     for(node *i=e[x];i;i=i->next){
 68         if(!dfn[i->n]){
 69             tarjan(i->n,fa);
 70             low[x]=min(low[x],low[i->n]);
 71             if(x==fa)
 72                 child++;
 73             if(low[i->n]>=dfn[x]&&x!=fa)
 74                 del[x]=1;
 75         }
 76         low[x]=min(low[x],dfn[i->n]);
 77     }
 78     if(child>1&&x==fa)
 79         del[x]=1;
 80 }
 81 
 82 int main(){
 83     in(n);in(m);
 84     For(i,1,m){
 85         in(x);in(y);
 86         push(x,y);
 87         push(y,x);
 88     }
 89     For(i,1,n)
 90         if(!dfn[i])
 91             tarjan(i,i);
 92     cnt=0;
 93     For(i,1,n)
 94         if(del[i])++cnt;
 95     o(cnt);p('\n');
 96 
 97     For(i,1,n)
 98         if(del[i])
 99             o(i),p(' ');
100     return 0;
101 }
割点(割顶)
各种自己用的模版(持续更_ios各种自己用的模版(持续更_i++_02
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<set>
 8 #include<map>
 9 #include<stack>
10 #include<cstring>
11 #define inf 2147483647
12 #define For(i,a,b) for(register long long i=a;i<=b;i++)
13 #define p(a) putchar(a)
14 #define g() getchar()
15 //by war
16 //2017.11.9
17 using namespace std;
18 long long a[3000010];
19 long long p;
20 long long n;
21 void in(long long &x)
22 {
23     long long y=1;
24     char c=g();x=0;
25     while(c<'0'||c>'9')
26     {
27     if(c=='-')
28     y=-1;
29     c=g();
30     }
31     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
32     x*=y;
33 }
34 void o(long long x)
35 {
36     if(x<0)
37     {
38         p('-');
39         x=-x;
40     }
41     if(x>9)o(x/10);
42     p(x%10+'0');
43 }
44 int main()
45 {
46     in(n),in(p);
47     a[1]=1;
48     o(1),p('\n');
49     For(i,2,n)
50     {
51     a[i]=(p-p/i)*a[p%i]%p;    
52     o(a[i]),p('\n');
53     }
54      return 0;
55 }
乘法逆元(线性)
各种自己用的模版(持续更_ios各种自己用的模版(持续更_i++_02
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<queue>
  4 #include<algorithm>
  5 #include<cmath>
  6 #include<ctime>
  7 #include<set>
  8 #include<map>
  9 #include<stack>
 10 #include<cstring>
 11 #define inf 2147483647
 12 #define For(i,a,b) for(long long i=a;i<=b;i++)
 13 #define p(a) putchar(a)
 14 #define g() getchar()
 15 #define N 100010
 16 using namespace std;
 17 long long a[N],n,m,x,y,k,f,P;
 18 
 19 struct seg{
 20     long long add;
 21     long long mul=1;
 22     long long sum;
 23 }s[N<<3];
 24 
 25 void in(long long &x){
 26     long long y=1;
 27     char c=g();x=0;
 28     while(c<'0'||c>'9'){
 29         if(c=='-')y=-1;
 30         c=g();
 31     }
 32     while(c<='9'&&c>='0'){
 33         x=(x<<1)+(x<<3)+c-'0';c=g();
 34     }
 35     x*=y;
 36 }
 37 void o(long long x){
 38     if(x<0){
 39         p('-');
 40         x=-x;
 41     }
 42     if(x>9)o(x/10);
 43     p(x%10+'0');
 44 }
 45 
 46 void build(long long rt,long long l,long long r){
 47     if(l==r){
 48         s[rt].sum=a[l];
 49         return;
 50     }
 51     long long mid=(l+r)>>1;
 52     build(rt<<1, l, mid);
 53     build(rt<<1|1, mid+1, r);
 54     s[rt].sum=(s[rt<<1].sum+s[rt<<1|1].sum)%P;
 55 }
 56 
 57 void pushdown(long long rt,long long l,long long r){
 58     if(s[rt].add||s[rt].mul^1){
 59         long long mid=(l+r)>>1;
 60         s[rt<<1].mul=(s[rt<<1].mul*s[rt].mul)%P;
 61         s[rt<<1|1].mul=(s[rt<<1|1].mul*s[rt].mul)%P;
 62         s[rt<<1].add=(s[rt<<1].add*s[rt].mul+s[rt].add)%P;
 63         s[rt<<1|1].add=(s[rt<<1|1].add*s[rt].mul+s[rt].add)%P;
 64         s[rt<<1].sum=(s[rt<<1].sum*s[rt].mul+(mid-l+1)*s[rt].add)%P;
 65         s[rt<<1|1].sum=(s[rt<<1|1].sum*s[rt].mul+(r-mid)*s[rt].add)%P;
 66         s[rt].add=0;
 67         s[rt].mul=1;
 68     }
 69 }
 70 
 71 void update_mul(long long rt,long long nl,long long nr,long long l,long long r,long long k){
 72     if(nl>r||nr<l)return;
 73     pushdown(rt,nl,nr);
 74     if(nl>=l&&nr<=r){
 75         s[rt].mul= (s[rt].mul*k)%P;
 76         s[rt].add=(s[rt].add*k)%P;
 77         s[rt].sum=(s[rt].sum*k)%P;
 78         return;
 79     }
 80     long long mid=(nl+nr)>>1;
 81     update_mul(rt<<1, nl, mid, l, r, k);
 82     update_mul(rt<<1|1, mid+1, nr, l, r, k);
 83     s[rt].sum=(s[rt<<1].sum+s[rt<<1|1].sum)%P;
 84 }
 85 
 86 void update_sum(long long rt,long long nl,long long nr,long long l,long long r,long long k){
 87     if(nl>r||nr<l)return;
 88     pushdown(rt,nl,nr);
 89     if(nl>=l&&nr<=r){
 90         s[rt].add= (s[rt].add+k)%P;
 91         s[rt].sum=(s[rt].sum+(nr-nl+1)*k)%P;
 92         return;
 93     }
 94     long long mid=(nl+nr)>>1;
 95     update_sum(rt<<1, nl, mid, l, r, k);
 96     update_sum(rt<<1|1, mid+1, nr, l, r, k);
 97     s[rt].sum=(s[rt<<1].sum+s[rt<<1|1].sum)%P;
 98 }
 99 
100 long long sum(long long rt,long long nl,long long nr,long long l,long long r) {
101     if(nl>r||nr<l)return 0;
102     pushdown(rt, nl, nr);
103     if(nl>=l&&nr<=r) return s[rt].sum%P;
104     long long mid=(nl+nr)>>1;
105     return (sum(rt<<1, nl, mid, l, r)+sum(rt<<1|1, mid+1, nr, l, r))%P;
106 }
107 
108 int main(){
109     in(n);in(m);in(P);
110     For(i, 1, n)in(a[i]);
111     build(1, 1, n);
112     For(i, 1, m){
113         in(f);in(x);in(y);
114         if(f==1){
115             in(k);
116             update_mul(1,1,n,x,y,k);
117         }
118         if(f==2){
119             in(k);
120             update_sum(1,1,n,x,y,k);
121         }
122         if(f==3){
123             o(sum(1,1,n,x,y));
124             p('\n');
125         }
126     }
127     return 0;
128 }
线段树(区间加,区间乘,区间求和)
各种自己用的模版(持续更_ios各种自己用的模版(持续更_i++_02
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<set>
 8 #include<map>
 9 #include<stack>
10 #include<cstring>
11 #define inf 2147483647
12 #define For(i,a,b) for(register long long i=a;i<=b;i++)
13 #define p(a) putchar(a)
14 #define g() getchar()
15 #define MOD1 1000000007
16 #define MOD2 1000000009
17 #define base 31
18 //by war
19 //2017.11.6
20 using namespace std;
21 struct ha
22 {
23     long long o;
24     long long t;
25     bool operator<(const ha&a)const
26     {
27         return o<a.o;
28     }
29 }h[10010];
30 char a[1510];
31 long long n;
32 long long ans;
33 void in(long long &x)
34 {
35     long long y=1;
36     char c=g();x=0;
37     while(c<'0'||c>'9')
38     {
39     if(c=='-')
40     y=-1;
41     c=g();
42     }
43     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
44     x*=y;
45 }
46 void o(long long x)
47 {
48     if(x<0)
49     {
50         p('-');
51         x=-x;
52     }
53     if(x>9)o(x/10);
54     p(x%10+'0');
55 }
56 
57 long long hash(long long mod)
58 {
59     long long ans=0;
60     long long len=strlen(a+1);
61     For(i,1,len)
62     ans=((ans*base)+a[i])%mod;
63     return ans;
64 }
65 
66 int main()
67 {
68     in(n);
69     For(i,1,n)
70     {
71         cin>>(a+1);
72         h[i].o=hash(MOD1);
73         h[i].t=hash(MOD2);
74     }
75     sort(h+1,h+n+1);
76     For(i,2,n)
77     if(h[i].o!=h[i-1].o||h[i].t!=h[i-1].t)
78     ans++;
79     o(ans+1);
80      return 0;
81 }
字符串hash
各种自己用的模版(持续更_ios各种自己用的模版(持续更_i++_02
 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 int n,m,cnt;
 5 bool vis[10000010];
 6 int prime[1000010];
 7 int x;
 8 void E( )
 9 {
10     vis[1]=true;
11     for(int i=2;i<=n;i++)
12     {
13         if(!vis[i])prime[++cnt]=i;
14         for(int j=1;j<=cnt&&prime[j]*i<=n;j++)
15         {
16             vis[prime[j]*i]=true;
17             if(i%prime[j]==0)break;
18         }
19     }
20 }
21 int main( )
22 {
23     cin>>n>>m;
24     E( );
25     for(int i=1;i<=m;i++)
26     {
27         cin>>x;
28         if(!vis[x])
29             printf("Yes\n");
30         else
31             printf("No\n");
32     }
33     return 0;
34 }
线性筛素数
各种自己用的模版(持续更_ios各种自己用的模版(持续更_i++_02
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<set>
 8 #include<map>
 9 #include<stack>
10 #include<cstring>
11 #define inf 2147483647
12 #define For(i,a,b) for(register int i=a;i<=b;i++)
13 #define p(a) putchar(a)
14 #define g() getchar()
15 //by war
16 //2017.11.9
17 using namespace std;
18 int t[500010];
19 int n,m;
20 int flag,x,y,k;
21 int pre;
22 void in(int &x)
23 {
24     int y=1;
25     char c=g();x=0;
26     while(c<'0'||c>'9')
27     {
28     if(c=='-')
29     y=-1;
30     c=g();
31     }
32     while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
33     x*=y;
34 }
35 void o(int x)
36 {
37     if(x<0)
38     {
39         p('-');
40         x=-x;
41     }
42     if(x>9)o(x/10);
43     p(x%10+'0');
44 }
45 
46 void modify(int k,int change)
47 {
48     for(;k<=n;k+=(-k)&k)
49     t[k]+=change;
50 }
51 
52 int get(int k)
53 {
54     int cnt=0;
55     for(;k>0;k-=(-k)&k)
56     cnt+=t[k];
57     return cnt;
58 }
59 
60 int main()
61 {
62     in(n),in(m);
63     For(i,1,n)
64     {
65         in(x);
66         modify(i,x-pre);
67         pre=x;
68     }
69     For(i,1,m)
70     {
71         in(flag);
72         if(flag==1)
73         {
74             in(x),in(y),in(k);
75             modify(x,k);
76             modify(y+1,-k);
77         }
78         else
79         {
80             in(x);
81             o(get(x)),p('\n');
82         }
83     }
84      return 0;
85 }
树状数组(区间加,单点求和)
各种自己用的模版(持续更_ios各种自己用的模版(持续更_i++_02
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<ctime>
 7 #include<set>
 8 #include<map>
 9 #include<stack>
10 #include<cstring>
11 #define inf 2147483647
12 #define For(i,a,b) for(int i=a;i<=b;i++)
13 #define p(a) putchar(a)
14 #define g() getchar()
15 
16 using namespace std;
17 int t[10000100],n,m,cz,x,y,k,f,ans;
18 void in(int &x)
19 {
20     int y=1;
21     char c=g();x=0;
22     while(c<'0'||c>'9')
23     {
24         if(c=='-')
25             y=-1;
26         c=g();
27     }
28     while(c<='9'&&c>='0')
29     {
30         x=(x<<1)+(x<<3)+c-'0';c=g();
31     }
32     x*=y;
33 }
34 void o(int x)
35 {
36     if(x<0)
37     {
38         p('-');
39         x=-x;
40     }
41     if(x>9)o(x/10);
42     p(x%10+'0');
43 }
44 int main()
45 {
46     in(n);in(cz);
47     for(m=1;m<=n;m<<=1);
48     For(i, m+1, m+n)
49     in(t[i]);
50     for(int i=m-1;i;i--)
51         t[i]=t[i<<1]+t[i<<1|1];
52     For(jj,1,cz)
53     {
54         in(f);
55         if(f==1)
56         {
57             in(x);in(k);
58             for(t[x+=m]+=k,x>>=1;x;x>>=1)
59                 t[x]=t[x<<1]+t[x<<1|1];
60         }
61         else
62         {
63             in(x);in(y);
64             ans=0;
65             for(int l=x+m-1,r=y+m+1;l^r^1;l>>=1,r>>=1)
66             {
67                 if(~l&1)ans+=t[l^1];
68                 if(r&1)ans+=t[r^1];
69             }
70             o(ans);p('\n');
71         }
72     }
73     return 0;
74 }
树状数组(单点加,区间求和)
各种自己用的模版(持续更_ios各种自己用的模版(持续更_i++_02
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(register long long i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2019.8.15
using namespace std;
long long a[200010];
long long n,m,p;
long long T;
void in(long long &x){
    long long y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(long long x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

long long ksm(long long a,long long b){
    long long r=1;
    while(b>0){
        if(b%2==1)
            r=r*a%p;
        a=a*a%p;
        b>>=1;
    }
    return r;
}

long long c(long long n,long long m){
    if(n<m) return 0;
    return a[n]*ksm(a[n-m]*a[m]%p,p-2)%p;
}

long long lucas(long long n,long long m){
    if(!m) return 1;
    return c(n%p,m%p)*lucas(n/p,m/p)%p;
}

int main(){
    in(T);
    a[0]=1;
    while(T--){
        in(n);in(m);in(p);
        For(i,1,n+m)
            a[i]=a[i-1]*i%p;
        o(lucas(n+m,m)%p);p('\n');
    }
     return 0;
}
卢卡斯定理
各种自己用的模版(持续更_ios各种自己用的模版(持续更_i++_02
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <queue>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <cstring>
 7 #define inf 2147483647
 8 #define N 1000010
 9 #define p(a) putchar(a)
10 #define For(i,a,b) for(long long i=a;i<=b;++i)
11 
12 using namespace std;
13 long long l,r;
14 long long prime[N],f[N],g[N],cnt,ans;
15 bool vis[N];
16 void in(long long &x){
17     long long y=1;char c=getchar();x=0;
18     while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
19     while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
20     x*=y;
21 }
22 void o(long long x){
23     if(x<0){p('-');x=-x;}
24     if(x>9)o(x/10);
25     p(x%10+'0');
26 }
27 
28 void Euler(){
29     For(i,2,r){
30         if(!vis[i]){
31             prime[++cnt]=i;
32             f[i]=g[i]=i+1;
33             vis[i]=1;
34         }
35         for(long long j=1;j<=cnt&&i*prime[j]<=r;j++){
36             vis[i*prime[j]]=1;
37             if(i%prime[j]==0){
38                 g[i*prime[j]]=g[i]*prime[j]+1;
39                 f[i*prime[j]]=f[i]/g[i]*g[i*prime[j]];
40                 break;
41             }
42             else{
43                 g[i*prime[j]]=prime[j]+1;
44                 f[i*prime[j]]=f[i]*f[prime[j]];
45             }
46         }
47     }
48 }
49 int main(){
50     in(l);in(r);
51     Euler();
52     For(i,l,r)
53         ans+=f[i];
54     o(ans);
55     return 0;
56 }
筛法求约数和
各种自己用的模版(持续更_ios各种自己用的模版(持续更_i++_02
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(long long i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
#define mod 1000000007
//by war
//2017.11.6
using namespace std;
long long n,k;
struct matrix{
    long long a[110][110];
    matrix operator*(const matrix&b)const{
        matrix r;
        For(i,1,n)
            For(j,1,n){
                r.a[i][j]=0;    
                For(k,1,n)
                r.a[i][j]=(r.a[i][j]+a[i][k]*b.a[k][j])%mod;
            }
        return r;
    }
}a;
void in(long long &x)
{
    long long y=1;
    char c=g();x=0;
    while(c<'0'||c>'9')
    {
    if(c=='-')
    y=-1;
    c=g();
    }
    while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+c-'0',c=g();
    x*=y;
}
void o(long long x)
{
    if(x<0)
    {
        p('-');
        x=-x;
    }
    if(x>9)o(x/10);
    p(x%10+'0');
}

matrix ksm(matrix a,long long b){
    matrix r=a;b--;
    while(b>0){
        if(b%2==1) r=r*a;
        a=a*a;
        b>>=1;
    }
    return r;
}

int main(){
    in(n),in(k);
    For(i,1,n)
      For(j,1,n)
        in(a.a[i][j]);
    matrix r=ksm(a,k);
    For(i,1,n){
        For(j,1,n)
            o(r.a[i][j]),p(' ');
        p('\n');
    }
     return 0;
}
矩阵快速幂
各种自己用的模版(持续更_i++_29各种自己用的模版(持续更_ios_30
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<set>
#include<map>
#include<stack>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(long long i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
#define N 100010
using namespace std;
long long a[N],n,m,k,x,y,f; 
struct seg{
    long long mark;
    long long sum;
}s[N<<3]; 
inline void in(long long &x)
{
    long long y=1;
    char c=g();x=0;
    while(c<'0'||c>'9')
    {
        if(c=='-')
            y=-1;
        c=g();
    }
    while(c<='9'&&c>='0')
    {
        x=(x<<1)+(x<<3)+c-'0';c=g();
    }
    x*=y;
}
inline void o(long long x)
{
    if(x<0)
    {
        p('-');
        x=-x;
    }
    if(x>9)o(x/10);
    p(x%10+'0');
} 
inline void build(long long rt,long long l,long long r){
    if(l==r){
        s[rt].sum=a[l];
        return;
    }
    long long mid=(l+r)>>1;
    build(rt<<1, l, mid);
    build(rt<<1|1, mid+1, r);
    s[rt].sum=s[rt<<1].sum+s[rt<<1|1].sum;
} 
inline void pushdown(long long rt,long long l,long long r){
    if(s[rt].mark){
        long long mid=(l+r)>>1;
        s[rt<<1].mark+=s[rt].mark;
        s[rt<<1|1].mark+=s[rt].mark;
        s[rt<<1].sum+=(mid-l+1)*s[rt].mark;
        s[rt<<1|1].sum+=(r-mid)*s[rt].mark;
        s[rt].mark=0;
    }
} 
inline void update(long long rt,long long nl,long long nr,long long l,long long r,long long k){
    if(nr<l||nl>r)return;
    if(nl>=l&&nr<=r){
        s[rt].mark+=k;
        s[rt].sum+=(nr-nl+1)*k;
        return;
    }
    pushdown(rt,nl,nr);
    long long mid=(nl+nr)>>1;
    update(rt<<1, nl, mid, l, r, k);
    update(rt<<1|1, mid+1, nr, l, r, k);
    s[rt].sum=s[rt<<1].sum+s[rt<<1|1].sum;
} 
inline long long sum(long long rt,long long nl,long long nr,long long l,long long r){
    if(nl>r||nr<l)return 0;
    pushdown(rt, nl, nr);
    if(nl>=l&&nr<=r)return s[rt].sum;
    long long mid=(nl+nr)>>1;
    return sum(rt<<1, nl, mid, l, r)+sum(rt<<1|1, mid+1, nr, l, r);
} 
int main()
{
   in(n);in(m);
   For(i,1,n) in(a[i]);
   build(1,1,n);
    For(i,1,m){
        in(f);in(x);in(y);
        if(f==1){
            in(k);
            update(1,1,n,x,y,k);
        }
        else{
            o(sum(1,1,n,x,y));p('\n');
        }
    }
    return 0;
}
线段树(区间加,区间求和
各种自己用的模版(持续更_i++_29各种自己用的模版(持续更_ios_30
#include <bits/stdc++.h>
#define inf 2333333333333333
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(int i=a;i<=b;++i)
//by war
//2020.8.3
using namespace std;
int n,x,y,m;
int c[N],son[N],sz[N],ans[N],sum;
bool vis[N];
struct node{
    int n;
    node *next;
}*e[N];

void in(int &x){
    int y=1;char c=getchar();x=0;
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    x*=y;
}
void o(int x){
    if(x<0){p('-');x=-x;}
    if(x>9)o(x/10);
    p(x%10+'0');
}

void push(int x,int y){
    node *p;
    p=new node();
    p->n=y;
    if(e[x]==0)
        e[x]=p;
    else{
        p->next=e[x]->next;
        e[x]->next=p;
    }
}

void clean(int x,int fa){
    vis[c[x]]=0;
    for(node *i=e[x];i;i=i->next){
        if(i->n==fa) continue;
        clean(i->n,x);
    }
}

void dfs1(int x,int fa){
    sz[x]=1;
    for(node *i=e[x];i;i=i->next){
        if(i->n==fa) continue;
        dfs1(i->n,x);
        sz[x]+=sz[i->n];
        if(sz[i->n]>sz[son[x]]) son[x]=i->n;
    }
}

void dfs3(int x,int fa,int so){
    for(node *i=e[x];i;i=i->next){
        if(i->n==fa || i->n==so) continue;
        dfs3(i->n,x,so);
    }
    //todo:计算答案
    if(!vis[c[x]]){
        vis[c[x]]=1;
        sum++;
    }
}

void dfs2(int x,int fa){
    for(node *i=e[x];i;i=i->next){
        if(i->n==fa || i->n==son[x]) continue;
        dfs2(i->n,x);
        clean(i->n,x);
        sum=0;
    }
    if(son[x]) dfs2(son[x],x);
    dfs3(x,fa,son[x]);
    ans[x]=sum;
    return;
}

signed main(){
    in(n);
    For(i,1,n-1){
        in(x);in(y);
        push(x,y);
        push(y,x);
    }
    For(i,1,n) in(c[i]);
    dfs1(1,1);
    dfs2(1,1);
    in(m);
    while(m--){
        in(x);
        o(ans[x]);p('\n');
    }
    return 0;
}
dsu on tree