#include <bits/stdc++.h>
#define DIMN 100010
using namespace std;
int tt[DIMN] , lvl[DIMN];
int up[DIMN] , l[DIMN] , d[DIMN] , poz[DIMN] , idx_aint[DIMN];
struct idk{
idk *st , *dr;
int sum;
int val[220];
idk(){
st = dr = 0;
sum = 0;
memset (val , 0 , sizeof(val));
}
};
vector < map <int,int> > aint[DIMN];
vector <int> v[DIMN] , w[DIMN];
int elem , lnt;
inline int code (int len , int rest){
return (len + 2) * (len + 1) / 2 + rest + 1;
}
void dfs_lant(int nod , int fth){
int i,vecin,ok=0,maxi,fiu;
tt[nod] = fth;
d[nod] = 1;
maxi=0;
fiu=0;
for (i=0;i<v[nod].size();i++){
vecin=v[nod][i];
if (vecin!=tt[nod]){
ok=1;
lvl[vecin] = 1 + lvl[nod];
dfs_lant(vecin , nod);
d[nod] += d[vecin];
if (d[vecin]>maxi){
maxi=d[vecin];
fiu=vecin;
}
}
}
if (!ok){ /// frunza
lnt++;
l[nod]=lnt; /// lantul din care apartine
w[lnt].push_back(nod);
}else { /// unim cu poz
w[l[fiu]].push_back(nod);
l[nod]=l[fiu];
for (i=0;i<v[nod].size();i++){
vecin=v[nod][i];
if (vecin!=tt[nod] && vecin!=fiu)
up[l[vecin]]=nod; /// din lantul vecinului trecem in lantul nodului
d[vecin] = 0;
}
}
v[nod].clear();
}
void build_aint (int lant , int p , int st , int dr){
int mid = (st + dr)/2;
if (st == dr){
idx_aint[w[lant][st]] = p;
return;
}
build_aint(lant , 2 * p , st , mid);
build_aint(lant , 2 * p + 1 , mid + 1 , dr);
}
void update_aint (int lant , int p , int st , int dr , int l , int r){
int mid , len , rest;
if (st == dr){
return;
}
mid=(st+dr)/2;
if (l <= mid){
update_aint(lant , 2 * p , st , mid , l , r);
}
if (mid + 1 <= r)
update_aint(lant , 2 * p + 1 , mid + 1 , dr , l , r);
for (len = 1 ; len <= 20 ; len++){
for (rest = 0 ; rest < len ; rest++)
aint[lant][p][code(len , rest)] = aint[lant][2 * p][code(len , rest)] + aint[lant][2 * p + 1][code(len , rest)];
}
}
void update (int x , int y){
if (l[x]!=l[y]){ /// avansam
if (lvl[up[l[x]]]>lvl[up[l[y]]]){
update ( up[l[x]] , y );
update_aint (l[x] , 1 , 0 , w[l[x]].size()-1 , 0 , poz[x] );
}
else{
update ( x , up[l[y]] );
update_aint( l[y] , 1 , 0 , w[l[y]].size()-1 , 0 , poz[y] );
}
}
else {
update_aint( l[x] , 1 , 0 , w[l[x]].size()-1 , min(poz[x],poz[y]) , max(poz[x],poz[y]) );
}
}
long long query_aint (int lant , int p , int st , int dr , int px , int py , int t1 , int t2){
int mid , len , rest;
long long sol = 0;
if (px <= st && dr <= py){
for (len = 1 ; len <= 20 ; len ++){
for (rest = 0 ; rest < len ; rest++){
sol += 1LL * aint[lant][p][code(len , rest)] * ( (t2 / len) + (t2 % len >= rest) - ((t1 - 1) / len) - ((t1 - 1) % len >= rest));
}
}
return sol;
}
mid = (st + dr)/2;
if (px <= mid)
sol = sol + query_aint(lant , 2 * p , st , mid , px , py , t1 , t2);
if (mid+1 <= py)
sol = sol + query_aint(lant , 2 * p + 1 , mid + 1 , dr , px , py , t1 , t2);
return sol;
}
long long query (int x , int y , int t1 , int t2){
if (l[x] != l[y]){ /// avansam
if (lvl[up[l[x]]]>lvl[up[l[y]]])
return query(up[l[x]] , y , t1 , t2) + query_aint(l[x] , 1 , 0 , w[l[x]].size()-1 , 0 , poz[x] , t1 , t2);
else{
return query(x , up[l[y]] , t1 , t2) + query_aint(l[y] , 1 , 0 , w[l[y]].size()-1 , 0 , poz[y] , t1 , t2);
}
}
else{
return query_aint(l[x] , 1 , 0 , w[l[x]].size()-1 , min(poz[x],poz[y]) , max(poz[x],poz[y]) , t1 , t2);
}
}
void adauga (int x , int y , int val , int op){
int lca , nod , poz , len , i , nod1 , nod2;
nod1 = x;
nod2 = y;
while (nod1 != nod2){
while (lvl[nod1] < lvl[nod2])
nod2 = tt[nod2];
while (lvl[nod1] > lvl[nod2])
nod1 = lvl[nod1];
}
lca = nod1;
len = lvl[x] + lvl[y] - 2 * lvl[lca] + 1;
nod = x;
poz = 0;
while (nod != tt[lca]){
aint[l[nod]][idx_aint[nod]][code(len , poz)] += val;
poz++;
nod = tt[nod];
}
nod = y;
poz = len - 1;
while (nod != lca){
aint[l[nod]][idx_aint[nod]][code(len , poz)] += val;
poz--;
nod = tt[nod];
}
}
int main()
{
FILE *fin = stdin;
FILE *fout = stdout;
int n , i , x , y , tip , k , q , t1 , t2 , p , vecin;
fscanf (fin,"%d",&n);
for (i = 1 ; i < n ; i++){
fscanf (fin,"%d%d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
}
lvl[1] = 1;
dfs_lant(1 , 0);
/// fiecare nod al catelea e
for (i=1;i<=lnt;i++){
reverse(w[i].begin(),w[i].end());
p=0;
for (vector <int> :: iterator j=w[i].begin();j!=w[i].end();j++){
/// pozitia lui din lantul curent
vecin=*j;
poz[vecin]=p;
p++;
}
aint[i].resize(w[i].size()*4);
build_aint(i , 1 , 0 , w[i].size() - 1);
}
fscanf (fin,"%d",&k);
int op = 1;
for (i = 1 ; i <= k ; i++){
fscanf (fin,"%d%d",&x,&y);
adauga(x , y , 1 , ++op);
update(x , y);
}
fscanf (fin,"%d",&q);
for (i = 1 ; i <= q ; i++){
fscanf (fin,"%d",&tip);
if (tip == 1){
fscanf (fin,"%d%d",&x,&y);
adauga(x , y , 1 , ++op);
update (x , y);
}
else if (tip == 2){
fscanf (fin,"%d%d",&x,&y);
adauga(x , y , -1 , ++op);
update (x , y);
}
else {
fscanf (fin,"%d%d%d%d",&x,&y,&t1,&t2);
fprintf (fout,"%lld\n",query (x , y , t1 , t2));
}
}
return 0;
}
Compilation message
traffickers.cpp: In function 'void dfs_lant(int, int)':
traffickers.cpp:32:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i=0;i<v[nod].size();i++){
~^~~~~~~~~~~~~~
traffickers.cpp:52:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i=0;i<v[nod].size();i++){
~^~~~~~~~~~~~~~
traffickers.cpp: In function 'void adauga(int, int, int, int)':
traffickers.cpp:140:33: warning: unused variable 'i' [-Wunused-variable]
int lca , nod , poz , len , i , nod1 , nod2;
^
traffickers.cpp: In function 'int main()':
traffickers.cpp:175:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf (fin,"%d",&n);
~~~~~~~^~~~~~~~~~~~~
traffickers.cpp:177:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf (fin,"%d%d",&x,&y);
~~~~~~~^~~~~~~~~~~~~~~~~~
traffickers.cpp:200:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf (fin,"%d",&k);
~~~~~~~^~~~~~~~~~~~~
traffickers.cpp:203:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf (fin,"%d%d",&x,&y);
~~~~~~~^~~~~~~~~~~~~~~~~~
traffickers.cpp:208:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf (fin,"%d",&q);
~~~~~~~^~~~~~~~~~~~~
traffickers.cpp:210:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf (fin,"%d",&tip);
~~~~~~~^~~~~~~~~~~~~~~
traffickers.cpp:212:20: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf (fin,"%d%d",&x,&y);
~~~~~~~^~~~~~~~~~~~~~~~~~
traffickers.cpp:217:20: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf (fin,"%d%d",&x,&y);
~~~~~~~^~~~~~~~~~~~~~~~~~
traffickers.cpp:222:20: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
fscanf (fin,"%d%d%d%d",&x,&y,&t1,&t2);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3575 ms |
8192 KB |
Time limit exceeded |
2 |
Execution timed out |
3590 ms |
9600 KB |
Time limit exceeded |
3 |
Execution timed out |
3582 ms |
7680 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3570 ms |
11392 KB |
Time limit exceeded |
2 |
Execution timed out |
3580 ms |
9856 KB |
Time limit exceeded |
3 |
Execution timed out |
3581 ms |
12672 KB |
Time limit exceeded |
4 |
Execution timed out |
3573 ms |
10752 KB |
Time limit exceeded |
5 |
Execution timed out |
3587 ms |
10240 KB |
Time limit exceeded |
6 |
Execution timed out |
3594 ms |
10368 KB |
Time limit exceeded |
7 |
Execution timed out |
3590 ms |
12160 KB |
Time limit exceeded |
8 |
Execution timed out |
3597 ms |
11136 KB |
Time limit exceeded |
9 |
Execution timed out |
3580 ms |
11904 KB |
Time limit exceeded |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3578 ms |
16512 KB |
Time limit exceeded |
2 |
Execution timed out |
3581 ms |
16384 KB |
Time limit exceeded |
3 |
Execution timed out |
3578 ms |
16248 KB |
Time limit exceeded |
4 |
Execution timed out |
3586 ms |
15616 KB |
Time limit exceeded |
5 |
Execution timed out |
3580 ms |
15488 KB |
Time limit exceeded |
6 |
Execution timed out |
3598 ms |
22136 KB |
Time limit exceeded |
7 |
Execution timed out |
3581 ms |
21624 KB |
Time limit exceeded |
8 |
Execution timed out |
3598 ms |
17656 KB |
Time limit exceeded |